home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ADDPATH.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  99 lines

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