File: unix.c

package info (click to toggle)
nspark 1.7.8B2%2Bgit20210317.cb30779-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, forky, sid, trixie
  • size: 548 kB
  • sloc: ansic: 3,025; makefile: 149; sed: 5
file content (106 lines) | stat: -rw-r--r-- 2,085 bytes parent folder | download
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

/*
 * Operating System specific function (UNIX)
 *
 * $Header: unix.c 1.6 94/12/13 $
 * $Log:	unix.c,v $
 * Revision 1.6  94/12/13  11:36:31  arb
 * Changed use of timelocal() to SunOS 4 only... hope that's right!
 *
 * Revision 1.5  92/12/07  17:18:49  duplain
 * reformatted source.
 * 
 * Revision 1.4  92/10/19  09:35:10  duplain
 * Changed use of timelocal() to SunOS only... hope that's right.
 * 
 * Revision 1.3  92/10/06  12:12:51  duplain
 * Date/time conversion now done by mktime() or timelocal().
 * 
 * Revision 1.2  92/10/01  11:23:10  duplain
 * Added filesize().
 * 
 * Revision 1.1  92/09/29  18:02:28  duplain
 * Initial revision
 * 
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <utime.h>
#include <stdio.h>
#include "spark.h"
#include "date.h"

/*
 * return the length of a file
 */
Word
filesize(char *pathname)
{
	struct stat statb;

	if (stat(pathname, &statb) < 0)
		return (0);
	else
		return ((Word) statb.st_size);
}

/*
 * test for the existance of a file or directory
 */
Ftype
exist(char *pathname)
{
	struct stat statb;

	if (stat(pathname, &statb) < 0)
		return (NOEXIST);		/* assumes error was because file
								   doesn't exist... could be wrong! */
	if (statb.st_mode & S_IFDIR)
		return (ISDIR);
	else
		return (ISFILE);		/* might not be a regular file... */
}

/*
 * make a directory
 */
int
makedir(char *pathname)
{
	return (mkdir(pathname, 0777));
}

/*
 * stamp a file with date and time
 */
int
filestamp(Header *header, char *filename)
{
	Date *date;
	struct tm tm;
	struct utimbuf utimbuf;
	time_t filetime;

	if ((header->load & (Word) 0xfff00000) != (Word) 0xfff00000)
		return (0);				/* not a timestamp */

	memset((char *) &tm, '\0', sizeof(tm));

	if (!(date = makedate(header)))
		return (-1);

	tm.tm_sec = date->second;
	tm.tm_min = date->minute;
	tm.tm_hour = date->hour;
	tm.tm_mday = date->day;
	tm.tm_mon = date->month - 1;
	tm.tm_year = date->year;
	filetime = mktime(&tm);

	utimbuf.actime = filetime;
	utimbuf.modtime = filetime;
	return (utime(filename, &utimbuf));
}