home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * UUHELP.C
- *
- * (c) Copyright 1993 Mike J. Bruins
- * hal9000.apana.org.au!bruins
- *
- * move files in UUNEWS:junk to their propper directories.
- * If the news directory doesn't exist, create it.
- * If the newgroup entry doesn't exist, create it.
- *
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <libraries/dos.h>
- #include "config.h"
- #include "version.h"
- #ifdef LATTICE
- #include <proto/dos.h>
- #include <proto/exec.h>
- #endif
-
- IDENT(".03");
-
- #ifndef SEEK_SET /* handle the case where these are not defined */
- #define SEEK_SET 0
- #define SEEK_CUR 1
- #define SEEK_END 2
- #endif
-
- BPTR lk=NULL;
- struct FileInfoBlock *fb = 0;
- int days=7;
- void CleanUp(int error);
- int EnsureDir(char *);
-
- main(argc,argv) int argc; char **argv;{
- /* pass each file of uunews:junk to SplitNews() */
- char path[256], datadir[256];
- void SplitNews(char *path);
-
- mountrequest(0); /* disable requestors */
- printf("UUHELP: V0.01, written by Mike Bruins, bruins@hal9000.apana.org.au\n");
- printf("uuhelp <days>\n");
- if(argc>2) printf("extra arguments ignored.\n");
- if(argc>1){
- days=atoi(argv[1]);
- printf("trim days set to %d\n",days);
- }
- /* Allocate a lock */
- fb = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), 0);
- if (!fb) {
- fprintf(stderr, "Can't allocate memory for file info block\n");
- CleanUp(1);
- }
- strcpy(path,GetConfigDir(UUNEWS));
- strcat(path,"junk");
- lk = Lock(path, ACCESS_READ);
-
- if (!lk) {
- fprintf(stderr, "Can't lock %s\n", path);
- CleanUp(1);
- }
- if (!Examine(lk, fb)) {
- fprintf(stderr, "Can't examine %s\n", path);
- CleanUp(1);
- }
- while (ExNext(lk, fb)){ /* step through junk directory */
- if (fb->fib_DirEntryType < 0) { /* is a file */
- sprintf(datadir,"%s/%s",path, fb->fib_FileName);
- SplitNews(datadir);
- remove(datadir);
- }
- }
- CleanUp(0);
- }
-
- void CleanUp(int error){
- FreeMem(fb, sizeof(struct FileInfoBlock));
- if (lk) UnLock(lk);
- mountrequest(1); /* enable requestors */
- exit(error);
- }
-
- void SplitNews(char *file){
- /* send the file to each newsgroup by calling NewsToGroup() */
- FILE *fp;
- char line[1024],group[128];
- char *ptr,*grp;
- void NewsToGroup(char *group,char *file);
-
- if((fp=fopen(file,"r"))==NULL){
- printf(" Can't open %s\n",file);
- return;
- }
- for(;;){ /* scan header */
- fgets(line,sizeof line,fp);
- if(feof(fp)){
- fclose(fp);
- return;
- }
- if(line[0]=='\r'){ /* finished header */
- fclose(fp);
- return;
- }
- if(!strncmp(line,"Newsgroups:",11)){
- fclose(fp);
- break;
- }
- }
- ptr=&line[11]; /* step through newsgroups */
- while((grp=strtok(ptr,","))!=NULL){
- ptr=NULL;
- sscanf(grp,"%s",group);
- NewsToGroup(group,file);
- }
- }
-
- void NewsToGroup(char *group,char *source){
- /* NewsToGroup: Updates newsgroups, directories and .next files */
- char dir[512],dest[512],*ptr;
- int i;
- int GetNext(char *grp, int *i);
- int SetNext(char *grp, int i);
-
- printf("source %s, ",source);
- strcpy(dir,GetConfigDir(UUNEWS));
- strcpy(dest,group); /* use dest as a temp variable */
- while((ptr=strchr(dest,'.'))!=NULL) *ptr='/';
- strcat(dir,dest);
- printf("dir %s\n",dir);
- if(!EnsureDir(dir)){ /* ensure the directory is there */
- printf("Can't make %s\n",dir);
- return;
- }
- EnsureNewsEntry(group); /* ensure linsted in newsgroups */
- if(!GetNext(dir,&i)) i=1; /* get next seq # */
- sprintf(dest,"%s/%d",dir,i); /* create filename */
- i++;
- SetNext(dir,i); /* store incrimented seq # */
- copy(source,dest);
- printf("Copy: %s to %s\n",source,dest);
- }
-
- int GetNext(char *grp, int *i){
- /* GetNext: Reads the value of a .next file in a news directory.
- * requires an existing directory path.
- */
- FILE *fp;
- char path[256],line[20];
- *i=0;
- sprintf(path,"%s/.next",grp);
- if((fp=fopen(path,"r"))==NULL) return FALSE;
- fgets(line,sizeof line,fp);
- fclose(fp);
- sscanf(line,"%d",i);
- return TRUE;
- }
-
- int SetNext(char *grp,int i){
- /* SetNext: Changes the value of a .next file in a news directory.
- * requires an existing directory path.
- */
- FILE *fp;
- char path[256];
- sprintf(path,"%s/.next",grp);
- if((fp=fopen(path,"w"))==NULL) return FALSE;
- fprintf(fp,"%d\n",i);
- fclose(fp);
- return TRUE;
- }
-
- int EnsureNewsEntry(char *grp){
- /* EnsureNews: Makes sure the newsgroup exists in the UULIB:newsgroup file.
- * Will add it if necessary. Requires a newsgroup in dot notation.
- */
- FILE *fp;
- char path[256],line[256],group[256];
-
- strcpy(path,GetConfigDir(UULIB));
- strcat(path,"newsgroups");
- if((fp=fopen(path,"r+"))==NULL){
- printf("%s doesn't exist, creating.\n",path);
- if((fp=fopen(path,"w+"))==NULL){
- printf("%s can't create.\n",path);
- return 0; /* error */
- }
- }
- for(;;){ /* scan header */ /* scan list */
- fgets(line,sizeof line,fp);
- if(feof(fp)) break;
- sscanf(line,"%s",group);
- if(!strcmp(group,grp)){ /* found, quit */
- fclose(fp);
- return 1;
- }
- }
- fseek(fp,0,SEEK_END); /* goto end then add */
- fprintf(fp,"%-49s %d\n",grp,days);
- fclose(fp);
- return 1;
- }
-
- int EnsureDir(char *dir){
- /* EnsureDir: multi level directory create routine.
- * returns TRUE on success, FALSE otherwise.
- */
- char temp[512],*ptr;
- if(IsDir(dir)) return TRUE;
- strcpy(temp,dir);
- if((ptr=strrchr(temp,'/'))==NULL){
- if((ptr=strrchr(temp,':'))==NULL){
- printf("EnsureDir:error, relative path given.\n");
- return FALSE;
- }
- if(ptr[1]=='\0') return FALSE; /* silly to make volume */
- if(!mkdir(temp)) return TRUE;
- return FALSE;
- }
- *ptr='\0'; /* split off tail */
- if(EnsureDir(temp) && !mkdir(dir)) return TRUE;
- return FALSE;
- }
-
- int copy(char *from,char *to){
- /* copy: copies the contents of one file into another. Destroys previous
- * contents. Requires two valid filenames.
- * Returns 0 on success, -1 on error.
- */
- FILE *fpi,*fpo;
- char line[4096];
- if((fpo=fopen(to,"w"))==NULL) return -1; /* can't open dest */
- if((fpi=fopen(from,"r"))==NULL){ /* can't open source */
- fclose(fpo);
- return -1;
- }
- while(!feof(fpi)){
- fgets(line,sizeof fpi,fpi);
- fputs(line,fpo);
- }
- fclose(fpo);
- fclose(fpi);
- return 0;
- }
-