home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / ADDPATH.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  3KB  |  102 lines

  1. /*
  2. **  Append a new directory to the AUTOEXEC.BAT path
  3. **
  4. **  public domain by Bob Stout
  5. **  also uses TRIM.C from SNIPPETS
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. char *trim(char *);
  12.  
  13. #define TAG_1 "set path="
  14. #define TAG_2 "path="
  15.  
  16. typedef enum {ERROR = -1, SUCCESS} LOGICAL;
  17.  
  18. #define NUL '\0'
  19. #define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
  20.  
  21. #ifdef TESTDIR
  22.  #define ROOT ""        /* While testing, do things in the current dir  */
  23. #else
  24.  #define ROOT "\\"      /* Otherwise, look for AUTOEXEC.BAT in the root */
  25. #endif
  26.  
  27. LOGICAL addpath(char *newdir)
  28. {
  29.       FILE *autoexec, *tmp;
  30.       char fname[FILENAME_MAX], bakname[FILENAME_MAX];
  31.       char tfname[L_tmpnam], tbakname[L_tmpnam];
  32.       char *ptr;
  33.  
  34.       strcat(strcpy(fname, ROOT), "autoexec.bat");
  35.       tmpnam(tfname);
  36.       tmpnam(tbakname);
  37.  
  38.       strcpy(bakname, fname);
  39.       if (NULL != (ptr = strrchr(bakname, '.')))
  40.       {
  41.             if (NULL == strchr(ptr, '\\') && NULL == strchr(ptr, '/'))
  42.                   *ptr = NUL;
  43.       }
  44.       strcat(bakname, ".bak");
  45.  
  46.       rename(bakname, tbakname);
  47.       rename(fname, bakname);
  48.  
  49.       if (NULL == (autoexec = fopen(bakname, "r")))
  50.       {
  51.             if (NULL == (autoexec = fopen(fname, "w")))
  52.                   return ERROR;
  53.             fprintf(autoexec, "SET PATH=%s\n", newdir);
  54.             fclose(autoexec);
  55.             remove(tbakname);
  56.             return SUCCESS;
  57.       }
  58.       if (NULL == (tmp = fopen(tfname, "w")))
  59.       {
  60.             fclose(autoexec);
  61.             rename(bakname, fname);
  62.             rename(tbakname, bakname);
  63.             return ERROR;
  64.       }
  65.       else  remove(tbakname);
  66.  
  67.       while (!feof(autoexec))
  68.       {
  69.             char rline[256 + FILENAME_MAX];
  70.             char tline[256 + FILENAME_MAX];
  71.  
  72.             if (fgets(rline, 256, autoexec))
  73.             {
  74.                   trim(strcpy(tline, rline));
  75.                   if ((SUCCESS == strnicmp(tline, TAG_1, strlen(TAG_1))) ||
  76.                         (SUCCESS == strnicmp(tline, TAG_2, strlen(TAG_2))))
  77.                   {
  78.                         if ('\n' == LAST_CHAR(rline))
  79.                               LAST_CHAR(rline) = NUL;
  80.                         strcat(rline, (';' == LAST_CHAR(rline) ? "" : ";"));
  81.                         strcat(strcat(rline, newdir), "\n");
  82.                   }
  83.                   fputs(rline, tmp);
  84.             }
  85.       }
  86.  
  87.       fclose(autoexec);
  88.       fclose(tmp);
  89.  
  90.       rename(tfname, fname);
  91.       return SUCCESS;
  92. }
  93.  
  94. #ifdef TEST
  95.  
  96. main()
  97. {
  98.       printf("addpath(mydir) returned %d\n", addpath("mydir"));
  99. }
  100.  
  101. #endif
  102.