home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / uucp_mods.lha.30.12.93 / uucp_mods / uuhelp / uuhelp.c < prev   
Encoding:
C/C++ Source or Header  |  1993-12-04  |  6.3 KB  |  250 lines

  1.  
  2. /*
  3.  *  UUHELP.C
  4.  *
  5.  *  (c) Copyright 1993 Mike J. Bruins
  6.  *  hal9000.apana.org.au!bruins
  7.  *
  8.  * move files in UUNEWS:junk to their propper directories.
  9.  * If the news directory doesn't exist, create it.
  10.  * If the newgroup entry doesn't exist, create it.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include <exec/types.h>
  19. #include <exec/memory.h>
  20. #include <libraries/dos.h>
  21. #include "config.h"
  22. #include "version.h"
  23. #ifdef LATTICE
  24. #include <proto/dos.h>
  25. #include <proto/exec.h>
  26. #endif
  27.  
  28. IDENT(".03");
  29.  
  30. #ifndef SEEK_SET   /* handle the case where these are not defined */
  31. #define SEEK_SET 0
  32. #define SEEK_CUR 1
  33. #define SEEK_END 2
  34. #endif
  35.  
  36. BPTR lk=NULL;
  37. struct FileInfoBlock *fb = 0;
  38. int days=7;
  39. void CleanUp(int error);
  40. int EnsureDir(char *);
  41.  
  42. main(argc,argv) int argc; char **argv;{
  43. /* pass each file of uunews:junk to SplitNews() */
  44.   char path[256], datadir[256];
  45.   void SplitNews(char *path);
  46.  
  47.   mountrequest(0); /* disable requestors */
  48.   printf("UUHELP: V0.01, written by Mike Bruins, bruins@hal9000.apana.org.au\n");
  49.   printf("uuhelp <days>\n");
  50.   if(argc>2) printf("extra arguments ignored.\n");
  51.   if(argc>1){
  52.     days=atoi(argv[1]);
  53.     printf("trim days set to %d\n",days);
  54.   }
  55. /* Allocate a lock */
  56.   fb = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), 0);
  57.   if (!fb) {
  58.     fprintf(stderr, "Can't allocate memory for file info block\n");
  59.     CleanUp(1);
  60.   }
  61.   strcpy(path,GetConfigDir(UUNEWS));
  62.   strcat(path,"junk");
  63.   lk = Lock(path, ACCESS_READ);
  64.   
  65.   if (!lk) {
  66.     fprintf(stderr, "Can't lock %s\n", path);
  67.     CleanUp(1);
  68.   }
  69.   if (!Examine(lk, fb)) {
  70.     fprintf(stderr, "Can't examine %s\n", path);
  71.     CleanUp(1);
  72.   }
  73.   while (ExNext(lk, fb)){         /* step through junk directory */
  74.     if (fb->fib_DirEntryType < 0) {  /* is a file */
  75.       sprintf(datadir,"%s/%s",path, fb->fib_FileName);
  76.       SplitNews(datadir);
  77.       remove(datadir);
  78.     }
  79.   }
  80.   CleanUp(0);
  81. }
  82.  
  83. void CleanUp(int error){
  84.   FreeMem(fb, sizeof(struct FileInfoBlock));
  85.   if (lk) UnLock(lk);
  86.   mountrequest(1); /* enable requestors */
  87.   exit(error);
  88. }
  89.  
  90. void SplitNews(char *file){
  91. /* send the file to each newsgroup by calling NewsToGroup() */
  92.   FILE *fp;
  93.   char line[1024],group[128];
  94.   char *ptr,*grp;
  95.   void NewsToGroup(char *group,char *file);
  96.  
  97.   if((fp=fopen(file,"r"))==NULL){
  98.     printf(" Can't open %s\n",file);
  99.     return;
  100.   }
  101.   for(;;){ /* scan header */
  102.     fgets(line,sizeof line,fp);
  103.     if(feof(fp)){
  104.       fclose(fp);
  105.       return;
  106.     }
  107.     if(line[0]=='\r'){  /* finished header */
  108.       fclose(fp);
  109.       return;
  110.     }
  111.     if(!strncmp(line,"Newsgroups:",11)){
  112.       fclose(fp);
  113.       break;
  114.     }
  115.   }
  116.   ptr=&line[11];                          /* step through newsgroups */
  117.   while((grp=strtok(ptr,","))!=NULL){
  118.     ptr=NULL;
  119.     sscanf(grp,"%s",group);
  120.     NewsToGroup(group,file);
  121.   }
  122. }
  123.  
  124. void NewsToGroup(char *group,char *source){
  125. /* NewsToGroup: Updates newsgroups, directories and .next files */
  126.   char dir[512],dest[512],*ptr;
  127.   int i;
  128.   int GetNext(char *grp, int *i);
  129.   int SetNext(char *grp, int i);
  130.  
  131.   printf("source  %s, ",source);
  132.   strcpy(dir,GetConfigDir(UUNEWS));
  133.   strcpy(dest,group);                   /* use dest as a temp variable */
  134.   while((ptr=strchr(dest,'.'))!=NULL) *ptr='/';
  135.   strcat(dir,dest);
  136.   printf("dir %s\n",dir);
  137.   if(!EnsureDir(dir)){                  /* ensure the directory is there */
  138.     printf("Can't make %s\n",dir);
  139.     return;
  140.   }
  141.   EnsureNewsEntry(group);               /* ensure linsted in newsgroups  */
  142.   if(!GetNext(dir,&i)) i=1;             /* get next seq # */
  143.   sprintf(dest,"%s/%d",dir,i);          /* create filename */
  144.   i++;
  145.   SetNext(dir,i);                       /* store incrimented seq # */
  146.   copy(source,dest);
  147.   printf("Copy: %s to %s\n",source,dest);
  148. }
  149.  
  150. int GetNext(char *grp, int *i){
  151. /* GetNext: Reads the value of a .next file in a news directory.
  152.  * requires an existing directory path.
  153.  */
  154.   FILE *fp;
  155.   char path[256],line[20];
  156.   *i=0;
  157.   sprintf(path,"%s/.next",grp);
  158.   if((fp=fopen(path,"r"))==NULL) return FALSE;
  159.   fgets(line,sizeof line,fp);
  160.   fclose(fp);
  161.   sscanf(line,"%d",i);
  162.   return TRUE;
  163. }
  164.  
  165. int SetNext(char *grp,int i){
  166. /* SetNext: Changes the value of a .next file in a news directory.
  167.  * requires an existing directory path.
  168.  */
  169.   FILE *fp;
  170.   char path[256];
  171.   sprintf(path,"%s/.next",grp);
  172.   if((fp=fopen(path,"w"))==NULL) return FALSE;
  173.   fprintf(fp,"%d\n",i);
  174.   fclose(fp);
  175.   return TRUE;
  176. }
  177.  
  178. int EnsureNewsEntry(char *grp){
  179. /* EnsureNews: Makes sure the newsgroup exists in the UULIB:newsgroup file.
  180.  *   Will add it if necessary. Requires a newsgroup in dot notation.
  181.  */
  182.   FILE *fp;
  183.   char path[256],line[256],group[256];
  184.  
  185.   strcpy(path,GetConfigDir(UULIB));
  186.   strcat(path,"newsgroups");
  187.   if((fp=fopen(path,"r+"))==NULL){
  188.     printf("%s doesn't exist, creating.\n",path);
  189.     if((fp=fopen(path,"w+"))==NULL){
  190.       printf("%s can't create.\n",path);
  191.       return 0;                              /* error */
  192.     }
  193.   }
  194.   for(;;){ /* scan header */                 /* scan list */
  195.     fgets(line,sizeof line,fp);
  196.     if(feof(fp)) break;
  197.     sscanf(line,"%s",group);
  198.     if(!strcmp(group,grp)){                  /* found, quit */
  199.       fclose(fp);
  200.       return 1;
  201.     }
  202.   }
  203.   fseek(fp,0,SEEK_END);                      /* goto end then add */
  204.   fprintf(fp,"%-49s %d\n",grp,days);
  205.   fclose(fp);
  206.   return 1;
  207. }
  208.  
  209. int EnsureDir(char *dir){
  210. /* EnsureDir: multi level directory create routine.
  211.  * returns TRUE on success, FALSE otherwise.
  212.  */
  213.   char temp[512],*ptr;
  214.   if(IsDir(dir)) return TRUE;
  215.   strcpy(temp,dir);
  216.   if((ptr=strrchr(temp,'/'))==NULL){
  217.     if((ptr=strrchr(temp,':'))==NULL){
  218.       printf("EnsureDir:error, relative path given.\n");
  219.       return FALSE;
  220.     }
  221.     if(ptr[1]=='\0') return FALSE;             /* silly to make volume */
  222.     if(!mkdir(temp)) return TRUE;
  223.     return FALSE;
  224.   }
  225.   *ptr='\0';                                   /* split off tail */
  226.   if(EnsureDir(temp) && !mkdir(dir)) return TRUE;
  227.   return FALSE;
  228. }
  229.  
  230. int copy(char *from,char *to){
  231. /* copy: copies the contents of one file into another. Destroys previous
  232.  *     contents.  Requires two valid filenames.
  233.  * Returns 0 on success, -1 on error.
  234.  */
  235.   FILE *fpi,*fpo;
  236.   char line[4096];
  237.   if((fpo=fopen(to,"w"))==NULL) return -1;   /* can't open dest */
  238.   if((fpi=fopen(from,"r"))==NULL){           /* can't open source */
  239.     fclose(fpo);
  240.     return -1;
  241.   }
  242.   while(!feof(fpi)){
  243.     fgets(line,sizeof fpi,fpi);
  244.     fputs(line,fpo);
  245.   }
  246.   fclose(fpo);
  247.   fclose(fpi);
  248.   return 0;
  249. }
  250.