home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / zipnote.c < prev    next >
C/C++ Source or Header  |  1997-08-23  |  13KB  |  452 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  zipnote.c by Mark Adler.
  13.  */
  14.  
  15. #ifndef UTIL
  16. #define UTIL
  17. #endif
  18. #include "zip.h"
  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 *, int));
  39. local int catalloc OF((char * far *, char *));
  40. int main OF((int, char **));
  41.  
  42. #ifdef QDOS
  43. #define exit(p1) QDOSexit()
  44. #endif
  45.  
  46. void ziperr(c, h)
  47. int c;                  /* error code from the ZE_ class */
  48. char *h;                /* message about how it happened */
  49. /* Issue a message for the error, clean up files and memory, and exit. */
  50. {
  51.   if (PERR(c))
  52.     perror("zipnote error");
  53.   fprintf(stderr, "zipnote error: %s (%s)\n", errors[c-1], h);
  54.   if (tempzf != NULL)
  55.     fclose(tempzf);
  56.   if (tempzip != NULL)
  57.   {
  58.     destroy(tempzip);
  59.     free((zvoid *)tempzip);
  60.   }
  61.   if (zipfile != NULL)
  62.     free((zvoid *)zipfile);
  63.   EXIT(c);
  64. }
  65.  
  66.  
  67. local void handler(s)
  68. int s;                  /* signal number (ignored) */
  69. /* Upon getting a user interrupt, abort cleanly using ziperr(). */
  70. {
  71. #ifndef MSDOS
  72.   putc('\n', stderr);
  73. #endif /* !MSDOS */
  74.   ziperr(ZE_ABORT, "aborting");
  75.   s++;                                  /* keep some compilers happy */
  76. }
  77.  
  78.  
  79. void zipwarn(a, b)
  80. char *a, *b;            /* message strings juxtaposed in output */
  81. /* Print a warning message to stderr and return. */
  82. {
  83.   fprintf(stderr, "zipnote warning: %s%s\n", a, b);
  84. }
  85.  
  86.  
  87. local void license()
  88. /* Print license information to stdout. */
  89. {
  90.   extent i;             /* counter for copyright array */
  91.  
  92.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
  93.     printf(copyright[i], "zipnote");
  94.     putchar('\n');
  95.   }
  96.   for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++)
  97.     puts(disclaimer[i]);
  98. }
  99.  
  100.  
  101. local void help()
  102. /* Print help (along with license info) to stdout. */
  103. {
  104.   extent i;             /* counter for help array */
  105.  
  106.   /* help array */
  107.   static ZCONST char *text[] = {
  108. "",
  109. "ZipNote %s (%s)",
  110. "Usage:  zipnote [-w] [-b path] zipfile",
  111. "  the default action is to write the comments in zipfile to stdout",
  112. "  -w   write the zipfile comments from stdin",
  113. "  -b   use \"path\" for the temporary zip file",
  114. "  -h   show this help    -v   show version info    -L   show software license",
  115. "",
  116. "Example:",
  117. #ifdef VMS
  118. "     define/user sys$output foo.tmp",
  119. "     zipnote foo.zip",
  120. "     edit foo.tmp",
  121. "     ... then you edit the comments, save, and exit ...",
  122. "     define/user sys$input foo.tmp",
  123. "     zipnote -w foo.zip",
  124. #else
  125. #ifdef RISCOS
  126. "     zipnote foo/zip > foo/tmp",
  127. "     <!Edit> foo/tmp",
  128. "     ... then you edit the comments, save, and exit ...",
  129. "     zipnote -w foo/zip < foo/tmp",
  130. #else
  131. #ifdef VM_CMS
  132. "     zipnote foo.zip > foo.tmp",
  133. "     xedit foo tmp",
  134. "     ... then you edit the comments, save, and exit ...",
  135. "     zipnote -w foo.zip < foo.tmp",
  136. #else
  137. "     zipnote foo.zip > foo.tmp",
  138. "     ed foo.tmp",
  139. "     ... then you edit the comments, save, and exit ...",
  140. "     zipnote -w foo.zip < foo.tmp",
  141. #endif /* VM_CMS */
  142. #endif /* RISCOS */
  143. #endif /* VMS */
  144. "",
  145. "  \"@ name\" can be followed by an \"@=newname\" line to change the name"
  146.   };
  147.  
  148.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) {
  149.     printf(copyright[i], "zipnote");
  150.     putchar('\n');
  151.   }
  152.   for (i = 0; i < sizeof(text)/sizeof(char *); i++)
  153.   {
  154.     printf(text[i], VERSION, REVDATE);
  155.     putchar('\n');
  156.   }
  157. }
  158.  
  159. /*
  160.  * XXX put this in version.c
  161.  */
  162.  
  163. local void version_info()
  164. /* Print verbose info about program version and compile time options
  165.    to stdout. */
  166. {
  167.   extent i;             /* counter in text arrays */
  168.  
  169.   /* Options info array */
  170.   static ZCONST char *comp_opts[] = {
  171. #ifdef DEBUG
  172.     "DEBUG",
  173. #endif
  174.     NULL
  175.   };
  176.  
  177.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  178.   {
  179.     printf(copyright[i], "zipnote");
  180.     putchar('\n');
  181.   }
  182.  
  183.   for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
  184.   {
  185.     printf(versinfolines[i], "ZipNote", VERSION, REVDATE);
  186.     putchar('\n');
  187.   }
  188.  
  189.   version_local();
  190.  
  191.   puts("ZipNote special compilation options:");
  192.   for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
  193.   {
  194.     printf("\t%s\n",comp_opts[i]);
  195.   }
  196.   if (i == 0)
  197.       puts("\t[none]");
  198. }
  199.  
  200.  
  201. local void putclean(s, n)
  202. char *s;                /* string to write to stdout */
  203. int n;                  /* length of string */
  204. /* Write the string s to stdout, filtering out control characters that are
  205.    not tab or newline (mainly to remove carriage returns), and prefix MARK's
  206.    and backslashes with a backslash.  Also, terminate with a newline if
  207.    needed. */
  208. {
  209.   int c;                /* next character in string */
  210.   int e;                /* last character written */
  211.  
  212.   e = '\n';                     /* if empty, write nothing */
  213.   while (n--)
  214.   {
  215.     c = *(uch *)s++;
  216.     if (c == MARK || c == '\\')
  217.       putchar('\\');
  218.     if (c >= ' ' || c == '\t' || c == '\n')
  219.       { e=c; putchar(e); }
  220.   }
  221.   if (e != '\n')
  222.     putchar('\n');
  223. }
  224.  
  225.  
  226. local int catalloc(a, s)
  227. char * far *a;          /* pointer to a pointer to a malloc'ed string */
  228. char *s;                /* string to concatenate on a */
  229. /* Concatentate the string s to the malloc'ed string pointed to by a.
  230.    Preprocess s by removing backslash escape characters. */
  231. {
  232.   char *p;              /* temporary pointer */
  233.   char *q;              /* temporary pointer */
  234.  
  235.   for (p = q = s; *q; *p++ = *q++)
  236.     if (*q == '\\' && *(q+1))
  237.       q++;
  238.   *p = 0;
  239.   if ((p = malloc(strlen(*a) + strlen(s) + 3)) == NULL)
  240.     return ZE_MEM;
  241.   strcat(strcat(strcpy(p, *a), **a ? "\r\n" : ""), s);
  242.   free((zvoid *)*a);
  243.   *a = p;
  244.   return ZE_OK;
  245. }
  246.  
  247.  
  248. int main(argc, argv)
  249. int argc;               /* number of tokens in command line */
  250. char **argv;            /* command line tokens */
  251. /* Write the comments in the zipfile to stdout, or read them from stdin. */
  252. {
  253.   char a[FNMAX+1];      /* input line buffer */
  254.   ulg c;                /* start of central directory */
  255.   int k;                /* next argument type */
  256.   char *q;              /* steps through option arguments */
  257.   int r;                /* arg counter, temporary variable */
  258.   ulg s;                /* length of central directory */
  259.   int t;                /* attributes of zip file */
  260.   int w;                /* true if updating zip file from stdin */
  261.   FILE *x, *y;          /* input and output zip files */
  262.   struct zlist far *z;  /* steps through zfiles linked list */
  263.  
  264.  
  265.   /* If no args, show help */
  266.   if (argc == 1)
  267.   {
  268.     help();
  269.     EXIT(0);
  270.   }
  271.  
  272.   init_upper();           /* build case map table */
  273.  
  274.   /* Go through args */
  275.   zipfile = tempzip = NULL;
  276.   tempzf = NULL;
  277.   signal(SIGINT, handler);
  278. #ifdef SIGTERM              /* AMIGA has no SIGTERM */
  279.   signal(SIGTERM, handler);
  280. #endif
  281.   k = w = 0;
  282.   for (r = 1; r < argc; r++)
  283.     if (*argv[r] == '-')
  284.       if (argv[r][1])
  285.         for (q = argv[r]+1; *q; q++)
  286.           switch (*q)
  287.           {
  288.             case 'b':   /* Specify path for temporary file */
  289.               if (k)
  290.                 ziperr(ZE_PARMS, "use -b before zip file name");
  291.               else
  292.                 k = 1;          /* Next non-option is path */
  293.               break;
  294.             case 'h':   /* Show help */
  295.               help();  EXIT(0);
  296.             case 'l':  case 'L':  /* Show copyright and disclaimer */
  297.               license();  EXIT(0);
  298.             case 'v':   /* Show version info */
  299.               version_info();  EXIT(0);
  300.             case 'w':
  301.               w = 1;  break;
  302.             default:
  303.               ziperr(ZE_PARMS, "unknown option");
  304.           }
  305.       else
  306.         ziperr(ZE_PARMS, "zip file cannot be stdin");
  307.     else
  308.       if (k == 0)
  309.         if (zipfile == NULL)
  310.         {
  311.           if ((zipfile = ziptyp(argv[r])) == NULL)
  312.             ziperr(ZE_MEM, "was processing arguments");
  313.         }
  314.         else
  315.           ziperr(ZE_PARMS, "can only specify one zip file");
  316.       else
  317.       {
  318.         tempath = argv[r];
  319.         k = 0;
  320.       }
  321.   if (zipfile == NULL)
  322.     ziperr(ZE_PARMS, "need to specify zip file");
  323.  
  324.   /* Read zip file */
  325.   if ((r = readzipfile()) != ZE_OK)
  326.     ziperr(r, zipfile);
  327.   if (zfiles == NULL)
  328.     ziperr(ZE_NAME, zipfile);
  329.  
  330.   /* Put comments to stdout, if not -w */
  331.   if (!w)
  332.   {
  333.     for (z = zfiles; z != NULL; z = z->nxt)
  334.     {
  335.       printf("%c %s\n", MARK, z->zname);
  336.       putclean(z->comment, z->com);
  337.       printf("%c%s\n", MARK, MARKE);
  338.     }
  339.     printf("%c%s\n", MARK, MARKZ);
  340.     putclean(zcomment, zcomlen);
  341.     EXIT(ZE_OK);
  342.   }
  343.  
  344.   /* If updating comments, make sure zip file is writeable */
  345.   if ((x = fopen(zipfile, "a")) == NULL)
  346.     ziperr(ZE_CREAT, zipfile);
  347.   fclose(x);
  348.   t = getfileattr(zipfile);
  349.  
  350.   /* Process stdin, replacing comments */
  351.   z = zfiles;
  352.   while (gets(a) != NULL && (a[0] != MARK || strcmp(a + 1, MARKZ)))
  353.   {                                     /* while input and not file comment */
  354.     if (a[0] != MARK || a[1] != ' ')    /* better be "@ name" */
  355.       ziperr(ZE_NOTE, "unexpected input");
  356.     while (z != NULL && strcmp(a + 2, z->zname))
  357.       z = z->nxt;                       /* allow missing entries in order */
  358.     if (z == NULL)
  359.       ziperr(ZE_NOTE, "unknown entry name");
  360.     if (gets(a) != NULL && a[0] == MARK && a[1] == '=')
  361.     {
  362.       if (z->name != z->iname)
  363.         free((zvoid *)z->iname);
  364.       if ((z->iname = malloc(strlen(a+1))) == NULL)
  365.         ziperr(ZE_MEM, "was changing name");
  366. #ifdef EBCDIC
  367.       strtoasc(z->iname, a+2);
  368. #else
  369.       strcpy(z->iname, a+2);
  370. #endif
  371.  
  372. /*
  373.  * Don't update z->nam here, we need the old value a little later.....
  374.  * The update is handled in zipcopy().
  375.  */
  376.       gets(a);
  377.     }
  378.     if (z->com)                         /* change zip entry comment */
  379.       free((zvoid *)z->comment);
  380.     z->comment = malloc(1);  *(z->comment) = 0;
  381.     while (a != NULL && *a != MARK)
  382.     {
  383.       if ((r = catalloc(&(z->comment), a)) != ZE_OK)
  384.         ziperr(r, "was building new comments");
  385.       gets(a);
  386.     }
  387.     z->com = strlen(z->comment);
  388.     z = z->nxt;                         /* point to next entry */
  389.   }
  390.   if (a != NULL)                        /* change zip file comment */
  391.   {
  392.     zcomment = malloc(1);  *zcomment = 0;
  393.     while (gets(a) != NULL)
  394.       if ((r = catalloc(&zcomment, a)) != ZE_OK)
  395.         ziperr(r, "was building new comments");
  396.     zcomlen = strlen(zcomment);
  397.   }
  398.  
  399.   /* Open output zip file for writing */
  400.   if ((tempzf = y = fopen(tempzip = tempname(zipfile), FOPW)) == NULL)
  401.     ziperr(ZE_TEMP, tempzip);
  402.  
  403.   /* Open input zip file again, copy preamble if any */
  404.   if ((x = fopen(zipfile, FOPR)) == NULL)
  405.     ziperr(ZE_NAME, zipfile);
  406.   if (zipbeg && (r = fcopy(x, y, zipbeg)) != ZE_OK)
  407.     ziperr(r, r == ZE_TEMP ? tempzip : zipfile);
  408.   tempzn = zipbeg;
  409.  
  410.   /* Go through local entries, copying them over as is */
  411.   fix = 3; /* needed for zipcopy if name changed */
  412.   for (z = zfiles; z != NULL; z = z->nxt) {
  413.     if ((r = zipcopy(z, x, y)) != ZE_OK)
  414.       ziperr(r, "was copying an entry");
  415.   }
  416.   fclose(x);
  417.  
  418.   /* Write central directory and end of central directory with new comments */
  419.   if ((c = ftell(y)) == (ulg)(-1L))    /* get start of central */
  420.     ziperr(ZE_TEMP, tempzip);
  421.   for (z = zfiles; z != NULL; z = z->nxt)
  422.     if ((r = putcentral(z, y)) != ZE_OK)
  423.       ziperr(r, tempzip);
  424.   if ((s = ftell(y)) == (ulg)-1L)    /* get end of central */
  425.     ziperr(ZE_TEMP, tempzip);
  426.   s -= c;                       /* compute length of central */
  427.   if ((r = putend((int)zcount, s, c, zcomlen, zcomment, y)) != ZE_OK)
  428.     ziperr(r, tempzip);
  429.   tempzf = NULL;
  430.   if (fclose(y))
  431.     ziperr(ZE_TEMP, tempzip);
  432.   if ((r = replace(zipfile, tempzip)) != ZE_OK)
  433.   {
  434.     zipwarn("new zip file left as: ", tempzip);
  435.     free((zvoid *)tempzip);
  436.     tempzip = NULL;
  437.     ziperr(r, "was replacing the original zip file");
  438.   }
  439.   free((zvoid *)tempzip);
  440.   tempzip = NULL;
  441.   setfileattr(zipfile, t);
  442. #ifdef RISCOS
  443.   /* Set the filetype of the zipfile to &DDC */
  444.   setfiletype(zipfile,0xDDC);
  445. #endif
  446.   free((zvoid *)zipfile);
  447.   zipfile = NULL;
  448.  
  449.   /* Done! */
  450.   RETURN(0);
  451. }
  452.