tops20.c 15 KB

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