home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / path.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  4KB  |  104 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*  path  --  break filename into directory and file
  29.  *
  30.  *  path (filename,direc,file);
  31.  *  char *filename,*direc,*file;
  32.  *  filename is input; direc and file are output (user-supplied).
  33.  *  file will not have any trailing /; direc might.
  34.  *
  35.  *  Note these rules:
  36.  *  1.  trailing / are ignored (except as first character)
  37.  *  2.  x/y is x;y where y contains no / (x may contain /)
  38.  *  3.  /y  is /;y where y contains no /
  39.  *  4.  y   is .;y where y contains no /
  40.  *  5.      is .;. (null filename)
  41.  *  6.  /   is /;. (the root directory)
  42.  *
  43.  * Algorithm is this:
  44.  *  1.  delete trailing / except in first position
  45.  *  2.  if any /, find last one; change to null; y++
  46.  *      else y = x;        (x is direc; y is file)
  47.  *  3.  if y is null, y = .
  48.  *  4.  if x equals y, x = .
  49.  *      else if x is null, x = /
  50.  *
  51.  *  HISTORY
  52.  * $Log:    path.c,v $
  53.  * Revision 1.2  90/12/11  17:57:35  mja
  54.  *     Add copyright/disclaimer for distribution.
  55.  * 
  56.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  57.  *    Copied verbatim from PDP-11.  Still as messy as ever.
  58.  *    Some people have asked for a modification (I think that's a better
  59.  *    idea than a new routine) which will change the directory name
  60.  *    into an absolute pathname if it isn't one already.  The change
  61.  *    involves doing a getwd() and prepending that if appropriate, with
  62.  *    a "/" in between that and the directory part of the path.
  63.  *    If you want to be cute, you can also resolve ".."s at that time.
  64.  *
  65.  */
  66.  
  67. path (original,direc,file)
  68. char *original,*direc,*file;
  69. {
  70.     register char *y;
  71.     /* x is direc */
  72.     register char *p;
  73.  
  74.     /* copy and note the end */
  75.     p = original;
  76.     y = direc;
  77.     while (*y++ = *p++) ;        /* copy string */
  78.     /* y now points to first char after null */
  79.     --y;    /* y now points to null */
  80.     --y;    /* y now points to last char of string before null */
  81.  
  82.     /* chop off trailing / except as first character */
  83.     while (y>direc && *y == '/') --y;    /* backpedal past / */
  84.     /* y now points to char before first trailing / or null */
  85.     *(++y) = 0;                /* chop off end of string */
  86.     /* y now points to null */
  87.  
  88.     /* find last /, if any.  If found, change to null and bump y */
  89.     while (y>direc && *y != '/') --y;
  90.     /* y now points to / or direc.  Note *direc may be / */
  91.     if (*y == '/') {
  92.         *y++ = 0;
  93.     }
  94.  
  95.     /* find file name part */
  96.     if (*y)  strcpy (file,y);
  97.     else     strcpy (file,".");
  98.  
  99.     /* find directory part */
  100.     if (direc == y)        strcpy (direc,".");
  101.     else if (*direc == 0)  strcpy (direc,"/");
  102.     /* else direc already has proper value */
  103. }
  104.