forked from bcd/exec09
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfileio.c
More file actions
60 lines (47 loc) · 982 Bytes
/
fileio.c
File metadata and controls
60 lines (47 loc) · 982 Bytes
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
#include "6809.h"
void
path_init (struct pathlist *path)
{
path->count = 0;
}
void
path_add (struct pathlist *path, const char *dir)
{
char *dir2;
dir2 = path->entry[path->count++] = malloc (strlen (dir) + 1);
strcpy (dir2, dir);
}
FILE *
file_open (struct pathlist *path, const char *filename, const char *mode)
{
FILE *fp;
char fqname[128];
int count;
const char dirsep = '/';
fp = fopen (filename, mode);
if (fp)
return fp;
if (!path || strchr (filename, dirsep) || *mode == 'w')
return NULL;
for (count = 0; count < path->count; count++)
{
sprintf (fqname, "%s%c%s", path->entry[count], dirsep, filename);
fp = fopen (fqname, mode);
if (fp)
return fp;
}
return NULL;
}
FILE *
file_require_open (struct pathlist *path, const char *filename, const char *mode)
{
FILE *fp = file_open (path, filename, mode);
if (!fp)
fprintf (stderr, "error: could not open '%s'\n", filename);
return fp;
}
void
file_close (FILE *fp)
{
fclose (fp);
}