Debian Codegraph @

tops20/tops20.c

raw
1 /*
2 Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
3
4 See the accompanying file LICENSE, version 1999-Oct-05 or later
5 (the contents of which are also included in zip.h) for terms of use.
6 If, for some reason, both of these files are missing, the Info-ZIP license
7 also may be found at: ftp://ftp.cdrom.com/pub/infozip/license.html
8 */
9 #include "zip.h"
10
11 #ifndef UTIL /* the companion #endif is a bit of ways down ... */
12
13 #include <time.h>
14
15 #define PATH_START '<'
16 #define PATH_END '>'
17
18 /* Extra malloc() space in names for cutpath() */
19 #define PAD 5 /* may have to change .FOO] to ]FOO.DIR;1 */
20
21
22 #define TRUE 1
23 #define FALSE 0
24 #define O_RDONLY (0)
25 #define O_T20_WILD (1<<18)
26 #include <monsym.h> /* Get amazing monsym() macro */
27 extern int jsys(), fstat();
28 extern char *getcwd();
29 extern int _gtjfn(), _rljfn();
30 #define JSYS_CLASS 0070000000000
31 #define FLD(val,mask) (((unsigned)(val)*((mask)&(-(mask))))&(mask))
32 #define _DEFJS(name,class) (FLD(class, JSYS_CLASS) | (monsym(name)&0777777))
33 #define JFNS _DEFJS("JFNS%", 1)
34 #define GNJFN _DEFJS("GNJFN%", 0)
35 static int wfopen(), wfnext(), strlower(), strupper();
36 static char *wfname();
37 typedef struct {
38 int wfjfn;
39 int more;
40 } DIR;
41
42 /* Library functions not in (most) header files */
43
44 extern int stat(), chmod(), toupper(), tolower();
45
46 int utime OF((char *, ztimbuf *));
47
48 extern char *label;
49 local ulg label_time = 0;
50 local ulg label_mode = 0;
51 local time_t label_utim = 0;
52
53 /* Local functions */
54 local char *readd OF((DIR *));
55
56
57 local DIR *opendir(n)
58 char *n; /* directory name to open */
59 /* Open the directory *n, returning a pointer to an allocated DIR, or
60 NULL if error. */
61 {
62 DIR *d; /* pointer to malloc'ed directory stream */
63 char *c; /* scans TOPS20 path */
64 int m; /* length of name */
65 char *p; /* temp string */
66
67 if (((d = (DIR *)malloc(sizeof(DIR))) == NULL) ||
68 ((p = (char *)malloc((m = strlen(n)) + 4)) == NULL)) {
69 return NULL;
70 }
71
72 /* Directory may be in form "<DIR.SUB1.SUB2>" or "<DIR.SUB1>SUB2.DIRECTORY".
73 ** If latter, convert to former. */
74
75 if ((m > 0) && (*(c = strcpy(p,n) + m-1) != '>')) {
76 c -= 10;
77 *c-- = '\0'; /* terminate at "DIRECTORY.1" */
78 *c = '>'; /* "." --> ">" */
79 while ((c > p) && (*--c != '>'));
80 *c = '.'; /* ">" --> "." */
81 }
82 strcat(p, "*.*");
83 if ((d->wfjfn = wfopen(p)) == 0) {
84 free((zvoid *)d);
85 free((zvoid *)p);
86 return NULL;
87 }
88 free((zvoid *)p);
89 d->more = TRUE;
90 return (d);
91 }
92
93 local char *readd(d)
94 DIR *d; /* directory stream to read from */
95 /* Return a pointer to the next name in the directory stream d, or NULL if
96 no more entries or an error occurs. */
97 {
98 char *p;
99 if ((d->more == FALSE) || ((p = wfname(d->wfjfn)) == NULL)) {
100 return NULL;
101 }
102 if (wfnext(d->wfjfn) == 0) {
103 d->more = FALSE;
104 }
105 return p;
106 }
107
108
109 local void closedir(d)
110 DIR *d; /* directory stream to close */
111 /* Close the directory stream */
112 {
113 free((zvoid *)d);
114 }
115
116 /* Wildcard filename routines */
117
118 /* WFOPEN - open wild card filename
119 ** Returns wild JFN for filespec, 0 if failure.
120 */
121 static int
122 wfopen(name)
123 char *name;
124 {
125 return (_gtjfn(name, (O_RDONLY | O_T20_WILD)));
126 }
127
128 /* WFNAME - Return filename for wild JFN
129 ** Returns pointer to dynamically allocated filename string
130 */
131 static char *
132 wfname(jfn)
133 int jfn;
134 {
135 char *fp, fname[200];
136 int ablock[5];
137
138 ablock[1] = (int) (fname - 1);
139 ablock[2] = jfn & 0777777; /* jfn, no flags */
140 ablock[3] = 0111110000001; /* DEV+DIR+NAME+TYPE+GEN, punctuate */
141 if (!jsys(JFNS, ablock))
142 return NULL; /* something bad happened */
143 if ((fp = (char *)malloc(strlen(fname) + 1)) == NULL) {
144 return NULL;
145 }
146 strcpy(fp, fname); /* copy the file name here */
147 return fp;
148 }
149
150 /* WFNEXT - Make wild JFN point to next real file
151 ** Returns success or failure (not JFN)
152 */
153 static int
154 wfnext(jfn)
155 int jfn;
156 {
157 int ablock[5];
158
159 ablock[1] = jfn; /* save jfn and flags */
160 return jsys(GNJFN, ablock);
161 }
162
163
164 static int
165 strupper(s) /* Returns s in uppercase */
166 char *s; /* String to be uppercased */
167 {
168 char *p;
169
170 p = s;
171 for (; *p; p++)
172 *p = toupper (*p);
173 }
174
175 static int
176 strlower(s) /* Returns s in lowercase. */
177 char *s; /* String to be lowercased */
178 {
179 char *p;
180
181 p = s;
182 for (; *p; p++)
183 *p = tolower (*p);
184 }
185
186 int procname(n, caseflag)
187 char *n; /* name to process */
188 int caseflag; /* true to force case-sensitive match */
189 /* Process a name or sh expression to operate on (or exclude). Return
190 an error code in the ZE_ class. */
191 {
192 DIR *d; /* directory stream from opendir() */
193 char *e; /* pointer to name from readd() */
194 int m; /* matched flag */
195 char *p; /* path for recursion */
196 struct stat s; /* result of stat() */
197 struct zlist far *z; /* steps through zfiles list */
198
199 if (strcmp(n, "-") == 0) /* if compressing stdin */
200 return newname(n, 0, caseflag);
201 else if (LSSTAT(n, &s))
202 {
203 /* Not a file or directory--search for shell expression in zip file */
204 if (caseflag) {
205 p = malloc(strlen(n) + 1);
206 if (p != NULL)
207 strcpy(p, n);
208 } else
209 p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
210 m = 1;
211 for (z = zfiles; z != NULL; z = z->nxt) {
212 if (MATCH(p, z->iname, caseflag))
213 {
214 z->mark = pcount ? filter(z->zname, caseflag) : 1;
215 if (verbose)
216 fprintf(mesg, "zip diagnostic: %scluding %s\n",
217 z->mark ? "in" : "ex", z->name);
218 m = 0;
219 }
220 }
221 free((zvoid *)p);
222 return m ? ZE_MISS : ZE_OK;
223 }
224
225 /* Live name--use if file, recurse if directory */
226 if ((s.st_mode & S_IFDIR) == 0)
227 {
228 /* add or remove name of file */
229 if ((m = newname(n, 0, caseflag)) != ZE_OK)
230 return m;
231 } else {
232 if (dirnames && (m = newname(n, 1, caseflag)) != ZE_OK) {
233 return m;
234 }
235 /* recurse into directory */
236 if (recurse && (d = opendir(n)) != NULL)
237 {
238 while ((e = readd(d)) != NULL) {
239 if ((m = procname(e, caseflag)) != ZE_OK) /* recurse on name */
240 {
241 closedir(d);
242 return m;
243 }
244 }
245 closedir(d);
246 }
247 } /* (s.st_mode & S_IFDIR) == 0) */
248 return ZE_OK;
249 }
250
251 char *ex2in(x, isdir, pdosflag)
252 char *x; /* external file name */
253 int isdir; /* input: x is a directory */
254 int *pdosflag; /* output: force MSDOS file attributes? */
255 /* Convert the external file name to a zip file name, returning the malloc'ed
256 string or NULL if not enough memory. */
257 {
258 char *n; /* internal file name (malloc'ed) */
259 char *t; /* shortened name */
260 int dosflag;
261
262 int jfn;
263 char *fp, fname[200];
264 int ablock[5];
265
266 jfn = _gtjfn(x, (O_RDONLY));
267 ablock[1] = (int) (fname - 1);
268 ablock[2] = jfn & 0777777; /* jfn, no flags */
269 ablock[3] = 0111100000001; /* DEV+DIR+NAME+TYPE, punctuate */
270 if (!jsys(JFNS, ablock)) {
271 _rljfn(jfn);
272 return NULL; /* something bad happened */
273 }
274 _rljfn(jfn);
275 if ((fp = (char *)malloc(strlen(fname) + 1)) == NULL) {
276 return NULL;
277 }
278 strcpy(fp, fname); /* copy the file name here */
279 x = fp;
280
281 dosflag = dosify; /* default for non-DOS and non-OS/2 */
282
283 /* Find starting point in name before doing malloc */
284 t = x;
285 if ((n = strrchr(t, ':')) != NULL)
286 t = n + 1;
287 if (*t == PATH_START && (n = strrchr(t, PATH_END)) != NULL)
288 if (*(++t) == '.')
289 /* path is relative to current directory, skip leading '.' */
290 t++;
291
292 /* Make changes, if any, to the copied name (leave original intact) */
293 if (!pathput)
294 t = last(t, PATH_END);
295
296 /* Malloc space for internal name and copy it */
297 if ((n = malloc(strlen(t) + 1)) == NULL)
298 return NULL;
299 strcpy(n, t);
300
301 if ((t = strrchr(n, PATH_END)) != NULL)
302 {
303 *t = '/';
304 while (--t > n)
305 if (*t == '.')
306 *t = '/';
307 }
308
309 /* Fix from Greg Roelofs: */
310 /* Get current working directory and strip from n (t now = n) */
311 {
312 char cwd[256], *p, *q;
313 int c;
314
315 if (getcwd(cwd, 256) && ((p = strchr(cwd, PATH_START)) != NULL))
316 {
317 if (*(++p) == '.')
318 p++;
319 if ((q = strrchr(p, PATH_END)) != NULL)
320 {
321 *q = '/';
322 while (--q > p)
323 if (*q == '.')
324 *q = '/';
325
326 /* strip bogus path parts from n */
327 if (strncmp(n, p, (c=strlen(p))) == 0)
328 {
329 q = n + c;
330 while (*t++ = *q++)
331 ;
332 }
333 }
334 }
335 }
336 strlower(n);
337
338 if (isdir)
339 {
340 if (strcmp((t=n+strlen(n)-6), ".dir;1"))
341 error("directory not version 1");
342 else
343 strcpy(t, "/");
344 }
345
346 if ((t = strrchr(n, '.')) != NULL)
347 {
348 if ( t[1] == '\0') /* "filename." -> "filename" */
349 *t = '\0';
350 }
351
352 if (dosify)
353 msname(n);
354
355 /* Returned malloc'ed name */
356 if (pdosflag)
357 *pdosflag = dosflag;
358 return n;
359 }
360
361
362 char *in2ex(n)
363 char *n; /* internal file name */
364 /* Convert the zip file name to an external file name, returning the malloc'ed
365 string or NULL if not enough memory. */
366 {
367 char *x; /* external file name */
368 char *t; /* scans name */
369
370 if ((t = strrchr(n, '/')) == NULL)
371 {
372 if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
373 return NULL;
374 strcpy(x, n);
375 }
376 else
377 {
378 if ((x = malloc(strlen(n) + 3 + PAD)) == NULL)
379 return NULL;
380 x[0] = PATH_START;
381 x[1] = '.';
382 strcpy(x + 2, n);
383 *(t = x + 2 + (t - n)) = PATH_END;
384 while (--t > x)
385 if (*t == '/')
386 *t = '.';
387 }
388 strupper(x);
389
390 return x;
391 }
392
393 void stamp(f, d)
394 char *f; /* name of file to change */
395 ulg d; /* dos-style time to change it to */
396 /* Set last updated and accessed time of file f to the DOS time d. */
397 {
398 ztimbuf u; /* argument for utime() */
399
400 /* Convert DOS time to time_t format in u */
401 u.actime = u.modtime = dos2unixtime(d);
402
403 /* Set updated and accessed times of f */
404 utime(f, &u);
405 }
406
407 ulg filetime(f, a, n, t)
408 char *f; /* name of file to get info on */
409 ulg *a; /* return value: file attributes */
410 long *n; /* return value: file size */
411 iztimes *t; /* return value: access, modific. and creation times */
412 /* If file *f does not exist, return 0. Else, return the file's last
413 modified date and time as an MSDOS date and time. The date and
414 time is returned in a long with the date most significant to allow
415 unsigned integer comparison of absolute times. Also, if a is not
416 a NULL pointer, store the file attributes there, with the high two
417 bytes being the Unix attributes, and the low byte being a mapping
418 of that to DOS attributes. If n is not NULL, store the file size
419 there. If t is not NULL, the file's access, modification and creation
420 times are stored there as UNIX time_t values.
421 If f is "-", use standard input as the file. If f is a device, return
422 a file size of -1 */
423 {
424 struct stat s; /* results of stat() */
425 /* converted from FNAMX to malloc - 11/8/04 EG */
426 char *name;
427 int len = strlen(f);
428
429 if (f == label) {
430 if (a != NULL)
431 *a = label_mode;
432 if (n != NULL)
433 *n = -2L; /* convention for a label name */
434 if (t != NULL)
435 t->atime = t->mtime = t->ctime = label_utim;
436 return label_time;
437 }
438 if ((name = malloc(len + 1)) == NULL) {
439 ZIPERR(ZE_MEM, "filetime");
440 }
441 strcpy(name, f);
442 if (name[len - 1] == '/')
443 name[len - 1] = '\0';
444 /* not all systems allow stat'ing a file with / appended */
445
446 if (strcmp(f, "-") == 0) {
447 if (fstat(fileno(stdin), &s) != 0) {
448 free(name);
449 error("fstat(stdin)");
450 }
451 } else if (LSSTAT(name, &s) != 0) {
452 /* Accept about any file kind including directories
453 * (stored with trailing / with -r option)
454 */
455 free(name);
456 return 0;
457 }
458 free(name);
459
460 if (a != NULL) {
461 *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
462 if ((s.st_mode & S_IFDIR) != 0) {
463 *a |= MSDOS_DIR_ATTR;
464 }
465 }
466 if (n != NULL)
467 *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
468 if (t != NULL) {
469 t->atime = s.st_atime;
470 t->mtime = s.st_mtime;
471 t->ctime = s.st_ctime;
472 }
473
474 return unix2dostime(&s.st_mtime);
475 }
476
477 #include <monsym.h> /* Get amazing monsym() macro */
478 #define _FBBYV monsym(".FBBYV")
479 #define FBBSZ_S -24 /* Obsolete, replace by FLDGET! */
480 #define FBBSZ_M 077 /* ditto */
481
482 extern int _gtjfn(), _rljfn(), _gtfdb(), stat();
483
484 int set_extra_field(z, z_utim)
485 struct zlist far *z;
486 iztimes *z_utim;
487 /* create extra field and change z->att if desired */
488 {
489 int jfn;
490
491 translate_eol = 0;
492 jfn = _gtjfn(z->name, O_RDONLY);
493 z->att = (((_gtfdb (jfn, _FBBYV) << FBBSZ_S) & FBBSZ_M) != 8) ?
494 ASCII :BINARY;
495 _rljfn(jfn);
496
497 #ifdef USE_EF_UT_TIME
498 if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)
499 return ZE_MEM;
500
501 z->extra[0] = 'U';
502 z->extra[1] = 'T';
503 z->extra[2] = EB_UT_LEN(1); /* length of data part of e.f. */
504 z->extra[3] = 0;
505 z->extra[4] = EB_UT_FL_MTIME;
506 z->extra[5] = (char)(z_utim->mtime);
507 z->extra[6] = (char)(z_utim->mtime >> 8);
508 z->extra[7] = (char)(z_utim->mtime >> 16);
509 z->extra[8] = (char)(z_utim->mtime >> 24);
510
511 z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));
512 z->cextra = z->extra;
513 #endif /* USE_EF_UT_TIME */
514
515 return ZE_OK;
516 }
517
518 int deletedir(d)
519 char *d; /* directory to delete */
520 /* Delete the directory *d if it is empty, do nothing otherwise.
521 Return the result of rmdir(), delete(), or system().
522 For VMS, d must be in format [x.y]z.dir;1 (not [x.y.z]).
523 */
524 {
525 zipwarn("deletedir not implemented yet", "");
526 return 127;
527 }
528
529 #endif /* !UTIL */
530
531
532 /******************************/
533 /* Function version_local() */
534 /******************************/
535
536 void version_local()
537 {
538 static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
539 #if 0
540 char buf[40];
541 #endif
542
543 printf(CompiledWith,
544
545 #ifdef __GNUC__
546 "gcc ", __VERSION__,
547 #else
548 # if 0
549 "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
550 # else
551 # ifdef __COMPILER_KCC__
552 "KCC", "",
553 # else
554 "unknown compiler", "",
555 # endif
556 # endif
557 #endif
558
559 "TOPS-20",
560
561 #if defined(foobar) || defined(FOOBAR)
562 " (Foo BAR)", /* OS version or hardware */
563 #else
564 "",
565 #endif /* Foo BAR */
566
567 #ifdef __DATE__
568 " on ", __DATE__
569 #else
570 "", ""
571 #endif
572 );
573
574 } /* end function version_local() */