Commit | Line | Data |
---|---|---|
15637ed4 RG |
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 | ||
38 | #include <sys/param.h> | |
39 | #include <stdio.h> | |
40 | #include <string.h> | |
41 | #include "cp.h" | |
42 | ||
43 | /* | |
44 | * These functions manipulate paths in PATH_T structures. | |
45 | * | |
46 | * They eliminate multiple slashes in paths when they notice them, | |
47 | * and keep the path non-slash terminated. | |
48 | * | |
49 | * Both path_set() and path_append() return 0 if the requested name | |
50 | * would be too long. | |
51 | */ | |
52 | ||
53 | #define STRIP_TRAILING_SLASH(p) { \ | |
54 | while ((p)->p_end > (p)->p_path && (p)->p_end[-1] == '/') \ | |
55 | *--(p)->p_end = 0; \ | |
56 | } | |
57 | ||
58 | /* | |
59 | * Move specified string into path. Convert "" to "." to handle BSD | |
60 | * semantics for a null path. Strip trailing slashes. | |
61 | */ | |
62 | path_set(p, string) | |
63 | register PATH_T *p; | |
64 | char *string; | |
65 | { | |
66 | if (strlen(string) > MAXPATHLEN) { | |
67 | (void)fprintf(stderr, | |
68 | "%s: %s: name too long.\n", progname, string); | |
69 | return(0); | |
70 | } | |
71 | ||
72 | (void)strcpy(p->p_path, string); | |
73 | p->p_end = p->p_path + strlen(p->p_path); | |
74 | ||
75 | if (p->p_path == p->p_end) { | |
76 | *p->p_end++ = '.'; | |
77 | *p->p_end = 0; | |
78 | } | |
79 | ||
80 | STRIP_TRAILING_SLASH(p); | |
81 | return(1); | |
82 | } | |
83 | ||
84 | /* | |
85 | * Append specified string to path, inserting '/' if necessary. Return a | |
86 | * pointer to the old end of path for restoration. | |
87 | */ | |
88 | char * | |
89 | path_append(p, name, len) | |
90 | register PATH_T *p; | |
91 | char *name; | |
92 | int len; | |
93 | { | |
94 | char *old; | |
95 | ||
96 | old = p->p_end; | |
97 | if (len == -1) | |
98 | len = strlen(name); | |
99 | ||
100 | /* The "+ 1" accounts for the '/' between old path and name. */ | |
101 | if ((len + p->p_end - p->p_path + 1) > MAXPATHLEN) { | |
102 | (void)fprintf(stderr, | |
103 | "%s: %s/%s: name too long.\n", progname, p->p_path, name); | |
104 | return(0); | |
105 | } | |
106 | ||
107 | /* | |
108 | * This code should always be executed, since paths shouldn't | |
109 | * end in '/'. | |
110 | */ | |
111 | if (p->p_end[-1] != '/') { | |
112 | *p->p_end++ = '/'; | |
113 | *p->p_end = 0; | |
114 | } | |
115 | ||
116 | (void)strncat(p->p_end, name, len); | |
117 | p->p_end += len; | |
118 | *p->p_end = 0; | |
119 | ||
120 | STRIP_TRAILING_SLASH(p); | |
121 | return(old); | |
122 | } | |
123 | ||
124 | /* | |
125 | * Restore path to previous value. (As returned by path_append.) | |
126 | */ | |
127 | void | |
128 | path_restore(p, old) | |
129 | PATH_T *p; | |
130 | char *old; | |
131 | { | |
132 | p->p_end = old; | |
133 | *p->p_end = 0; | |
134 | } | |
135 | ||
136 | /* | |
137 | * Return basename of path. | |
138 | */ | |
139 | char * | |
140 | path_basename(p) | |
141 | PATH_T *p; | |
142 | { | |
143 | char *basename; | |
144 | ||
145 | basename = rindex(p->p_path, '/'); | |
146 | return(basename ? basename + 1 : p->p_path); | |
147 | } |