File: wtmpparse.cpp

package info (click to toggle)
deepin-log-viewer 5.9.7%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,656 kB
  • sloc: cpp: 51,541; ansic: 1,732; sh: 51; xml: 25; makefile: 13
file content (212 lines) | stat: -rw-r--r-- 4,390 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
*
* Author:     zyc <zyc@uniontech.com>
*
* Maintainer:  zyc <zyc@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "wtmpparse.h"
#include <QDebug>

int wtmp_open(char *filename)
{
    fdWtmp = open(filename, O_RDONLY);
    cur_rec = num_recs = 0;
    return fdWtmp;
}


int wtmp_open_back(char *filename)
{
    fdWtmp = open(filename, O_RDONLY);
    //    qDebug() << "fdWtmp" << fdWtmp;
    bpos = cur_rec = num_recs = 0;
    fpos = lseek(fdWtmp, 0, SEEK_END);
    return fdWtmp;
}

int seek_end(void)
{
    //    qDebug() <<  lseek(0, fdWtmp, SEEK_END);
    return 0;
}

int wtmp_reload(void)
{
    int amt_read;
    amt_read = read(fdWtmp, utmpbuf, NRECS * UTSIZE);
    num_recs = amt_read / UTSIZE;

    cur_rec = 0;

    return num_recs;
}


struct utmp *wtmp_next(void)
{
    struct utmp *recp;

    if (fdWtmp == -1)
        return NULLUT;

    if (cur_rec == num_recs && wtmp_reload() == 0)
        return NULLUT;

    recp = (struct utmp *)&utmpbuf[cur_rec * UTSIZE];
    cur_rec++;

    return recp;
}


int wtmp_reload_back(void)
{
    int amt_read;

//   qDebug() << "endsize" << endsize << NRECS << UTSIZE;

    off_t o;
    o = ((fpos - 1) / (NRECS * UTSIZE)) * (NRECS * UTSIZE);
    bpos = (int)(fpos - o);
    fpos -= NRECS * UTSIZE;
    amt_read = pread(fdWtmp, utmpbuf, NRECS * UTSIZE, fpos);
    num_recs = amt_read / UTSIZE;

    cur_rec = 0;

    return num_recs;
}
struct utmp *wtmp_back(void)
{
    struct utmp *recp;

    if (fdWtmp == -1)
        return NULLUT;

    if (cur_rec == 0 && wtmp_reload_back() == 0)
        return NULLUT;

    recp = (struct utmp *)&utmpbuf[cur_rec * UTSIZE];
    cur_rec--;

    return recp;
}

void wtmp_close(void)
{
    if (fdWtmp != -1)
        close(fdWtmp);
}

struct utmp_list *st_list_init(void)
{
    struct utmp_list *list = (struct utmp_list *)malloc(sizeof(struct utmp_list));
    if (!list) {
        printf("struct utmp_list malloc failed\n");
        return NULL;
    }

    memset(list, 0, sizeof(struct utmp_list));

    return list;
}

struct utmp *st_utmp_init(void)
{
    struct utmp *stUTMP = (struct utmp *)malloc(sizeof(struct utmp));
    if (!stUTMP) {
        printf("struct utmp malloc failed\n");
        return NULL;
    }

    memset(stUTMP, 0, sizeof(struct utmp));

    return stUTMP;
}

utmp_list * list_delete(struct utmp_list *list)
{
    while (list->next) {
        struct utmp_list *p = list;
        list = p->next;
        free(p);
    }
    return list;
}

void list_insert(QList<utmp *> &plist, struct utmp *value)
{
    utmp v = *value;
    utmp *value_ = &(v);
    plist.append(value_);

}

utmp list_get_ele_and_del(QList<utmp > &list, char *value, int &rs)
{
    utmp temp = {};
    for (int i = 0; i < list.length(); ++i) {
        utmp itemValue = list.value(i);
        QString a(itemValue.ut_line);
        QString b(value);
        int result = QString::compare(a, b);
        if (result == 0) {
            list.removeAt(i);
            rs = 0;
            return itemValue;
        }
    }
    rs = -1;
    return temp;
}

char *show_end_time(long timeval)
{
    struct tm *t;
    char tt[256] = {0};

    t = localtime(&timeval);
    strftime(tt, sizeof(tt) - 1, "%R", t);
    //printf("%s", tt);
    return asctime(t);
}

char *show_start_time(long timeval)
{
    struct tm *t;
    char tt[256] = {0};

    t = localtime(&timeval);
    strftime(tt, sizeof(tt) - 1, "%a %b %e %R", t);
    //    printf("%s", tt);
    //    printf(" - ");

    return asctime(t);
}
void show_base_info(struct utmp *uBuf)
{
    printf("%-9.8s", uBuf->ut_name);

    if (uBuf->ut_type == BOOT_TIME)
        printf("%-13s", "system boot");
    else
        printf("%-13.8s", uBuf->ut_line);

    printf("%-17.16s", uBuf->ut_host);
}