home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part13 / env.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  2KB  |  119 lines

  1. /*
  2.  * Copyright 1989, 1990, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  *
  11.  * This software is provided on an AS-IS basis and the author makes
  12.  * no warrantee of any kind.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #ifndef    BSD
  17. #include <string.h>
  18. #else
  19. #define    strchr    index
  20. #define    strrchr    rindex
  21. #include <strings.h>
  22. #endif
  23.  
  24. #ifndef    lint
  25. static    char    _sccsid[] = "@(#)env.c    3.1    13:00:03    27 Jul 1992";
  26. #endif
  27.  
  28. extern    char    **environ;
  29. extern    char    *newenvp[];
  30. extern    int    newenvc;
  31. extern    int    maxenv;
  32.  
  33. char    *strdup ();
  34. void    free ();
  35.  
  36. static    char    *forbid[] = {
  37.     "HOME",
  38.     "IFS",
  39.     "PATH",
  40.     "SHELL",
  41.     (char *) 0
  42. };
  43.  
  44. /*
  45.  * addenv - add a new environmental entry
  46.  */
  47.  
  48. void
  49. addenv (entry)
  50. char    *entry;
  51. {
  52.     char    *cp;
  53.     int    i;
  54.     int    len;
  55.  
  56.     if (cp = strchr (entry, '='))
  57.         len = cp - entry;
  58.     else
  59.         return;
  60.  
  61.     for (i = 0;i < newenvc;i++)
  62.         if (strncmp (entry, newenvp[i], len) == 0 &&
  63.             (newenvp[i][len] == '=' || newenvp[i][len] == '\0'))
  64.             break;
  65.  
  66.     if (i == maxenv) {
  67.         puts ("Environment overflow");
  68.         return;
  69.     }
  70.     if (i == newenvc) {
  71.         newenvp[newenvc++] = strdup (entry);
  72.     } else {
  73.         free (newenvp[i]);
  74.         newenvp[i] = strdup (entry);
  75.     }
  76. }
  77.  
  78. /*
  79.  * setenv - copy command line arguments into the environment
  80.  */
  81.  
  82. void
  83. setenv (argc, argv)
  84. int    argc;
  85. char    **argv;
  86. {
  87.     int    i;
  88.     int    n;
  89.     int    noname = 1;
  90.     char    variable[BUFSIZ];
  91.     char    *cp;
  92.  
  93.     for (i = 0;i < argc;i++) {
  94.         if ((n = strlen (argv[i])) >= BUFSIZ)
  95.             continue;    /* ignore long entries */
  96.  
  97.         if (! (cp = strchr (argv[i], '='))) {
  98.             (void) strcpy (variable, argv[i]);
  99.         } else {
  100.             (void) strncpy (variable, argv[i], cp - argv[i]);
  101.             variable[cp - argv[i]] = '\0';
  102.         }
  103.         for (n = 0;forbid[n] != (char *) 0;n++)
  104.             if (strcmp (variable, forbid[n]) == 0)
  105.                 break;
  106.  
  107.         if (forbid[n] != (char *) 0) {
  108.             printf ("You may not change $%s\n", forbid[n]);
  109.             continue;
  110.         }
  111.         if (cp) {
  112.             addenv (argv[i]);
  113.         } else {
  114.             sprintf (variable, "L%d=%s", noname++, argv[i]);
  115.             addenv (variable);
  116.         }
  117.     }
  118. }
  119.