home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / COMMENT.C < prev    next >
C/C++ Source or Header  |  1987-02-07  |  6KB  |  186 lines

  1. /* comment.c */
  2. /*
  3. Copyright (C) 1986 Rahul Dhesi -- All rights reserved
  4. */
  5.  
  6. #include "options.h"
  7. #include "portable.h"
  8. /* comment() */
  9. /* Updates comments */
  10.  
  11. /* buffer size for any one comment line */
  12. #define  COMMENT_LINE_SIZE 77
  13.  
  14. #define  MAX_COMMENT_SIZE  65535
  15. #include <stdio.h>
  16. #include "various.h"
  17.  
  18. #ifndef NOSIGNAL
  19. #include <signal.h>
  20. #endif
  21.  
  22. #include "zoo.h"
  23. #include "zoofns.h"
  24. #include "errors.i"
  25.  
  26. #ifdef LINT_ARGS
  27. void show_comment (struct direntry *, FILE *, int, char *);
  28. get_comment (struct direntry *, FILE *, char *);
  29. #else
  30. void show_comment ();
  31. get_comment ();
  32. #endif
  33.  
  34. void comment(zoo_path, option)
  35. char *zoo_path, *option;
  36. {
  37. #ifndef NOSIGNAL  
  38. int (*oldsignal)();
  39. #endif
  40. FILE *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.  
  48. /* on entry option points to first letter */
  49. ++option;
  50. if (*option != '\0')
  51.    prterror ('f', inv_option, *option);
  52.  
  53. if ((zoo_file = fopen (zoo_path, FRWSTR)) == NULL)
  54.    prterror ('f', could_not_open, zoo_path);
  55.  
  56. /* save archive timestamp */
  57. #ifdef GETUTIME
  58. getutime (zoo_path, &zoo_date, &zoo_time);
  59. #else
  60. gettime (fileno(zoo_file), &zoo_date, &zoo_time);
  61. #endif
  62.  
  63. /* read header and rewrite with updated version numbers */
  64. rwheader (&zoo_header, zoo_file);
  65.  
  66. /* Loop through and add comments for matching files */
  67. while (1) {
  68.    this_dir_offset = ftell (zoo_file);    /* save pos'n of this dir entry */
  69.    readdir (&direntry, zoo_file, 1);      /* read directory entry */
  70.    next_ptr = direntry.next;              /* ptr to next dir entry */
  71.  
  72.    /* exit on end of directory chain or end of file */
  73.    if (next_ptr == 0L || feof(stdin))
  74.       break;
  75.  
  76.    /* add comments for matching non-deleted files */
  77.    if (!direntry.deleted && needed (direntry.fname)) {
  78.       matched++;
  79.       show_comment (&direntry, zoo_file, 1, direntry.fname);
  80.       get_comment (&direntry, zoo_file, direntry.fname);
  81.       fseek (zoo_file, this_dir_offset, 0);
  82. #ifndef NOSIGNAL
  83.       oldsignal = signal (SIGINT, SIG_IGN);
  84. #endif
  85.       fwr_dir (&direntry, zoo_file);
  86.       /* fwrite ((char *) &direntry, sizeof(direntry), 1, zoo_file); */
  87. #ifndef NOSIGNAL
  88.       signal (SIGINT, oldsignal);
  89. #endif
  90.    }
  91.    fseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  92. } /* end while */
  93.  
  94. #ifdef NIXTIME
  95. fclose (zoo_file);
  96. setutime (zoo_path, zoo_date, zoo_time); /* restore timestamp */
  97. #else
  98. settime (fileno(zoo_file), zoo_date, zoo_time); /* restore timestamp */
  99. fclose (zoo_file);
  100. #endif
  101.  
  102. if (!matched)
  103.    printf ("Zoo:  %s", no_match);
  104. } /* comment */
  105.  
  106. /* show_comment() */
  107. /* shows comment on screen.  If show=1, says "Current comment is..." */
  108.  
  109. void show_comment (direntry, zoo_file, show, name)
  110. struct direntry *direntry;
  111. FILE *zoo_file;
  112. int show;
  113. char *name;       /* name of file for which comment is being added */
  114. {
  115.    if (direntry->cmt_size != 0) {
  116.       unsigned int i;
  117.       char ch;
  118.       int newline = 1;
  119.       fseek (zoo_file, direntry->comment, 0);   
  120.       if (show)
  121.          printf ("Current comment for %s is:\n", name);
  122.       for (i = 0; i < direntry->cmt_size; i++) {/* show it */
  123.          ch = fgetc (zoo_file) & 0x7f;          /* 7 bits only */
  124.          if (newline)
  125.             printf (" |");    /* indent and mark comment lines thus */
  126.          fputchar (ch);
  127.          if (ch == '\n')
  128.             newline = 1;
  129.          else
  130.             newline = 0;
  131.       }
  132.       if (!newline)              /* always terminate with newline */
  133.          fputchar ('\n');
  134.    }
  135. } /* show_comment() */
  136.  
  137.  
  138. /* get_comment() */
  139. /* Shows user old comment and updates it */
  140.  
  141. /* INPUT:
  142.    direntry points to current directory entry.
  143.    zoo_file is archive file.
  144.    this_path is full pathname of file being updated/added.
  145.  
  146.    OUTPUT:
  147.    Comment is added to file and supplied directory entry is updated
  148.    with comment size and seek position but directory entry is
  149.    not written to file.  Exceptions:  If RETURN is hit as first line,
  150.    previous comment is left unchanged.  If /END is hit, previous
  151.    comment is superseded, even if new comment is null.
  152. */
  153.  
  154. get_comment (direntry, zoo_file, this_path)  /* update comment */
  155. register struct direntry *direntry;
  156. FILE *zoo_file;
  157. char *this_path;
  158. {
  159.    unsigned int line_count = 0;        /* count of new comment lines */
  160.  
  161.    fseek (zoo_file, 0L, 2);            /* ready to append new comment */
  162.    fprintf (stderr, "[Enter comment for %s then type /END]\n", this_path);
  163.    while (1) {
  164.       char cmt_line[COMMENT_LINE_SIZE];
  165.       int cmt_size;
  166.       if (fgets (cmt_line, sizeof(cmt_line), stdin) == NULL)
  167.          break;
  168.       line_count++;
  169.       if (line_count == 1) {                 /* first line typed */
  170.          if (!strcmp (cmt_line, "\n"))   /* exit if first line blank */
  171.             break;
  172.          direntry->comment = ftell (zoo_file);
  173.          direntry->cmt_size = 0;
  174.       }
  175.       if (!strcmpi (cmt_line, "/end\n"))
  176.          break;
  177.       cmt_size = strlen (cmt_line);
  178.       if (MAX_COMMENT_SIZE - direntry->cmt_size > cmt_size) {
  179.          direntry->cmt_size += (unsigned int) cmt_size;
  180.          if (fwrite (cmt_line, cmt_size, 1, zoo_file) < 1)
  181.             prterror ('f', disk_full);
  182.       }
  183.    } /* end while */
  184. } /* get_comment() */
  185.  
  186.