home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / queuer / part02 / dequeue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.4 KB  |  93 lines

  1. /* Copyright 1990  The President and Fellows of Harvard University
  2.  
  3. Permission to use, copy, modify, and distribute this program for any
  4. purpose and without fee is hereby granted, provided that this
  5. copyright and permission notice appear on all copies and supporting
  6. documentation, the name of Harvard University not be used in advertising
  7. or publicity pertaining to distribution of the program, or to results
  8. derived from its use, without specific prior written permission, and notice
  9. be given in supporting documentation that copying and distribution is by
  10. permission of Harvard University.  Harvard University makes no
  11. representations about the suitability of this software for any purpose.
  12. It is provided "as is" without express or implied warranty.    */
  13.  
  14.  
  15. /* dequeue.c - Dan Lanciani '85 */
  16.  
  17. #include <stdio.h>
  18. #include <signal.h>
  19.  
  20. #include "queue.h"
  21.  
  22. dequeue(s)
  23. {
  24.     char buf[BUFSIZ], what[BUFSIZ], prog[BUFSIZ], user[BUFSIZ];
  25.     register FILE *n, *m;
  26.     register int i, t, k;
  27.     register long c, u;
  28.  
  29.     for(i = 0; i < 3; i++)
  30.         dup2(s, i);
  31.     getstr(s, user);
  32.     getstr(s, prog);
  33.     getstr(s, what);
  34.     if(strcmp(what, "all"))
  35.         c = atol(what);
  36.     else
  37.         c = -1;
  38.     if(isunid(c)) {
  39.         sprintf(buf, "%s/%ld", SPOOLDIR, c);
  40.         if(!(n = fopen(buf, "r")))
  41.             exit(0);
  42.         fgetstr(n, buf);
  43.         fclose(n);
  44.         if(!(c = atoi(buf)))
  45.             exit(0);
  46.     }
  47.     if(i = readconf(prog)) {
  48.         fprintf(stderr, "Unknown remote configuration (%d)\n", i);
  49.         exit(1);
  50.     }
  51.     sprintf(buf, "%s/%s", SPOOLDIR, queue);
  52.     if(!(n = fopen(buf, "r"))) {
  53.         fprintf(stderr, "Queue file missing\n");
  54.         exit(1);
  55.     }
  56.     while(fgets(buf, sizeof(buf), n))
  57.         if(c == (k = atoi(buf)) || !c || c == -1) {
  58.             sprintf(buf, "%s/%d", SPOOLDIR, k);
  59.             if(!(m = fopen(buf, "r"))) {
  60.                 fprintf(stderr, "Spool file missing (%d)\n", k);
  61.                 continue;
  62.             }
  63.             fgetstr(m, buf);
  64.             fgetstr(m, buf);
  65.             u = atol(buf);
  66.             fgetstr(m, buf);
  67.             t = atoi(buf);
  68.             for(i = 0; i < t; i++)
  69.                 fgetstr(m, buf);
  70.             fgetstr(m, buf);
  71.             if(!c && strcmp(buf, what)) {
  72.                 fclose(m);
  73.                 continue;
  74.             }
  75.             if(strcmp(buf, user) && strcmp(user, "root")) {
  76.                 if(u)
  77.                 fprintf(stderr, "Not owner (%s, %ld)\n", buf,u);
  78.                 else
  79.                 fprintf(stderr, "Not owner (%s, %d)\n", buf, k);
  80.                 fclose(m);
  81.                 continue;
  82.             }
  83.             kill(k, SIGTERM);
  84.             if(u)
  85.             fprintf(stderr, "Killed %ld of %s for %s\n",u,buf,user);
  86.             else
  87.             fprintf(stderr, "Killed %d of %s for %s\n", k,buf,user);
  88.             fclose(m);
  89.         }
  90.     fclose(n);
  91.     exit(0);
  92. }
  93.