home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / util / fname.c < prev    next >
C/C++ Source or Header  |  1995-01-14  |  2KB  |  103 lines

  1. /*
  2.  *  fname.c:  Mess with file names, paths.
  3.  *  C Durland    Public Domain
  4.  *  See also:  fpath.c
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include "os.h"
  9.  
  10. #define DOT   '.'
  11. #define DRIVE ':'        /* for MS-DOS anyway */
  12.  
  13. char *strcpy();
  14.  
  15.     /* Point to the end of the path.
  16.      * The resulting path is such that you can cd to it.
  17.      * Use nanex() if you want to append a new name.
  18.      * To leave just the path:  *pathend(fname) = '\0';
  19.      * Examples: (^ shows what pathend() points to)
  20.      *   "foo/bar", "/foo", "/", "/foo/"
  21.      *       ^        ^       ^       ^
  22.      *   "..", ".", "/..", "foo."
  23.      *      ^    ^      ^   ^
  24.      *   ".foo", "foo", ""
  25.      *    ^       ^      ^
  26.      * MS-DOS:
  27.      *   "A:", "A:foo", "A:/", "A:/foo"
  28.      *      ^     ^         ^      ^
  29.      */
  30. char *pathend(fname) register char *fname;
  31. {
  32.   register char *ptr = fname + strlen(fname);
  33.  
  34.   while (ptr != fname)
  35.   {
  36.     ptr--;
  37.     if (ISSLASH(*ptr))
  38.     {
  39.       if (ptr == fname) ptr++;            /* /, /foo */
  40. #if MSDOZ || ATARI
  41.       else if (ptr[-1] == DRIVE) ptr++;        /* A:/, A:/foo */
  42. #endif
  43.       break;
  44.     }
  45. #if MSDOZ || ATARI
  46.     if (*ptr == DRIVE) { ptr++; break; }    /* A:, A:foo */
  47. #endif
  48.   }
  49.   if (*ptr == DOT)            /* might have . or .. */
  50.   {
  51.     if (ptr[1] == '\0') ptr++;                /* "." */
  52.     else
  53.       if (ptr[1] == DOT && ptr[2] == '\0') ptr += 2;    /* ".." */
  54.   }    
  55.   return ptr;
  56. }
  57.  
  58.     /* Point to the filename & extension.
  59.      * Assumes a real filename & no trailing junk.
  60.      */
  61. char *nanex(fname) register char *fname;
  62. {
  63.   fname = pathend(fname);
  64.   if (ISSLASH(*fname)) fname++;
  65.   return fname;
  66. }
  67.  
  68.     /* Point to start of extension (ie the '.')
  69.      *   The first "." after the last "/".
  70.      * Assumes a real filename.
  71.      * Notes:
  72.      *   foo.c => ".c"
  73.      *   foo => ""
  74.      *   .foo.bar => ".bar" (UNIX hidden files).
  75.      *   .foo => ".foo"  (csh does this).
  76.      *   y.tab.c => ".c".
  77.      *   "" => ""
  78.      *   foo. => "."
  79.      */
  80. char *ext(fname) register char *fname;
  81. {
  82.   char *ptr, *end;
  83.  
  84.   fname = nanex(fname);                /* start of the file name */
  85.   end = ptr = fname + strlen(fname);        /* end of file name ('\0') */
  86.  
  87.   for ( ; (ptr != fname) && (*ptr != DOT); ptr--) ;
  88.  
  89.   if (*ptr != DOT) return end;            /* no extension */
  90.   return ptr;
  91. }
  92.  
  93.     /* for UNIX: .foo => .foo.ext?  (not yet)
  94.      * ..\fred.foo
  95.      *  ext MUST start with a '.'
  96.      */
  97. char *new_ext(newname,nameext,newext) char *newname, *nameext, *newext;
  98. {
  99.   strcpy(ext(strcpy(newname,nameext)),newext);
  100.   return newname;
  101. }
  102.  
  103.