117829Sralph #ifndef lint 2*33971Srick static char sccsid[] = "@(#)uuq.c 4.9 (Berkeley) 04/05/88"; 317829Sralph #endif 417829Sralph 517829Sralph /* 633576Srick * This file contains no ATT code and is not subject to the ATT 733576Srick * license provisions regarding redistribution. 833576Srick * Rick Adams 2/23/88 933576Srick */ 1033576Srick 1133576Srick /* 1217829Sralph * uuq - looks at uucp queues 1317829Sralph * 1417829Sralph * Lou Salkind 1517829Sralph * New York University 1617829Sralph * 1717829Sralph */ 1817829Sralph 1917829Sralph #include "uucp.h" 2017829Sralph #include <stdio.h> 2117829Sralph 2217829Sralph #ifdef NDIR 2317829Sralph #include "libndir/ndir.h" 2417829Sralph #else !NDIR 2517829Sralph #include <sys/dir.h> 2617829Sralph #endif !NDIR 2717829Sralph #include <sys/stat.h> 2817829Sralph 2917829Sralph #define NOSYS (struct sys *)0 3017829Sralph 3117829Sralph #define W_TYPE wrkvec[0] 3217829Sralph #define W_FILE1 wrkvec[1] 3317829Sralph #define W_FILE2 wrkvec[2] 3417829Sralph #define W_USER wrkvec[3] 3517829Sralph #define W_OPTNS wrkvec[4] 3617829Sralph #define W_DFILE wrkvec[5] 3717829Sralph #define W_MODE wrkvec[6] 3817829Sralph #define WSUFSIZE 5 /* work file name suffix size */ 3917829Sralph 4017829Sralph struct sys { 4117829Sralph char s_name[8]; 4217829Sralph int s_njobs; 4317829Sralph off_t s_bytes; 4417829Sralph struct job *s_jobp; 4517829Sralph struct sys *s_sysp; 4617829Sralph }; 4717829Sralph 4817829Sralph struct job { 4917829Sralph int j_files; 5017829Sralph int j_flags; 5117829Sralph char j_jobno[WSUFSIZE]; 5217829Sralph char j_user[22]; 5317829Sralph char j_fname[128]; 5417829Sralph char j_grade; 5517829Sralph off_t j_bytes; 5617829Sralph time_t j_date; 5717829Sralph struct job *j_jobp; 5817829Sralph }; 5917829Sralph 6017829Sralph struct sys *syshead; 6117829Sralph struct sys *getsys(); 6217829Sralph int jcompare(); 6317829Sralph char *sysname; 6417829Sralph char *user; 6517829Sralph char *rmjob; 6617829Sralph int hflag; 6717829Sralph int lflag; 6817829Sralph 6917829Sralph char *malloc(), *calloc(); 70*33971Srick double atof(); 7133576Srick float baudrate = 2400.; 7217829Sralph char Username[BUFSIZ]; 7317829Sralph char Filename[BUFSIZ]; 7417829Sralph int Maxulen = 0; 7525144Sbloom struct timeb Now; 7617829Sralph 7717829Sralph main(argc, argv) 78*33971Srick int argc; 7917829Sralph char **argv; 8017829Sralph { 81*33971Srick register int i; 8217829Sralph register struct sys *sp; 8317829Sralph register struct job *jp; 8417829Sralph struct job **sortjob; 8517829Sralph int nsys; 86*33971Srick extern char *optarg; 87*33971Srick extern int optind; 8817829Sralph 8917829Sralph strcpy(Progname, "uuq"); 9017829Sralph uucpname(Myname); 9117829Sralph 92*33971Srick while ((i = getopt(argc, argv, "r:s:u:d:b:hl")) != EOF) 93*33971Srick switch (i) { 9417829Sralph case 'r': 95*33971Srick Spool = optarg; 9617829Sralph break; 9717829Sralph case 's': 98*33971Srick sysname = optarg; 9923689Sbloom if (strlen(sysname) > SYSNSIZE) 10023689Sbloom sysname[SYSNSIZE] = '\0'; 10117829Sralph break; 10217829Sralph case 'u': 103*33971Srick user = optarg; 10417829Sralph break; 10517829Sralph case 'd': 106*33971Srick rmjob = optarg; 10717829Sralph break; 10817829Sralph case 'b': 109*33971Srick baudrate = atof(optarg); 11017829Sralph break; 11117829Sralph case 'h': 11217829Sralph hflag++; 11317829Sralph break; 11417829Sralph case 'l': 11517829Sralph lflag++; 11617829Sralph break; 11717829Sralph default: 11817829Sralph fprintf(stderr, 11917829Sralph "usage: uuq [-l] [-h] [-ssystem] [-uuser] [-djobno] [-rspool] [-bbaudrate]\n"); 12017829Sralph exit(0); 12117829Sralph } 12217829Sralph 12317829Sralph subchdir(Spool); 12423689Sbloom baudrate *= 0.7; /* reduce speed because of protocol overhead */ 12533576Srick baudrate *= 7.5; /* convert to chars/minute (60/8) */ 12617829Sralph gather(); 12717829Sralph nsys = 0; 12817829Sralph for (sp = syshead; sp; sp = sp->s_sysp) { 12917829Sralph if (sp->s_njobs == 0) 13017829Sralph continue; 13117829Sralph if (!hflag && nsys++ > 0) 13217829Sralph putchar('\n'); 13317829Sralph printf("%s: %d %s", sp->s_name, 13417829Sralph sp->s_njobs, sp->s_njobs > 1 ? "jobs" : "job"); 13517829Sralph if (lflag) { 13617829Sralph float minutes; 13717829Sralph int hours; 13817829Sralph /* The 80 * njobs is because of the uucp handshaking */ 13917829Sralph minutes = (float)(sp->s_bytes + 80 * sp->s_njobs)/baudrate; 14017829Sralph hours = minutes/60; 14133576Srick printf(", %ld bytes, ", sp->s_bytes); 14217829Sralph if (minutes > 60){ 14317829Sralph printf("%d hour%s, ",hours, 14417829Sralph hours > 1 ? "s": ""); 14517829Sralph minutes -= 60 * hours; 14617829Sralph } 14723729Sbloom printf("%3.1f minutes (@ effective baudrate of %d)", 148*33971Srick minutes,(int)(baudrate/6)); 14917829Sralph } 15017829Sralph putchar('\n'); 15117829Sralph if (hflag) 15217829Sralph continue; 15317829Sralph /* sort them babies! */ 15433576Srick sortjob = (struct job **)calloc(sp->s_njobs, sizeof (struct job *)); 15517829Sralph for (i=0, jp=sp->s_jobp; i < sp->s_njobs; i++, jp=jp->j_jobp) 15617829Sralph sortjob[i] = jp; 15717829Sralph qsort(sortjob, sp->s_njobs, sizeof (struct job *), jcompare); 15817829Sralph for (i = 0; i < sp->s_njobs; i++) { 15917829Sralph jp = sortjob[i]; 16017829Sralph if (lflag) { 16133576Srick printf("%s %2d %-*s%7ld%5.1f %-12.12s %c %.*s\n", 16217829Sralph jp->j_jobno, jp->j_files, Maxulen, jp->j_user, jp->j_bytes, jp->j_bytes/baudrate, 16317829Sralph ctime(&jp->j_date) + 4, jp->j_flags, sizeof (jp->j_fname), jp->j_fname 16417829Sralph ); 16517829Sralph } else { 16617829Sralph printf("%s", jp->j_jobno); 16717829Sralph putchar((i+1)%10 ? '\t' : '\n'); 16817829Sralph } 16917829Sralph /* There's no need to keep the force poll if jobs > 1*/ 17017829Sralph if (sp->s_njobs > 1 && strcmp("POLL", jp->j_jobno)==0) { 17117829Sralph char pbuf[BUFSIZ]; 17233576Srick sprintf(pbuf,"%s/%c.%s%cPOLL", 17333576Srick subdir(Spool, CMDPRE), CMDPRE, 17433576Srick sp->s_name, jp->j_grade); 17533576Srick (void) unlink(pbuf); 17617829Sralph } 17717829Sralph } 17817829Sralph if (!lflag && (sp->s_njobs%10)) 17917829Sralph putchar('\n'); 18017829Sralph } 18117829Sralph exit(0); 18217829Sralph } 18317829Sralph 18417829Sralph jcompare(j1, j2) 18517829Sralph struct job **j1, **j2; 18617829Sralph { 18717829Sralph int delta; 18817829Sralph 18917829Sralph delta = (*j1)->j_grade - (*j2)->j_grade; 19017829Sralph if (delta) 19117829Sralph return delta; 19217829Sralph return(strcmp((*j1)->j_jobno,(*j2)->j_jobno)); 19317829Sralph } 19417829Sralph 19517829Sralph /* 19617829Sralph * Get all the command file names 19717829Sralph */ 19817829Sralph gather() 19917829Sralph { 20017829Sralph struct direct *d; 20117829Sralph DIR *df; 20217829Sralph 20317829Sralph /* 20417829Sralph * Find all the spool files in the spooling directory 20517829Sralph */ 20617829Sralph if ((df = opendir(subdir(Spool, CMDPRE))) == NULL) { 207*33971Srick fprintf(stderr, "can't examine spooling area\n"); 20817829Sralph exit(1); 20917829Sralph } 21017829Sralph for (;;) { 21117829Sralph if ((d = readdir(df)) == NULL) 21217829Sralph break; 21317829Sralph if (d->d_namlen <= 2 || d->d_name[0] != CMDPRE || 21417829Sralph d->d_name[1] != '.') 21517829Sralph continue; 21617829Sralph if (analjob(d->d_name) < 0) { 21717829Sralph fprintf(stderr, "out of memory\n"); 21817829Sralph break; 21917829Sralph } 22017829Sralph } 22117829Sralph closedir(df); 22217829Sralph } 22317829Sralph 22417829Sralph /* 22517829Sralph * analjob does the grunge work of verifying jobs 22617829Sralph */ 227*33971Srick #include <pwd.h> 22817829Sralph analjob(filename) 22917829Sralph char *filename; 23017829Sralph { 23117829Sralph struct job *jp; 23217829Sralph struct sys *sp; 23317829Sralph char sbuf[MAXNAMLEN+1], str[256], nbuf[256]; 23417829Sralph char *jptr, *wrkvec[20]; 23517829Sralph char grade; 23617829Sralph FILE *fp, *df; 23717829Sralph struct stat statb; 23817829Sralph int files, gotname, i; 23917829Sralph off_t bytes; 24017829Sralph 24117829Sralph strncpy(sbuf, filename, MAXNAMLEN); 24217829Sralph sbuf[MAXNAMLEN] = '\0'; 24317829Sralph jptr = sbuf + strlen(sbuf) - WSUFSIZE; 24417829Sralph grade = *jptr; 24517829Sralph *jptr++ = 0; 24617829Sralph /* 24717829Sralph * sbuf+2 now points to sysname name (null terminated) 24817829Sralph * jptr now points to job number (null terminated) 24917829Sralph */ 25017829Sralph if (rmjob) { 25117829Sralph if (strcmp(rmjob, jptr)) 25217829Sralph return(0); 25317829Sralph } else { 25417829Sralph if ((sp = getsys(sbuf+2)) == NOSYS) 25517829Sralph return(0); 25617829Sralph if (!lflag) { 25717829Sralph /* SHOULD USE A SMALLER STRUCTURE HERE */ 25817829Sralph jp = (struct job *)malloc(sizeof(struct job)); 25917829Sralph if (jp == (struct job *)0) 26017829Sralph return(-1); 26117829Sralph strcpy(jp->j_jobno, jptr); 26217829Sralph jp->j_jobp = sp->s_jobp; 26317829Sralph jp->j_grade = grade; 26417829Sralph sp->s_jobp = jp; 26517829Sralph sp->s_njobs++; 26617829Sralph return(1); 26717829Sralph } 26817829Sralph } 26917829Sralph if ((fp = fopen(subfile(filename), "r")) == NULL) { 27017829Sralph perror(subfile(filename)); 27117829Sralph return(0); 27217829Sralph } 27317829Sralph files = 0; 27417829Sralph bytes = 0; 27517829Sralph gotname = 0; 27617829Sralph while (fgets(str, sizeof str, fp)) { 27717829Sralph if (getargs(str, wrkvec, 20) <= 0) 27817829Sralph continue; 27917829Sralph if (rmjob) { 280*33971Srick int myuid; 281*33971Srick struct passwd *pw; 282*33971Srick /* 283*33971Srick * Make sure person who is removing data files is 284*33971Srick * the person who created it or root. 285*33971Srick */ 286*33971Srick myuid = getuid(); 287*33971Srick pw = getpwnam(W_USER); 288*33971Srick if (myuid && (pw == NULL || myuid != pw->pw_uid)) { 289*33971Srick fprintf(stderr, "Permission denied.\n"); 290*33971Srick exit(1); 291*33971Srick } 29217829Sralph if (W_TYPE[0] == 'S' && !index(W_OPTNS, 'c')) { 29317829Sralph unlink(subfile(W_DFILE)); 29417829Sralph fprintf(stderr, "Removing data file %s\n", W_DFILE); 29517829Sralph } 29617829Sralph continue; 29717829Sralph } 29817829Sralph if (user && (W_TYPE[0] == 'X' || !prefix(user, W_USER))) { 29917829Sralph fclose(fp); 30017829Sralph return(0); 30117829Sralph } 30217829Sralph files++; 30317829Sralph if (W_TYPE[0] == 'S') { 30417829Sralph if (strcmp(W_DFILE, "D.0") && 30517829Sralph stat(subfile(W_DFILE), &statb) >= 0) 30617829Sralph bytes += statb.st_size; 30717829Sralph else if (stat(subfile(W_FILE1), &statb) >= 0) 30817829Sralph bytes += statb.st_size; 30917829Sralph } 31017829Sralph /* amusing heuristic */ 31117829Sralph #define isXfile(s) (s[0]=='D' && s[strlen(s)-WSUFSIZE]=='X') 31217829Sralph if (gotname == 0 && isXfile(W_FILE1)) { 31317829Sralph if ((df = fopen(subfile(W_FILE1), "r")) == NULL) 31417829Sralph continue; 31517829Sralph while (fgets(nbuf, sizeof nbuf, df)) { 31617829Sralph nbuf[strlen(nbuf) - 1] = '\0'; 31717829Sralph if (nbuf[0] == 'C' && nbuf[1] == ' ') { 31817829Sralph strcpy(Filename, nbuf+2); 31917829Sralph gotname++; 32017829Sralph } else if (nbuf[0] == 'R' && nbuf[1] == ' ') { 32117829Sralph register char *p, *q, *r; 32217829Sralph r = q = p = nbuf+2; 32317829Sralph do { 32417829Sralph if (*p == '!' || *p == '@'){ 32517829Sralph r = q; 32617829Sralph q = p+1; 32717829Sralph } 32817829Sralph } while (*p++); 32917829Sralph 33017829Sralph strcpy(Username, r); 33117829Sralph W_USER = Username; 33217829Sralph } 33317829Sralph } 33417829Sralph fclose(df); 33517829Sralph } 33617829Sralph } 33717829Sralph fclose(fp); 33817829Sralph if (rmjob) { 33917829Sralph unlink(subfile(filename)); 34017829Sralph fprintf(stderr, "Removing command file %s\n", filename); 34117829Sralph exit(0); 34217829Sralph } 34317829Sralph if (files == 0) { 34417829Sralph static char *wtype = "X"; 34517829Sralph static char *wfile = "forced poll"; 34617829Sralph if (strcmp("POLL", &filename[strlen(filename)-4])) { 34717829Sralph fprintf(stderr, "%.14s: empty command file\n", filename); 34817829Sralph return(0); 34917829Sralph } 35017829Sralph W_TYPE = wtype; 35117829Sralph W_FILE1 = wfile; 35217829Sralph } 35317829Sralph jp = (struct job *)malloc(sizeof(struct job)); 35417829Sralph if (jp == (struct job *)0) 35517829Sralph return(-1); 35617829Sralph strcpy(jp->j_jobno, jptr); 35717829Sralph jp->j_files = files; 35817829Sralph jp->j_bytes = bytes; 35917829Sralph jp->j_grade = grade; 36017829Sralph jp->j_flags = W_TYPE[0]; 36117829Sralph strncpy(jp->j_user, W_TYPE[0]=='X' ? "---" : W_USER, 20 ); 36217829Sralph jp->j_user[20] = '\0'; 36317829Sralph i = strlen(jp->j_user); 36417829Sralph if (i > Maxulen) 36517829Sralph Maxulen = i; 36617829Sralph /* SHOULD ADD ALL INFORMATION IN THE WHILE LOOP */ 36717829Sralph if (gotname) 36817829Sralph strncpy(jp->j_fname, Filename, sizeof jp->j_fname); 36917829Sralph else 37017829Sralph strncpy(jp->j_fname, W_FILE1, sizeof jp->j_fname); 37117829Sralph stat(subfile(filename), &statb); 37217829Sralph jp->j_date = statb.st_mtime; 37317829Sralph jp->j_jobp = sp->s_jobp; 37417829Sralph sp->s_jobp = jp; 37517829Sralph sp->s_njobs++; 37617829Sralph sp->s_bytes += jp->j_bytes; 37717829Sralph return(1); 37817829Sralph } 37917829Sralph 38017829Sralph struct sys * 38117829Sralph getsys(s) 38217829Sralph register char *s; 38317829Sralph { 38417829Sralph register struct sys *sp; 38517829Sralph 38617829Sralph for (sp = syshead; sp; sp = sp->s_sysp) 38717829Sralph if (strcmp(s, sp->s_name) == 0) 38817829Sralph return(sp); 38917829Sralph if (sysname && !prefix(sysname, s)) 39017829Sralph return(NOSYS); 39117829Sralph sp = (struct sys *)malloc(sizeof(struct sys)); 39217829Sralph if (sp == NOSYS) 39317829Sralph return(NOSYS); 39417829Sralph strcpy(sp->s_name, s); 39517829Sralph sp->s_njobs = 0; 39617829Sralph sp->s_jobp = (struct job *)0; 39717829Sralph sp->s_sysp = syshead; 39817829Sralph sp->s_bytes = 0; 39917829Sralph syshead = sp; 40017829Sralph return(sp); 40117829Sralph } 402