home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff319.lzh / CNewsSrc / cnews.orig.lzh / rna / history.c < prev    next >
C/C++ Source or Header  |  1989-06-27  |  4KB  |  224 lines

  1. /*
  2.  * History file
  3.  * each line contains a message-id, install or expire time
  4.  * names of linked files
  5.  */
  6.  
  7. #include "defs.h"
  8.  
  9. static char histname[]     = HISTORY;
  10. static char *histid;            /* messageid to save */
  11. static char *histline;            /* list of linked files */
  12. static long etime;            /* expire time */
  13.  
  14. typedef enum stypes { 
  15.     chk, delete } stype;
  16.  
  17. /*
  18.  * do things with history file
  19.  * chk - see if id present
  20.  * delete - delete article with id
  21.  */
  22. static bool
  23. searchhist(id, type)
  24. char *id;
  25. stype type;
  26. {
  27.     register FILE    *f;
  28.     register char *s, *name;
  29.     register bool    found;
  30.     char buf[BUFSIZ * 2];
  31.  
  32.     extern char *newsdir;
  33.  
  34.     f = fopenl(histname);
  35.  
  36.     found = false;
  37.     while (fgets(buf, sizeof(buf), f)) {
  38.         if (s = strchr(buf, ' '))
  39.             *s = '\0';
  40.         else
  41.             error("Bad format: %s", histname);
  42.         if (CMP(buf, id) == 0) {
  43.             found = true;
  44.             break;
  45.         }
  46.     }
  47.     if (found && type == delete) {
  48.         if ((name = strchr(s + 1, ' ')) == NIL(char))
  49.             error("Bad format: %s", histname);
  50.         name++;
  51.         while (name && (s = strpbrk(name, " \n"))) {
  52.             *s = '\0';
  53.             name = newstr3(newsdir, "/", name);
  54.             remove(name);
  55.             free(name);
  56.             name = s + 1;
  57.         }
  58.     }
  59.     fclose(f);
  60. #if !AUSAM
  61.     unlock(histname);
  62. #endif
  63.     return found;
  64. }
  65.  
  66.  
  67. /*
  68.  * delete files given id
  69.  */
  70. bool
  71. cancel(id)
  72. char *id;
  73. {
  74.     bool searchhist();
  75.  
  76.     return searchhist(id, delete);
  77. }
  78.  
  79.  
  80. /*
  81.  * check if article has been recieved
  82.  */
  83. bool
  84. chkhist(id)
  85. char *id;
  86. {
  87.     bool searchhist();
  88.  
  89.     return searchhist(id, chk);
  90. }
  91.  
  92.  
  93. /*
  94.  * scan history, clearing uflag list entry if id not seen
  95.  */
  96. scanhist(ulist, usize)
  97. char **ulist;
  98. int usize;
  99. {
  100.     register FILE    *f;
  101.     register char *s, **found;
  102.     register int i;
  103.     char *key[1];
  104.     char buf[BUFSIZ * 2];
  105.     bool         * seen;
  106.  
  107.     extern char *newsdir;
  108.  
  109.     seen = (bool * ) myalloc((int) sizeof(bool) * usize);
  110.     memset((char *)seen, 0, (int) sizeof(bool) * usize);
  111.  
  112.     f = fopenf(histname, "r");
  113.     while (fgets(buf, sizeof(buf), f)) {
  114.         if (s = strchr(buf, ' '))
  115.             *s = '\0';
  116.         else
  117.             error("Bad format: %s", histname);
  118.         key[0] = buf;
  119.         found = (char **) bsearch((char *) key, (char *) ulist, (unsigned) usize,
  120.              sizeof(char *), strpcmp);
  121.         if (found)
  122.             seen[found - ulist] = true;
  123.     }
  124.     fclose(f);
  125.  
  126.     for (i = 0; i < usize; i++)
  127.         if (!seen[i]) {
  128.             free(ulist[i]);
  129.             ulist[i] = NIL(char);
  130.         }
  131.     free((char *)seen);
  132. }
  133.  
  134.  
  135. /*
  136.  * open hist file, write id and time
  137.  */
  138. openhist(hp)
  139. header *hp;
  140. {
  141.  
  142.     histid = newstr(hp->h_messageid);
  143.     if (hp->h_expires)
  144.         etime = atot(hp->h_expires);
  145.     else
  146.         etime = 0L;
  147.     histline = NIL(char);
  148. }
  149.  
  150.  
  151. /*
  152.  * write name of file article resides into history file
  153.  */
  154. writehist(fname)
  155. char *fname;
  156. {
  157.     histline = (histline ? catstr2(histline, " ", fname) : newstr(fname));
  158. }
  159.  
  160.  
  161. /*
  162.  * close history file
  163.  */
  164. closehist()
  165. {
  166.     register FILE    *f;
  167.     extern long now;
  168.  
  169.     f = fopenl(histname);
  170.     fseek(f, 0L, 2);
  171.     (void) fprintf(f, "%s %s%ld %s\n", histid, etime ? "E" : "", etime ? etime :
  172.         now, histline);
  173.     fclose(f);
  174. #if !AUSAM
  175.     unlock(histname);
  176. #endif
  177.     free(histid); 
  178.     free(histline);
  179. }
  180.  
  181.  
  182. /*
  183.  * remove a news item
  184.  * check owner first
  185.  */
  186. static
  187. remove(fname)
  188. char *fname;
  189. {
  190.     header            h;
  191.     FILE             * f;
  192.     register char *s, *mname;
  193.  
  194. #if AUSAM
  195.     extern struct pwent pe;
  196. #else
  197.     extern struct passwd *pp;
  198. #endif
  199.     extern char systemid[];
  200.     extern bool        su;
  201.     extern bool        pflag;
  202.  
  203.     if (!su && !pflag) {
  204.         f = fopenf(fname, "r");
  205.         gethead(f, &h);
  206.         fclose(f);
  207.         if (s = strchr(h.h_from, ' '))
  208.             *s = '\0';
  209.         mname = newstr5(
  210. #if AUSAM
  211.                 pe.pw_strings[LNAME],
  212. #else
  213.                 pp->pw_name,
  214. #endif
  215.             "@", systemid, ".", MYDOMAIN);
  216.         if (CMP(mname, h.h_from) != 0)
  217.             error("Can't cancel articles you didn't write.");
  218.         free(mname);
  219.     }
  220.     if (unlink(fname) != 0)
  221.         error("Couldn't unlink %s", fname);
  222.  
  223. }
  224.