forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathco.c
More file actions
252 lines (215 loc) · 5.55 KB
/
Copy pathco.c
File metadata and controls
252 lines (215 loc) · 5.55 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* co - check out Author: Peter S. Housel 12/24/87 */
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define SUFFIX ",S" /* svc indicator */
#define SVCDIR "SVC" /* svc postfix indicator */
#define LINELEN 256 /* maximum line length */
#ifdef MAXPATHLEN
#define PATHLEN MAXPATHLEN
#else
#define PATHLEN 128 /* buffer length for filenames */
#endif
char file[PATHLEN]; /* file to be checked in */
char svc[PATHLEN]; /* filename for svc file */
char newsvc[PATHLEN]; /* new copy of SVC file */
char line[LINELEN]; /* temporary line buffer */
char *p; /* scratch character pointer */
FILE *svcfp; /* svc file */
int rev; /* old revision number */
int lastrev, lockrev; /* latest file revision, lock into */
int status; /* wait() buffer */
int svclock; /* lock the SVC file */
struct stat stb; /* stat() buffer */
char *base; /* basename of file */
char difftemp[PATHLEN]; /* extract() fix/patch input */
int main(int argc, char **argv);
void fname(char *src, char *dst);
void svcname(char *src, char *dst);
void extract(char *script, char *out, int rev);
char *basename(char *name);
char *whoami(void);
int getyn(void);
int main(argc, argv)
int argc;
char **argv;
{
#ifdef perprintf
char errbuf[BUFSIZ];
setbuf(stderr, errbuf);
perprintf(stderr);
#endif
while (++argv, --argc) {
if ('-' == (*argv)[0]) {
if ('r' == (*argv)[1]) {
--argc;
rev = atoi(*++argv);
if (rev < 1) {
fprintf(stderr, "Illegal revision number\n");
exit(1);
}
} else if ('l' == (*argv)[1])
++svclock;
else {
fprintf(stderr, "co: illegal option -%c\n", (*argv)[1]);
exit(1);
}
} else
break;
}
if (1 != argc) {
fprintf(stderr, "co: bad number of files arguments\n");
exit(1);
}
fname(*argv, file);
svcname(file, svc);
fprintf(stderr, "%s -> %s\n", svc, base = basename(file));
if (NULL == (svcfp = fopen(svc, "r"))) {
perror("co: can't read SVC file");
exit(1);
}
if (1 != fscanf(svcfp, "# %d", &lastrev) || lastrev < 1) {
fprintf(stderr, "co: illegal SVC file format\n");
exit(1);
}
fclose(svcfp);
if (stat(base, &stb) >= 0 && (stb.st_mode & 0222)) {
fprintf(stderr, "Writable %s exists - overwrite (n/y)? ", base);
if (!getyn()) {
fprintf(stderr, "Checkout aborted\n");
exit(1);
}
}
if (strlen(base)) unlink(base);
if (0 == rev) rev = lastrev;
fprintf(stderr, "Checking out revision %d", rev);
extract(svc, base, rev);
if (svclock) {
lockrev = lastrev + 1;
fprintf(stderr, "; Locking into revision %d\n", lockrev);
if (stat(svc, &stb) < 0 || chmod(svc, stb.st_mode | 0200) < 0)
perror("co: can't chmod SVC file");
if (stat(base, &stb) < 0 || chmod(base, stb.st_mode | 0200) < 0)
perror("co: can't chmod source file");
if (NULL == (svcfp = fopen(svc, "a"))
|| (fprintf(svcfp, "#***SVCLOCK*** %s %d\n", whoami(), lockrev), ferror(svcfp))) {
fprintf(stderr, "co: can't lock %s\n", svc);
exit(1);
}
if (stat(svc, &stb) < 0 || chmod(svc, stb.st_mode & 0555))
perror("co: can't chmod SVC file");
} else {
putchar('\n');
if (stat(base, &stb) < 0 || chmod(base, stb.st_mode & 0555))
perror("co: can't chmod source file");
}
return(0);
}
void fname(src, dst)
char *src, *dst;
{
char *p;
strcpy(dst, src);
p = &dst[strlen(src) - strlen(SUFFIX)];
if (!strcmp(p, SUFFIX)) *p = '\0';
}
void svcname(src, dst)
char *src, *dst;
{
char *p;
strcpy(dst, src);
strcat(dst, SUFFIX);
if (0 != access(dst, 4)) {
char dirname[PATHLEN];
if (NULL != (p = strrchr(src, '/')))
strncpy(dirname, src, (size_t)(p - src) + 1);
else
dirname[0] = '\0';
strcat(dirname, SVCDIR);
if (0 == access(dirname, 1)) {
strcpy(dst, dirname);
if (NULL == p) {
strcat(dst, "/");
strcat(dst, src);
} else
strcat(dst, p);
strcat(dst, SUFFIX);
}
}
}
void extract(script, out, rev)
char *script, *out;
int rev;
{
FILE *outfp;
int testrev;
char buf[80];
sprintf(difftemp, "Fix.%s", out);
svcfp = fopen(script, "r");
fgets(line, LINELEN, svcfp); /* skip '# rev' line */
fgets(line, LINELEN, svcfp); /* skip 'cat <***MAIN-eof***' line */
if (NULL == (outfp = fopen(out, "w"))) {
perror("co: can't create output file");
return;
}
while (NULL != fgets(line, LINELEN, svcfp) &&
strcmp(line, "***MAIN-eof***\n"))
fputs(line, outfp);
fclose(outfp);
while (NULL != fgets(line, LINELEN, svcfp)) {
if (!strncmp(line, "if ", (size_t)3)) {
sscanf(line, "if test $2 -ge %d", &testrev);
if (rev >= testrev) {
unlink(difftemp);
return;
}
if (NULL == (outfp = fopen(difftemp, "w"))) {
perror("co: can't create output file");
return;
}
sprintf(buf, "***%d-eof***\n", testrev);
while (NULL != fgets(line, LINELEN, svcfp) &&
strcmp(line, buf))
fputs(line, outfp);
fclose(outfp);
} else if (!strncmp(line, "mv ", (size_t)3)) {
sprintf(buf, "mv Fix.%s %s", out, out);
system(buf);
} else if (!strncmp(line, "fix ", (size_t)4)) {
sprintf(buf, "fix %s Fix.%s > New.%s; mv New.%s %s", out, out, out, out, out);
system(buf);
} else if (!strncmp(line, "patch ", (size_t)6)) {
sprintf(buf, "patch -n -s %s < Fix.%s; rm -f %s.orig", out, out, out);
system(buf);
} else { /* ignore */
}
}
unlink(difftemp);
return;
}
char *basename(name)
char *name;
{
char *p;
if (NULL == (p = strrchr(name, '/')))
return name;
else
return p + 1;
}
char *whoami()
{
struct passwd *pw;
if (NULL != (pw = getpwuid(getuid())))
return pw->pw_name;
else
return "nobody";
}
int getyn()
{
char ans[10];
return(NULL != fgets(ans, 10, stdin)) && ('y' == ans[0] || 'Y' == ans[0]);
}