home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / atrun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-05-07  |  1.9 KB  |  109 lines

  1. /*
  2.  * Run programs submitted by at.
  3.  */
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/dir.h>
  7. #include <time.h>
  8. #include <sys/stat.h>
  9.  
  10. # define DIR "/usr/spool/at"
  11. # define PDIR    "past"
  12. # define LASTF "/usr/spool/at/lasttimedone"
  13.  
  14. int    nowtime;
  15. int    nowdate;
  16. int    nowyear;
  17.  
  18. main(argc, argv)
  19. char **argv;
  20. {
  21.     int tt, day, year, uniq;
  22.     struct direct dirent;
  23.     char file[DIRSIZ+1];
  24.     FILE *dirf;
  25.  
  26.     chdir(DIR);
  27.     makenowtime();
  28.     if ((dirf = fopen(".", "r")) == NULL) {
  29.         fprintf(stderr, "Cannot read at directory\n");
  30.         exit(1);
  31.     }
  32.     while (fread((char *)&dirent, sizeof(dirent), 1, dirf) == 1) {
  33.         if (dirent.d_ino==0)
  34.             continue;
  35.         strncpy(file, dirent.d_name, DIRSIZ);
  36.         file[DIRSIZ] = '\0';
  37.         if (sscanf(file, "%2d.%3d.%4d.%2d", &year, &day, &tt, &uniq) != 4)
  38.             continue;
  39.         if (nowyear < year)
  40.             continue;
  41.         if (nowyear==year && nowdate < day)
  42.             continue;
  43.         if (nowyear==year && nowdate==day && nowtime < tt)
  44.             continue;
  45.         run(file);
  46.     }
  47.     fclose(dirf);
  48.     updatetime(nowtime);
  49.     exit(0);
  50. }
  51.  
  52. makenowtime()
  53. {
  54.     long t;
  55.     struct tm *localtime();
  56.     register struct tm *tp;
  57.  
  58.     time(&t);
  59.     tp = localtime(&t);
  60.     nowtime = tp->tm_hour*100 + tp->tm_min;
  61.     nowdate = tp->tm_yday;
  62.     nowyear = tp->tm_year;
  63. }
  64.  
  65. updatetime(t)
  66. {
  67.     FILE *tfile;
  68.  
  69.     tfile = fopen(LASTF, "w");
  70.     if (tfile == NULL) {
  71.         fprintf(stderr, "can't write lastfile\n");
  72.         exit(1);
  73.     }
  74.     fprintf(tfile, "%04d\n", t);
  75. }
  76.  
  77. run(file)
  78. char *file;
  79. {
  80.     struct stat stbuf;
  81.     register pid, i;
  82.     char sbuf[64];
  83.  
  84.     if (fork()!=0)
  85.         return;
  86.     for (i=0; i<15; i++)
  87.         close(i);
  88.     dup(dup(open("/dev/null", 0)));
  89.     sprintf(sbuf, "/bin/mv %.14s %s", file, PDIR);
  90.     system(sbuf);
  91.     chdir(PDIR);
  92.     if (stat(file, &stbuf) == -1)
  93.         exit(1);
  94.     setgid(stbuf.st_gid);
  95.     setuid(stbuf.st_uid);
  96.     if (pid = fork()) {
  97.         if (pid == -1)
  98.             exit(1);
  99.         wait((int *)0);
  100.         unlink(file);
  101.         exit(0);
  102.     }
  103.     nice(3);
  104.     execl("/bin/sh", "sh", file, 0);
  105.     execl("/usr/bin/sh", "sh", file, 0);
  106.     fprintf(stderr, "Can't execl shell\n");
  107.     exit(1);
  108. }
  109.