-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlast.c
71 lines (63 loc) · 1.39 KB
/
last.c
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
/* last.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#include "trn.h"
#include "list.h"
#include "util.h"
#include "util2.h"
#include "intrp.h"
#include "init.h"
#include "INTERN.h"
#include "last.h"
static char* lastfile = NULL; /* path name of .rnlast file */
static long starttime;
void
last_init()
{
lastfile = savestr(filexp(LASTNAME));
starttime = (long)time((time_t*)NULL);
readlast();
}
void
readlast()
{
if ((tmpfp = fopen(lastfile,"r")) != NULL) {
if (fgets(buf,sizeof buf,tmpfp) != NULL) {
long old_last = lasttime;
buf[strlen(buf)-1] = '\0';
if (*buf) {
safefree0(lastngname);
lastngname = savestr(buf);
}
fscanf(tmpfp,"%ld %ld %ld %ld",&lasttime,&lastactsiz,
&lastnewtime,&lastextranum);
if (!lastnewtime)
lastnewtime = starttime;
if (old_last > lasttime)
lasttime = old_last;
}
fclose(tmpfp);
}
}
/* Put out certain values for next run of trn */
void
writelast()
{
sprintf(buf,"%s.%ld", lastfile, our_pid);
if ((tmpfp = fopen(buf,"w")) != NULL) {
if (lasttime < starttime)
lasttime = starttime;
fprintf(tmpfp,"%s\n%ld\n%ld\n%ld\n%ld\n",
ngname? ngname : nullstr,lasttime,
lastactsiz,lastnewtime,lastextranum);
fclose(tmpfp);
UNLINK(lastfile);
RENAME(buf,lastfile);
}
else {
printf(cantcreate,buf) FLUSH;
/*termdown(1);*/
}
}