117829Sralph #ifndef lint 2*34174Srick static char sccsid[] = "@(#)uuq.c 4.10 (Berkeley) 05/04/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(); 7033971Srick 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) 7833971Srick int argc; 7917829Sralph char **argv; 8017829Sralph { 8133971Srick register int i; 8217829Sralph register struct sys *sp; 8317829Sralph register struct job *jp; 8417829Sralph struct job **sortjob; 8517829Sralph int nsys; 8633971Srick extern char *optarg; 8733971Srick extern int optind; 8817829Sralph 8917829Sralph strcpy(Progname, "uuq"); 9017829Sralph uucpname(Myname); 9117829Sralph 92*34174Srick while ((i = getopt(argc, argv, "r:S:s:u:d:b:hl")) != EOF) 9333971Srick switch (i) { 9417829Sralph case 'r': 95*34174Srick case 'S': 9633971Srick Spool = optarg; 9717829Sralph break; 9817829Sralph case 's': 9933971Srick sysname = optarg; 10023689Sbloom if (strlen(sysname) > SYSNSIZE) 10123689Sbloom sysname[SYSNSIZE] = '\0'; 10217829Sralph break; 10317829Sralph case 'u': 10433971Srick user = optarg; 10517829Sralph break; 10617829Sralph case 'd': 10733971Srick rmjob = optarg; 10817829Sralph break; 10917829Sralph case 'b': 11033971Srick baudrate = atof(optarg); 11117829Sralph break; 11217829Sralph case 'h': 11317829Sralph hflag++; 11417829Sralph break; 11517829Sralph case 'l': 11617829Sralph lflag++; 11717829Sralph break; 11817829Sralph default: 11917829Sralph fprintf(stderr, 12017829Sralph "usage: uuq [-l] [-h] [-ssystem] [-uuser] [-djobno] [-rspool] [-bbaudrate]\n"); 12117829Sralph exit(0); 12217829Sralph } 12317829Sralph 12417829Sralph subchdir(Spool); 12523689Sbloom baudrate *= 0.7; /* reduce speed because of protocol overhead */ 12633576Srick baudrate *= 7.5; /* convert to chars/minute (60/8) */ 12717829Sralph gather(); 12817829Sralph nsys = 0; 12917829Sralph for (sp = syshead; sp; sp = sp->s_sysp) { 13017829Sralph if (sp->s_njobs == 0) 13117829Sralph continue; 13217829Sralph if (!hflag && nsys++ > 0) 13317829Sralph putchar('\n'); 13417829Sralph printf("%s: %d %s", sp->s_name, 13517829Sralph sp->s_njobs, sp->s_njobs > 1 ? "jobs" : "job"); 13617829Sralph if (lflag) { 13717829Sralph float minutes; 13817829Sralph int hours; 13917829Sralph /* The 80 * njobs is because of the uucp handshaking */ 14017829Sralph minutes = (float)(sp->s_bytes + 80 * sp->s_njobs)/baudrate; 14117829Sralph hours = minutes/60; 14233576Srick printf(", %ld bytes, ", sp->s_bytes); 14317829Sralph if (minutes > 60){ 14417829Sralph printf("%d hour%s, ",hours, 14517829Sralph hours > 1 ? "s": ""); 14617829Sralph minutes -= 60 * hours; 14717829Sralph } 14823729Sbloom printf("%3.1f minutes (@ effective baudrate of %d)", 14933971Srick minutes,(int)(baudrate/6)); 15017829Sralph } 15117829Sralph putchar('\n'); 15217829Sralph if (hflag) 15317829Sralph continue; 15417829Sralph /* sort them babies! */ 15533576Srick sortjob = (struct job **)calloc(sp->s_njobs, sizeof (struct job *)); 15617829Sralph for (i=0, jp=sp->s_jobp; i < sp->s_njobs; i++, jp=jp->j_jobp) 15717829Sralph sortjob[i] = jp; 15817829Sralph qsort(sortjob, sp->s_njobs, sizeof (struct job *), jcompare); 15917829Sralph for (i = 0; i < sp->s_njobs; i++) { 16017829Sralph jp = sortjob[i]; 16117829Sralph if (lflag) { 16233576Srick printf("%s %2d %-*s%7ld%5.1f %-12.12s %c %.*s\n", 16317829Sralph jp->j_jobno, jp->j_files, Maxulen, jp->j_user, jp->j_bytes, jp->j_bytes/baudrate, 16417829Sralph ctime(&jp->j_date) + 4, jp->j_flags, sizeof (jp->j_fname), jp->j_fname 16517829Sralph ); 16617829Sralph } else { 16717829Sralph printf("%s", jp->j_jobno); 16817829Sralph putchar((i+1)%10 ? '\t' : '\n'); 16917829Sralph } 17017829Sralph /* There's no need to keep the force poll if jobs > 1*/ 17117829Sralph if (sp->s_njobs > 1 && strcmp("POLL", jp->j_jobno)==0) { 17217829Sralph char pbuf[BUFSIZ]; 17333576Srick sprintf(pbuf,"%s/%c.%s%cPOLL", 17433576Srick subdir(Spool, CMDPRE), CMDPRE, 17533576Srick sp->s_name, jp->j_grade); 17633576Srick (void) unlink(pbuf); 17717829Sralph } 17817829Sralph } 17917829Sralph if (!lflag && (sp->s_njobs%10)) 18017829Sralph putchar('\n'); 18117829Sralph } 18217829Sralph exit(0); 18317829Sralph } 18417829Sralph 18517829Sralph jcompare(j1, j2) 18617829Sralph struct job **j1, **j2; 18717829Sralph { 18817829Sralph int delta; 18917829Sralph 19017829Sralph delta = (*j1)->j_grade - (*j2)->j_grade; 19117829Sralph if (delta) 19217829Sralph return delta; 19317829Sralph return(strcmp((*j1)->j_jobno,(*j2)->j_jobno)); 19417829Sralph } 19517829Sralph 19617829Sralph /* 19717829Sralph * Get all the command file names 19817829Sralph */ 19917829Sralph gather() 20017829Sralph { 20117829Sralph struct direct *d; 20217829Sralph DIR *df; 20317829Sralph 20417829Sralph /* 20517829Sralph * Find all the spool files in the spooling directory 20617829Sralph */ 20717829Sralph if ((df = opendir(subdir(Spool, CMDPRE))) == NULL) { 20833971Srick fprintf(stderr, "can't examine spooling area\n"); 20917829Sralph exit(1); 21017829Sralph } 21117829Sralph for (;;) { 21217829Sralph if ((d = readdir(df)) == NULL) 21317829Sralph break; 21417829Sralph if (d->d_namlen <= 2 || d->d_name[0] != CMDPRE || 21517829Sralph d->d_name[1] != '.') 21617829Sralph continue; 21717829Sralph if (analjob(d->d_name) < 0) { 21817829Sralph fprintf(stderr, "out of memory\n"); 21917829Sralph break; 22017829Sralph } 22117829Sralph } 22217829Sralph closedir(df); 22317829Sralph } 22417829Sralph 22517829Sralph /* 22617829Sralph * analjob does the grunge work of verifying jobs 22717829Sralph */ 22833971Srick #include <pwd.h> 22917829Sralph analjob(filename) 23017829Sralph char *filename; 23117829Sralph { 23217829Sralph struct job *jp; 23317829Sralph struct sys *sp; 23417829Sralph char sbuf[MAXNAMLEN+1], str[256], nbuf[256]; 23517829Sralph char *jptr, *wrkvec[20]; 23617829Sralph char grade; 23717829Sralph FILE *fp, *df; 23817829Sralph struct stat statb; 23917829Sralph int files, gotname, i; 24017829Sralph off_t bytes; 24117829Sralph 24217829Sralph strncpy(sbuf, filename, MAXNAMLEN); 24317829Sralph sbuf[MAXNAMLEN] = '\0'; 24417829Sralph jptr = sbuf + strlen(sbuf) - WSUFSIZE; 24517829Sralph grade = *jptr; 24617829Sralph *jptr++ = 0; 24717829Sralph /* 24817829Sralph * sbuf+2 now points to sysname name (null terminated) 24917829Sralph * jptr now points to job number (null terminated) 25017829Sralph */ 25117829Sralph if (rmjob) { 25217829Sralph if (strcmp(rmjob, jptr)) 25317829Sralph return(0); 25417829Sralph } else { 25517829Sralph if ((sp = getsys(sbuf+2)) == NOSYS) 25617829Sralph return(0); 25717829Sralph if (!lflag) { 25817829Sralph /* SHOULD USE A SMALLER STRUCTURE HERE */ 25917829Sralph jp = (struct job *)malloc(sizeof(struct job)); 26017829Sralph if (jp == (struct job *)0) 26117829Sralph return(-1); 26217829Sralph strcpy(jp->j_jobno, jptr); 26317829Sralph jp->j_jobp = sp->s_jobp; 26417829Sralph jp->j_grade = grade; 26517829Sralph sp->s_jobp = jp; 26617829Sralph sp->s_njobs++; 26717829Sralph return(1); 26817829Sralph } 26917829Sralph } 27017829Sralph if ((fp = fopen(subfile(filename), "r")) == NULL) { 27117829Sralph perror(subfile(filename)); 27217829Sralph return(0); 27317829Sralph } 27417829Sralph files = 0; 27517829Sralph bytes = 0; 27617829Sralph gotname = 0; 27717829Sralph while (fgets(str, sizeof str, fp)) { 27817829Sralph if (getargs(str, wrkvec, 20) <= 0) 27917829Sralph continue; 28017829Sralph if (rmjob) { 28133971Srick int myuid; 28233971Srick struct passwd *pw; 28333971Srick /* 28433971Srick * Make sure person who is removing data files is 28533971Srick * the person who created it or root. 28633971Srick */ 28733971Srick myuid = getuid(); 28833971Srick pw = getpwnam(W_USER); 28933971Srick if (myuid && (pw == NULL || myuid != pw->pw_uid)) { 29033971Srick fprintf(stderr, "Permission denied.\n"); 29133971Srick exit(1); 29233971Srick } 29317829Sralph if (W_TYPE[0] == 'S' && !index(W_OPTNS, 'c')) { 29417829Sralph unlink(subfile(W_DFILE)); 29517829Sralph fprintf(stderr, "Removing data file %s\n", W_DFILE); 29617829Sralph } 29717829Sralph continue; 29817829Sralph } 29917829Sralph if (user && (W_TYPE[0] == 'X' || !prefix(user, W_USER))) { 30017829Sralph fclose(fp); 30117829Sralph return(0); 30217829Sralph } 30317829Sralph files++; 30417829Sralph if (W_TYPE[0] == 'S') { 30517829Sralph if (strcmp(W_DFILE, "D.0") && 30617829Sralph stat(subfile(W_DFILE), &statb) >= 0) 30717829Sralph bytes += statb.st_size; 30817829Sralph else if (stat(subfile(W_FILE1), &statb) >= 0) 30917829Sralph bytes += statb.st_size; 31017829Sralph } 31117829Sralph /* amusing heuristic */ 31217829Sralph #define isXfile(s) (s[0]=='D' && s[strlen(s)-WSUFSIZE]=='X') 31317829Sralph if (gotname == 0 && isXfile(W_FILE1)) { 31417829Sralph if ((df = fopen(subfile(W_FILE1), "r")) == NULL) 31517829Sralph continue; 31617829Sralph while (fgets(nbuf, sizeof nbuf, df)) { 31717829Sralph nbuf[strlen(nbuf) - 1] = '\0'; 31817829Sralph if (nbuf[0] == 'C' && nbuf[1] == ' ') { 31917829Sralph strcpy(Filename, nbuf+2); 32017829Sralph gotname++; 32117829Sralph } else if (nbuf[0] == 'R' && nbuf[1] == ' ') { 32217829Sralph register char *p, *q, *r; 32317829Sralph r = q = p = nbuf+2; 32417829Sralph do { 32517829Sralph if (*p == '!' || *p == '@'){ 32617829Sralph r = q; 32717829Sralph q = p+1; 32817829Sralph } 32917829Sralph } while (*p++); 33017829Sralph 33117829Sralph strcpy(Username, r); 33217829Sralph W_USER = Username; 33317829Sralph } 33417829Sralph } 33517829Sralph fclose(df); 33617829Sralph } 33717829Sralph } 33817829Sralph fclose(fp); 33917829Sralph if (rmjob) { 34017829Sralph unlink(subfile(filename)); 34117829Sralph fprintf(stderr, "Removing command file %s\n", filename); 34217829Sralph exit(0); 34317829Sralph } 34417829Sralph if (files == 0) { 34517829Sralph static char *wtype = "X"; 34617829Sralph static char *wfile = "forced poll"; 34717829Sralph if (strcmp("POLL", &filename[strlen(filename)-4])) { 34817829Sralph fprintf(stderr, "%.14s: empty command file\n", filename); 34917829Sralph return(0); 35017829Sralph } 35117829Sralph W_TYPE = wtype; 35217829Sralph W_FILE1 = wfile; 35317829Sralph } 35417829Sralph jp = (struct job *)malloc(sizeof(struct job)); 35517829Sralph if (jp == (struct job *)0) 35617829Sralph return(-1); 35717829Sralph strcpy(jp->j_jobno, jptr); 35817829Sralph jp->j_files = files; 35917829Sralph jp->j_bytes = bytes; 36017829Sralph jp->j_grade = grade; 36117829Sralph jp->j_flags = W_TYPE[0]; 36217829Sralph strncpy(jp->j_user, W_TYPE[0]=='X' ? "---" : W_USER, 20 ); 36317829Sralph jp->j_user[20] = '\0'; 36417829Sralph i = strlen(jp->j_user); 36517829Sralph if (i > Maxulen) 36617829Sralph Maxulen = i; 36717829Sralph /* SHOULD ADD ALL INFORMATION IN THE WHILE LOOP */ 36817829Sralph if (gotname) 36917829Sralph strncpy(jp->j_fname, Filename, sizeof jp->j_fname); 37017829Sralph else 37117829Sralph strncpy(jp->j_fname, W_FILE1, sizeof jp->j_fname); 37217829Sralph stat(subfile(filename), &statb); 37317829Sralph jp->j_date = statb.st_mtime; 37417829Sralph jp->j_jobp = sp->s_jobp; 37517829Sralph sp->s_jobp = jp; 37617829Sralph sp->s_njobs++; 37717829Sralph sp->s_bytes += jp->j_bytes; 37817829Sralph return(1); 37917829Sralph } 38017829Sralph 38117829Sralph struct sys * 38217829Sralph getsys(s) 38317829Sralph register char *s; 38417829Sralph { 38517829Sralph register struct sys *sp; 38617829Sralph 38717829Sralph for (sp = syshead; sp; sp = sp->s_sysp) 38817829Sralph if (strcmp(s, sp->s_name) == 0) 38917829Sralph return(sp); 39017829Sralph if (sysname && !prefix(sysname, s)) 39117829Sralph return(NOSYS); 39217829Sralph sp = (struct sys *)malloc(sizeof(struct sys)); 39317829Sralph if (sp == NOSYS) 39417829Sralph return(NOSYS); 39517829Sralph strcpy(sp->s_name, s); 39617829Sralph sp->s_njobs = 0; 39717829Sralph sp->s_jobp = (struct job *)0; 39817829Sralph sp->s_sysp = syshead; 39917829Sralph sp->s_bytes = 0; 40017829Sralph syshead = sp; 40117829Sralph return(sp); 40217829Sralph } 403