2
Copyright (c) 1990-1999 Info-ZIP. All rights reserved.
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
10
* zipnote.c by Mark Adler.
18
#define DEFCPYRT /* main module: enable copyright string defines! */
23
/* Character to mark zip entry names in the comment file */
25
#define MARKE " (comment above this line)"
26
#define MARKZ " (zip file comment below this line)"
28
/* Temporary zip file name and file pointer */
34
local void handler OF((int));
35
local void license OF((void));
36
local void help OF((void));
37
local void version_info OF((void));
38
local void putclean OF((char *, extent));
39
local char *getline OF((char *, extent));
40
local int catalloc OF((char * far *, char *));
41
int main OF((int, char **));
44
#define ziperr(c, h) zipnoteerr(c, h)
45
#define zipwarn(a, b) zipnotewarn(a, b)
47
void zipnoteerr(int c,char *h);
48
void zipnotewarn(char *a,char *b);
52
#define exit(p1) QDOSexit()
56
int c; /* error code from the ZE_ class */
57
char *h; /* message about how it happened */
58
/* Issue a message for the error, clean up files and memory, and exit. */
61
perror("zipnote error");
62
fprintf(stderr, "zipnote error: %s (%s)\n", errors[c-1], h);
68
free((zvoid *)tempzip);
71
free((zvoid *)zipfile);
77
int s; /* signal number (ignored) */
78
/* Upon getting a user interrupt, abort cleanly using ziperr(). */
83
ziperr(ZE_ABORT, "aborting");
84
s++; /* keep some compilers happy */
89
char *a, *b; /* message strings juxtaposed in output */
90
/* Print a warning message to stderr and return. */
92
fprintf(stderr, "zipnote warning: %s%s\n", a, b);
97
/* Print license information to stdout. */
99
extent i; /* counter for copyright array */
101
for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
102
printf(copyright[i], "zipnote");
105
for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++)
111
/* Print help (along with license info) to stdout. */
113
extent i; /* counter for help array */
116
static ZCONST char *text[] = {
120
"Usage: zipnote [-w] [-b fm] zipfile",
122
"Usage: zipnote [-w] [-b path] zipfile",
124
" the default action is to write the comments in zipfile to stdout",
125
" -w write the zipfile comments from stdin",
127
" -b use \"fm\" as the filemode for the temporary zip file",
129
" -b use \"path\" for the temporary zip file",
131
" -h show this help -v show version info -L show software license",
135
" define/user sys$output foo.tmp",
138
" ... then you edit the comments, save, and exit ...",
139
" define/user sys$input foo.tmp",
140
" zipnote -w foo.zip",
143
" zipnote foo/zip > foo/tmp",
145
" ... then you edit the comments, save, and exit ...",
146
" zipnote -w foo/zip < foo/tmp",
149
" zipnote foo.zip > foo.tmp",
151
" ... then you edit the comments, save, and exit ...",
152
" zipnote -w foo.zip < foo.tmp",
154
" zipnote foo.zip > foo.tmp",
156
" ... then you edit the comments, save, and exit ...",
157
" zipnote -w foo.zip < foo.tmp",
162
" \"@ name\" can be followed by an \"@=newname\" line to change the name"
165
for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
166
printf(copyright[i], "zipnote");
169
for (i = 0; i < sizeof(text)/sizeof(char *); i++)
171
printf(text[i], VERSION, REVDATE);
177
* XXX put this in version.c
180
local void version_info()
181
/* Print verbose info about program version and compile time options
184
extent i; /* counter in text arrays */
186
/* Options info array */
187
static ZCONST char *comp_opts[] = {
194
for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
196
printf(copyright[i], "zipnote");
200
for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
202
printf(versinfolines[i], "ZipNote", VERSION, REVDATE);
208
puts("ZipNote special compilation options:");
209
for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
211
printf("\t%s\n",comp_opts[i]);
218
local void putclean(s, n)
219
char *s; /* string to write to stdout */
220
extent n; /* length of string */
221
/* Write the string s to stdout, filtering out control characters that are
222
not tab or newline (mainly to remove carriage returns), and prefix MARK's
223
and backslashes with a backslash. Also, terminate with a newline if
226
int c; /* next character in string */
227
int e; /* last character written */
229
e = '\n'; /* if empty, write nothing */
233
if (c == MARK || c == '\\')
235
if (c >= ' ' || c == '\t' || c == '\n')
243
local char *getline(buf, size)
246
/* Read a line of text from stdin into string buffer 'buf' of size 'size'.
247
In case of buffer overflow or EOF, a NULL pointer is returned. */
252
line = fgets(buf, size, stdin);
253
if (line != NULL && (len = strlen(line)) > 0) {
254
if (len == size-1 && line[len-1] != '\n') {
255
/* buffer is full and record delimiter not seen -> overflow */
258
/* delete trailing record delimiter */
259
if (line[len-1] == '\n') line[len-1] = '\0';
266
local int catalloc(a, s)
267
char * far *a; /* pointer to a pointer to a malloc'ed string */
268
char *s; /* string to concatenate on a */
269
/* Concatentate the string s to the malloc'ed string pointed to by a.
270
Preprocess s by removing backslash escape characters. */
272
char *p; /* temporary pointer */
273
char *q; /* temporary pointer */
275
for (p = q = s; *q; *p++ = *q++)
276
if (*q == '\\' && *(q+1))
279
if ((p = malloc(strlen(*a) + strlen(s) + 3)) == NULL)
281
strcat(strcat(strcpy(p, *a), **a ? "\r\n" : ""), s);
288
#ifndef USE_ZIPNOTEMAIN
291
int zipnotemain(argc, argv)
293
int argc; /* number of tokens in command line */
294
char **argv; /* command line tokens */
295
/* Write the comments in the zipfile to stdout, or read them from stdin. */
297
char a[FNMAX+1]; /* input line buffer */
298
ulg c; /* start of central directory */
299
int k; /* next argument type */
300
char *q; /* steps through option arguments */
301
int r; /* arg counter, temporary variable */
302
ulg s; /* length of central directory */
303
int t; /* attributes of zip file */
304
int w; /* true if updating zip file from stdin */
305
FILE *x, *y; /* input and output zip files */
306
struct zlist far *z; /* steps through zfiles linked list */
309
setlocale(LC_CTYPE, "I");
312
/* If no args, show help */
319
init_upper(); /* build case map table */
321
/* Go through args */
322
zipfile = tempzip = NULL;
325
for (r = 1; r < argc; r++)
326
if (*argv[r] == '-') {
328
for (q = argv[r]+1; *q; q++)
331
case 'b': /* Specify path for temporary file */
333
ziperr(ZE_PARMS, "use -b before zip file name");
335
k = 1; /* Next non-option is path */
337
case 'h': /* Show help */
339
case 'l': case 'L': /* Show copyright and disclaimer */
341
case 'v': /* Show version info */
342
version_info(); EXIT(0);
346
ziperr(ZE_PARMS, "unknown option");
349
ziperr(ZE_PARMS, "zip file cannot be stdin");
355
if ((zipfile = ziptyp(argv[r])) == NULL)
356
ziperr(ZE_MEM, "was processing arguments");
359
ziperr(ZE_PARMS, "can only specify one zip file");
367
ziperr(ZE_PARMS, "need to specify zip file");
370
if ((r = readzipfile()) != ZE_OK)
373
ziperr(ZE_NAME, zipfile);
375
/* Put comments to stdout, if not -w */
378
for (z = zfiles; z != NULL; z = z->nxt)
380
printf("%c %s\n", MARK, z->zname);
381
putclean(z->comment, z->com);
382
printf("%c%s\n", MARK, MARKE);
384
printf("%c%s\n", MARK, MARKZ);
385
putclean(zcomment, zcomlen);
389
/* If updating comments, make sure zip file is writeable */
390
if ((x = fopen(zipfile, "a")) == NULL)
391
ziperr(ZE_CREAT, zipfile);
393
t = getfileattr(zipfile);
395
/* Process stdin, replacing comments */
397
while (getline(a, FNMAX+1) != NULL && (a[0] != MARK || strcmp(a + 1, MARKZ)))
398
{ /* while input and not file comment */
399
if (a[0] != MARK || a[1] != ' ') /* better be "@ name" */
400
ziperr(ZE_NOTE, "unexpected input");
401
while (z != NULL && strcmp(a + 2, z->zname))
402
z = z->nxt; /* allow missing entries in order */
404
ziperr(ZE_NOTE, "unknown entry name");
405
if (getline(a, FNMAX+1) != NULL && a[0] == MARK && a[1] == '=')
407
if (z->name != z->iname)
408
free((zvoid *)z->iname);
409
if ((z->iname = malloc(strlen(a+1))) == NULL)
410
ziperr(ZE_MEM, "was changing name");
412
strtoasc(z->iname, a+2);
414
strcpy(z->iname, a+2);
418
* Don't update z->nam here, we need the old value a little later.....
419
* The update is handled in zipcopy().
423
if (z->com) /* change zip entry comment */
424
free((zvoid *)z->comment);
425
z->comment = malloc(1); *(z->comment) = 0;
426
while (a != NULL && *a != MARK)
428
if ((r = catalloc(&(z->comment), a)) != ZE_OK)
429
ziperr(r, "was building new comments");
432
z->com = strlen(z->comment);
433
z = z->nxt; /* point to next entry */
435
if (a != NULL) /* change zip file comment */
437
zcomment = malloc(1); *zcomment = 0;
438
while (getline(a, FNMAX+1) != NULL)
439
if ((r = catalloc(&zcomment, a)) != ZE_OK)
440
ziperr(r, "was building new comments");
441
zcomlen = strlen(zcomment);
444
/* Open output zip file for writing */
445
if ((tempzf = y = fopen(tempzip = tempname(zipfile), FOPW)) == NULL)
446
ziperr(ZE_TEMP, tempzip);
448
/* Open input zip file again, copy preamble if any */
449
if ((x = fopen(zipfile, FOPR)) == NULL)
450
ziperr(ZE_NAME, zipfile);
451
if (zipbeg && (r = fcopy(x, y, zipbeg)) != ZE_OK)
452
ziperr(r, r == ZE_TEMP ? tempzip : zipfile);
455
/* Go through local entries, copying them over as is */
456
fix = 3; /* needed for zipcopy if name changed */
457
for (z = zfiles; z != NULL; z = z->nxt) {
458
if ((r = zipcopy(z, x, y)) != ZE_OK)
459
ziperr(r, "was copying an entry");
463
/* Write central directory and end of central directory with new comments */
464
if ((c = ftell(y)) == (ulg)(-1L)) /* get start of central */
465
ziperr(ZE_TEMP, tempzip);
466
for (z = zfiles; z != NULL; z = z->nxt)
467
if ((r = putcentral(z, y)) != ZE_OK)
469
if ((s = ftell(y)) == (ulg)-1L) /* get end of central */
470
ziperr(ZE_TEMP, tempzip);
471
s -= c; /* compute length of central */
472
if ((r = putend((int)zcount, s, c, zcomlen, zcomment, y)) != ZE_OK)
476
ziperr(ZE_TEMP, tempzip);
477
if ((r = replace(zipfile, tempzip)) != ZE_OK)
479
zipwarn("new zip file left as: ", tempzip);
480
free((zvoid *)tempzip);
482
ziperr(r, "was replacing the original zip file");
484
free((zvoid *)tempzip);
486
setfileattr(zipfile, t);
488
/* Set the filetype of the zipfile to &DDC */
489
setfiletype(zipfile,0xDDC);
491
free((zvoid *)zipfile);
498
const char *BOINC_RCSID_74827256e1 = "$Id: zipnote.c 4979 2005-01-02 18:29:53Z ballen $";