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

  1. /* JOQ.C */
  2. static   char sccsid[] = "@(#)joq.c 1.24 93/06/09 Copyright (c)1993 thalerd";
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include "config.h"
  8. #include "struct.h"
  9. #include "lib.h"
  10. #include "globals.h"
  11. #include "joq.h"
  12. #include "item.h"
  13. #include "sum.h"
  14. #include "xalloc.h"
  15. #include "files.h"
  16. #include "stats.h"
  17.  
  18. static int pdebug = 0;
  19.  
  20. /******************************************************************************/
  21. /* DISPATCH CONTROL TO APPROPRIATE JOQ COMMAND FUNCTION                       */
  22. /******************************************************************************/
  23. char                        /* RETURNS: 0 on abort, 1 else */
  24. joq_cmd_dispatch(argc,argv) /* ARGUMENTS:                  */
  25. int    argc;                /*    Number of arguments      */
  26. char **argv;                /*    Argument list            */
  27. {
  28.    char **ulst,file[MAX_LINE_LENGTH],buff[MAX_LINE_LENGTH];
  29.    short  j;
  30.    char **config;
  31.  
  32.    if (match(argv[0],"r_egister") 
  33.     || match(argv[0],"j_oin")
  34.     || match(argv[0],"p_articipate"))   { 
  35.       if (confidx>=0) leave(0,(char**)0);
  36.       write_part((char *)NULL);
  37.       st_new.c_status   |= CS_JUSTJOINED; 
  38.       mode=M_OK; 
  39.  
  40.       /* Add login to ulist */
  41.       if (config = get_config(joinidx)) {
  42.          char sec;
  43.          sec=(xsizeof(config)>CF_SECURITY)? atoi(config[CF_SECURITY]) : 0;
  44.          if (!(sec & CT_READONLY)
  45.           && (sec & CT_BASIC)!=CT_PRESELECT
  46.           && (sec & CT_BASIC)!=CT_PARANOID) {
  47.  
  48.       sprintf(file,"%s/%s",conflist[joinidx].location,"ulist");
  49.       sprintf(buff,"%s\n",login);
  50.       if (!(ulst=grab_file(conflist[joinidx].location,"ulist",
  51.        GF_SILENT|GF_WORD|GF_IGNCMT)))
  52.          write_file(file,buff);
  53.       else {
  54.          for (j=xsizeof(ulst)-1; 
  55.               j>=0 && strcmp(ulst[j],login) && uid!=atoi(ulst[j]); 
  56.               j--);
  57.          if (j<0)
  58.             write_file(file,buff);
  59.          xfree(ulst);
  60.             }
  61.          }
  62.       }
  63.  
  64.    } else if (match(argv[0],"o_bserver")) { 
  65.       sumentry_t sum2[MAX_ITEMS];
  66.       short i;
  67.  
  68.       if (confidx>=0) leave(0,(char**)0);
  69.       st_new.c_status |= (CS_OBSERVER|CS_JUSTJOINED); 
  70.       mode=M_OK; 
  71.  
  72.       /* Initialize part[] */
  73.       for (i=0; i<MAX_ITEMS; i++) part[i].nr=part[i].last=0;
  74.       get_status(&st_new,sum2,part,joinidx);
  75.       st_new.sumtime = 0;
  76.       for (i=st_new.i_first+1; i<st_new.i_last; i++) time(&(part[i-1].last));
  77.    } else if (match(argv[0],"h_elp"))     help(argc,argv); 
  78.    else if (match(argv[0],"q_uit"))     { status |= S_QUIT; mode=M_OK; }
  79.    else {
  80.       printf("I don't understand \"%s\" - type HELP for help\n",argv[0]);
  81.    }
  82.    return 1;
  83. }
  84.  
  85. /******************************************************************************/
  86. /* WRITE OUT A USER PARTICIPATION FILE FOR THE CURRENT CONFERENCE             */
  87. /******************************************************************************/
  88. void                 /* RETURNS: (nothing) */
  89. write_part(partfile) /* ARGUMENTS:         */
  90. char *partfile;      /*    Filename        */
  91. {
  92.    char       buff[MAX_LINE_LENGTH],*file;
  93.    FILE      *fp;
  94.    short      i,cpid,wpid;
  95.    status_t   *stt;
  96.    sumentry_t sum2[MAX_ITEMS],*sum3;
  97.  
  98.    if (st_glob.c_status & CS_OBSERVER) 
  99.       return; 
  100.  
  101.    if (partfile) {
  102.       file=partfile;
  103.       sum3 = sum;
  104.       stt  = &st_glob;
  105.    } else {
  106.       char **config;
  107.  
  108.       if (!(config = get_config(joinidx)))
  109.          return;
  110.       file = config[CF_PARTFILE];
  111.       sum3 = sum2;
  112.       stt  = &st_new;
  113.  
  114.       /* Initialize part[] */
  115.       for (i=0; i<MAX_ITEMS; i++) part[i].nr=part[i].last=0;
  116.       get_status(stt,sum2,part,joinidx);
  117.       for (i=st_new.i_first+1; i<st_new.i_last; i++)
  118.          time(&(part[i-1].last));
  119.       stt->sumtime = 0;
  120.    }
  121.  
  122.    /* Create WORK/.name.cf */
  123.    sprintf(buff,"%s/%s",work,file);
  124.    if (flags & O_DEBUG) printf("join: Partfile=%s\n",buff);
  125.    if (pdebug) printf("WRITING %s\n",buff);
  126.  
  127.    /* FORK */
  128.    if (cpid=fork()) { /* parent */
  129.       if (cpid<0) return; /* error: couldn't fork */
  130.       while ((wpid = wait((int *)0)) != cpid && wpid != -1);
  131.    } else { /* child */
  132.       setuid(getuid());
  133.       setgid(getgid());
  134.  
  135.       /* KKK*** in the future, allocate string array of #items+2,
  136.          save lines in there, call dump_file, and free the array */
  137.       if ((fp=mopen(buff,O_W))==NULL) /* "w" */
  138.          exit(1);
  139.       fprintf(fp,"!<pr03>\n%s\n",st_glob.fullname);
  140.  
  141.       if (pdebug) printf("first %d last %d\n",stt->i_first, stt->i_last);
  142.       for (i=stt->i_first; i<=stt->i_last; i++) {
  143.          if (pdebug) printf("sum3[%d]=%d ",i-1,sum3[i-1].nr);
  144.          if (sum3[i-1].nr || part[i-1].last) {
  145.             fprintf(fp,"%d %d %X\n",i,part[i-1].nr,part[i-1].last);
  146.             if (pdebug) printf(": %d %d %X",i,part[i-1].nr,part[i-1].last);
  147.             fflush(fp);
  148.          }
  149.          if (pdebug) printf("\n");
  150.       }
  151.       mclose(fp);
  152.       exit(0);
  153.    } /* ENDFORK */
  154.  
  155.    if (flags & O_DEBUG) printf("write_part: fullname=%s\n",st_glob.fullname);
  156. }
  157.  
  158. /******************************************************************************/
  159. /* READ IN A USER PARTICIPATION FILE FOR SOME CONFERENCE                      */
  160. /******************************************************************************/
  161. char                          /* RETURNS: (nothing)  */
  162. read_part(partfile,part,stt,idx)      /* ARGUMENTS:          */
  163. char        *partfile;        /*    Filename         */
  164. partentry_t  part[MAX_ITEMS]; /*    Array to fill in */
  165. status_t    *stt;
  166. short idx;
  167. {
  168.    char **partf,buff[MAX_LINE_LENGTH];
  169.    short sz,i,a,b;
  170.    long d;
  171.    struct stat st;
  172.    sumentry_t sum2[MAX_ITEMS];
  173.  
  174.    for (i=0; i<MAX_ITEMS; i++) part[i].nr=part[i].last=0;
  175.    strcpy(stt->fullname,st_glob.fullname);
  176.    if (!(partf=grab_file(work,partfile,GF_SILENT))) {
  177.  
  178.       /* Newly joined, Initialize part[] */
  179.       for (i=0; i<MAX_ITEMS; i++) part[i].nr=part[i].last=0;
  180.       get_status(stt,sum2,part,idx);
  181.       stt->sumtime = 0;
  182.       for (i=stt->i_first+1; i<stt->i_last; i++) time(&(part[i-1].last));
  183.  
  184.       return 0;
  185.    }
  186.    sz=xsizeof(partf);
  187.    if (!sz || strcmp(partf[0],"!<pr03>"))
  188.        printf("UNK Invalid participation file format.\n");
  189.    else if (sz>1)
  190.        strcpy(stt->fullname,partf[1]);
  191.    for (i=2; i<xsizeof(partf); i++) {
  192.       sscanf(partf[i],"%hd %hd %lx",&a,&b,&d);
  193.       part[a-1].nr   = b;
  194.       part[a-1].last = d;
  195.    }
  196.    xfree(partf);
  197.    
  198.    sprintf(buff,"%s/%s",work,partfile);
  199.    if (!stat(buff,&st) && st.st_size>0)
  200.       stt->parttime = st.st_mtime;
  201.  
  202.    if (flags & O_DEBUG) printf("read_part: fullname=%s\n",st_glob.fullname);
  203.    return 1;
  204. }
  205.