home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / SNIP9404.ZIP / PSPLIT.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  3KB  |  111 lines

  1. /*
  2. **  psplit() - Portable replacement for fnsplit(), _splitpath(), etc.
  3. **
  4. **  Splits a full DOS pathname into drive, path, file, and extension
  5. **  specifications. Works with forward or back slash path separators and
  6. **  network file names, e.g. NET:LOONEY/BIN\WUMPUS.COM, Z:\MYDIR.NEW/NAME.EXT
  7. **
  8. **  Arguments: 1 - Full pathname to split
  9. **             2 - Buffer for drive
  10. **             3 - Buffer for path
  11. **             4 - Buffer for name
  12. **             5 - Buffer for extension
  13. **
  14. **  Returns: Nothing
  15. **
  16. **  public domain by Bob Stout
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define NUL '\0'
  23.  
  24. void psplit(char *path, char *drv, char *dir, char *fname, char *ext)
  25. {
  26.       char ch, *ptr, *p;
  27.  
  28.       /* convert slashes to backslashes for searching       */
  29.  
  30.       for (ptr = path; *ptr; ++ptr)
  31.       {
  32.             if ('/' == *ptr)
  33.                   *ptr = '\\';
  34.       }
  35.  
  36.       /* look for drive spec                                */
  37.  
  38.       if (NULL != (ptr = strchr(path, ':')))
  39.       {
  40.             ++ptr;
  41.             if (drv)
  42.             {
  43.                   strncpy(drv, path, ptr - path);
  44.                   drv[ptr - path] = NUL;
  45.             }
  46.             path = ptr;
  47.       }
  48.       else if (drv)
  49.             *drv = NUL;
  50.       
  51.       /* find rightmost backslash or leftmost colon         */
  52.  
  53.       if (NULL == (ptr = strrchr(path, '\\')))
  54.             ptr = (strchr(path, ':'));
  55.  
  56.       if (!ptr)
  57.       {
  58.             ptr = path;             /* obviously, no path   */
  59.             if (dir)
  60.                   *dir = NUL;
  61.       }
  62.       else
  63.       {
  64.             ++ptr;                  /* skip the delimiter   */
  65.             if (dir)
  66.             {
  67.                   ch = *ptr;
  68.                   *ptr = NUL;
  69.                   strcpy(dir, path);
  70.                   *ptr = ch;
  71.             }
  72.       }
  73.  
  74.       if (NULL == (p = strrchr(ptr, '.')))
  75.       {
  76.             if (fname)
  77.                   strcpy(fname, ptr);
  78.             if (ext)
  79.                   *ext = NUL;
  80.       }
  81.       else
  82.       {
  83.             *p = NUL;
  84.             if (fname)
  85.                   strcpy(fname, ptr);
  86.             *p = '.';
  87.             if (ext)
  88.                   strcpy(ext, p);
  89.       }
  90. }
  91.  
  92. #ifdef TEST
  93.  
  94. #include <stdio.h>
  95.  
  96. int main(int argc, char *argv[])
  97. {
  98.       char drive[10], pathname[FILENAME_MAX], fname[9], ext[5];
  99.  
  100.       while (--argc)
  101.       {
  102.             psplit(*++argv, drive, pathname, fname, ext);
  103.             printf("psplit(%s) returns:\n drive = %s\n path  = %s\n"
  104.                   " name  = %s\n ext   = %s\n",
  105.                   *argv, drive, pathname, fname, ext);
  106.       }
  107.       return EXIT_SUCCESS;
  108. }
  109.  
  110. #endif
  111.