home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / FORTH / FLEX.ARC / MSDOS.C < prev    next >
Text File  |  1988-10-10  |  4KB  |  185 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7.  
  8. #define PUBLIC
  9. #define PRIVATE     static
  10.  
  11.  
  12. PUBLIC convert_path(path)
  13. char    *path;
  14. {
  15.     for ( ; *path; ++path) {
  16.         if (*path == '\\') {
  17.             *path = '/';
  18.         }
  19.     }
  20. }
  21.  
  22.  
  23. PUBLIC char *drop_drive(path)
  24. char    path[];
  25. {
  26.     char        *src, *dest;
  27.  
  28.     if ((path[0] != '\0') && (path[1] == ':')) {
  29.         for (dest = path, src = &path[2]; *src; ) {
  30.             *dest++ = *src++;
  31.         }
  32.     }
  33.     return (path);
  34. }
  35.  
  36.  
  37. /*****************************************************************************
  38. *
  39. *    drop_extension    -- Drop the extension off a path and filename
  40. *        This may not always work well with UN*X names.
  41. *
  42. *****************************************************************************/
  43.  
  44. PUBLIC drop_extenstion(pathname)
  45. char    *pathname;
  46. {
  47.     int    len;
  48.     char    *s;
  49.     char    *lastdot;
  50.  
  51.     if ((len = strlen(pathname)) == 0)
  52.         return;
  53.     if (!strcmp(pathname, ".") || !strcmp(pathname, ".."))
  54.         return;
  55.     for (s = pathname + len - 1, lastdot = NULL; s >= pathname; --s) {
  56.         if ((*s == '\\') || (*s == '/') || (*s == ':'))
  57.             break;
  58.         if (*s == '.')
  59.             lastdot = s;
  60.     }
  61.     if (s < pathname) {
  62.         s = pathname;
  63.     } else {
  64.         ++s;            /* move past slash or colon */
  65.     }
  66.     if (lastdot != NULL && lastdot > pathname) {
  67.         *lastdot = '\0';        /* drop extension */
  68.         if ((lastdot - s) > 8) {
  69.             s[8] = '\0';
  70.         }
  71.     } else {
  72.         if (((pathname + len) - s) > 8) {
  73.             s[8] = '\0';
  74.         }
  75.     }
  76. }
  77.  
  78.  
  79. PUBLIC char *drop_path(pathname)
  80. char    *pathname;
  81. {
  82.     int    len;
  83.     char    *path;
  84.  
  85.     if ((len = strlen(pathname)) > 0) {
  86.         for (path = pathname + len - 1; path > pathname; --path) {
  87.             if (*path == '\\' || *path == '/')
  88.                 break;
  89.         }
  90.     } else {
  91.         path = pathname;
  92.     }
  93.     return (path);
  94. }
  95.  
  96.  
  97. /******************************************************************************
  98. ******************************************************************************/
  99.  
  100.  
  101. #define MAX_FILENAME    20
  102. #define MAX_PATHSIZE    80
  103. #define ENVIRON_SIZE    1000
  104.  
  105.  
  106. PUBLIC FILE *localfopen(filename, mode)
  107. char    *filename, *mode;
  108. {
  109.     return (fopen(drop_path(filename), mode));
  110. }
  111.  
  112.  
  113. PUBLIC FILE *variable_fopen(env_var, filename, mode)
  114. char    *env_var;
  115. char    *filename, *mode;
  116. {
  117.     FILE    *fp;
  118.     char    *env, environ[ENVIRON_SIZE];
  119.     char    name[MAX_FILENAME];
  120.     char    pathname[MAX_PATHSIZE];
  121.     char    *dir;
  122.  
  123.     if (env_var == (char *) NULL) {
  124.         return ((FILE *) NULL);
  125.     }
  126.     if (*env_var == '\0') {
  127.         return ((FILE *) NULL);
  128.     }
  129.     if ((env = getenv(env_var)) != NULL) {
  130.         strncpy(environ, env, sizeof(environ) - 1);
  131.         environ[sizeof(environ) - 1] = '\0';
  132.         strncpy(name, drop_path(filename),
  133.             sizeof(name) - 1);
  134.         name[sizeof(name) - 1] = '\0';
  135.         for (dir = strtok(environ, ";"); dir != NULL;
  136.              dir = strtok((char *) NULL, ";")) {
  137.             strncpy(pathname, dir, sizeof(pathname) - 1);
  138.             pathname[sizeof(pathname) - 1] = '\0';
  139.             strncat(pathname, "/", sizeof(pathname) - 1
  140.                 - strlen(pathname));
  141.             pathname[sizeof(pathname) - 1] = '\0';
  142.             strncat(pathname, name, sizeof(pathname) - 1
  143.                 - strlen(pathname));
  144.             pathname[sizeof(pathname) - 1] = '\0';
  145.             if ((fp = fopen(pathname, mode)) != (FILE *) NULL) {
  146.                 return (fp);
  147.             }
  148.         }
  149.     }
  150.     return ((FILE *) NULL);
  151. }
  152.  
  153.  
  154. FILE *pathfopen(env_var, search_local, filename, mode)
  155. char    *env_var;
  156. int    search_local;
  157. char    *filename, *mode;
  158. {
  159.     FILE    *fp;
  160.  
  161.     if (tolower(*mode) != 'r') {
  162.         return (fopen(filename, mode));
  163.     }
  164.     if (search_local) {
  165.         if ((fp = localfopen(filename, mode)) != NULL) {
  166.             return (fp);
  167.         }
  168.     }
  169.     if ((fp = fopen(filename, mode)) != NULL) {
  170.         return (fp);
  171.     }
  172.     if (!search_local) {
  173.         if ((fp = localfopen(filename, mode)) != NULL) {
  174.             return (fp);
  175.         }
  176.     }
  177.     if ((fp = variable_fopen(env_var, filename, mode)) != NULL) {
  178.         return (fp);
  179.     }
  180.     if ((fp = variable_fopen("PATH", filename, mode)) != NULL) {
  181.         return (fp);
  182.     }
  183.     return ((FILE *) NULL);
  184. }
  185.