home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / COMMENT.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  10KB  |  308 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) comment.c 2.14 88/01/24 12:42:13";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  8. */
  9.  
  10. #include "options.h"
  11. #include "portable.h"
  12. /* comment() */
  13. /* Updates comments */
  14.  
  15. /* buffer size for any one comment line */
  16. #define  COMMENT_LINE_SIZE 76
  17.  
  18. #define  MAX_COMMENT_SIZE  32767
  19. #include "zooio.h"
  20. #include "various.h"
  21.  
  22. #ifndef NOSIGNAL
  23. #include <signal.h>
  24. #endif
  25.  
  26. #include "zoo.h"
  27. #include "zoofns.h"
  28. #include "errors.i"
  29.  
  30. void show_comment PARMS ((struct direntry *, ZOOFILE, int, char *));
  31. void get_comment PARMS ((struct direntry *, ZOOFILE, char *));
  32. int needed PARMS ((char *, struct direntry *, struct zoo_header *));
  33.  
  34. void comment(zoo_path, option)
  35. char *zoo_path, *option;
  36. {
  37. #ifndef NOSIGNAL
  38. T_SIGNAL (*oldsignal)();
  39. #endif
  40. ZOOFILE zoo_file;                         /* stream for open archive */
  41. long next_ptr;                            /* pointers to within archive */
  42. long this_dir_offset;                     /* pointers to within archive */
  43. struct direntry direntry;                 /* directory entry */
  44. struct zoo_header zoo_header;
  45. int matched = 0;                          /* any files matched? */
  46. unsigned int zoo_date, zoo_time;          /* for restoring archive timestamp */
  47. char whichname[PATHSIZE];                 /* which name to use */
  48. #ifdef ZOOCOMMENT
  49. int acmt = 0;                                        /* if changing archive comment */
  50. #endif
  51.  
  52. /* on entry option points to first letter */
  53. option++;                                            /* skip 'c' */
  54. #ifdef ZOOCOMMENT
  55. while (*option != '\0') {
  56.     if (*option == 'A') {
  57.         acmt++;                                        /* changing archive comment */
  58.         option++;
  59.     } else
  60.        prterror ('f', inv_option, *option);
  61. }
  62. #else
  63. if (*option != '\0')
  64.     prterror ('f', inv_option, *option);
  65. #endif /* ZOOCOMMENT */
  66.  
  67. if ((zoo_file = zooopen (zoo_path, Z_RDWR)) == NOFILE)
  68.    prterror ('f', could_not_open, zoo_path);
  69.  
  70. /* save archive timestamp */
  71. #ifdef GETUTIME
  72. getutime (zoo_path, &zoo_date, &zoo_time);
  73. #else
  74. gettime (zoo_file, &zoo_date, &zoo_time);
  75. #endif
  76.  
  77. /* read header and rewrite with updated version numbers, but ask user to pack
  78. archive first if archive comment is to be added and header type is 0 */
  79. #ifdef ZOOCOMMENT
  80. if (acmt)
  81.     rwheader (&zoo_header, zoo_file, 0);
  82. else
  83.     rwheader (&zoo_header, zoo_file, 1);
  84. #else
  85. rwheader (&zoo_header, zoo_file, 1);
  86. #endif
  87.  
  88. #ifdef ZOOCOMMENT
  89. /* if archive comment being added, handle it and return */
  90. if (acmt) {
  91.     void do_acmt PARMS ((struct zoo_header *, ZOOFILE, char *));
  92.     do_acmt (&zoo_header, zoo_file, zoo_path);
  93. #ifdef NIXTIME
  94.     zooclose (zoo_file);
  95.     setutime (zoo_path, zoo_date, zoo_time);    /* restore timestamp */
  96. #else
  97.     settime (zoo_file, zoo_date, zoo_time);    /* restore timestamp */
  98.     zooclose (zoo_file);
  99. #endif
  100.     return;
  101. }
  102. #endif /* ZOOCOMMENT */
  103.  
  104. /* Loop through and add comments for matching files */
  105. while (1) {
  106.    this_dir_offset = zootell (zoo_file);  /* save pos'n of this dir entry */
  107.    readdir (&direntry, zoo_file, 1);      /* read directory entry */
  108.    next_ptr = direntry.next;              /* ptr to next dir entry */
  109.  
  110.    /* exit on end of directory chain or end of file */
  111.    if (next_ptr == 0L || feof(stdin))
  112.       break;
  113.  
  114.     strcpy (whichname, fullpath (&direntry));        /* full pathname */
  115.     add_version (whichname, &direntry);                /* add version suffix */
  116.    /* add comments for matching non-deleted files */
  117.    if (!direntry.deleted && needed (whichname, &direntry, &zoo_header)) {
  118.       matched++;
  119.       show_comment (&direntry, zoo_file, 1, whichname);
  120.       get_comment (&direntry, zoo_file, whichname);
  121.       zooseek (zoo_file, this_dir_offset, 0);
  122. #ifndef NOSIGNAL
  123.       oldsignal = signal (SIGINT, SIG_IGN);
  124. #endif
  125.       fwr_dir (&direntry, zoo_file);
  126. #ifndef NOSIGNAL
  127.       signal (SIGINT, oldsignal);
  128. #endif
  129.    }
  130.    zooseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  131. } /* end while */
  132.  
  133. #ifdef NIXTIME
  134. zooclose (zoo_file);
  135. setutime (zoo_path, zoo_date, zoo_time);    /* restore timestamp */
  136. #else
  137. settime (zoo_file, zoo_date, zoo_time);    /* restore timestamp */
  138. zooclose (zoo_file);
  139. #endif
  140.  
  141. if (!matched)
  142.    printf ("Zoo:  %s", no_match);
  143. } /* comment */
  144.  
  145. /* show_comment() */
  146. /* shows comment on screen.  If show=1, says "Current comment is..." */
  147.  
  148. void show_comment (direntry, zoo_file, show, name)
  149. struct direntry *direntry;
  150. ZOOFILE zoo_file;
  151. int show;
  152. char *name;       /* name of file for which comment is being added */
  153. {
  154.    if (direntry->cmt_size != 0) {
  155.       unsigned int i;
  156.       char ch;
  157.       int newline = 1;
  158.       zooseek (zoo_file, direntry->comment, 0);
  159.       if (show)
  160.          printf ("Current comment for %s is:\n", name);
  161.       for (i = 0; i < direntry->cmt_size; i++) {/* show it */
  162.          ch = zgetc (zoo_file) & 0x7f;          /* 7 bits only */
  163.          if (newline)
  164.             printf (" |");    /* indent and mark comment lines thus */
  165.          zputchar (ch);
  166.          if (ch == '\n')
  167.             newline = 1;
  168.          else
  169.             newline = 0;
  170.       }
  171.       if (!newline)              /* always terminate with newline */
  172.          zputchar ('\n');
  173.    }
  174. } /* show_comment() */
  175.  
  176.  
  177. /* get_comment() */
  178. /* Shows user old comment and updates it */
  179.  
  180. /* INPUT:
  181.    direntry points to current directory entry.
  182.    zoo_file is archive file.
  183.    this_path is full pathname of file being updated/added.
  184.  
  185.    OUTPUT:
  186.    Comment is added to file and supplied directory entry is updated
  187.    with comment size and seek position but directory entry is
  188.    not written to file.  Exceptions:  If RETURN is hit as first line,
  189.    previous comment is left unchanged.  If /END is hit, previous
  190.    comment is superseded, even if new comment is null.
  191. */
  192.  
  193. char cmt_prompt[]="[Enter %scomment for %s then type /END]\n";
  194.  
  195. void get_comment (direntry, zoo_file, this_path)  /* update comment */
  196. register struct direntry *direntry;
  197. ZOOFILE zoo_file;
  198. char *this_path;
  199. {
  200.    unsigned int line_count = 0;        /* count of new comment lines */
  201.  
  202.    zooseek (zoo_file, 0L, 2);            /* ready to append new comment */
  203. #if 0
  204.    fprintf (stderr, "[Enter comment for %s then type /END]\n", this_path);
  205. #else
  206.    fprintf (stderr, cmt_prompt, "", this_path);
  207. #endif
  208.    while (1) {
  209.       char cmt_line[COMMENT_LINE_SIZE];
  210.       int cmt_size;
  211.       if (fgets (cmt_line, sizeof(cmt_line), stdin) == NULL)
  212.          break;
  213.       line_count++;
  214.       if (line_count == 1) {                 /* first line typed */
  215.          if (!strcmp (cmt_line, "\n"))   /* exit if first line blank */
  216.             break;
  217.          direntry->comment = zootell (zoo_file);
  218.          direntry->cmt_size = 0;
  219.       }
  220.       if (!str_icmp (cmt_line, "/end\n"))
  221.          break;
  222.       cmt_size = strlen (cmt_line);
  223.       if (MAX_COMMENT_SIZE - direntry->cmt_size > cmt_size) {
  224.          direntry->cmt_size += (unsigned int) cmt_size;
  225.          if (zoowrite (zoo_file, cmt_line, cmt_size) < cmt_size)
  226.             prterror ('f', disk_full);
  227.       }
  228.    } /* end while */
  229. } /* get_comment() */
  230.  
  231. #ifdef ZOOCOMMENT
  232. /*
  233. do_acmt() updates archive comment by showing it to user and
  234. requesting a new one.  Typed input terminates as with file comment,
  235. i.e., empty initial line leaves comment unchanged, case-insensitive
  236. "/end" terminates input comment.
  237. */
  238. void do_acmt (zoo_header, zoo_file, zoo_path)
  239. struct zoo_header *zoo_header;
  240. ZOOFILE zoo_file;
  241. char *zoo_path;
  242. {
  243.    unsigned int line_count = 0;        /* count of new comment lines */
  244.     void show_acmt PARMS ((struct zoo_header *, ZOOFILE, int));
  245.  
  246.     show_acmt (zoo_header, zoo_file, 1);    /* show current archive comment */
  247.    zooseek (zoo_file, 0L, 2);            /* ready to append new comment */
  248. #if 0
  249.    fprintf (stderr, "[Enter archive comment for %s then type /END]\n",
  250.                             zoo_path);
  251. #else
  252.    fprintf (stderr, cmt_prompt, "archive ", zoo_path);
  253. #endif
  254.  
  255.    while (1) {
  256.       char cmt_line[COMMENT_LINE_SIZE];
  257.       int cmt_size;
  258.       if (fgets (cmt_line, sizeof(cmt_line), stdin) == NULL)
  259.          break;
  260.       line_count++;
  261.       if (line_count == 1) {                 /* first line typed */
  262.          if (!strcmp (cmt_line, "\n"))   /* exit if first line blank */
  263.             break;
  264.          zoo_header->acmt_pos = zootell (zoo_file);
  265.          zoo_header->acmt_len = 0;
  266.       }
  267.       if (!str_icmp (cmt_line, "/end\n"))
  268.          break;
  269.       cmt_size = strlen (cmt_line);
  270.       if (MAX_COMMENT_SIZE - zoo_header->acmt_len > cmt_size) {
  271.          zoo_header->acmt_len += (unsigned int) cmt_size;
  272.          if (zoowrite (zoo_file, cmt_line, cmt_size) < cmt_size)
  273.             prterror ('f', disk_full);
  274.       }
  275.    } /* end while */
  276.     zooseek (zoo_file, 0L, 0);                    /* seek back to beginning */
  277.     fwr_zooh (zoo_header, zoo_file);            /* write update zoo_header */
  278. } /* do_acmt() */
  279. #endif /* ZOOCOMMENT */
  280.  
  281. /* Prints archive comment.  If show==1, says "Current archive comment is:" */
  282. void show_acmt (zoo_header, zoo_file, show)
  283. struct zoo_header *zoo_header;
  284. ZOOFILE zoo_file;
  285. int show;
  286. {
  287.    if (zoo_header->zoo_start != FIXED_OFFSET && zoo_header->acmt_len > 0) {
  288.       unsigned int i;
  289.       char ch;
  290.       int newline = 1;
  291.       zooseek (zoo_file, zoo_header->acmt_pos, 0);
  292.         if (show)
  293.           printf ("Current archive comment is:\n");
  294.       for (i = 0; i < zoo_header->acmt_len; i++) {/* show it */
  295.          ch = zgetc (zoo_file) & 0x7f;          /* 7 bits only */
  296.          if (newline)
  297.             printf (">> ");        /* indent and mark comment lines thus */
  298.          zputchar (ch);
  299.          if (ch == '\n')
  300.             newline = 1;
  301.          else
  302.             newline = 0;
  303.       }
  304.       if (!newline)              /* always terminate with newline */
  305.          zputchar ('\n');
  306.    }
  307. } /* show_acmt() */
  308.