home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume13 / atl < prev    next >
Encoding:
Text File  |  1988-09-11  |  6.3 KB  |  322 lines

  1. Subject:  v13i075:  List jobs in at queue for 4.3BSD
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Roger Southwick <dadla.TEK.COM!rogers>
  7. Posting-number: Volume 13, Issue 75
  8. Archive-name: atl
  9.  
  10. [  Short program that fills a gap nicely.  --r$  ]
  11.  
  12. Upon getting 4.3BSD, we found the Berkeley had come up with a nice new
  13. version of 'at', which allowed listing the queue and job removal.  One
  14. thing was missing, a way for the user to examine their job in the queue.
  15.  
  16. Well here it is.  I call it 'atl'.
  17.  
  18.         -Roger (rogers@dadla.tek.com)
  19.  
  20. # This is a shell archive.  Remove anything before this line, then
  21. # unpack it by saving it in a file and typing "sh file".  (Files
  22. # unpacked will be owned by you and have default permissions.)
  23. #
  24. # This archive contains:
  25. # Makefile atl.1 atl.c
  26.  
  27. echo x - Makefile
  28. cat > "Makefile" << '//E*O*F Makefile//'
  29. BINDIR = /usr/local
  30. MANDIR = /usr/manl/man1
  31.  
  32. atl : atl.c
  33.     cc -O -s -o atl atl.c
  34.  
  35. install : $(BINDIR)/atl $(MANDIR)/atl.1
  36.  
  37. $(BINDIR)/atl : atl
  38.     install -c -o daemon -g daemon -m 4711 atl $(BINDIR)
  39.  
  40. $(MANDIR)/atl.1 : atl.1
  41.     install -c -o daemon -g daemon -m 0644 atl.1 $(MANDIR)
  42.  
  43. clean :
  44.     rm -f atl
  45.  
  46. //E*O*F Makefile//
  47.  
  48. echo x - atl.1
  49. cat > "atl.1" << '//E*O*F atl.1//'
  50. .TH ATL 1 "Local"
  51. .SH NAME
  52. atl - list a job waiting to be run
  53. .SH SYNOPSIS
  54. .B "atl job#
  55. [ job# ... ]
  56. .SH DESCRIPTION
  57. .I Atl 
  58. lists on stdout the contents of the 
  59. .I job#
  60. which is waiting to be run at a later date. These jobs were
  61. created with the
  62. .IR at (1)
  63. command.  To obtain the needed 
  64. .I job#,
  65. the user should use the
  66. .IR atq (1)
  67. command.
  68. .PP
  69. Only the job's owner (or root) may list the contents of the
  70. job.
  71. .SH BUGS
  72. To be discovered.
  73. .SH FILES
  74. /usr/spool/at        spool area
  75. .SH AUTHOR
  76. Roger Southwick (rogers@dadla.TEK.COM)
  77. .SH "SEE ALSO"
  78. at(1),
  79. atq(1),
  80. atrm(1),
  81. cron(8)
  82. //E*O*F atl.1//
  83.  
  84. echo x - atl.c
  85. cat > "atl.c" << '//E*O*F atl.c//'
  86. /*-------------------------------------------------------------
  87.  
  88. The atl program
  89.  
  90. By: Roger S. Southwick
  91.  
  92. April 21, 1986
  93.  
  94. This program lists the jobs in the /usr/spool/at 
  95. directory which belong to him.
  96.  
  97. usage: atl job# [job#....]
  98.  
  99. The job# is the job number reported by atq.
  100.  
  101. (This turns out to be the inode number of the file, but
  102. we don't tell the user that!)
  103.  
  104. ---------------------------------------------------------------
  105.  *
  106.  * MODIFICATION HISTORY:
  107.  *
  108.  * $Log:    atl.c,v $
  109.  * Revision 1.2  86/04/21  16:45:36  rogers
  110.  * Fixed so that the user may be su'ed to
  111.  * the file's owner. (Definable).
  112.  * 
  113.  * Revision 1.1  86/04/21  15:15:21  rogers
  114.  * Initial revision
  115.  * 
  116.  *
  117. -------------------------------------------------------------*/
  118.  
  119. #ifndef lint
  120. static char *RCSid = "$Header: atl.c,v 1.2 86/04/21 16:45:36 rogers Exp $";
  121. #endif
  122.  
  123. #define ATDIR    "/usr/spool/at"
  124.  
  125. /*
  126.  * Define STRICT if you require the user to be logged in
  127.  * Without this, being su'ed will work.
  128.  */
  129.  
  130. /* #define STRICT    /* Strict login required    */
  131.  
  132. #include <stdio.h>
  133. #include <sys/types.h>
  134. #include <sys/stat.h>
  135. #include <sys/dir.h>
  136. #include <pwd.h>
  137. #include <ctype.h>
  138.  
  139. extern char *sys_errlist[];
  140. extern int errno;
  141. #define ERROR    sys_errlist[errno]
  142.  
  143. char *myname = NULL;
  144. FILE *fp = NULL;
  145.  
  146. main(argc, argv)
  147.  
  148. int argc;
  149. char *argv[];
  150. {
  151.     DIR *opendir();
  152.     struct direct *readdir();
  153.     int chdir(), stat(), sscanf();
  154.     register DIR *dirp;
  155.     register struct direct *dp;
  156.     register int i;
  157.     int job;
  158.     struct stat stb;
  159.  
  160.     if(chdir(ATDIR) == -1){
  161.     (void)fprintf(stderr,"atl: could not chdir %s - %s\n", ATDIR, ERROR);
  162.     exit(1);
  163.     }
  164.  
  165.     if(argc < 2){
  166.         (void)fprintf(stderr, "usage: atl job# [job#....]\n");
  167.     exit(1);
  168.     }
  169.     
  170.     whoami();
  171.  
  172.     if((dirp = opendir(".")) == NULL){
  173.     (void)fprintf(stderr,"atl: could not opendir(%s)\n", ATDIR);
  174.     exit(1);
  175.     }
  176.  
  177.     for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
  178.     if(dp->d_ino == 0)
  179.         continue;
  180.     
  181.     if(strcmp(".", dp->d_name) == 0 || strcmp("..", dp->d_name) == 0)
  182.         continue;
  183.     
  184.     if(!isdigit(dp->d_name[0]))
  185.         continue;
  186.  
  187.     if(stat(dp->d_name, &stb) == -1){
  188.         (void)fprintf(stderr,"atl: could not stat %s/%s - %s\n", ATDIR, dp->d_name, ERROR);
  189.         exit(1);
  190.     }
  191.  
  192.     if((stb.st_mode & S_IFMT) != S_IFREG)
  193.         continue;
  194.  
  195.     for(i = 1; i < argc; i++){
  196.         if(sscanf(argv[i], "%d", &job) != 1){
  197.         (void)fprintf(stderr,"atl: job number (%s) must be numeric\n", argv[i]);
  198.         continue;
  199.         }
  200.  
  201.         if(job < 0){
  202.         (void)fprintf(stderr,"atl: job number (%d) must be positive\n", job);
  203.         continue;
  204.         }
  205.  
  206.         if(job != stb.st_ino)
  207.         continue;
  208.         
  209.         if(notowner(dp->d_name)){
  210.         (void)fprintf(stderr, "atl: must be job's owner\n");
  211.         continue;
  212.         }
  213.  
  214.         if(argc > 2)
  215.         (void)printf("\n>>>>>> Job # %d <<<<<<\n", job);
  216.  
  217.         output();
  218.     }
  219.     }
  220.     exit(0);
  221. }
  222.  
  223. int myuid;
  224.  
  225. whoami()
  226. {
  227.  
  228. #ifdef STRICT
  229.     char *getlogin();
  230. #endif
  231.     int getuid();
  232.     struct passwd *getpwuid();
  233.     register struct passwd *pwd;
  234.     extern char *myname;
  235.     extern int myuid;
  236.  
  237.     myuid = getuid();
  238.  
  239. #ifdef STRICT
  240.     if((myname = getlogin()) == NULL){
  241.     if((pwd = getpwuid(myuid)) == NULL){
  242.         (void)fprintf(stderr, "atl: Say, who are you, anyway?\n");
  243.         exit(1);
  244.     }
  245.     myname = pwd->pw_name;
  246.     }
  247. #else
  248.     if((pwd = getpwuid(myuid)) == NULL){
  249.     (void)fprintf(stderr, "atl: Say, who are you, anyway?\n");
  250.     exit(1);
  251.     }
  252.     myname = pwd->pw_name;
  253. #endif
  254. }
  255.  
  256. char owner[80];
  257.  
  258. int
  259. notowner(name)
  260.  
  261. register char *name;
  262. {
  263.     FILE *fopen();
  264.     int fscanf();
  265.     extern char *myname;
  266.     extern FILE *fp;
  267.     extern char owner[];
  268.     extern myuid;
  269.  
  270.     if((fp = fopen(name, "r")) == NULL){
  271.     (void)fprintf(stderr,"atl: could not open %s/%s for read\n", ATDIR, name);
  272.     exit(1);
  273.     }
  274.  
  275.     if(fscanf(fp, "# owner: %s\n", owner) != 1){
  276.     (void)fprintf(stderr,"atl: unknown owner %s/%s\n", ATDIR, name);
  277.     return(1);
  278.     }
  279.  
  280.     if(myuid == 0)
  281.     return(0);
  282.  
  283.     if(strcmp(owner, myname) != 0){
  284.     (void)fclose(fp);
  285.     fp = NULL;
  286.     return(1);
  287.     }
  288.  
  289.     return(0);
  290. }
  291.  
  292. output()
  293. {
  294.     char *fgets();
  295.     extern FILE *fp;
  296.     extern int myuid;
  297.     extern char owner[];
  298.     char buf[BUFSIZ];
  299.  
  300.     if(myuid == 0)
  301.     (void)printf("# owner: %s\n", owner);
  302.  
  303.     while(fgets(buf, BUFSIZ, fp) != NULL)
  304.     (void)fputs(buf, stdout);
  305.     
  306.     (void)fclose(fp);
  307. }
  308. //E*O*F atl.c//
  309.  
  310. echo Possible errors detected by \'wc\' [hopefully none]:
  311. temp=/tmp/shar$$
  312. trap "rm -f $temp; exit" 0 1 2 3 15
  313. cat > $temp <<\!!!
  314.       17      50     298 Makefile
  315.       32     108     573 atl.1
  316.      222     526    4262 atl.c
  317.      271     684    5133 total
  318. !!!
  319. wc  Makefile atl.1 atl.c | sed 's=[^ ]*/==' | diff -b $temp -
  320. exit 0
  321.  
  322.