home *** CD-ROM | disk | FTP | other *** search
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <dos.h>
- #include <exec/types.h>
- #include <exec/nodes.h>
- #include <clib/exec_protos.h>
- #include <clib/dos_protos.h>
- #include <pragmas/exec_pragmas.h>
- #include <pragmas/dos_pragmas.h>
-
- /*
- * Sendbatches - a program to batch news to other sites.
- *
- * Version 1.0
- * ⌐ by Walter Mildenberger in 1991. All rights reserved.
- *
- * I have tried to be as far as close to the <real, unix-> sendbatches
- * as I could. Processing of sendbatches is determined by a "batchparms"-
- * file compatible to the unix-format. Most of the actual programs are
- * AmigaOS-scripts.
- *
- * This program is provided "as is", no promises are made for the fitness
- * of this program. The programer is not responsible for any damage that
- * might or might not be caused by this program. Use it on your OWN risk!
- *
- * Distribution:
- * this program is NOT put in the public domain, but you can freely
- * (re-)distribute it as long as you follow the following terms:
- * o no fee is charged except small donations for copying and redistri-
- * buting (like postage). Everything above $US 7 or DM10 is not small.
- * o You are only allowed to copy and redistribute the programs, as long
- * as you keep ALL sources, scripts, and configuration-files together.
- * You are allowed to drop the binaries, if you have to, but remember:
- * not everybody has the right compiler or memory to compile the stuff.
- * o You SHOULD send <only to> ME any changes you made, but if you HAVE TO
- * redistribute modified sources, YOU MUST NOT remove or alter this
- * copyright-header! (well, you are allowed to fix spelling-errors :-) )
- *
- * Please send any enhancements, suggestions, bug-reports etc.pp. to the
- * following address: wasp@chumly.ka.sub.org
- *
- */
-
- /* define it correct for your actual News-System (use -d<value> on command */
- /* line instead) */
-
- /* #define CNEWS */
- /* #define AUCPP */
- /* #define DUUCP */
-
- #define TRANSMISSION_GRADE "N"
- /* which transmission-grade get uux'ed batches ? Dillon's uux defaults to "N" */
-
- /*
- * The names of the batch-file vary widely from News-System to News-System:
- * in CNews, this is "NewsArts:out.going/<systemname>/togo", while in
- * //\\migaUUCP+ it is "UUNEWS:<systemname>.nbatch", and in Dillon's UUCP
- * it is "UUSPOOL:Batch/<systemname>.
- *
- * the actual batch-file is determinated by concatenating the strings
- * OUT_GOING <systemname> BATCHFILE .
- *
- */
-
- #if defined(CNEWS)
-
- # define OUT_GOING "NewsArts:out.going/"
- # define BATCH_FILE "/togo"
- # define BATCHPARMS_FILENAME "NewsCtl:batch/batchparms"
-
- #elif defined(AUCPP)
-
- # define OUT_GOING "UUNEWS:"
- # define BATCH_FILE ".nbatch"
- # define BATCHPARMS_FILENAME "UULIB:news/batchparms"
-
- #elif defined(DUUCP)
-
- # define OUT_GOING "UUSPOOL:Batch/"
- # define BATCH_FILE ""
- # define BATCHPARMS_FILENAME "UULIB:batchparms"
-
- # else
- # ERROR_NO_NEWS_SYSTEM_SPECIFIED
- #endif
-
- #define LOCKSTRING "sendbatches"
-
- #define DEFAULT "/default/"
- #define WHITESPACE " \t\n"
-
- #define MAX_LINKS 128
- /*
- * I rather doubt someone using this program has really 128+ links, but
- * who can know it exactly ?
- *
- */
- char *SystemName[MAX_LINKS];
-
- #define SN_POOLSIZE (MAX_LINKS << 5)
- char *sn_pool = NULL;
-
- extern struct Library *SysBase, *DOSBase;
-
- /* The file locking functions are taken from Matt Dillon's AmigaUUCP V1.06D, */
- /* copyright by Matt Dillon. */
- typedef struct List LIST;
- typedef struct Node NODE;
-
- typedef struct {
- NODE Node;
- FILE *Fi;
- short Refs;
- } LNode;
-
-
- LIST LockList = { (NODE *)&LockList.lh_Tail, NULL, (NODE *)&LockList.lh_Head };
-
- void __regargs myerror(char *msg, int errorlevel);
- void main(long argc, char *argv[]);
- void __regargs DoBatch(char *name);
- void __regargs DoRealBatch(char *name);
-
- void __regargs LockFile(char *file);
- void __regargs UnLockFile( char *file);
- void __regargs FreeLockNode(LNode *node);
- void UnLockFiles(void);
-
- void __regargs myerror(char *msg, int errorlevel)
- { fputs(msg,stderr);
- fflush(stderr);
- if(errorlevel > 5)
- { if(sn_pool)
- free(sn_pool);
- exit(errorlevel);
- }
- return;
- }
-
- void main(argc,argv)
- LONG argc;
- char *argv[];
- { char *pathname;
- int i;
- if(!argc)
- myerror("can't run from Workbench, try again from cli\n",20);
- pathname = getcwd(NULL,256); /* directory */
- if(!pathname)
- myerror("can't get cd !\n",20);
- for( i = 1; i < argc; i++)
- { if(*argv[i] != '?')
- SystemName[i-1] = argv[i];
- else
- myerror( "USAGE: sendbatches [<systems to be batched>]\n"
- "⌐ by Walter Mildenberger in 1991, all rights reserved\n",20);
- }
- if(!SystemName[0]) /* No parameter given -> get the site-names from the */
- { chdir(OUT_GOING);
- sn_pool = malloc(SN_POOLSIZE);
- #if defined (CNEWS)
- if(i = getfnl("#?",sn_pool,SN_POOLSIZE,1))
- {
- #elif defined (DUUCP)
- if(i = getfnl("#?",sn_pool,SN_POOLSIZE,0))
- {
- #elif defined(AUCPP)
- if(i = getfnl("#?" BATCH_FILE,sn_pool,SN_POOLSIZE,0))
- {
- #endif
- if(strbpl(SystemName,MAX_LINKS,sn_pool) != i)
- myerror("to much out.going-systems, program needs recompiling "
- "(or use system-list as parameter)!\n",20);
- }
- }
- #ifdef AUCPP
- for(i=0; SystemName[i]; i++) /* <system>.nbatch -> <system> ! */
- if(ptr = strchr(SystemName[i],'.'))
- *ptr = 0;
- #endif
- if(!SystemName[0])
- myerror("don't know any system to batch to !\n",20);
- LockFile(LOCKSTRING);
- #ifdef CNEWS
- chdir("NewsArts:");
- #else
- chdir("UUNEWS:");
- #endif
- for(i = 0; SystemName[i] ; i++)
- DoBatch(SystemName[i]);
- UnLockFile(LOCKSTRING);
- if(sn_pool)
- free(sn_pool);
- chdir(pathname);
- free(pathname);
- }
-
- struct
- { long size,
- limit;
- char batcher[256],
- muncher[256],
- sender[256];
- }BatchParms;
-
- char workfile[256], tmpfile1[256],tmpfile2[256];
- char Buf[1024];
-
- void __regargs DoBatch(char *name)
- { long anzahl;
- BOOL found = FALSE;
- char *ptr;
- FILE *bpf; /* b)atchp)arms f)ile */
- setmem(&BatchParms,sizeof(BatchParms),0);
- if(bpf = fopen(BATCHPARMS_FILENAME,"r"))
- { while(!(found || feof(bpf) || ferror(bpf)))
- { fgets(Buf,sizeof(Buf),bpf);
- found = !strncmp(name,Buf,strlen(name));
- }
- if(! found)
- {
- #ifdef DEBUG
- sprintf(Buf,"can't find batch-parameters for %s, using " DEFAULT "!\n",name);
- myerror(Buf,0);
- #endif
- rewind(bpf);
- clrerr(bpf);
- while(!(found || feof(bpf) || ferror(bpf)))
- { fgets(Buf,sizeof(Buf),bpf);
- found = !strncmp(DEFAULT,Buf,strlen(DEFAULT));
- }
- }
- fclose(bpf);
- if(! found)
- myerror("can't get batch parameters!\n",20);
- if(ptr = strtok(Buf,WHITESPACE))
- { if(ptr = strtok(NULL,WHITESPACE)) /* skip sitename, ptr now points to size */
- { BatchParms.size = atol(ptr);
- if(ptr = strtok(NULL,WHITESPACE))
- { BatchParms.limit = atol(ptr);
- if(ptr = strtok(NULL,WHITESPACE))
- { stccpy(BatchParms.batcher,ptr,sizeof(BatchParms.batcher));
- if(ptr = strtok(NULL,WHITESPACE))
- { stccpy(BatchParms.muncher,ptr,sizeof(BatchParms.muncher));
- if(ptr = strtok(NULL,WHITESPACE))
- stccpy(BatchParms.sender,ptr,sizeof(BatchParms.sender));
- #define ERRTEXT "something is wrong with batchparms-line, "
- else
- myerror(ERRTEXT "sender not found !\n",5);
- }
- else
- myerror(ERRTEXT "muncher not found !\n",5);
- }
- else
- myerror(ERRTEXT "batcher not found !\n",5);
- }
- else
- myerror(ERRTEXT "limit not found !\n",5);
- }
- else
- myerror(ERRTEXT "size not found !\n",5);
- }
- else
- myerror(ERRTEXT "sitename not found !\n",5);
- }
- else
- myerror("can't open batchparms-file !\n",20);
- if(BatchParms.sender[0])
- { sprintf(workfile,"UUSPOOL:C.%.7s" TRANSMISSION_GRADE "#?",name);
- anzahl = getfnl(workfile,Buf,sizeof(Buf),0);
- BatchParms.limit -= anzahl;
- if(BatchParms.limit <= 0)
- printf("There are already %ld batchfiles for site %s !\n",anzahl,name);
- else
- DoRealBatch(name);
- }
- }
-
- void __regargs DoRealBatch(char *name)
- { FILE *fi, *fo;
- int error;
- strcpy(workfile,OUT_GOING);
- strcat(workfile,name);
- strcat(workfile,BATCH_FILE);
- strcpy(tmpfile1,workfile);
- strcat(tmpfile1,".$$$");
- if(access(workfile,0)==0)
- { rename(workfile,tmpfile1);
- strcat(workfile,".work");
- if(fo = fopen(workfile,"a"))
- { if(fi = fopen(tmpfile1,"r"))
- { while(!(feof(fi) || ferror(fi) || ferror(fo)))
- { fgets(Buf,sizeof(Buf),fi);
- fputs(Buf,fo);
- }
- fclose(fi);
- }
- else
- myerror("can't open tmpfile !\n",20);
- fclose(fo);
- }
- else
- myerror("can't open workfile !\n",20);
- unlink(tmpfile1);
- }
- else
- strcat(workfile,".work");
- sprintf(tmpfile1,"t:batch.%lx.1\0",(LONG)time(NULL));
- sprintf(tmpfile2,"t:batch.%lx.2\0",(LONG)time(NULL));
- if(access(workfile,0)!=0)
- fprintf(stdout,"nothing to queue for %s\n",name);
- while((BatchParms.limit > 0) && (access(workfile,0)==0))
- { sprintf(Buf,"%s %s %s %ld",BatchParms.batcher,tmpfile1,workfile,BatchParms.size);
- error = system(Buf);
- if(!error)
- { sprintf(Buf,"%s %s %s",BatchParms.muncher,tmpfile1,tmpfile2);
- error = system(Buf);
- if(! error)
- { unlink(tmpfile1);
- sprintf(Buf,"%s %s %s",BatchParms.sender,tmpfile2,name);
- error = system(Buf);
- if(! error)
- { unlink(tmpfile2);
- BatchParms.limit--;
- }
- }
- }
- if(error)
- { fprintf(stderr," >>%s<< failed with code %d !\n",Buf,error);
- BatchParms.limit = 0;
- }
- }/* End of while */
- }
-
- void __regargs LockFile(char *file)
- { char *ptr;
- LNode *node;
- LNode *n;
- for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
- ;
- ++ptr;
- if (node = malloc(sizeof(LNode) + strlen(ptr) + 16))
- { node->Node.ln_Name = (char *)(node + 1);
- sprintf(node->Node.ln_Name, "T:%s.LOCK", ptr);
- for (n = (LNode *)LockList.lh_Head; n != (LNode *)&LockList.lh_Tail; n = (LNode *)n->Node.ln_Succ)
- { if (strcmp(node->Node.ln_Name, n->Node.ln_Name) == 0)
- { ++n->Refs;
- free(node);
- return;
- }
- }
- while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL)
- { Delay(2);
- chkabort();
- }
- node->Refs = 1;
- AddTail(&LockList, &node->Node);
- }
- }
-
- void __regargs UnLockFile( char *file)
- { LNode *node;
- short len;
- char *ptr;
- for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr)
- ;
- ++ptr;
- len = strlen(ptr);
-
- for (node = (LNode *)LockList.lh_Head; node != (LNode *)&LockList.lh_Tail; node = (LNode *)node->Node.ln_Succ)
- { if (strnicmp(ptr, node->Node.ln_Name + 2, len) == 0 && strlen(node->Node.ln_Name) == len + 7)
- { if (--node->Refs == 0)
- FreeLockNode(node);
- return;
- }
- }
- }
-
- void UnLockFiles(void)
- { register LNode *node;
- while ((node = (LNode *)LockList.lh_Head) != (LNode *)&LockList.lh_Tail)
- FreeLockNode(node);
- }
-
- void __regargs FreeLockNode(LNode *node)
- { Remove((struct Node*)node);
- fclose(node->Fi);
- unlink(node->Node.ln_Name);
- free(node);
- }
-