home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / queuer / part02 / defer.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  4KB  |  154 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. /* defer.c - Dan Lanciani '85 */
  16.  
  17. #include <sys/param.h>
  18. #include <pwd.h>
  19. #include <grp.h>
  20. #include <stdio.h>
  21.  
  22. char *getenv(), *index(), *rindex(), *malloc();
  23. FILE *popen();
  24. struct passwd *getpwuid();
  25. struct group *getgrgid();
  26.  
  27. main(argc, argv)
  28. char **argv;
  29. {
  30.     register int i, ng;
  31.     gid_t groups[NGROUPS];
  32.     char buf[BUFSIZ], buf1[BUFSIZ], user[30];
  33.     register char *p;
  34.     FILE *n, *popen();
  35.     register struct passwd *pw;
  36.     register struct group *gr;
  37.  
  38.     if((i = getgid()) != getegid()) {
  39.         if(!(gr = getgrgid(i))) {
  40.             fprintf(stderr, "What group are you?\n");
  41.             exit(1);
  42.         }
  43.         setenv("GROUP=", gr->gr_name);
  44.     }
  45.     ng = getgroups(NGROUPS, groups);
  46.     setgid(getegid());
  47.     setgroups(ng, groups);
  48.     setuid(i = getuid());
  49.     if(!(pw = getpwuid(i))) {
  50.         fprintf(stderr, "Who are you?\n");
  51.         exit(1);
  52.     }
  53.     strcpy(user, pw->pw_name);
  54.     strcat(user, "\t");
  55.     ng = strlen(user);
  56.     if(p = rindex(argv[0], '/'))
  57.         argv[0] = p + 1;
  58.     for(i = 1; i < argc && argv[i][0] == '-'; i++)
  59.         setenv("QNOTIFY=", &argv[i][1]);
  60.     if(argc == i) {
  61.         printf("Enter command:\n");
  62.         gets(buf);
  63.     }
  64.     else {
  65.         buf[0] = '\0';
  66.         for(; i < argc; i++) {
  67.             strcat(buf, argv[i]);
  68.             if(i < argc - 1)
  69.                 strcat(buf, " ");
  70.         }
  71.     }
  72.     if(strcmp(argv[0], "defer.long")) {
  73.         if(n = popen("exec /usr/local/bin/qs bsh", "r")) {
  74.             i = 0;
  75.             while(fgets(p = buf1, sizeof(buf1), n)) {    
  76.                 if(!(p = index(p, '\t')))
  77.                     continue;
  78.                 if(!(p = index(p + 1, '\t')))
  79.                     continue;
  80.                 if(!strncmp(p + 1, user, ng))
  81.                     i++;
  82.             }
  83.             pclose(n);
  84.             if(i > 2) {
  85.                 fprintf(stderr, "You have 3 queued already\n");
  86.                 exit(1);
  87.             }
  88.         }
  89.         if((p = getenv("SHELL")) && !strcmp(p, "/bin/csh"))
  90.             execl("/usr/queued_pgms/bcsh", "bcsh", "-c", buf, 0);
  91.         else
  92.             execl("/usr/queued_pgms/bsh", "bsh", "-c", buf, 0);
  93.     }
  94.     else {
  95.         if(n = popen("exec /usr/local/bin/qs bsh.long", "r")) {
  96.             i = 0;
  97.             while(fgets(p = buf1, sizeof(buf1), n)) {    
  98.                 if(!(p = index(p, '\t')))
  99.                     continue;
  100.                 if(!(p = index(p + 1, '\t')))
  101.                     continue;
  102.                 if(!strncmp(p + 1, user, ng))
  103.                     i++;
  104.             }
  105.             pclose(n);
  106.             if(i > 2) {
  107.                 fprintf(stderr, "You have 3 queued already\n");
  108.                 exit(1);
  109.             }
  110.         }
  111.         if((p = getenv("SHELL")) && !strcmp(p, "/bin/csh"))
  112.             execl("/usr/queued_pgms/bcsh","bcsh.long","-c", buf, 0);
  113.         else
  114.             execl("/usr/queued_pgms/bsh", "bsh.long", "-c", buf, 0);
  115.     }
  116.     fprintf(stderr, "Can't find queuer?!?\n");
  117.     exit(1);
  118. }
  119.  
  120. setenv(var, value)
  121. char *var, *value;
  122. {
  123.     extern char **environ;
  124.     char **envnew;
  125.     int i;
  126.     int varlen = strlen(var);
  127.     int vallen = strlen(value);
  128.     static first = 1;
  129.  
  130.     if(first) {
  131.         first = 0;
  132.         i = 0;
  133.         while(environ[i])
  134.             i++;
  135.         envnew = (char **) malloc(sizeof (char *) * (i + 1));
  136.         for(; i >= 0; i--)
  137.             envnew[i] = environ[i];
  138.         environ = envnew;
  139.     }
  140.     for(i = 0; environ[i]; i++) {
  141.         if (strncmp(environ[i], var, varlen) == 0) {
  142.             environ[i] = malloc(varlen + vallen + 1);
  143.             strcpy(environ[i], var);
  144.             strcat(environ[i], value);
  145.             return;
  146.         }
  147.     }
  148.     environ = (char **) realloc(environ, sizeof (char *) * (i + 2));
  149.     environ[i] = malloc(varlen + vallen + 1);
  150.     strcpy(environ[i], var);
  151.     strcat(environ[i], value);
  152.     environ[++i] = 0;
  153. }
  154.