home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / yapp / part01 / arch.c next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  6.5 KB  |  203 lines

  1. /* ARCH.C Copyright */
  2. static    char sccsid[] = "@(#)arch.c 1.8 93/05/01 (c)1993 thalerd";
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "config.h"
  6. #include "struct.h"
  7. #include "item.h"
  8. #include "lib.h"
  9. #include "globals.h"
  10. #include "arch.h"
  11. #include "xalloc.h"
  12. #include "news.h"
  13. #include "sum.h"
  14.  
  15. #ifdef NEWS
  16. void
  17. check_news(re)
  18. response_t *re;
  19. {
  20.    /* News article? */
  21.    if (xsizeof(re->text) && !strncmp(re->text[0],",N",2)) {
  22.  
  23.       /* Get article # */
  24.       if (sscanf(re->text[0]+2,"%d",&(re->article))>0 
  25.        && re->article) {
  26.       xfree(re->text);
  27.       get_article(re);
  28.       }
  29.  
  30.       /* Get message id# */
  31.       if (re->text && xsizeof(re->text)>1 && !strncmp(re->text[1],",M",2)) {
  32.          if (re->mid) xfree(re->mid);
  33.          re->mid=xstrdup(re->text[1]+2);
  34.       }
  35.    }
  36. }
  37. #endif
  38.  
  39. /******************************************************************************/
  40. /* READ IN A SINGLE RESPONSE                                                  */
  41. /* Starting at current file position, read in a response.  The ending file    */
  42. /* position will be the start of the next response.  Also, allocates space    */
  43. /* for text which needs to be freed.                                          */
  44. /* Assumes (sum) that this is always done within the current conference       */
  45. /******************************************************************************/
  46. void                         /* RETURNS: (nothing)             */
  47. get_resp(fp,re,fast,num) /* ARGUMENTS                      */
  48. FILE       *fp;              /*    Current file position       */
  49. response_t *re;              /*    Response to fill in         */
  50. short       fast;            /*    Don't save the actual text? */
  51. short       num;
  52. {
  53. /*
  54. gr: at offset %ld got line %s
  55. get_resp: returning response author %s date %lx flags %d textoff %ld
  56. Corrupted item (missing fields before offset %ld)
  57.  */
  58.    char       buff[MAX_LINE_LENGTH];
  59.    char     **who;
  60.    char       done=0;
  61.  
  62. #if 0
  63. /* ifdef NEWS */
  64.    if (st_glob.c_security & CT_NEWS) {
  65.       news_get_resp(fp,re,fast,num);
  66.       return;
  67.    }
  68. #endif
  69.  
  70.    /* Get response */
  71.    if (re->offset>=0 && re->numchars>0 && fast==GR_ALL) {
  72.       if (fseek(fp,re->textoff,0)) {
  73.          sprintf(buff,"%d",re->textoff);
  74.          error("fseeking to ",buff);
  75.       }
  76.       re->text = grab_more(fp,",E",0);
  77. #ifdef NEWS
  78.       check_news(re);
  79. #endif
  80.       return;
  81.    } 
  82.    if (re->offset>=0 && fseek(fp,re->offset,0)) {
  83.       sprintf(buff,"%d",re->textoff);
  84.       error("fseeking to ",buff);
  85.    }
  86.    if (re->offset<0) { /* Find start of response */
  87.       short i,j;
  88.       
  89.       for (i=1; i<=num && re[-i].endoff<0; i++); /* find prev offset */
  90.       for (j=i-1; j>0; j--) {
  91.          get_resp(fp,&(re[-j]),GR_OFFSET,num-j);
  92.       }
  93.       if (num && fseek(fp,re[-1].endoff,0)) {
  94.          sprintf(buff,"%d",re[-1].endoff);
  95.          error("fseeking to ",buff);
  96.       }
  97.    }
  98.    if (fast==GR_OFFSET) {
  99.          re->offset  = ftell(fp); 
  100.          while (ngets(buff,fp) && buff[1]!='T');
  101.          re->textoff = ftell(fp); 
  102.          while (ngets(buff,fp) && strcmp(buff,",E") && strncmp(buff,",R",2));
  103.          if (!strncmp(buff,",R",2)) re->endoff = ftell(fp)-strlen(buff)-1;
  104.          else re->endoff  = ftell(fp); 
  105.          re->numchars = -1;
  106.    } else {
  107.  
  108. #ifdef NEWS
  109.       if (re->mid) 
  110.             xfree(re->mid);
  111.       re->mid = NULL;
  112.         re->article = 0;
  113. #endif
  114.       re->parent = 0;
  115.  
  116. /*printf("Lines: ");*/
  117.       while (!done && !(status & S_INT)) {
  118.          if (!ngets(buff,fp)) break; /* UNK error */
  119. /*putchar(buff[1]);*/
  120.          switch(buff[1]) {
  121.          case 'A': if (re->fullname) xfree(re->fullname);
  122.                    re->fullname=xstrdup(buff+2); 
  123.                    break;
  124.          case 'D': sscanf(buff+2,"%lx",&(re->date)); break;
  125.          case 'E': done=1; re->endoff = ftell(fp); break;
  126.       /* case 'H': strcpy(subj,buff+2); break; */
  127.          case 'R': re->offset = ftell(fp)-strlen(buff)-1;
  128.                    sscanf(buff+2,"%hx",&(re->flags)); break;
  129. /*
  130.          case 'M': if (re->mid) xfree(re->mid);
  131.            re->mid=xstrdup(buff+2);
  132.            break;
  133. */
  134.          case 'P': sscanf(buff+2,"%hd",&(re->parent)); 
  135.                    re->parent++;
  136.                    break;
  137.          case 'T': re->textoff = ftell(fp); 
  138.                    re->numchars= 0;
  139.                    if (fast==GR_ALL) {
  140.                       re->text = grab_more(fp,",E",0);
  141.                    } else {
  142.                       while (ngets(buff,fp) && strcmp(buff,",E") && strncmp(buff,",R",2));
  143.                       re->text = NULL;
  144.                    } 
  145.                    re->numchars= ftell(fp)-re->textoff-3; /* -",E" */
  146. #ifdef NEWS
  147.                    check_news(re);
  148. #endif
  149.                    done=1;
  150.                    break;
  151.          case 'U': who=explode(buff+2,",");
  152.                    re->uid = atoi(who[0]);
  153.                    if (re->login) xfree(re->login);
  154.                    re->login = xstrdup(who[1]);
  155.                    xfree(who); 
  156.                    break;
  157.          }
  158.       }
  159.    }
  160.    if (flags & O_DEBUG)
  161.       printf("get_resp: returning response author %s date %lx flags %d textoff %ld\n", re->login,get_date(re->date,0),re->flags, re->textoff);
  162. }
  163.  
  164. /******************************************************************************/
  165. /* READ IN INFORMATION SUMMARIZING ALL THE RESPONSES IN AN ITEM               */
  166. /* Note that this is currently only used within the current cf, but could     */
  167. /* easily be used for a remote cf by passing in the right sum & re, confidx   */
  168. /******************************************************************************/
  169. void                          /* RETURNS: (nothing)                    */
  170. get_item(fp,n,re,sum)         /* ARGUMENTS:                            */
  171. FILE      *fp;                /*    File pointer to read from          */
  172. short      n;                 /*    Which item # we're reading         */
  173. response_t re[MAX_RESPONSES]; /*    Buffer array to hold response info */
  174. sumentry_t sum[MAX_ITEMS];    /*    Buffer array holding info on items */
  175. {
  176.    short i;
  177.    long offset=0;
  178.  
  179.    /* For each response */
  180.    for (i=0; i<MAX_RESPONSES; i++) {
  181.       re[i].offset = re[i].endoff = -1;
  182.       re[i].date = 0;
  183.    }
  184.  
  185.    /* Find EOF */
  186.    fseek(fp, 0L, 2);  
  187.    offset = ftell(fp);
  188.    rewind(fp);
  189.  
  190.    /* Get all responses, and fix sum file NR value */
  191.    for (i=0; !i || re[i-1].endoff < offset; i++) {
  192.       get_resp(fp,&(re[i]),(short)GR_OFFSET,i);
  193.       if (flags & O_DEBUG)
  194.          printf("%2d Offset = %4o Textoff = %4o\n",i,re[i].offset, 
  195.           re[i].textoff);
  196.    }
  197. /*printf("GR: %d =?= %d\n", sum[n-1].nr, i);*/
  198.    if (sum[n-1].nr != i) {
  199.       sum[n-1].nr = i;
  200.       save_sum(sum,(short)(n-1),confidx,&st_glob);
  201.    }
  202. }
  203.