~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to zip/zip/zipnote.c

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
/*
10
 
 *  zipnote.c by Mark Adler.
11
 
 */
12
 
#define __ZIPNOTE_C
13
 
 
14
 
#ifndef UTIL
15
 
#define UTIL
16
 
#endif
17
 
#include "zip.h"
18
 
#define DEFCPYRT        /* main module: enable copyright string defines! */
19
 
#include "revision.h"
20
 
#include <signal.h>
21
 
 
22
 
 
23
 
/* Character to mark zip entry names in the comment file */
24
 
#define MARK '@'
25
 
#define MARKE " (comment above this line)"
26
 
#define MARKZ " (zip file comment below this line)"
27
 
 
28
 
/* Temporary zip file name and file pointer */
29
 
local char *tempzip;
30
 
local FILE *tempzf;
31
 
 
32
 
 
33
 
/* Local functions */
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 **));
42
 
 
43
 
#ifdef MACOS
44
 
#define ziperr(c, h)    zipnoteerr(c, h)
45
 
#define zipwarn(a, b)   zipnotewarn(a, b)
46
 
 
47
 
void zipnoteerr(int c,char *h);
48
 
void zipnotewarn(char *a,char *b);
49
 
#endif
50
 
 
51
 
#ifdef QDOS
52
 
#define exit(p1) QDOSexit()
53
 
#endif
54
 
 
55
 
void ziperr(c, h)
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. */
59
 
{
60
 
  if (PERR(c))
61
 
    perror("zipnote error");
62
 
  fprintf(stderr, "zipnote error: %s (%s)\n", errors[c-1], h);
63
 
  if (tempzf != NULL)
64
 
    fclose(tempzf);
65
 
  if (tempzip != NULL)
66
 
  {
67
 
    destroy(tempzip);
68
 
    free((zvoid *)tempzip);
69
 
  }
70
 
  if (zipfile != NULL)
71
 
    free((zvoid *)zipfile);
72
 
  EXIT(c);
73
 
}
74
 
 
75
 
 
76
 
local void handler(s)
77
 
int s;                  /* signal number (ignored) */
78
 
/* Upon getting a user interrupt, abort cleanly using ziperr(). */
79
 
{
80
 
#ifndef MSDOS
81
 
  putc('\n', stderr);
82
 
#endif /* !MSDOS */
83
 
  ziperr(ZE_ABORT, "aborting");
84
 
  s++;                                  /* keep some compilers happy */
85
 
}
86
 
 
87
 
 
88
 
void zipwarn(a, b)
89
 
char *a, *b;            /* message strings juxtaposed in output */
90
 
/* Print a warning message to stderr and return. */
91
 
{
92
 
  fprintf(stderr, "zipnote warning: %s%s\n", a, b);
93
 
}
94
 
 
95
 
 
96
 
local void license()
97
 
/* Print license information to stdout. */
98
 
{
99
 
  extent i;             /* counter for copyright array */
100
 
 
101
 
  for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
102
 
    printf(copyright[i], "zipnote");
103
 
    putchar('\n');
104
 
  }
105
 
  for (i = 0; i < sizeof(swlicense)/sizeof(char *); i++)
106
 
    puts(swlicense[i]);
107
 
}
108
 
 
109
 
 
110
 
local void help()
111
 
/* Print help (along with license info) to stdout. */
112
 
{
113
 
  extent i;             /* counter for help array */
114
 
 
115
 
  /* help array */
116
 
  static ZCONST char *text[] = {
117
 
"",
118
 
"ZipNote %s (%s)",
119
 
#ifdef VM_CMS
120
 
"Usage:  zipnote [-w] [-b fm] zipfile",
121
 
#else
122
 
"Usage:  zipnote [-w] [-b path] zipfile",
123
 
#endif
124
 
"  the default action is to write the comments in zipfile to stdout",
125
 
"  -w   write the zipfile comments from stdin",
126
 
#ifdef VM_CMS
127
 
"  -b   use \"fm\" as the filemode for the temporary zip file",
128
 
#else
129
 
"  -b   use \"path\" for the temporary zip file",
130
 
#endif
131
 
"  -h   show this help    -v   show version info    -L   show software license",
132
 
"",
133
 
"Example:",
134
 
#ifdef VMS
135
 
"     define/user sys$output foo.tmp",
136
 
"     zipnote foo.zip",
137
 
"     edit foo.tmp",
138
 
"     ... then you edit the comments, save, and exit ...",
139
 
"     define/user sys$input foo.tmp",
140
 
"     zipnote -w foo.zip",
141
 
#else
142
 
#ifdef RISCOS
143
 
"     zipnote foo/zip > foo/tmp",
144
 
"     <!Edit> foo/tmp",
145
 
"     ... then you edit the comments, save, and exit ...",
146
 
"     zipnote -w foo/zip < foo/tmp",
147
 
#else
148
 
#ifdef VM_CMS
149
 
"     zipnote foo.zip > foo.tmp",
150
 
"     xedit foo tmp",
151
 
"     ... then you edit the comments, save, and exit ...",
152
 
"     zipnote -w foo.zip < foo.tmp",
153
 
#else
154
 
"     zipnote foo.zip > foo.tmp",
155
 
"     ed foo.tmp",
156
 
"     ... then you edit the comments, save, and exit ...",
157
 
"     zipnote -w foo.zip < foo.tmp",
158
 
#endif /* VM_CMS */
159
 
#endif /* RISCOS */
160
 
#endif /* VMS */
161
 
"",
162
 
"  \"@ name\" can be followed by an \"@=newname\" line to change the name"
163
 
  };
164
 
 
165
 
  for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
166
 
    printf(copyright[i], "zipnote");
167
 
    putchar('\n');
168
 
  }
169
 
  for (i = 0; i < sizeof(text)/sizeof(char *); i++)
170
 
  {
171
 
    printf(text[i], VERSION, REVDATE);
172
 
    putchar('\n');
173
 
  }
174
 
}
175
 
 
176
 
/*
177
 
 * XXX put this in version.c
178
 
 */
179
 
 
180
 
local void version_info()
181
 
/* Print verbose info about program version and compile time options
182
 
   to stdout. */
183
 
{
184
 
  extent i;             /* counter in text arrays */
185
 
 
186
 
  /* Options info array */
187
 
  static ZCONST char *comp_opts[] = {
188
 
#ifdef DEBUG
189
 
    "DEBUG",
190
 
#endif
191
 
    NULL
192
 
  };
193
 
 
194
 
  for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
195
 
  {
196
 
    printf(copyright[i], "zipnote");
197
 
    putchar('\n');
198
 
  }
199
 
 
200
 
  for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
201
 
  {
202
 
    printf(versinfolines[i], "ZipNote", VERSION, REVDATE);
203
 
    putchar('\n');
204
 
  }
205
 
 
206
 
  version_local();
207
 
 
208
 
  puts("ZipNote special compilation options:");
209
 
  for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
210
 
  {
211
 
    printf("\t%s\n",comp_opts[i]);
212
 
  }
213
 
  if (i == 0)
214
 
      puts("\t[none]");
215
 
}
216
 
 
217
 
 
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
224
 
   needed. */
225
 
{
226
 
  int c;                /* next character in string */
227
 
  int e;                /* last character written */
228
 
 
229
 
  e = '\n';                     /* if empty, write nothing */
230
 
  while (n--)
231
 
  {
232
 
    c = *(uch *)s++;
233
 
    if (c == MARK || c == '\\')
234
 
      putchar('\\');
235
 
    if (c >= ' ' || c == '\t' || c == '\n')
236
 
      { e=c; putchar(e); }
237
 
  }
238
 
  if (e != '\n')
239
 
    putchar('\n');
240
 
}
241
 
 
242
 
 
243
 
local char *getline(buf, size)
244
 
char *buf;
245
 
extent 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. */
248
 
{
249
 
    char *line;
250
 
    unsigned len;
251
 
 
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 */
256
 
            line = NULL;
257
 
        } else {
258
 
            /* delete trailing record delimiter */
259
 
            if (line[len-1] == '\n') line[len-1] = '\0';
260
 
        }
261
 
    }
262
 
    return line;
263
 
}
264
 
 
265
 
 
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. */
271
 
{
272
 
  char *p;              /* temporary pointer */
273
 
  char *q;              /* temporary pointer */
274
 
 
275
 
  for (p = q = s; *q; *p++ = *q++)
276
 
    if (*q == '\\' && *(q+1))
277
 
      q++;
278
 
  *p = 0;
279
 
  if ((p = malloc(strlen(*a) + strlen(s) + 3)) == NULL)
280
 
    return ZE_MEM;
281
 
  strcat(strcat(strcpy(p, *a), **a ? "\r\n" : ""), s);
282
 
  free((zvoid *)*a);
283
 
  *a = p;
284
 
  return ZE_OK;
285
 
}
286
 
 
287
 
 
288
 
#ifndef USE_ZIPNOTEMAIN
289
 
int main(argc, argv)
290
 
#else
291
 
int zipnotemain(argc, argv)
292
 
#endif
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. */
296
 
{
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 */
307
 
 
308
 
#ifdef THEOS
309
 
  setlocale(LC_CTYPE, "I");
310
 
#endif
311
 
 
312
 
  /* If no args, show help */
313
 
  if (argc == 1)
314
 
  {
315
 
    help();
316
 
    EXIT(0);
317
 
  }
318
 
 
319
 
  init_upper();           /* build case map table */
320
 
 
321
 
  /* Go through args */
322
 
  zipfile = tempzip = NULL;
323
 
  tempzf = NULL;
324
 
  k = w = 0;
325
 
  for (r = 1; r < argc; r++)
326
 
    if (*argv[r] == '-') {
327
 
      if (argv[r][1])
328
 
        for (q = argv[r]+1; *q; q++)
329
 
          switch (*q)
330
 
          {
331
 
            case 'b':   /* Specify path for temporary file */
332
 
              if (k)
333
 
                ziperr(ZE_PARMS, "use -b before zip file name");
334
 
              else
335
 
                k = 1;          /* Next non-option is path */
336
 
              break;
337
 
            case 'h':   /* Show help */
338
 
              help();  EXIT(0);
339
 
            case 'l':  case 'L':  /* Show copyright and disclaimer */
340
 
              license();  EXIT(0);
341
 
            case 'v':   /* Show version info */
342
 
              version_info();  EXIT(0);
343
 
            case 'w':
344
 
              w = 1;  break;
345
 
            default:
346
 
              ziperr(ZE_PARMS, "unknown option");
347
 
          }
348
 
      else
349
 
        ziperr(ZE_PARMS, "zip file cannot be stdin");
350
 
    } else
351
 
      if (k == 0)
352
 
      {
353
 
        if (zipfile == NULL)
354
 
        {
355
 
          if ((zipfile = ziptyp(argv[r])) == NULL)
356
 
            ziperr(ZE_MEM, "was processing arguments");
357
 
        }
358
 
        else
359
 
          ziperr(ZE_PARMS, "can only specify one zip file");
360
 
      }
361
 
      else
362
 
      {
363
 
        tempath = argv[r];
364
 
        k = 0;
365
 
      }
366
 
  if (zipfile == NULL)
367
 
    ziperr(ZE_PARMS, "need to specify zip file");
368
 
 
369
 
  /* Read zip file */
370
 
  if ((r = readzipfile()) != ZE_OK)
371
 
    ziperr(r, zipfile);
372
 
  if (zfiles == NULL)
373
 
    ziperr(ZE_NAME, zipfile);
374
 
 
375
 
  /* Put comments to stdout, if not -w */
376
 
  if (!w)
377
 
  {
378
 
    for (z = zfiles; z != NULL; z = z->nxt)
379
 
    {
380
 
      printf("%c %s\n", MARK, z->zname);
381
 
      putclean(z->comment, z->com);
382
 
      printf("%c%s\n", MARK, MARKE);
383
 
    }
384
 
    printf("%c%s\n", MARK, MARKZ);
385
 
    putclean(zcomment, zcomlen);
386
 
    EXIT(ZE_OK);
387
 
  }
388
 
 
389
 
  /* If updating comments, make sure zip file is writeable */
390
 
  if ((x = fopen(zipfile, "a")) == NULL)
391
 
    ziperr(ZE_CREAT, zipfile);
392
 
  fclose(x);
393
 
  t = getfileattr(zipfile);
394
 
 
395
 
  /* Process stdin, replacing comments */
396
 
  z = zfiles;
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 */
403
 
    if (z == NULL)
404
 
      ziperr(ZE_NOTE, "unknown entry name");
405
 
    if (getline(a, FNMAX+1) != NULL && a[0] == MARK && a[1] == '=')
406
 
    {
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");
411
 
#ifdef EBCDIC
412
 
      strtoasc(z->iname, a+2);
413
 
#else
414
 
      strcpy(z->iname, a+2);
415
 
#endif
416
 
 
417
 
/*
418
 
 * Don't update z->nam here, we need the old value a little later.....
419
 
 * The update is handled in zipcopy().
420
 
 */
421
 
      getline(a, FNMAX+1);
422
 
    }
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)
427
 
    {
428
 
      if ((r = catalloc(&(z->comment), a)) != ZE_OK)
429
 
        ziperr(r, "was building new comments");
430
 
      getline(a, FNMAX+1);
431
 
    }
432
 
    z->com = strlen(z->comment);
433
 
    z = z->nxt;                         /* point to next entry */
434
 
  }
435
 
  if (a != NULL)                        /* change zip file comment */
436
 
  {
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);
442
 
  }
443
 
 
444
 
  /* Open output zip file for writing */
445
 
  if ((tempzf = y = fopen(tempzip = tempname(zipfile), FOPW)) == NULL)
446
 
    ziperr(ZE_TEMP, tempzip);
447
 
 
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);
453
 
  tempzn = zipbeg;
454
 
 
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");
460
 
  }
461
 
  fclose(x);
462
 
 
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)
468
 
      ziperr(r, tempzip);
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)
473
 
    ziperr(r, tempzip);
474
 
  tempzf = NULL;
475
 
  if (fclose(y))
476
 
    ziperr(ZE_TEMP, tempzip);
477
 
  if ((r = replace(zipfile, tempzip)) != ZE_OK)
478
 
  {
479
 
    zipwarn("new zip file left as: ", tempzip);
480
 
    free((zvoid *)tempzip);
481
 
    tempzip = NULL;
482
 
    ziperr(r, "was replacing the original zip file");
483
 
  }
484
 
  free((zvoid *)tempzip);
485
 
  tempzip = NULL;
486
 
  setfileattr(zipfile, t);
487
 
#ifdef RISCOS
488
 
  /* Set the filetype of the zipfile to &DDC */
489
 
  setfiletype(zipfile,0xDDC);
490
 
#endif
491
 
  free((zvoid *)zipfile);
492
 
  zipfile = NULL;
493
 
 
494
 
  /* Done! */
495
 
  RETURN(0);
496
 
}
497
 
 
498
 
const char *BOINC_RCSID_74827256e1 = "$Id: zipnote.c 4979 2005-01-02 18:29:53Z ballen $";