-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings_posix.c
169 lines (162 loc) · 5.19 KB
/
settings_posix.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
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
/* settings_posix.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
/* internal data definitions */
static int settings_fd = -1;
static stringbuf entry_buffer;
/* internal function definitions */
void fatal_stop(const char* message)
{
fprintf(stderr,"%s: fatal error: %s\n",PROGRAM_NAME,message);
_exit(1);
}
const char* check_settings_path()
{
static char fnbuf[FILENAME_MAX];
static const char* init_dir = "/.compile"; /* path relative to home directory */
uid_t uid;
struct passwd* pwd;
int i;
struct stat istat;
short flag;
/* lookup user info to find home directory */
uid = getuid();
pwd = getpwuid(uid);
if (pwd == NULL)
fatal_stop("could not obtain user information for accessing settings");
flag = 0;
/* compile settings directory name */
i = strlen(pwd->pw_dir);
strcpy(fnbuf,pwd->pw_dir);
strcpy(fnbuf+i,init_dir);
/* check to see if settings directory exists as directory */
if (stat(fnbuf,&istat) == -1) {
if (errno == ENOENT) {
/* attempt to create settings directory */
if (mkdir(fnbuf,S_IRWXU) == -1) {
if (errno == EACCES)
fprintf(stderr,"%s: error: cannot create settings directory: permission denied\n",PROGRAM_NAME);
else
fprintf(stderr,"%s: error: cannot create settings directory",PROGRAM_NAME);
flag = 1;
}
}
else {
if (errno == EACCES)
fprintf(stderr,"%s: error: cannot access settings directory: permission denied\n",PROGRAM_NAME);
flag = 1;
}
}
else if ( !S_ISDIR(istat.st_mode) ) {
fprintf(stderr,"%s: error: settings directory name exists as something other than a directory!\n",PROGRAM_NAME);
flag = 1;
}
if (flag == 1)
fatal_stop("settings directory is unreachable");
return fnbuf;
}
const char* find_targets_file(const char* settingsDir)
{
static char fnbuf[FILENAME_MAX];
static const char* targ_fname = "/targets"; /* path relative to init_dir */
int i;
struct stat istat;
short flag;
/* compile targets file name */
i = strlen(settingsDir);
strcpy(fnbuf,settingsDir);
strcpy(fnbuf+i,targ_fname);
/* check to see if targets file exists as regular file */
flag = 0;
if (stat(fnbuf,&istat) == -1) {
if (errno == ENOENT) {
/* attempt to create a default targets file */
int fd;
if ((fd = open(fnbuf,O_CREAT|O_WRONLY,S_IWUSR|S_IRUSR)) == -1) {
fprintf(stderr,"%s: error: cannot create default targets file\n",PROGRAM_NAME);
flag = 1;
}
else {
ssize_t nwritten;
nwritten = write(fd,DEFAULT_TARGET_ENTRIES,strlen(DEFAULT_TARGET_ENTRIES));
close(fd);
if (nwritten == -1) {
fprintf(stderr,"%s: error: failed to write default targets file\n",PROGRAM_NAME);
flag = 1;
}
else {
printf("%s: created 'targets' file with default entries in '%s'\n",
PROGRAM_NAME,fnbuf);
}
}
}
else {
if (errno == EACCES)
fprintf(stderr,"%s: error: cannot access targets file: permission denied\n",PROGRAM_NAME);
flag = 1;
}
}
else if ( !S_ISREG(istat.st_mode) ) {
fprintf(stderr,"%s: error: targets file name exists as something other than a regular file!\n",PROGRAM_NAME);
flag = 1;
}
if (flag == 1)
fatal_stop("targets file is unreachable");
return fnbuf;
}
void open_settings_file(const char* fname)
{
assert(settings_fd == -1);
settings_fd = open(fname,O_RDONLY);
if (settings_fd == -1) {
fprintf(stderr,"%s: error: cannot open file '%s'\n",PROGRAM_NAME,fname);
fatal_stop("could not open needed settings file");
}
/* allocate string buffer for entry input */
init_stringbuf(&entry_buffer);
}
void close_settings_file()
{
assert(settings_fd != -1);
close(settings_fd);
settings_fd = -1;
/* deallocate entry buffer */
destroy_stringbuf(&entry_buffer);
}
const char* read_next_entry()
{
static int n = 0;
static char ibuf[120];
static const char* pbuf = NULL;
assert(settings_fd != -1);
reset_stringbuf(&entry_buffer);
while (1) {
int len;
char last;
if (n <= 0) { /* (n could be -1) */
n = read(settings_fd,ibuf,120);
if (n < 0)
fatal_stop("could not read from settings file");
else if (n == 0) {
if (entry_buffer.used == 0)
return NULL;
break;
}
pbuf = ibuf;
}
len = 0;
while (len<n && pbuf[len]!='\n')
++len;
concat_stringbuf_ex(&entry_buffer,pbuf,len);
last = len<n ? pbuf[len] : 0;
n -= ++len; /* increment len to count pbuf[len] */
pbuf += len;
if (last == '\n')
break;
}
return entry_buffer.buffer;
}