-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathos-posix.c
More file actions
168 lines (156 loc) · 3.11 KB
/
os-posix.c
File metadata and controls
168 lines (156 loc) · 3.11 KB
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
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#ifndef NO_POSIX_SPAWN
#include <spawn.h>
#endif
#include "graph.h"
#include "os.h"
#include "util.h"
void
osgetcwd(char *buf, size_t len)
{
if (!getcwd(buf, len))
fatal("getcwd:");
}
void
oschdir(const char *dir)
{
if (chdir(dir) < 0)
fatal("chdir %s:", dir);
}
int
osmkdirs(struct string *path, bool parent)
{
int ret;
struct stat st;
char *s, *end;
ret = 0;
end = path->s + path->n;
for (s = end - parent; s > path->s; --s) {
if (*s != '/' && *s)
continue;
*s = '\0';
if (stat(path->s, &st) == 0)
break;
if (errno != ENOENT) {
warn("stat %s:", path->s);
ret = -1;
break;
}
}
if (s > path->s && s < end)
*s = '/';
while (++s <= end - parent) {
if (*s != '\0')
continue;
if (ret == 0 && mkdir(path->s, 0777) < 0 && errno != EEXIST) {
warn("mkdir %s:", path->s);
ret = -1;
}
if (s < end)
*s = '/';
}
return ret;
}
int64_t
osmtime(const char *name)
{
struct stat st;
if (stat(name, &st) < 0) {
if (errno != ENOENT)
fatal("stat %s:", name);
return MTIME_MISSING;
} else {
#ifdef __APPLE__
return (int64_t)st.st_mtime * 1000000000 + st.st_mtimensec;
/*
Illumos hides the members of st_mtim when you define _POSIX_C_SOURCE
since it has not been updated to support POSIX.1-2008:
https://www.illumos.org/issues/13327
*/
#elif defined(__sun)
return (int64_t)st.st_mtim.__tv_sec * 1000000000 + st.st_mtim.__tv_nsec;
#else
return (int64_t)st.st_mtim.tv_sec * 1000000000 + st.st_mtim.tv_nsec;
#endif
}
}
long
osnproc(void)
{
#ifdef _SC_NPROCESSORS_ONLN
return sysconf(_SC_NPROCESSORS_ONLN);
#else
return 1;
#endif
}
pid_t
osspawn(char *const argv[], int outfd)
{
#ifdef NO_POSIX_SPAWN
pid_t pid;
int i, fd[3];
pid = fork();
switch (pid) {
case 0:
if (outfd != -1) {
fd[0] = open("/dev/null", O_RDONLY | O_CLOEXEC);
if (fd[0] == -1)
_exit(1);
fd[1] = outfd;
fd[2] = outfd;
for (i = 0; i <= 2; ++i) {
if (dup2(fd[i], i) == -1)
_exit(1);
}
}
execvp(argv[0], argv);
_exit(1);
/* unreachable */
return -1;
default:
return pid;
case -1:
warn("fork:");
return -1;
}
#else
extern char **environ;
pid_t pid;
posix_spawn_file_actions_t actions;
if ((errno = posix_spawn_file_actions_init(&actions))) {
warn("posix_spawn_file_actions_init:");
goto err0;
}
if (outfd != -1) {
if ((errno = posix_spawn_file_actions_addopen(&actions, 0, "/dev/null", O_RDONLY, 0))) {
warn("posix_spawn_file_actions_adddup2:");
goto err1;
}
if ((errno = posix_spawn_file_actions_adddup2(&actions, outfd, 1))) {
warn("posix_spawn_file_actions_adddup2:");
goto err1;
}
if ((errno = posix_spawn_file_actions_adddup2(&actions, outfd, 2))) {
warn("posix_spawn_file_actions_adddup2:");
goto err1;
}
}
if ((errno = posix_spawn(&pid, argv[0], &actions, NULL, argv, environ))) {
warn("posix_spawn %s:", argv[0]);
goto err1;
}
posix_spawn_file_actions_destroy(&actions);
return pid;
err1:
posix_spawn_file_actions_destroy(&actions);
err0:
return -1;
#endif
}