home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2286 / env.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.7 KB  |  103 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #ifndef    BSD
  11. #include <string.h>
  12. #else
  13. #define    strchr    index
  14. #define    strrchr    rindex
  15. #include <strings.h>
  16. #endif
  17.  
  18. #ifndef    lint
  19. static    char    _sccsid[] = "@(#)env.c    2.2    19:23:43    7/29/90";
  20. #endif
  21.  
  22. extern    char    **environ;
  23. extern    char    *newenvp[];
  24. extern    int    newenvc;
  25. extern    int    maxenv;
  26.  
  27. char    *strdup ();
  28. void    free ();
  29.  
  30. static    char    *forbid[] = {
  31.     "HOME",
  32.     "IFS",
  33.     "PATH",
  34.     "SHELL",
  35.     (char *) 0
  36. };
  37.  
  38. void    addenv (entry)
  39. char    *entry;
  40. {
  41.     char    *cp;
  42.     int    i;
  43.     int    len;
  44.  
  45.     if (cp = strchr (entry, '='))
  46.         len = cp - entry;
  47.     else
  48.         return;
  49.  
  50.     for (i = 0;i < newenvc;i++)
  51.         if (strncmp (entry, newenvp[i], len) == 0 &&
  52.             (newenvp[i][len] == '=' || newenvp[i][len] == '\0'))
  53.             break;
  54.  
  55.     if (i == maxenv) {
  56.         puts ("Environment overflow");
  57.         return;
  58.     }
  59.     if (i == newenvc) {
  60.         newenvp[newenvc++] = strdup (entry);
  61.     } else {
  62.         free (newenvp[i]);
  63.         newenvp[i] = strdup (entry);
  64.     }
  65. }
  66.  
  67. void    setenv (argc, argv)
  68. int    argc;
  69. char    **argv;
  70. {
  71.     int    i;
  72.     int    n;
  73.     int    noname = 1;
  74.     char    variable[BUFSIZ];
  75.     char    *cp;
  76.  
  77.     for (i = 0;i < argc;i++) {
  78.         if ((n = strlen (argv[i])) >= BUFSIZ)
  79.             continue;    /* ignore long entries */
  80.  
  81.         if (! (cp = strchr (argv[i], '='))) {
  82.             (void) strcpy (variable, argv[i]);
  83.         } else {
  84.             (void) strncpy (variable, argv[i], cp - argv[i]);
  85.             variable[cp - argv[i]] = '\0';
  86.         }
  87.         for (n = 0;forbid[n] != (char *) 0;n++)
  88.             if (strcmp (variable, forbid[n]) == 0)
  89.                 break;
  90.  
  91.         if (forbid[n] != (char *) 0) {
  92.             printf ("You may not change $%s\n", forbid[n]);
  93.             continue;
  94.         }
  95.         if (cp) {
  96.             addenv (argv[i]);
  97.         } else {
  98.             sprintf (variable, "L%d=%s", noname++, argv[i]);
  99.             addenv (variable);
  100.         }
  101.     }
  102. }
  103.