home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / CDPATH12.ZIP / CDPATH.C next >
C/C++ Source or Header  |  1992-09-01  |  3KB  |  141 lines

  1. /* File cdpath.c
  2. **
  3. ** Program to Generate CDPATH environmental Variable
  4. **
  5. ** Usage: CDPATH <filename> <drivelist>
  6. **    where <filename> is name of file to write Command to
  7. **          <drivelist> is list of drives to search
  8. */
  9. #include <stdio.h>
  10. #include <dos.h>
  11. #include <string.h>
  12.  
  13. #define  MAX_DIR 68
  14. int npath = 0;        /* No. of path specs in CDPATH */
  15. FILE *fp;
  16.  
  17. char tmppath[100][MAX_DIR];
  18.  
  19. /* ################### ORDERPATH ############################# */
  20. void orderpath(void)
  21. {
  22.     int i,    cdpathno = 0;    /* Level of CDPATH# variable */
  23.     static char cdpath[256];
  24.     
  25.     qsort(tmppath[0], npath, sizeof(char) * MAX_DIR, strcmp);
  26.  
  27.     sprintf(cdpath, "set cdpath=%s",  tmppath[0]);
  28.     
  29.     for (i = 1; i < npath; i++)
  30.     {
  31.         if (strlen(cdpath) + strlen(tmppath[i]) > 254)
  32.         {
  33.             fprintf(fp, "\n%s\n", cdpath);
  34.             printf("\n%s\n", cdpath);
  35.             cdpathno++;
  36.             sprintf(cdpath, "set cdpath%d=%s", cdpathno, tmppath[i]);
  37.         }
  38.         else
  39.         {
  40.             strcat(cdpath, ";");
  41.             strcat(cdpath, tmppath[i]);
  42.         }
  43.     }
  44.     fprintf(fp, "\n%s\n", cdpath);
  45.     printf("\n%s\n", cdpath);
  46.     return;
  47. }
  48.     
  49. /* ####################### PATHFIND ######################### */
  50. int pathfind(char path[])
  51. {
  52.     /* Function recursively searches through drive to find all directories
  53.     ** that have subdirectories
  54.     ** path: starting directory path
  55.     */
  56.     struct find_t find;
  57.     char test[MAX_DIR];
  58.     char *ptr;
  59.     int root;
  60.  
  61.     root = 0;
  62.     strcpy(test, path);
  63.     ptr = strchr(test, '\0');
  64.     if (*(ptr - 1) == '\\')
  65.         strcat(test, "*.*");
  66.     else
  67.     {
  68.         strcat(test, "\\*.*");
  69.         ptr++;
  70.     }
  71.  
  72.     if (_dos_findfirst(test, 0xffff, &find) == 0)
  73.     {
  74.         if ((find.attrib & _A_SUBDIR) && (find.name[0] != '.'))
  75.         {
  76.             strcpy(ptr, find.name);
  77.             pathfind(test);
  78.             *ptr = '\0';
  79.             if (root == 0)
  80.             {
  81.                 strlwr(path);
  82.                 strcpy(tmppath[npath++], path);
  83.                 root = 1;
  84.             }
  85.         }
  86.         while (_dos_findnext(&find) == 0)
  87.         {
  88.             if ((find.attrib & _A_SUBDIR) && (find.name[0] != '.'))
  89.             {
  90.                 strcpy(ptr, find.name);
  91.                 pathfind(test);
  92.                 *ptr = '\0';
  93.                 if (root == 0)
  94.                 {
  95.                     strlwr(path);
  96.                     strcpy(tmppath[npath++], path);
  97.                     root = 1;
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     return root;
  103. }
  104.  
  105. /* ########################## CDPATH ############################## */
  106. int main(int argc, char *argv[])
  107. {
  108.     char path[MAX_DIR];
  109.     int i;
  110.     
  111.     if ((argc < 3) || (strchr(argv[1], '?') != (char *)NULL))
  112.     {
  113.         printf("\nUsage:   CDPATH  <filename> <drive> [<drive> ...]\n\n");
  114.         printf("         <filename> is name of file to write command to\n");
  115.         printf("         <drive> is letter of drive to serach\n\n");
  116.         return 0;
  117.     }
  118.  
  119.     if ((fp = fopen(argv[1], "a")) == (FILE *)NULL)
  120.     {
  121.         printf("\nUnable to open file %s!\007", argv[1]);
  122.         return 0;
  123.     }
  124.  
  125.     for (i = 2; i < argc; i++)
  126.     {
  127.         path[0] = argv[i][0];
  128.         path[1] = ':';
  129.         path[2] = '\\';
  130.         path[3] = '\0';
  131.         if (pathfind(path) == 0)
  132.         {
  133.             if (npath > 0)
  134.                 strcpy(tmppath[npath++], path);
  135.         }
  136.     }
  137.     orderpath();
  138.     return 0;
  139. }
  140.  
  141.