Merge pull request #12 from davel/davel/sqsh
[debian-nspark.git] / unix.c
blobcd909d0e4838836449c3ce997ebafc446aef45de
2 /*
3 * Operating System specific function (UNIX)
5 * $Header: unix.c 1.6 94/12/13 $
6 * $Log: unix.c,v $
7 * Revision 1.6 94/12/13 11:36:31 arb
8 * Changed use of timelocal() to SunOS 4 only... hope that's right!
10 * Revision 1.5 92/12/07 17:18:49 duplain
11 * reformatted source.
13 * Revision 1.4 92/10/19 09:35:10 duplain
14 * Changed use of timelocal() to SunOS only... hope that's right.
16 * Revision 1.3 92/10/06 12:12:51 duplain
17 * Date/time conversion now done by mktime() or timelocal().
19 * Revision 1.2 92/10/01 11:23:10 duplain
20 * Added filesize().
22 * Revision 1.1 92/09/29 18:02:28 duplain
23 * Initial revision
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <time.h>
30 #include <string.h>
31 #include <utime.h>
32 #include <stdio.h>
33 #include "spark.h"
34 #include "date.h"
37 * return the length of a file
39 Word
40 filesize(char *pathname)
42 struct stat statb;
44 if (stat(pathname, &statb) < 0)
45 return (0);
46 else
47 return ((Word) statb.st_size);
51 * test for the existance of a file or directory
53 Ftype
54 exist(char *pathname)
56 struct stat statb;
58 if (stat(pathname, &statb) < 0)
59 return (NOEXIST); /* assumes error was because file
60 doesn't exist... could be wrong! */
61 if (statb.st_mode & S_IFDIR)
62 return (ISDIR);
63 else
64 return (ISFILE); /* might not be a regular file... */
68 * make a directory
70 int
71 makedir(char *pathname)
73 return (mkdir(pathname, 0777));
77 * stamp a file with date and time
79 int
80 filestamp(Header *header, char *filename)
82 Date *date;
83 struct tm tm;
84 struct utimbuf utimbuf;
85 time_t filetime;
87 if ((header->load & (Word) 0xfff00000) != (Word) 0xfff00000)
88 return (0); /* not a timestamp */
90 memset((char *) &tm, '\0', sizeof(tm));
92 if (!(date = makedate(header)))
93 return (-1);
95 tm.tm_sec = date->second;
96 tm.tm_min = date->minute;
97 tm.tm_hour = date->hour;
98 tm.tm_mday = date->day;
99 tm.tm_mon = date->month - 1;
100 tm.tm_year = date->year;
101 filetime = mktime(&tm);
103 utimbuf.actime = filetime;
104 utimbuf.modtime = filetime;
105 return (utime(filename, &utimbuf));