Debian Codegraph @

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