qfileio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include <utime.h>
  12. #include <errno.h>
  13. #ifdef S_IWRITE
  14. # undef S_IWRITE
  15. #endif /* S_IWRITE */
  16. #define S_IWRITE S_IWUSR
  17. extern char *label;
  18. local ulg label_time = 0;
  19. local ulg label_mode = 0;
  20. local time_t label_utim = 0;
  21. typedef time_t statime;
  22. #define PAD 0
  23. #define PATH_END '/'
  24. /* Local functions */
  25. int procname(n, caseflag)
  26. char *n; /* name to process */
  27. int caseflag; /* true to force case-sensitive match */
  28. /* Process a name or sh expression to operate on (or exclude). Return
  29. an error code in the ZE_ class. */
  30. {
  31. char *a; /* path and name for recursion */
  32. int m; /* matched flag */
  33. char *p; /* path for recursion */
  34. struct stat s; /* result of stat() */
  35. struct zlist far *z; /* steps through zfiles list */
  36. if (strcmp(n, "-") == 0) /* if compressing stdin */
  37. return newname(n, 0, caseflag);
  38. else if (SSTAT(n, &s) )
  39. {
  40. /* Not a file or directory--search for shell expression in zip file */
  41. p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */
  42. m = 1;
  43. for (z = zfiles; z != NULL; z = z->nxt) {
  44. if (MATCH(p, z->iname, caseflag))
  45. {
  46. z->mark = pcount ? filter(z->zname, caseflag) : 1;
  47. if (verbose)
  48. fprintf(mesg, "zip diagnostic: %scluding %s\n",
  49. z->mark ? "in" : "ex", z->name);
  50. m = 0;
  51. }
  52. }
  53. free(p);
  54. return m ? ZE_MISS : ZE_OK;
  55. }
  56. /* Live name--use if file, recurse if directory */
  57. if ((s.st_mode & S_IFDIR) == 0)
  58. {
  59. /* add or remove name of file */
  60. if ((m = newname(n, 0, caseflag)) != ZE_OK)
  61. return m;
  62. }
  63. return ZE_OK;
  64. }
  65. char *ex2in(x, isdir, pdosflag)
  66. char *x; /* external file name */
  67. int isdir; /* input: x is a directory */
  68. int *pdosflag; /* output: force MSDOS file attributes? */
  69. /* Convert the external file name to a zip file name, returning the malloc'ed
  70. string or NULL if not enough memory. */
  71. {
  72. char *n; /* internal file name (malloc'ed) */
  73. char *t; /* shortened name */
  74. int dosflag;
  75. t = ql2Unix(x);
  76. dosflag = dosify; /* default for non-DOS and non-OS/2 */
  77. /* Make changes, if any, to the copied name (leave original intact) */
  78. if (!pathput)
  79. t = last(t, PATH_END);
  80. /* Discard directory names with zip -rj */
  81. if (*t == '\0')
  82. return t;
  83. /* Malloc space for internal name and copy it */
  84. if ((n = malloc(strlen(t) + 1)) == NULL)
  85. return NULL;
  86. strcpy(n, t);
  87. if (dosify)
  88. msname(n);
  89. /* Returned malloc'ed name */
  90. if (pdosflag)
  91. *pdosflag = dosflag;
  92. return n;
  93. }
  94. char *in2ex(n)
  95. char *n; /* internal file name */
  96. /* Convert the zip file name to an external file name, returning the malloc'ed
  97. string or NULL if not enough memory. */
  98. {
  99. char *x; /* external file name */
  100. if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  101. return NULL;
  102. strcpy(x, Unix2ql(n, NULL));
  103. return x;
  104. }
  105. void stamp(f, d)
  106. char *f; /* name of file to change */
  107. ulg d; /* dos-style time to change it to */
  108. /* Set last updated and accessed time of file f to the DOS time d. */
  109. {
  110. struct utimbuf u; /* argument for utime() const ?? */
  111. /* Convert DOS time to time_t format in u */
  112. u.actime = u.modtime = dos2unixtime(d);
  113. utime(f, &u);
  114. }
  115. ulg filetime(f, a, n, t)
  116. char *f; /* name of file to get info on */
  117. ulg *a; /* return value: file attributes */
  118. long *n; /* return value: file size */
  119. iztimes *t; /* return value: access, modific. and creation times */
  120. /* If file *f does not exist, return 0. Else, return the file's last
  121. modified date and time as an MSDOS date and time. The date and
  122. time is returned in a long with the date most significant to allow
  123. unsigned integer comparison of absolute times. Also, if a is not
  124. a NULL pointer, store the file attributes there, with the high two
  125. bytes being the Unix attributes, and the low byte being a mapping
  126. of that to DOS attributes. If n is not NULL, store the file size
  127. there. If t is not NULL, the file's access, modification and creation
  128. times are stored there as UNIX time_t values.
  129. If f is "-", use standard input as the file. If f is a device, return
  130. a file size of -1 */
  131. {
  132. struct stat s; /* results of stat() */
  133. /* from FNMAX to malloc - 11/8/04 EG */
  134. char *name;
  135. int len = strlen(f);
  136. if (f == label) {
  137. if (a != NULL)
  138. *a = label_mode;
  139. if (n != NULL)
  140. *n = -2L; /* convention for a label name */
  141. if (t != NULL)
  142. t->atime = t->mtime = t->ctime = label_utim;
  143. return label_time;
  144. }
  145. if ((name = malloc(len + 1)) == NULL) {
  146. ZIPERR(ZE_MEM, "filetime");
  147. }
  148. strcpy(name, f);
  149. if (name[len - 1] == '/')
  150. name[len - 1] = '\0';
  151. /* not all systems allow stat'ing a file with / appended */
  152. if (strcmp(f, "-") == 0) {
  153. if (fstat(fileno(stdin), &s) != 0) {
  154. free(name);
  155. error("fstat(stdin)");
  156. }
  157. } else if (LSSTAT(name, &s) != 0) {
  158. /* Accept about any file kind including directories
  159. * (stored with trailing / with -r option)
  160. */
  161. free(name);
  162. return 0;
  163. }
  164. free(name);
  165. if (a != NULL) {
  166. *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  167. if ((s.st_mode & S_IFMT) == S_IFDIR) {
  168. *a |= MSDOS_DIR_ATTR;
  169. }
  170. }
  171. if (n != NULL)
  172. *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  173. if (t != NULL) {
  174. t->atime = s.st_atime;
  175. t->mtime = s.st_mtime;
  176. t->ctime = s.st_ctime;
  177. }
  178. return unix2dostime(&s.st_mtime);
  179. }
  180. int deletedir(d)
  181. char *d; /* directory to delete */
  182. /* Delete the directory *d if it is empty, do nothing otherwise.
  183. Return the result of rmdir(), delete(), or system().
  184. For VMS, d must be in format [x.y]z.dir;1 (not [x.y.z]).
  185. */
  186. {
  187. return rmdir(d);
  188. }
  189. #endif /* !UTIL */
  190. void version_local()
  191. {
  192. puts ("Compiled with c68 v4.2x on " __DATE__);
  193. }