home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / win3 / nt / source.exe / POSIX / CP / PATH.C < prev    next >
C/C++ Source or Header  |  1992-10-25  |  5KB  |  169 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)path.c    5.1 (Berkeley) 4/3/91";
  36. #endif /* not lint */
  37. #ifdef DF_POSIX /* DF_DSC */
  38. #include <misc.h>
  39. #include <bsdlib.h>
  40. #else
  41. #include <sys/param.h>
  42. #endif
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include "cp.h"
  46.  
  47. /*
  48.  * These functions manipulate paths in PATH_T structures.
  49.  * 
  50.  * They eliminate multiple slashes in paths when they notice them,
  51.  * and keep the path non-slash terminated.
  52.  *
  53.  * Both path_set() and path_append() return 0 if the requested name
  54.  * would be too long.
  55.  */
  56.  
  57. #define    STRIP_TRAILING_SLASH(p) { \
  58.     while ((p)->p_end > (p)->p_path && (p)->p_end[-1] == '/') \
  59.         *--(p)->p_end = 0; \
  60. }
  61.  
  62. /*
  63.  * Move specified string into path.  Convert "" to "." to handle BSD
  64.  * semantics for a null path.  Strip trailing slashes.
  65.  */
  66. int
  67. #if __STDC__
  68. path_set (register PATH_T *p, char *string)
  69. #else
  70. path_set(p, string)
  71.     register PATH_T *p;
  72.     char *string;
  73. #endif
  74. {
  75.     if (strlen(string) > MAXPATHLEN) {
  76.         (void)fprintf(stderr,
  77.             "%s: %s: name too long.\n", progname, string);
  78.         return(0);
  79.     }
  80.  
  81.     (void)strcpy(p->p_path, string);
  82.     p->p_end = p->p_path + strlen(p->p_path);
  83.  
  84.     if (p->p_path == p->p_end) {
  85.         *p->p_end++ = '.';
  86.         *p->p_end = 0;
  87.     }
  88.  
  89.     STRIP_TRAILING_SLASH(p);
  90.     return(1);
  91. }
  92.  
  93. /*
  94.  * Append specified string to path, inserting '/' if necessary.  Return a
  95.  * pointer to the old end of path for restoration.
  96.  */
  97. char *
  98. #if __STDC__
  99. path_append (register PATH_T *p, char *name, int len)
  100. #else
  101. path_append(p, name, len)
  102.     register PATH_T *p;
  103.     char *name;
  104.     int len;
  105. #endif
  106. {
  107.     char *old;
  108.  
  109.     old = p->p_end;
  110.     if (len == -1)
  111.         len = strlen(name);
  112.  
  113.     /* The "+ 1" accounts for the '/' between old path and name. */
  114.     if ((len + p->p_end - p->p_path + 1) > MAXPATHLEN) {
  115.         (void)fprintf(stderr,
  116.             "%s: %s/%s: name too long.\n", progname, p->p_path, name);
  117.         return(0);
  118.     }
  119.  
  120.     /*
  121.      * This code should always be executed, since paths shouldn't
  122.      * end in '/'.
  123.      */
  124.     if (p->p_end[-1] != '/') {
  125.         *p->p_end++ = '/';
  126.         *p->p_end = 0;
  127.     }
  128.  
  129.     (void)strncat(p->p_end, name, len);
  130.     p->p_end += len;
  131.     *p->p_end = 0;
  132.  
  133.     STRIP_TRAILING_SLASH(p);
  134.     return(old);
  135. }
  136.  
  137. /*
  138.  * Restore path to previous value.  (As returned by path_append.)
  139.  */
  140. void
  141. #if __STDC__
  142. path_restore (PATH_T *p, char *old)
  143. #else
  144. path_restore(p, old)
  145.     PATH_T *p;
  146.     char *old;
  147. #endif
  148. {
  149.     p->p_end = old;
  150.     *p->p_end = 0;
  151. }
  152.  
  153. /*
  154.  * Return basename of path.
  155.  */
  156. char *
  157. #if __STDC__
  158. path_basename (PATH_T *p)
  159. #else
  160. path_basename(p)
  161.     PATH_T *p;
  162. #endif
  163. {
  164.     char *basename;
  165.  
  166.     basename = rindex(p->p_path, '/');
  167.     return(basename ? basename + 1 : p->p_path);
  168. }
  169.