BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / at / atq / atq.c
index adac35e..b46434b 100644 (file)
@@ -1,5 +1,17 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)atq.c      1.2     (Berkeley)      %G%";
+static char sccsid[] = "@(#)atq.c      5.6 (Berkeley) 5/11/89";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -26,10 +38,7 @@ static char sccsid[] = "@(#)atq.c    1.2     (Berkeley)      %G%";
 # include <sys/time.h>
 # include <pwd.h>
 # include <ctype.h>
 # include <sys/time.h>
 # include <pwd.h>
 # include <ctype.h>
-# define ATDIR         "/usr/spool/at"                 /* spooling area */
-# define LASTFILE      "/usr/spool/at/lasttimedone"    /* update time record 
-                                                          file */
+# include "pathnames.h"
 
 /*
  * Months of the year
 
 /*
  * Months of the year
@@ -39,7 +48,7 @@ static char *mthnames[12] = {
        "Aug","Sep","Oct","Nov","Dec",
 };
 
        "Aug","Sep","Oct","Nov","Dec",
 };
 
-
+char *nullentry = NULL;                        /* avoid 'namelist' NULL ptr problems */
 int numentries;                                /* number of entries in spooling area */
 int namewanted = 0;                    /* only print jobs belonging to a 
                                           certain person */
 int numentries;                                /* number of entries in spooling area */
 int namewanted = 0;                    /* only print jobs belonging to a 
                                           certain person */
@@ -61,7 +70,7 @@ char **argv;
        int printqueue();               /* print the queue */
        int countfiles();               /* count the number of files in queue
                                           for a given person */
        int printqueue();               /* print the queue */
        int countfiles();               /* count the number of files in queue
                                           for a given person */
-       char **namelist;                /* array of specific name(s) requested*/
+       char **namelist = &nullentry;   /* array of specific name(s) requested*/
 
 
        --argc, ++argv;
 
 
        --argc, ++argv;
@@ -89,7 +98,7 @@ char **argv;
         * If a certain name (or names) is requested, set a pointer to the
         * beginning of the list.
         */
         * If a certain name (or names) is requested, set a pointer to the
         * beginning of the list.
         */
-       if (**argv) {
+       if (argc > 0) {
                ++namewanted;
                namelist = argv;
        }
                ++namewanted;
                namelist = argv;
        }
@@ -99,13 +108,13 @@ char **argv;
         * files in the queue structure. The queue comes back sorted by
         * execution time or creation time.
         */
         * files in the queue structure. The queue comes back sorted by
         * execution time or creation time.
         */
-       if (chdir(ATDIR) == -1) {
-               perror(ATDIR);
+       if (chdir(_PATH_ATDIR) == -1) {
+               perror(_PATH_ATDIR);
                exit(1);
        }
        if ((numentries = scandir(".",&queue,filewanted, (cflag) ? creation : 
                                alphasort)) < 0) {
                exit(1);
        }
        if ((numentries = scandir(".",&queue,filewanted, (cflag) ? creation : 
                                alphasort)) < 0) {
-               perror(ATDIR);
+               perror(_PATH_ATDIR);
                exit(1);
        }
 
                exit(1);
        }
 
@@ -153,8 +162,6 @@ char **namelist;
        int numfiles = 0;                       /* number of files owned by a
                                                   certain person(s) */
        char **ptr;                             /* scratch pointer */
        int numfiles = 0;                       /* number of files owned by a
                                                   certain person(s) */
        char **ptr;                             /* scratch pointer */
-       struct stat stbuf;                      /* buffer for file stats */
-       
 
 
        /*
 
 
        /*
@@ -164,14 +171,11 @@ char **namelist;
         * don't double the number of files owned by him/her.
         */
        for (i = 0; i < numentries ; i++) {
         * don't double the number of files owned by him/her.
         */
        for (i = 0; i < numentries ; i++) {
-               if ((stat(queue[i]->d_name, &stbuf)) < 0) {
-                       continue;
-               }
                ptr = namelist;
                entryfound = 0;
 
                while (*ptr) {
                ptr = namelist;
                entryfound = 0;
 
                while (*ptr) {
-                       if (getid(*ptr) == stbuf.st_uid)
+                       if (isowner(*ptr,queue[i]->d_name))
                                ++entryfound;
                        ++ptr;
                }
                                ++entryfound;
                        ++ptr;
                }
@@ -194,10 +198,9 @@ char **namelist;
        int printrank();                        /* print the rank of a job */
        int plastrun();                         /* print the last time the 
                                                   spooling area was updated */
        int printrank();                        /* print the rank of a job */
        int plastrun();                         /* print the last time the 
                                                   spooling area was updated */
-       int getid();                            /* get uid of a person */
+       int powner();                           /* print the name of the owner
+                                                  of the job */
        char **ptr;                             /* scratch pointer */
        char **ptr;                             /* scratch pointer */
-       char *getname();                        /* get the login name of a 
-                                                  person using their uid */
        struct stat stbuf;                      /* buffer for file stats */
 
 
        struct stat stbuf;                      /* buffer for file stats */
 
 
@@ -229,7 +232,7 @@ char **namelist;
                        entryfound = 0;
 
                        while (*ptr) {
                        entryfound = 0;
 
                        while (*ptr) {
-                               if (getid(*ptr) == stbuf.st_uid)
+                               if (isowner(*ptr,queue[i]->d_name))
                                        ++entryfound;
                                ++ptr;
                        }
                                        ++entryfound;
                                ++ptr;
                        }
@@ -238,7 +241,7 @@ char **namelist;
                }
                printrank(rank++);
                printdate(queue[i]->d_name);
                }
                printrank(rank++);
                printdate(queue[i]->d_name);
-               printf("%-10.9s",getname(stbuf.st_uid));
+               powner(queue[i]->d_name);
                printf("%5d",stbuf.st_ino);
                printjobname(queue[i]->d_name);
        }
                printf("%5d",stbuf.st_ino);
                printjobname(queue[i]->d_name);
        }
@@ -246,39 +249,62 @@ char **namelist;
 }
 
 /*
 }
 
 /*
- * Get the full login name of a person using his/her user id.
+ * See if "name" owns "job".
  */
  */
-char *
-getname(uid)
-int uid;
+isowner(name,job)
+char *name;
+char *job;
 {
 {
-       struct passwd *pwdinfo;                 /* password info structure */
-       
+       char buf[128];                  /* buffer for 1st line of spoolfile 
+                                          header */
+       FILE *infile;                   /* I/O stream to spoolfile */
+
+       if ((infile = fopen(job,"r")) == NULL) {
+               fprintf(stderr,"Couldn't open spoolfile ");
+               perror(job);
+               return(0);
+       }
 
 
-       if ((pwdinfo = getpwuid(uid)) == 0) {
-               perror(uid);
-               exit(1);
+       if (fscanf(infile,"# owner: %127s%*[^\n]\n",buf) != 1) {
+               fclose(infile);
+               return(0);
        }
        }
-       return(pwdinfo->pw_name);
+
+       fclose(infile);
+       return((strcmp(name,buf) == 0) ? 1 : 0);
 }
 }
-       
+
 /*
 /*
- * Get the uid of a person using his/her login name. Return -1 if no
- * such account name exists.
+ * Print the owner of the job. This is stored on the first line of the
+ * spoolfile. If we run into trouble getting the name, we'll just print "???".
  */
  */
-getid(name)
-char *name;
+powner(file)
+char *file;
 {
 {
+       char owner[10];                         /* the owner */
+       FILE *infile;                           /* I/O stream to spoolfile */
+
+       /*
+        * Open the job file and grab the first line.
+        */
 
 
-       struct passwd *pwdinfo;                 /* password info structure */
+       if ((infile = fopen(file,"r")) == NULL) {
+               printf("%-10.9s","???");
+               perror(file);
+               return;
+       }
 
 
+       if (fscanf(infile,"# owner: %9s%*[^\n]\n",owner) != 1) {
+               printf("%-10.9s","???");
+               fclose(infile);
+               return;
+       }
 
 
-       if ((pwdinfo = getpwnam(name)) == 0)
-               return(-1);
+       fclose(infile);
+       printf("%-10.9s",owner);
 
 
-       return(pwdinfo->pw_uid);
 }
 }
-
+       
 /*
  * Print the time the spooling area was updated.
  */
 /*
  * Print the time the spooling area was updated.
  */
@@ -298,11 +324,11 @@ plastrun()
         * last update hour. The update time is measured in seconds since
         * 1/1/70.
         */
         * last update hour. The update time is measured in seconds since
         * 1/1/70.
         */
-       if ((last = fopen(LASTFILE,"r")) == NULL) {
-               perror(LASTFILE);
+       if ((last = fopen(_PATH_LASTFILE,"r")) == NULL) {
+               perror(_PATH_LASTFILE);
                exit(1);
        }
                exit(1);
        }
-       fscanf(last,"%d",(u_long) &lasttime);
+       fscanf(last,"%lu",&lasttime);
        fclose(last);
 
        /*
        fclose(last);
 
        /*
@@ -350,7 +376,7 @@ char *filename;
        int year  =  0;                         /* year file will be executed */
        int get_mth_day();                      /* convert a day of year to a
                                                   month and day of month */
        int year  =  0;                         /* year file will be executed */
        int get_mth_day();                      /* convert a day of year to a
                                                   month and day of month */
-       char date[18];                          /* reformatted execution date */
+       char date[19];                          /* reformatted execution date */
 
        /*
         * Pick off the necessary info from the file name and convert the day
 
        /*
         * Pick off the necessary info from the file name and convert the day
@@ -417,21 +443,29 @@ int year, dayofyear, *month, *day;
 printjobname(file)
 char *file;
 {
 printjobname(file)
 char *file;
 {
-       char *ch;                               /* char ptr */
-       char jobname[80];                       /* the job name */
+       char *ptr;                              /* scratch pointer */
+       char jobname[28];                       /* the job name */
        FILE *filename;                         /* job file in spooling area */
 
        /*
        FILE *filename;                         /* job file in spooling area */
 
        /*
-        * Open the job file and grab the first line.
+        * Open the job file and grab the second line.
         */
        printf("   ");
 
        if ((filename = fopen(file,"r")) == NULL) {
                printf("%.27s\n", "???");
         */
        printf("   ");
 
        if ((filename = fopen(file,"r")) == NULL) {
                printf("%.27s\n", "???");
-               fclose(filename);
+               perror(file);
                return;
        }
                return;
        }
-       if (fscanf(filename,"# jobname: %s",jobname) != 1) {
+       /*
+        * Skip over the first line.
+        */
+       fscanf(filename,"%*[^\n]\n");
+
+       /*
+        * Now get the job name.
+        */
+       if (fscanf(filename,"# jobname: %27s%*[^\n]\n",jobname) != 1) {
                printf("%.27s\n", "???");
                fclose(filename);
                return;
                printf("%.27s\n", "???");
                fclose(filename);
                return;
@@ -442,13 +476,16 @@ char *file;
         * Put a pointer at the begining of the line and remove the basename
         * from the job file.
         */
         * Put a pointer at the begining of the line and remove the basename
         * from the job file.
         */
-       ch = jobname;
-       if ((ch = (char *)rindex(jobname,'/')) != 0)
-               ++ch;
+       ptr = jobname;
+       if ((ptr = (char *)rindex(jobname,'/')) != 0)
+               ++ptr;
        else 
        else 
-               ch = jobname;
+               ptr = jobname;
 
 
-       printf("%.27s\n",ch);
+       if (strlen(ptr) > 23)
+               printf("%.23s ...\n",ptr);
+       else
+               printf("%.27s\n",ptr);
 }
 
 /*
 }
 
 /*