home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / GET_IT.ZIP / GET.C next >
Encoding:
C/C++ Source or Header  |  1988-02-09  |  5.4 KB  |  169 lines

  1. /* GET.C - From page 20 of Micro Cornucopia, No. 40,            */
  2. /* March/April 1988.                                            */
  3. /****************************************************************/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>     /* Has prototypes for "un-headered" functs */
  7. #include <string.h>     /* Need for string functions */
  8.  
  9. #define DATAFILE "get.dat" /* Name of directory-info file */
  10. #define MAXBUFF 256
  11. #define MATCH 0
  12. #define FAIL 1
  13.  
  14. char buff[MAXBUFF],     /* Input buffer for file reads  */
  15.      path[MAXBUFF],     /* Buffer for full path name    */
  16.      drive[3];          /* Buffer for disk drive        */
  17.    /*** Prototypes for non-standard functions ***/
  18. int search_file(FILE *fp, char *where);
  19. int do_switch(void);
  20. FILE *open_data_file(char *name);
  21.  
  22. int main(int argc, char **argv)
  23. {
  24. int flag;
  25. FILE *fpin;
  26.  
  27.    if (argc != 2) {
  28.       fprintf(stderr, "Usage: GET directory_abbreviation_name");
  29.       exit(0);
  30.    }
  31.    fpin = open_data_file(DATAFILE);    /* Open the data file */
  32.    flag = search_file(fpin, argv[1]);  /* Find the pseudo-name */
  33.    fclose(fpin);                       /* We're done reading */
  34.  
  35.    if (flag == FAIL) {
  36.       fprintf(stderr, "\nCannot find %s in data file.\n", argv[1]);
  37.       exit(0);
  38.    }
  39.  
  40.    flag = do_switch();           /* Try the change */
  41.    if (flag != MATCH) {
  42.       fprintf(stderr, "\nCannot find directory %s\n", path);
  43.       exit(0);
  44.    }
  45. }
  46.  
  47. /*
  48.    do_switch()
  49.    Function to do the work.
  50.    Argument list:    void
  51.    Return value:     int      0 if successful, 1 on error
  52. */
  53.  
  54. int do_switch(void)
  55. {
  56. int flag;
  57. int res;
  58. res = system(drive);       /* Function to issue command to command.com */
  59. if(res < 0)       /* and places us on the correct drive.      */
  60.    perror("System call error");
  61. flag = chdir(path);  /* We should be there now.                  */
  62. if(flag < 0)
  63.    perror("Change directory error");
  64. system("DIR");
  65. return flag;
  66. }
  67.  
  68. /*
  69.    search_file()
  70.    Function reads the data file that holds the information about the
  71.    directory locations, lookeng for a match on the string passed in.
  72.    The format for the input file is:
  73.  
  74.    abbreviation  drive   full_pathname\n
  75.  
  76.    where:
  77.       abbreviation  --  the abbreviation for the full path name
  78.              drive  --  the disk drive designator for the search
  79.      full_pathname  --  the full MSDOS pathname desired
  80.                 \n  --  a newline terminates the data for each
  81.                         possible directory
  82.    Example:
  83.  
  84.    tut   c  \lessons\prog\tutorial
  85.  
  86.    When the user types: go tut, the user is moved to the \lessons\prog\
  87.                         \tutorial directory on drive C:
  88.  
  89.    Argument list:    FILE *fp       a FILE pointer to the open file.
  90.                      char *where    a string constant that is the short
  91.                                     name for the directory desired.
  92.    Return value:     int            0 if successful, 1 on no match
  93. */
  94.  
  95. int search_file(FILE *fp, char *where)
  96. {
  97. char *ptr, temp[MAXBUFF];
  98. int c, i;
  99.  
  100.    i = 0;
  101.    while ((c = fgetc(fp)) != EOF) {
  102.       if (c != '\n') {
  103.          buff[i++] = (char) c;
  104.          continue;
  105.       }
  106.       buff[i] = '\0';
  107.       strcpy(temp, buff);
  108.       ptr = strtok(temp, " ");
  109.       if (strcmp(ptr, where) == MATCH) {
  110.          ptr = strtok((char *) 0, " ");      /* Get the disk drive */
  111.          strcpy(drive, ptr);
  112. /*       strcat(drive, ":");   */ /* Removed from book version-doesn't work*/
  113.          ptr = strtok((char *) 0, " ");      /* Get full path name */
  114.          strcpy(path, ptr);
  115.          return MATCH;                    /* ... and we're done */
  116.       }
  117.       i = 0;                  /* No match, start over */
  118.    }
  119.    return FAIL;
  120. }
  121.  
  122. /*
  123.          open_data_file()
  124.    Function attempts to open the data file that holds the information
  125.    about the directory locations. It does this by searching the PATH
  126.    environment variable. The program assumes that the dat file is in
  127.    the PATH directory.
  128.  
  129.    Argument list:    char *name     a string constant that is the name
  130.                                     of the file containing the directory
  131.                                     information.
  132.    Return value:     FILE *fp       a FILE pointer to the open file if
  133.                                     successful, program aborts on error.
  134. */
  135.  
  136. FILE *open_data_file(char *name)
  137. {
  138. char *ptr, p[MAXBUFF], temp[MAXBUFF];
  139. FILE *fpin;
  140.  
  141.    ptr = getenv("PATH");            /* Find out where go.dat is.        */
  142.                                     /* NOTE: PATH must be in caps       */
  143.    if (ptr == NULL) {
  144.       fprintf(stderr, "\nPATH not set. Program and data file must \
  145. be on PATH.\n");
  146.       exit(0);
  147.    }
  148.    strcpy(p, ptr);
  149.    ptr = strtok(p, ";");            /* Find first PATH                  */
  150.    while (ptr != NULL) {
  151.       strcpy(temp, ptr);            /* Save a copy of substring         */
  152. /*    strcat(temp, "\\");  */ /* Form a path and file name-doesn't work*/
  153.       strcat(temp, name);
  154.       if ((fpin = fopen(temp, "r")) == NULL) {
  155.          temp[0] = '\0';            /* Start over again         */
  156.          ptr = strtok((char *) 0, ";");   /* Try next path      */
  157.       }
  158.       else
  159.          break;               /* Must have a good fpin          */
  160.    }
  161.    if (fpin == NULL) {
  162.       fprintf(stderr, "\nCannot find %s on default path(s).\n", name);
  163.       exit(0);
  164.    }
  165.    else
  166.       return fpin;
  167. }
  168.  
  169.