xref: /csrg-svn/usr.bin/at/atq/atq.c (revision 46817)
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*46817Sbostic static char sccsid[] = "@(#)atq.c	5.7 (Berkeley) 03/01/91";
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>
4137962Sbostic # include "pathnames.h"
4216818Swall 
4316818Swall /*
4416818Swall  * Months of the year
4516818Swall  */
4616818Swall static char *mthnames[12] = {
4716828Smckusick 	"Jan","Feb","Mar","Apr","May","Jun","Jul",
4816828Smckusick 	"Aug","Sep","Oct","Nov","Dec",
4916818Swall };
5016818Swall 
5128872Smckusick char *nullentry = NULL;			/* avoid 'namelist' NULL ptr problems */
5216828Smckusick int numentries;				/* number of entries in spooling area */
5316828Smckusick int namewanted = 0;			/* only print jobs belonging to a
5416828Smckusick 					   certain person */
5516828Smckusick struct direct **queue;			/* the queue itself */
5616818Swall 
5716818Swall 
5816818Swall main(argc,argv)
5916818Swall int argc;
6016818Swall char **argv;
6116818Swall {
6216818Swall 
6316828Smckusick 	int cflag = 0;			/* print in order of creation time */
6416828Smckusick 	int nflag = 0;			/* just print the number of jobs in
6516828Smckusick 					   queue */
6616828Smckusick 	int usage();			/* print usage info and exit */
6716828Smckusick 	int creation();			/* sort jobs by date of creation */
6816828Smckusick 	int alphasort();		/* sort jobs by date of execution */
6916828Smckusick 	int filewanted();		/* should a file be included in queue?*/
7016828Smckusick 	int printqueue();		/* print the queue */
7116828Smckusick 	int countfiles();		/* count the number of files in queue
7216828Smckusick 					   for a given person */
7328872Smckusick 	char **namelist = &nullentry;	/* array of specific name(s) requested*/
7416818Swall 
7516818Swall 
7616828Smckusick 	--argc, ++argv;
7716818Swall 
7816828Smckusick 	/*
7916828Smckusick 	 * Interpret command line flags if they exist.
8016828Smckusick 	 */
8116828Smckusick 	while (argc > 0 && **argv == '-') {
8216828Smckusick 		(*argv)++;
8316828Smckusick 		while (**argv) switch (*(*argv)++) {
8416818Swall 
8516828Smckusick 			case 'c' :	cflag++;
8616828Smckusick 					break;
8716818Swall 
8816828Smckusick 			case 'n' :	nflag++;
8916828Smckusick 					break;
9016818Swall 
9116828Smckusick 			default	 :	usage();
9216818Swall 
9316828Smckusick 		}
9416828Smckusick 		--argc, ++argv;
9516828Smckusick 	}
9616818Swall 
9716828Smckusick 	/*
9816828Smckusick 	 * If a certain name (or names) is requested, set a pointer to the
9916828Smckusick 	 * beginning of the list.
10016828Smckusick 	 */
10128872Smckusick 	if (argc > 0) {
10216828Smckusick 		++namewanted;
10316828Smckusick 		namelist = argv;
10416828Smckusick 	}
10516818Swall 
10616828Smckusick 	/*
10716828Smckusick 	 * Move to the spooling area and scan the directory, placing the
10816828Smckusick 	 * files in the queue structure. The queue comes back sorted by
10916828Smckusick 	 * execution time or creation time.
11016828Smckusick 	 */
11137962Sbostic 	if (chdir(_PATH_ATDIR) == -1) {
11237962Sbostic 		perror(_PATH_ATDIR);
11316828Smckusick 		exit(1);
11416828Smckusick 	}
11516828Smckusick 	if ((numentries = scandir(".",&queue,filewanted, (cflag) ? creation :
11616828Smckusick 				alphasort)) < 0) {
11737962Sbostic 		perror(_PATH_ATDIR);
11816828Smckusick 		exit(1);
11916828Smckusick 	}
12016818Swall 
12116828Smckusick 	/*
12216828Smckusick 	 * Either print a message stating:
12316828Smckusick 	 *
12416828Smckusick 	 *	1) that the spooling area is empty.
12516828Smckusick 	 *	2) the number of jobs in the spooling area.
12616828Smckusick 	 *	3) the number of jobs in the spooling area belonging to
12716828Smckusick 	 *	   a certain person.
12816828Smckusick 	 *	4) that the person requested doesn't have any files in the
12916828Smckusick 	 *	   spooling area.
13016828Smckusick 	 *
13116828Smckusick 	 * or send the queue off to "printqueue" for printing.
13216828Smckusick 	 *
13316828Smckusick 	 * This whole process might seem a bit elaborate, but it's worthwhile
13416828Smckusick 	 * to print some informative messages for the user.
13516828Smckusick 	 *
13616828Smckusick 	 */
13716828Smckusick 	if ((numentries == 0) && (!nflag)) {
13816828Smckusick 		printf("no files in queue.\n");
13916828Smckusick 		exit(0);
14016828Smckusick 	}
14116828Smckusick 	if (nflag) {
14216828Smckusick 		printf("%d\n",(namewanted) ? countfiles(namelist) : numentries);
14316828Smckusick 		exit(0);
14416828Smckusick 	}
14516828Smckusick 	if ((namewanted) && (countfiles(namelist) == 0)) {
14616828Smckusick 		printf("no files for %s.\n", (argc == 1) ?
14716828Smckusick 					*argv : "specified users");
14816828Smckusick 		exit(0);
14916828Smckusick 	}
15016828Smckusick 	printqueue(namelist);
15116828Smckusick 	exit(0);
15216818Swall }
15316818Swall 
15416818Swall /*
15516818Swall  * Count the number of jobs in the spooling area owned by a certain person(s).
15616818Swall  */
15716818Swall countfiles(namelist)
15816818Swall char **namelist;
15916818Swall {
16016828Smckusick 	int i;					/* for loop index */
16116828Smckusick 	int entryfound;				/* found file owned by user(s)*/
16216828Smckusick 	int numfiles = 0;			/* number of files owned by a
16316828Smckusick 						   certain person(s) */
16416828Smckusick 	char **ptr;				/* scratch pointer */
16516818Swall 
16616818Swall 
16716828Smckusick 	/*
16816828Smckusick 	 * For each file in the queue, see if the user(s) own the file. We
16916828Smckusick 	 * have to use "entryfound" (rather than simply incrementing "numfiles")
17016828Smckusick 	 * so that if a person's name appears twice on the command line we
17116828Smckusick 	 * don't double the number of files owned by him/her.
17216828Smckusick 	 */
17316828Smckusick 	for (i = 0; i < numentries ; i++) {
17416828Smckusick 		ptr = namelist;
17516828Smckusick 		entryfound = 0;
17616818Swall 
17716828Smckusick 		while (*ptr) {
17816865Swall 			if (isowner(*ptr,queue[i]->d_name))
17916828Smckusick 				++entryfound;
18016828Smckusick 			++ptr;
18116828Smckusick 		}
18216828Smckusick 		if (entryfound)
18316828Smckusick 			++numfiles;
18416828Smckusick 	}
18516828Smckusick 	return(numfiles);
18616818Swall }
18716818Swall 
18816818Swall /*
18916818Swall  * Print the queue. If only jobs belonging to a certain person(s) are requested,
19016818Swall  * only print jobs that belong to that person(s).
19116818Swall  */
19216818Swall printqueue(namelist)
19316818Swall char **namelist;
19416818Swall {
19516828Smckusick 	int i;					/* for loop index */
19616828Smckusick 	int rank = 1;				/* rank of a job */
19716828Smckusick 	int entryfound;				/* found file owned by user(s)*/
198*46817Sbostic 	static int printrank();			/* print the rank of a job */
19916828Smckusick 	int plastrun();				/* print the last time the
20016828Smckusick 						   spooling area was updated */
20116865Swall 	int powner();				/* print the name of the owner
20216865Swall 						   of the job */
20316828Smckusick 	char **ptr;				/* scratch pointer */
20416828Smckusick 	struct stat stbuf;			/* buffer for file stats */
20516818Swall 
20616818Swall 
20716828Smckusick 	/*
20816828Smckusick 	 * Print the time the spooling area was last modified and the header
20916828Smckusick 	 * for the queue.
21016828Smckusick 	 */
21116828Smckusick 	plastrun();
21216828Smckusick 	printf(" Rank	  Execution Date     Owner     Job #   Job Name\n");
21316818Swall 
21416828Smckusick 	/*
21516828Smckusick 	 * Print the queue. If a certain name(s) was requested, print only jobs
21616828Smckusick 	 * belonging to that person(s), otherwise print the entire queue.
21716828Smckusick 	 * Once again, we have to use "entryfound" (rather than simply
21816828Smckusick 	 * comparing each command line argument) so that if a person's name
21916828Smckusick 	 * appears twice we don't print each file owned by him/her twice.
22016828Smckusick 	 *
22116828Smckusick 	 *
22216828Smckusick 	 * "printrank", "printdate", and "printjobname" all take existing
22316828Smckusick 	 * data and display it in a friendly manner.
22416828Smckusick 	 *
22516828Smckusick 	 */
22616828Smckusick 	for (i = 0; i < numentries; i++) {
22716828Smckusick 		if ((stat(queue[i]->d_name, &stbuf)) < 0) {
22816828Smckusick 			continue;
22916828Smckusick 		}
23016828Smckusick 		if (namewanted) {
23116828Smckusick 			ptr = namelist;
23216828Smckusick 			entryfound = 0;
23316818Swall 
23416828Smckusick 			while (*ptr) {
23516865Swall 				if (isowner(*ptr,queue[i]->d_name))
23616828Smckusick 					++entryfound;
23716828Smckusick 				++ptr;
23816828Smckusick 			}
23916828Smckusick 			if (!entryfound)
24016828Smckusick 				continue;
24116828Smckusick 		}
24216828Smckusick 		printrank(rank++);
24316828Smckusick 		printdate(queue[i]->d_name);
24416865Swall 		powner(queue[i]->d_name);
24516828Smckusick 		printf("%5d",stbuf.st_ino);
24616828Smckusick 		printjobname(queue[i]->d_name);
24716828Smckusick 	}
24816828Smckusick 	++ptr;
24916818Swall }
25016818Swall 
25116818Swall /*
25216865Swall  * See if "name" owns "job".
25316818Swall  */
25416865Swall isowner(name,job)
25516865Swall char *name;
25616865Swall char *job;
25716818Swall {
25828872Smckusick 	char buf[128];			/* buffer for 1st line of spoolfile
25916865Swall 					   header */
26016865Swall 	FILE *infile;			/* I/O stream to spoolfile */
26116818Swall 
26216865Swall 	if ((infile = fopen(job,"r")) == NULL) {
26328872Smckusick 		fprintf(stderr,"Couldn't open spoolfile ");
26416865Swall 		perror(job);
26516865Swall 		return(0);
26616828Smckusick 	}
26716865Swall 
26828872Smckusick 	if (fscanf(infile,"# owner: %127s%*[^\n]\n",buf) != 1) {
26916865Swall 		fclose(infile);
27016865Swall 		return(0);
27116865Swall 	}
27216865Swall 
27316865Swall 	fclose(infile);
27416865Swall 	return((strcmp(name,buf) == 0) ? 1 : 0);
27516818Swall }
27616865Swall 
27716865Swall /*
27816865Swall  * Print the owner of the job. This is stored on the first line of the
27916865Swall  * spoolfile. If we run into trouble getting the name, we'll just print "???".
28016865Swall  */
28116865Swall powner(file)
28216865Swall char *file;
28316865Swall {
28428872Smckusick 	char owner[10];				/* the owner */
28516865Swall 	FILE *infile;				/* I/O stream to spoolfile */
28616865Swall 
28716865Swall 	/*
28816865Swall 	 * Open the job file and grab the first line.
28916865Swall 	 */
29016865Swall 
29116865Swall 	if ((infile = fopen(file,"r")) == NULL) {
29216865Swall 		printf("%-10.9s","???");
29328872Smckusick 		perror(file);
29416865Swall 		return;
29516865Swall 	}
29616865Swall 
29728872Smckusick 	if (fscanf(infile,"# owner: %9s%*[^\n]\n",owner) != 1) {
29816865Swall 		printf("%-10.9s","???");
29916865Swall 		fclose(infile);
30016865Swall 		return;
30116865Swall 	}
30216865Swall 
30316865Swall 	fclose(infile);
30416865Swall 	printf("%-10.9s",owner);
30516865Swall 
30616865Swall }
30716828Smckusick 
30816818Swall /*
30916818Swall  * Print the time the spooling area was updated.
31016818Swall  */
31116818Swall plastrun()
31216818Swall {
31316828Smckusick 	struct timeval now;			/* time it is right now */
31416828Smckusick 	struct timezone zone;			/* NOT USED */
31516828Smckusick 	struct tm *loc;				/* detail of time it is right */
316*46817Sbostic 	time_t lasttime;			/* last update time in seconds
31716828Smckusick 						   since 1/1/70 */
31816828Smckusick 	FILE *last;				/* file where last update hour
31916828Smckusick 						   is stored */
32016818Swall 
32116818Swall 
32216828Smckusick 	/*
32316828Smckusick 	 * Open the file where the last update time is stored, and grab the
32416828Smckusick 	 * last update hour. The update time is measured in seconds since
32516828Smckusick 	 * 1/1/70.
32616828Smckusick 	 */
32737962Sbostic 	if ((last = fopen(_PATH_LASTFILE,"r")) == NULL) {
32837962Sbostic 		perror(_PATH_LASTFILE);
32916828Smckusick 		exit(1);
33016828Smckusick 	}
33130804Sbostic 	fscanf(last,"%lu",&lasttime);
33216828Smckusick 	fclose(last);
33316818Swall 
33416828Smckusick 	/*
33516828Smckusick 	 * Get a broken down representation of the last update time.
33616828Smckusick 	 */
33716828Smckusick 	loc = localtime(&lasttime);
33816818Swall 
33916828Smckusick 	/*
34016828Smckusick 	 * Print the time that the spooling area was last updated.
34116828Smckusick 	 */
34216828Smckusick 	printf("\n LAST EXECUTION TIME: %s ",mthnames[loc->tm_mon]);
34316828Smckusick 	printf("%d, 19%d ",loc->tm_mday,loc->tm_year);
34416828Smckusick 	printf("at %d:%02d\n\n",loc->tm_hour,loc->tm_min);
34516818Swall }
34616818Swall 
34716818Swall /*
34816818Swall  * Print the rank of a job. (I've got to admit it, I stole it from "lpq")
34916818Swall  */
35016818Swall static
35116818Swall printrank(n)
35216818Swall {
35316828Smckusick 	static char *r[] = {
35416828Smckusick 		"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
35516828Smckusick 	};
35616818Swall 
35716828Smckusick 	if ((n/10) == 1)
35816828Smckusick 		 printf("%3d%-5s", n,"th");
35916828Smckusick 	else
36016828Smckusick 		 printf("%3d%-5s", n, r[n%10]);
36116818Swall }
36216818Swall 
36316818Swall /*
36416818Swall  * Print the date that a job is to be executed. This takes some manipulation
36516818Swall  * of the file name.
36616818Swall  */
36716818Swall printdate(filename)
36816818Swall char *filename;
36916818Swall {
37016828Smckusick 	int yday  =  0;				/* day of year file will be
37116828Smckusick 						   executed */
37216828Smckusick 	int min	  =  0;				/* min. file will be executed */
37316828Smckusick 	int hour  =  0;				/* hour file will be executed */
37416828Smckusick 	int day	  =  0;				/* day file will be executed */
37516828Smckusick 	int month =  0;				/* month file will be executed*/
37616828Smckusick 	int year  =  0;				/* year file will be executed */
37716828Smckusick 	int get_mth_day();			/* convert a day of year to a
37816828Smckusick 						   month and day of month */
37936357Sbostic 	char date[19];				/* reformatted execution date */
38016818Swall 
38116828Smckusick 	/*
38216828Smckusick 	 * Pick off the necessary info from the file name and convert the day
38316828Smckusick 	 * of year to a month and day of month.
38416828Smckusick 	 */
38516828Smckusick 	sscanf(filename,"%2d.%3d.%2d%2d",&year,&yday,&hour,&min);
38616828Smckusick 	get_mth_day(year,yday,&month,&day);
38716818Swall 
38816828Smckusick 	/*
38916828Smckusick 	 * Format the execution date of a job.
39016828Smckusick 	 */
39116828Smckusick 	sprintf(date,"%3s %2d, 19%2d %02d:%02d",mthnames[month],
39216828Smckusick 						    day, year,hour,min);
39316818Swall 
39416828Smckusick 	/*
39516828Smckusick 	 * Print the date the job will be executed.
39616828Smckusick 	 */
39716828Smckusick 	printf("%-21.18s",date);
39816818Swall }
39916818Swall 
40016818Swall /*
40116818Swall  * Given a day of the year, calculate the month and day of month.
40216818Swall  */
40316818Swall get_mth_day(year,dayofyear,month,day)
40416818Swall int year, dayofyear, *month, *day;
40516818Swall 
40616818Swall {
40716818Swall 
40816828Smckusick 	int i = 1;				/* for loop index */
40916828Smckusick 	int leap;				/* are we dealing with a leap
41016828Smckusick 						   year? */
41116828Smckusick 						/* Table of the number of days
41216828Smckusick 						   in each month of the year.
41316818Swall 
41416828Smckusick 						     dofy_tab[1] -- regular year
41516828Smckusick 						     dofy_tab[2] -- leap year
41616828Smckusick 									      */
41716818Swall 
41816828Smckusick 	static int dofy_tab[2][13] = {
41916828Smckusick 		{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
42016828Smckusick 		{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
42116828Smckusick 	};
42216818Swall 
42316828Smckusick 	/*
42416828Smckusick 	 * Are we dealing with a leap year?
42516828Smckusick 	 */
42616828Smckusick 	leap = ((year%4 == 0 && year%100 != 0) || year%100 == 0);
42716818Swall 
42816828Smckusick 	/*
42916828Smckusick 	 * Calculate the month of the year and day of the month.
43016828Smckusick 	 */
43116828Smckusick 	while (dayofyear >= dofy_tab[leap][i]) {
43216828Smckusick 		dayofyear -= dofy_tab[leap][i++];
43316828Smckusick 		++(*month);
43416828Smckusick 	}
43516828Smckusick 	*day = (dayofyear + 1);
43616818Swall }
43716828Smckusick 
43816818Swall /*
43916818Swall  * Print a job name. If the old "at" has been used to create the spoolfile,
44016818Swall  * the three line header that the new version of "at" puts in the spoolfile.
44116818Swall  * Thus, we just print "???".
44216818Swall  */
44316818Swall printjobname(file)
44416818Swall char *file;
44516818Swall {
44616865Swall 	char *ptr;				/* scratch pointer */
44728872Smckusick 	char jobname[28];			/* the job name */
44816828Smckusick 	FILE *filename;				/* job file in spooling area */
44916818Swall 
45016828Smckusick 	/*
45116865Swall 	 * Open the job file and grab the second line.
45216828Smckusick 	 */
45316828Smckusick 	printf("   ");
45416818Swall 
45516828Smckusick 	if ((filename = fopen(file,"r")) == NULL) {
45616828Smckusick 		printf("%.27s\n", "???");
45728872Smckusick 		perror(file);
45816828Smckusick 		return;
45916828Smckusick 	}
46016865Swall 	/*
46128872Smckusick 	 * Skip over the first line.
46216865Swall 	 */
46328872Smckusick 	fscanf(filename,"%*[^\n]\n");
46416865Swall 
46516865Swall 	/*
46616865Swall 	 * Now get the job name.
46716865Swall 	 */
46828872Smckusick 	if (fscanf(filename,"# jobname: %27s%*[^\n]\n",jobname) != 1) {
46916828Smckusick 		printf("%.27s\n", "???");
47016828Smckusick 		fclose(filename);
47116828Smckusick 		return;
47216828Smckusick 	}
47316828Smckusick 	fclose(filename);
47416818Swall 
47516828Smckusick 	/*
47616828Smckusick 	 * Put a pointer at the begining of the line and remove the basename
47716828Smckusick 	 * from the job file.
47816828Smckusick 	 */
47916865Swall 	ptr = jobname;
48016865Swall 	if ((ptr = (char *)rindex(jobname,'/')) != 0)
48116865Swall 		++ptr;
48216828Smckusick 	else
48316865Swall 		ptr = jobname;
48416818Swall 
48516865Swall 	if (strlen(ptr) > 23)
48616865Swall 		printf("%.23s ...\n",ptr);
48716865Swall 	else
48816865Swall 		printf("%.27s\n",ptr);
48916818Swall }
49016818Swall 
49116818Swall /*
49216818Swall  * Do we want to include a file in the queue? (used by "scandir") We are looking
49316818Swall  * for files with following syntax: yy.ddd.hhhh. so the test is made to see if
49416818Swall  * the file name has three dots in it. This test will suffice since the only
49516818Swall  * other files in /usr/spool/at don't have any dots in their name.
49616818Swall  */
49716818Swall filewanted(direntry)
49816818Swall struct direct *direntry;
49916818Swall {
50016828Smckusick 	int numdot = 0;
50116828Smckusick 	char *filename;
50216818Swall 
50316828Smckusick 	filename = direntry->d_name;
50416828Smckusick 	while (*filename)
50516828Smckusick 		numdot += (*(filename++) == '.');
50616828Smckusick 	return(numdot == 3);
50716818Swall }
50816818Swall 
50916818Swall /*
51016818Swall  * Sort files by time of creation. (used by "scandir")
51116818Swall  */
51216818Swall creation(d1, d2)
51316818Swall struct direct **d1, **d2;
51416818Swall {
51516828Smckusick 	struct stat stbuf1, stbuf2;
51616818Swall 
51716828Smckusick 	if (stat((*d1)->d_name,&stbuf1) < 0)
51816828Smckusick 		return(1);
51916818Swall 
52016828Smckusick 	if (stat((*d2)->d_name,&stbuf2) < 0)
52116828Smckusick 		return(1);
52216818Swall 
52316828Smckusick 	return(stbuf1.st_ctime < stbuf2.st_ctime);
52416818Swall }
52516828Smckusick 
52616818Swall /*
52716818Swall  * Print usage info and exit.
52816818Swall  */
52916818Swall usage()
53016818Swall {
53116828Smckusick 	fprintf(stderr,"usage:	atq [-c] [-n] [name ...]\n");
53216828Smckusick 	exit(1);
53316818Swall }
534