home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / zoo / part01 / comment.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-16  |  5.5 KB  |  189 lines

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