forked from kastian/dictator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.c
51 lines (42 loc) · 834 Bytes
/
save.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
/*
Load previous record and save new
Copyright (C) 1983 Don Priestley - original zx-basic code.
Copyright (C) 1983 DkTRONICS - original zx-basic code.
2015 #kstn - port to C.
*/
#include "save.h"
#define SAVEFILE ".dictator"
char* savedir() /* Define savedir */
{
struct passwd* pw = getpwuid(getuid());
return pw->pw_dir;
}
int load() /* Load history */
{
FILE* fp;
int history = 0;
if (chdir(savedir()) == 0)
{
fp = fopen(SAVEFILE, "r");
if (fp != NULL)
{
if (fscanf(fp, "%d", &history) == EOF)
history = 0;
fclose(fp);
}
}
return history;
}
void save(int history) /* Save history */
{
FILE *fp;
if (chdir(savedir()) == 0)
{
fp = fopen(SAVEFILE, "w");
if (fp != NULL)
{
fprintf(fp, "%d", history);
fclose(fp);
}
}
}