xref: /csrg-svn/usr.bin/at/atq/atq.c (revision 30804)
122419Sdist /*
222419Sdist  * Copyright (c) 1983 Regents of the University of California.
322419Sdist  * All rights reserved.  The Berkeley software License Agreement
422419Sdist  * specifies the terms and conditions for redistribution.
522419Sdist  */
622419Sdist 
716818Swall #ifndef lint
822419Sdist char copyright[] =
922419Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1022419Sdist  All rights reserved.\n";
1116828Smckusick #endif not lint
1216818Swall 
1322419Sdist #ifndef lint
14*30804Sbostic static char sccsid[] = "@(#)atq.c	5.4 (Berkeley) 04/06/87";
1522419Sdist #endif not lint
1622419Sdist 
1716818Swall /*
1816818Swall  *
1916828Smckusick  *	Synopsis:  atq [ -c ] [ -n ] [ name ... ]
2016818Swall  *
2116818Swall  *
2216828Smckusick  *	Print the queue of files waiting to be executed. These files
2316828Smckusick  *	were created by using the "at" command and are located in the
2416828Smckusick  *	directory "/usr/spool/at".
2516818Swall  *
2616818Swall  *
2716828Smckusick  *	Author: Steve Wall
2816828Smckusick  *		Computer Systems Research Group
2916828Smckusick  *		University of California @ Berkeley
3016818Swall  *
3116818Swall  */
3216818Swall 
3316818Swall # include <stdio.h>
3416818Swall # include <sys/types.h>
3516818Swall # include <sys/file.h>
3616818Swall # include <sys/dir.h>
3716818Swall # include <sys/stat.h>
3816818Swall # include <sys/time.h>
3916818Swall # include <pwd.h>
4016818Swall # include <ctype.h>
4116818Swall 
4216828Smckusick # define ATDIR		"/usr/spool/at"			/* spooling area */
4316828Smckusick # define LASTFILE	"/usr/spool/at/lasttimedone"	/* update time record
4416828Smckusick 							   file */
4516818Swall 
4616818Swall /*
4716818Swall  * Months of the year
4816818Swall  */
4916818Swall static char *mthnames[12] = {
5016828Smckusick 	"Jan","Feb","Mar","Apr","May","Jun","Jul",
5116828Smckusick 	"Aug","Sep","Oct","Nov","Dec",
5216818Swall };
5316818Swall 
5428872Smckusick char *nullentry = NULL;			/* avoid 'namelist' NULL ptr problems */
5516828Smckusick int numentries;				/* number of entries in spooling area */
5616828Smckusick int namewanted = 0;			/* only print jobs belonging to a
5716828Smckusick 					   certain person */
5816828Smckusick struct direct **queue;			/* the queue itself */
5916818Swall 
6016818Swall 
6116818Swall main(argc,argv)
6216818Swall int argc;
6316818Swall char **argv;
6416818Swall {
6516818Swall 
6616828Smckusick 	int cflag = 0;			/* print in order of creation time */
6716828Smckusick 	int nflag = 0;			/* just print the number of jobs in
6816828Smckusick 					   queue */
6916828Smckusick 	int usage();			/* print usage info and exit */
7016828Smckusick 	int creation();			/* sort jobs by date of creation */
7116828Smckusick 	int alphasort();		/* sort jobs by date of execution */
7216828Smckusick 	int filewanted();		/* should a file be included in queue?*/
7316828Smckusick 	int printqueue();		/* print the queue */
7416828Smckusick 	int countfiles();		/* count the number of files in queue
7516828Smckusick 					   for a given person */
7628872Smckusick 	char **namelist = &nullentry;	/* array of specific name(s) requested*/
7716818Swall 
7816818Swall 
7916828Smckusick 	--argc, ++argv;
8016818Swall 
8116828Smckusick 	/*
8216828Smckusick 	 * Interpret command line flags if they exist.
8316828Smckusick 	 */
8416828Smckusick 	while (argc > 0 && **argv == '-') {
8516828Smckusick 		(*argv)++;
8616828Smckusick 		while (**argv) switch (*(*argv)++) {
8716818Swall 
8816828Smckusick 			case 'c' :	cflag++;
8916828Smckusick 					break;
9016818Swall 
9116828Smckusick 			case 'n' :	nflag++;
9216828Smckusick 					break;
9316818Swall 
9416828Smckusick 			default	 :	usage();
9516818Swall 
9616828Smckusick 		}
9716828Smckusick 		--argc, ++argv;
9816828Smckusick 	}
9916818Swall 
10016828Smckusick 	/*
10116828Smckusick 	 * If a certain name (or names) is requested, set a pointer to the
10216828Smckusick 	 * beginning of the list.
10316828Smckusick 	 */
10428872Smckusick 	if (argc > 0) {
10516828Smckusick 		++namewanted;
10616828Smckusick 		namelist = argv;
10716828Smckusick 	}
10816818Swall 
10916828Smckusick 	/*
11016828Smckusick 	 * Move to the spooling area and scan the directory, placing the
11116828Smckusick 	 * files in the queue structure. The queue comes back sorted by
11216828Smckusick 	 * execution time or creation time.
11316828Smckusick 	 */
11416828Smckusick 	if (chdir(ATDIR) == -1) {
11516828Smckusick 		perror(ATDIR);
11616828Smckusick 		exit(1);
11716828Smckusick 	}
11816828Smckusick 	if ((numentries = scandir(".",&queue,filewanted, (cflag) ? creation :
11916828Smckusick 				alphasort)) < 0) {
12016828Smckusick 		perror(ATDIR);
12116828Smckusick 		exit(1);
12216828Smckusick 	}
12316818Swall 
12416828Smckusick 	/*
12516828Smckusick 	 * Either print a message stating:
12616828Smckusick 	 *
12716828Smckusick 	 *	1) that the spooling area is empty.
12816828Smckusick 	 *	2) the number of jobs in the spooling area.
12916828Smckusick 	 *	3) the number of jobs in the spooling area belonging to
13016828Smckusick 	 *	   a certain person.
13116828Smckusick 	 *	4) that the person requested doesn't have any files in the
13216828Smckusick 	 *	   spooling area.
13316828Smckusick 	 *
13416828Smckusick 	 * or send the queue off to "printqueue" for printing.
13516828Smckusick 	 *
13616828Smckusick 	 * This whole process might seem a bit elaborate, but it's worthwhile
13716828Smckusick 	 * to print some informative messages for the user.
13816828Smckusick 	 *
13916828Smckusick 	 */
14016828Smckusick 	if ((numentries == 0) && (!nflag)) {
14116828Smckusick 		printf("no files in queue.\n");
14216828Smckusick 		exit(0);
14316828Smckusick 	}
14416828Smckusick 	if (nflag) {
14516828Smckusick 		printf("%d\n",(namewanted) ? countfiles(namelist) : numentries);
14616828Smckusick 		exit(0);
14716828Smckusick 	}
14816828Smckusick 	if ((namewanted) && (countfiles(namelist) == 0)) {
14916828Smckusick 		printf("no files for %s.\n", (argc == 1) ?
15016828Smckusick 					*argv : "specified users");
15116828Smckusick 		exit(0);
15216828Smckusick 	}
15316828Smckusick 	printqueue(namelist);
15416828Smckusick 	exit(0);
15516818Swall }
15616818Swall 
15716818Swall /*
15816818Swall  * Count the number of jobs in the spooling area owned by a certain person(s).
15916818Swall  */
16016818Swall countfiles(namelist)
16116818Swall char **namelist;
16216818Swall {
16316828Smckusick 	int i;					/* for loop index */
16416828Smckusick 	int entryfound;				/* found file owned by user(s)*/
16516828Smckusick 	int numfiles = 0;			/* number of files owned by a
16616828Smckusick 						   certain person(s) */
16716828Smckusick 	char **ptr;				/* scratch pointer */
16816818Swall 
16916818Swall 
17016828Smckusick 	/*
17116828Smckusick 	 * For each file in the queue, see if the user(s) own the file. We
17216828Smckusick 	 * have to use "entryfound" (rather than simply incrementing "numfiles")
17316828Smckusick 	 * so that if a person's name appears twice on the command line we
17416828Smckusick 	 * don't double the number of files owned by him/her.
17516828Smckusick 	 */
17616828Smckusick 	for (i = 0; i < numentries ; i++) {
17716828Smckusick 		ptr = namelist;
17816828Smckusick 		entryfound = 0;
17916818Swall 
18016828Smckusick 		while (*ptr) {
18116865Swall 			if (isowner(*ptr,queue[i]->d_name))
18216828Smckusick 				++entryfound;
18316828Smckusick 			++ptr;
18416828Smckusick 		}
18516828Smckusick 		if (entryfound)
18616828Smckusick 			++numfiles;
18716828Smckusick 	}
18816828Smckusick 	return(numfiles);
18916818Swall }
19016818Swall 
19116818Swall /*
19216818Swall  * Print the queue. If only jobs belonging to a certain person(s) are requested,
19316818Swall  * only print jobs that belong to that person(s).
19416818Swall  */
19516818Swall printqueue(namelist)
19616818Swall char **namelist;
19716818Swall {
19816828Smckusick 	int i;					/* for loop index */
19916828Smckusick 	int rank = 1;				/* rank of a job */
20016828Smckusick 	int entryfound;				/* found file owned by user(s)*/
20116828Smckusick 	int printrank();			/* print the rank of a job */
20216828Smckusick 	int plastrun();				/* print the last time the
20316828Smckusick 						   spooling area was updated */
20416865Swall 	int powner();				/* print the name of the owner
20516865Swall 						   of the job */
20616828Smckusick 	char **ptr;				/* scratch pointer */
20716828Smckusick 	struct stat stbuf;			/* buffer for file stats */
20816818Swall 
20916818Swall 
21016828Smckusick 	/*
21116828Smckusick 	 * Print the time the spooling area was last modified and the header
21216828Smckusick 	 * for the queue.
21316828Smckusick 	 */
21416828Smckusick 	plastrun();
21516828Smckusick 	printf(" Rank	  Execution Date     Owner     Job #   Job Name\n");
21616818Swall 
21716828Smckusick 	/*
21816828Smckusick 	 * Print the queue. If a certain name(s) was requested, print only jobs
21916828Smckusick 	 * belonging to that person(s), otherwise print the entire queue.
22016828Smckusick 	 * Once again, we have to use "entryfound" (rather than simply
22116828Smckusick 	 * comparing each command line argument) so that if a person's name
22216828Smckusick 	 * appears twice we don't print each file owned by him/her twice.
22316828Smckusick 	 *
22416828Smckusick 	 *
22516828Smckusick 	 * "printrank", "printdate", and "printjobname" all take existing
22616828Smckusick 	 * data and display it in a friendly manner.
22716828Smckusick 	 *
22816828Smckusick 	 */
22916828Smckusick 	for (i = 0; i < numentries; i++) {
23016828Smckusick 		if ((stat(queue[i]->d_name, &stbuf)) < 0) {
23116828Smckusick 			continue;
23216828Smckusick 		}
23316828Smckusick 		if (namewanted) {
23416828Smckusick 			ptr = namelist;
23516828Smckusick 			entryfound = 0;
23616818Swall 
23716828Smckusick 			while (*ptr) {
23816865Swall 				if (isowner(*ptr,queue[i]->d_name))
23916828Smckusick 					++entryfound;
24016828Smckusick 				++ptr;
24116828Smckusick 			}
24216828Smckusick 			if (!entryfound)
24316828Smckusick 				continue;
24416828Smckusick 		}
24516828Smckusick 		printrank(rank++);
24616828Smckusick 		printdate(queue[i]->d_name);
24716865Swall 		powner(queue[i]->d_name);
24816828Smckusick 		printf("%5d",stbuf.st_ino);
24916828Smckusick 		printjobname(queue[i]->d_name);
25016828Smckusick 	}
25116828Smckusick 	++ptr;
25216818Swall }
25316818Swall 
25416818Swall /*
25516865Swall  * See if "name" owns "job".
25616818Swall  */
25716865Swall isowner(name,job)
25816865Swall char *name;
25916865Swall char *job;
26016818Swall {
26128872Smckusick 	char buf[128];			/* buffer for 1st line of spoolfile
26216865Swall 					   header */
26316865Swall 	FILE *infile;			/* I/O stream to spoolfile */
26416818Swall 
26516865Swall 	if ((infile = fopen(job,"r")) == NULL) {
26628872Smckusick 		fprintf(stderr,"Couldn't open spoolfile ");
26716865Swall 		perror(job);
26816865Swall 		return(0);
26916828Smckusick 	}
27016865Swall 
27128872Smckusick 	if (fscanf(infile,"# owner: %127s%*[^\n]\n",buf) != 1) {
27216865Swall 		fclose(infile);
27316865Swall 		return(0);
27416865Swall 	}
27516865Swall 
27616865Swall 	fclose(infile);
27716865Swall 	return((strcmp(name,buf) == 0) ? 1 : 0);
27816818Swall }
27916865Swall 
28016865Swall /*
28116865Swall  * Print the owner of the job. This is stored on the first line of the
28216865Swall  * spoolfile. If we run into trouble getting the name, we'll just print "???".
28316865Swall  */
28416865Swall powner(file)
28516865Swall char *file;
28616865Swall {
28728872Smckusick 	char owner[10];				/* the owner */
28816865Swall 	FILE *infile;				/* I/O stream to spoolfile */
28916865Swall 
29016865Swall 	/*
29116865Swall 	 * Open the job file and grab the first line.
29216865Swall 	 */
29316865Swall 
29416865Swall 	if ((infile = fopen(file,"r")) == NULL) {
29516865Swall 		printf("%-10.9s","???");
29628872Smckusick 		perror(file);
29716865Swall 		return;
29816865Swall 	}
29916865Swall 
30028872Smckusick 	if (fscanf(infile,"# owner: %9s%*[^\n]\n",owner) != 1) {
30116865Swall 		printf("%-10.9s","???");
30216865Swall 		fclose(infile);
30316865Swall 		return;
30416865Swall 	}
30516865Swall 
30616865Swall 	fclose(infile);
30716865Swall 	printf("%-10.9s",owner);
30816865Swall 
30916865Swall }
31016828Smckusick 
31116818Swall /*
31216818Swall  * Print the time the spooling area was updated.
31316818Swall  */
31416818Swall plastrun()
31516818Swall {
31616828Smckusick 	struct timeval now;			/* time it is right now */
31716828Smckusick 	struct timezone zone;			/* NOT USED */
31816828Smckusick 	struct tm *loc;				/* detail of time it is right */
31916828Smckusick 	u_long lasttime;			/* last update time in seconds
32016828Smckusick 						   since 1/1/70 */
32116828Smckusick 	FILE *last;				/* file where last update hour
32216828Smckusick 						   is stored */
32316818Swall 
32416818Swall 
32516828Smckusick 	/*
32616828Smckusick 	 * Open the file where the last update time is stored, and grab the
32716828Smckusick 	 * last update hour. The update time is measured in seconds since
32816828Smckusick 	 * 1/1/70.
32916828Smckusick 	 */
33016828Smckusick 	if ((last = fopen(LASTFILE,"r")) == NULL) {
33116828Smckusick 		perror(LASTFILE);
33216828Smckusick 		exit(1);
33316828Smckusick 	}
334*30804Sbostic 	fscanf(last,"%lu",&lasttime);
33516828Smckusick 	fclose(last);
33616818Swall 
33716828Smckusick 	/*
33816828Smckusick 	 * Get a broken down representation of the last update time.
33916828Smckusick 	 */
34016828Smckusick 	loc = localtime(&lasttime);
34116818Swall 
34216828Smckusick 	/*
34316828Smckusick 	 * Print the time that the spooling area was last updated.
34416828Smckusick 	 */
34516828Smckusick 	printf("\n LAST EXECUTION TIME: %s ",mthnames[loc->tm_mon]);
34616828Smckusick 	printf("%d, 19%d ",loc->tm_mday,loc->tm_year);
34716828Smckusick 	printf("at %d:%02d\n\n",loc->tm_hour,loc->tm_min);
34816818Swall }
34916818Swall 
35016818Swall /*
35116818Swall  * Print the rank of a job. (I've got to admit it, I stole it from "lpq")
35216818Swall  */
35316818Swall static
35416818Swall printrank(n)
35516818Swall {
35616828Smckusick 	static char *r[] = {
35716828Smckusick 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
35816828Smckusick 	};
35916818Swall 
36016828Smckusick 	if ((n/10) == 1)
36116828Smckusick 		 printf("%3d%-5s", n,"th");
36216828Smckusick 	else
36316828Smckusick 		 printf("%3d%-5s", n, r[n%10]);
36416818Swall }
36516818Swall 
36616818Swall /*
36716818Swall  * Print the date that a job is to be executed. This takes some manipulation
36816818Swall  * of the file name.
36916818Swall  */
37016818Swall printdate(filename)
37116818Swall char *filename;
37216818Swall {
37316828Smckusick 	int yday  =  0;				/* day of year file will be
37416828Smckusick 						   executed */
37516828Smckusick 	int min	  =  0;				/* min. file will be executed */
37616828Smckusick 	int hour  =  0;				/* hour file will be executed */
37716828Smckusick 	int day	  =  0;				/* day file will be executed */
37816828Smckusick 	int month =  0;				/* month file will be executed*/
37916828Smckusick 	int year  =  0;				/* year file will be executed */
38016828Smckusick 	int get_mth_day();			/* convert a day of year to a
38116828Smckusick 						   month and day of month */
38216828Smckusick 	char date[18];				/* reformatted execution date */
38316818Swall 
38416828Smckusick 	/*
38516828Smckusick 	 * Pick off the necessary info from the file name and convert the day
38616828Smckusick 	 * of year to a month and day of month.
38716828Smckusick 	 */
38816828Smckusick 	sscanf(filename,"%2d.%3d.%2d%2d",&year,&yday,&hour,&min);
38916828Smckusick 	get_mth_day(year,yday,&month,&day);
39016818Swall 
39116828Smckusick 	/*
39216828Smckusick 	 * Format the execution date of a job.
39316828Smckusick 	 */
39416828Smckusick 	sprintf(date,"%3s %2d, 19%2d %02d:%02d",mthnames[month],
39516828Smckusick 						    day, year,hour,min);
39616818Swall 
39716828Smckusick 	/*
39816828Smckusick 	 * Print the date the job will be executed.
39916828Smckusick 	 */
40016828Smckusick 	printf("%-21.18s",date);
40116818Swall }
40216818Swall 
40316818Swall /*
40416818Swall  * Given a day of the year, calculate the month and day of month.
40516818Swall  */
40616818Swall get_mth_day(year,dayofyear,month,day)
40716818Swall int year, dayofyear, *month, *day;
40816818Swall 
40916818Swall {
41016818Swall 
41116828Smckusick 	int i = 1;				/* for loop index */
41216828Smckusick 	int leap;				/* are we dealing with a leap
41316828Smckusick 						   year? */
41416828Smckusick 						/* Table of the number of days
41516828Smckusick 						   in each month of the year.
41616818Swall 
41716828Smckusick 						     dofy_tab[1] -- regular year
41816828Smckusick 						     dofy_tab[2] -- leap year
41916828Smckusick 									      */
42016818Swall 
42116828Smckusick 	static int dofy_tab[2][13] = {
42216828Smckusick 		{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
42316828Smckusick 		{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
42416828Smckusick 	};
42516818Swall 
42616828Smckusick 	/*
42716828Smckusick 	 * Are we dealing with a leap year?
42816828Smckusick 	 */
42916828Smckusick 	leap = ((year%4 == 0 && year%100 != 0) || year%100 == 0);
43016818Swall 
43116828Smckusick 	/*
43216828Smckusick 	 * Calculate the month of the year and day of the month.
43316828Smckusick 	 */
43416828Smckusick 	while (dayofyear >= dofy_tab[leap][i]) {
43516828Smckusick 		dayofyear -= dofy_tab[leap][i++];
43616828Smckusick 		++(*month);
43716828Smckusick 	}
43816828Smckusick 	*day = (dayofyear + 1);
43916818Swall }
44016828Smckusick 
44116818Swall /*
44216818Swall  * Print a job name. If the old "at" has been used to create the spoolfile,
44316818Swall  * the three line header that the new version of "at" puts in the spoolfile.
44416818Swall  * Thus, we just print "???".
44516818Swall  */
44616818Swall printjobname(file)
44716818Swall char *file;
44816818Swall {
44916865Swall 	char *ptr;				/* scratch pointer */
45028872Smckusick 	char jobname[28];			/* the job name */
45116828Smckusick 	FILE *filename;				/* job file in spooling area */
45216818Swall 
45316828Smckusick 	/*
45416865Swall 	 * Open the job file and grab the second line.
45516828Smckusick 	 */
45616828Smckusick 	printf("   ");
45716818Swall 
45816828Smckusick 	if ((filename = fopen(file,"r")) == NULL) {
45916828Smckusick 		printf("%.27s\n", "???");
46028872Smckusick 		perror(file);
46116828Smckusick 		return;
46216828Smckusick 	}
46316865Swall 	/*
46428872Smckusick 	 * Skip over the first line.
46516865Swall 	 */
46628872Smckusick 	fscanf(filename,"%*[^\n]\n");
46716865Swall 
46816865Swall 	/*
46916865Swall 	 * Now get the job name.
47016865Swall 	 */
47128872Smckusick 	if (fscanf(filename,"# jobname: %27s%*[^\n]\n",jobname) != 1) {
47216828Smckusick 		printf("%.27s\n", "???");
47316828Smckusick 		fclose(filename);
47416828Smckusick 		return;
47516828Smckusick 	}
47616828Smckusick 	fclose(filename);
47716818Swall 
47816828Smckusick 	/*
47916828Smckusick 	 * Put a pointer at the begining of the line and remove the basename
48016828Smckusick 	 * from the job file.
48116828Smckusick 	 */
48216865Swall 	ptr = jobname;
48316865Swall 	if ((ptr = (char *)rindex(jobname,'/')) != 0)
48416865Swall 		++ptr;
48516828Smckusick 	else
48616865Swall 		ptr = jobname;
48716818Swall 
48816865Swall 	if (strlen(ptr) > 23)
48916865Swall 		printf("%.23s ...\n",ptr);
49016865Swall 	else
49116865Swall 		printf("%.27s\n",ptr);
49216818Swall }
49316818Swall 
49416818Swall /*
49516818Swall  * Do we want to include a file in the queue? (used by "scandir") We are looking
49616818Swall  * for files with following syntax: yy.ddd.hhhh. so the test is made to see if
49716818Swall  * the file name has three dots in it. This test will suffice since the only
49816818Swall  * other files in /usr/spool/at don't have any dots in their name.
49916818Swall  */
50016818Swall filewanted(direntry)
50116818Swall struct direct *direntry;
50216818Swall {
50316828Smckusick 	int numdot = 0;
50416828Smckusick 	char *filename;
50516818Swall 
50616828Smckusick 	filename = direntry->d_name;
50716828Smckusick 	while (*filename)
50816828Smckusick 		numdot += (*(filename++) == '.');
50916828Smckusick 	return(numdot == 3);
51016818Swall }
51116818Swall 
51216818Swall /*
51316818Swall  * Sort files by time of creation. (used by "scandir")
51416818Swall  */
51516818Swall creation(d1, d2)
51616818Swall struct direct **d1, **d2;
51716818Swall {
51816828Smckusick 	struct stat stbuf1, stbuf2;
51916818Swall 
52016828Smckusick 	if (stat((*d1)->d_name,&stbuf1) < 0)
52116828Smckusick 		return(1);
52216818Swall 
52316828Smckusick 	if (stat((*d2)->d_name,&stbuf2) < 0)
52416828Smckusick 		return(1);
52516818Swall 
52616828Smckusick 	return(stbuf1.st_ctime < stbuf2.st_ctime);
52716818Swall }
52816828Smckusick 
52916818Swall /*
53016818Swall  * Print usage info and exit.
53116818Swall  */
53216818Swall usage()
53316818Swall {
53416828Smckusick 	fprintf(stderr,"usage:	atq [-c] [-n] [name ...]\n");
53516828Smckusick 	exit(1);
53616818Swall }
537