BSD 4_3_Net_2 release
[unix-history] / usr / src / contrib / isode / others / rfa / dirname.c
CommitLineData
11492ebf
C
1/*
2 * RFA - Remote File Access
3 *
4 * Access and Management for a partial file system tree that exists
5 * at two sites either as master files or slave files
6 *
7 * dirname.c : create local filenames
8 *
9 * Contributed by Oliver Wenzel, GMD Berlin, 1990
10 *
11 * $Header: /f/osi/others/rfa/RCS/dirname.c,v 7.3 91/02/22 09:27:50 mrose Interim $
12 *
13 * $Log: dirname.c,v $
14 * Revision 7.3 91/02/22 09:27:50 mrose
15 * Interim 6.8
16 *
17 * Revision 7.2 91/01/14 13:54:25 mrose
18 * update
19 *
20 * Revision 1.1 91/01/04 16:01:47 ow
21 * Initial revision
22 *
23 */
24
25#ifndef lint
26static char *rcsid = "$Header: /f/osi/others/rfa/RCS/dirname.c,v 7.3 91/02/22 09:27:50 mrose Interim $";
27#endif
28
29/*
30 * NOTICE
31 *
32 * Acquisition, use, and distribution of this module and related
33 * materials are subject to the restrictions of a license agreement.
34 * Consult the Preface in the User's Manual for the full terms of
35 * this agreement.
36 *
37 */
38
39#include <stdio.h>
40#include <string.h>
41#include <errno.h>
42#include <sys/param.h>
43#include "rfa.h"
44
45static char p[MAXPATHLEN];
46char *fsBase = FS_BASE;
47
48extern char *rindex();
49extern int errno;
50int commandMode = 0;
51
52char *basename(fn)
53 char *fn;
54{
55 register char *f;
56
57 if (f = rindex(fn,'/'))
58 return f+1;
59 else
60 return fn;
61}
62
63
64char *dirname(fn)
65 char *fn;
66{
67 static char buf[MAXPATHLEN];
68 register char *f;
69
70 strcpy(buf, fn);
71 if ((f = rindex(buf,'/')) && (f != buf)) {
72
73 *f = '\0';
74 return buf;
75 } else
76 return "/";
77}
78
79
80char *makeFN(fn)
81char *fn;
82{
83 return makeFN2("", fn);
84}
85
86char *makeFN2(dir ,fn)
87char *dir;
88char *fn;
89{
90 register char *s = p;
91
92 strcpy(s,fsBase);
93 s += strlen(fsBase);
94 if (*(s-1) != '/')
95 *(s++) = '/';
96 while (*dir == '/')
97 dir++;
98 if (*dir != '\0') {
99 strcpy(s, dir);
100 s += strlen(dir);
101 }
102 if (*(s-1) != '/')
103 *(s++) = '/';
104
105 while (*fn == '/')
106 fn++;
107 strcpy(s, fn);
108
109 return p;
110}
111
112
113char * getRelativeFN(fn)
114 char *fn;
115{
116 if (strncmp(fsBase, fn, strlen(fsBase)))
117 return NULL;
118 return fn + strlen(fsBase);
119}
120
121
122char *expandSymLinks(path)
123 char *path;
124{
125 static char exp[MAXPATHLEN];
126 char *r;
127
128 /*--- expand symbolic links in fn ---*/
129 if (realpath(makeFN(path), exp, sizeof exp) == NULL) {
130 if (errno != ENOENT) {
131 sprintf(rfaErrStr,"%s - %s", sys_errname(errno),getRelativeFN(exp));
132 return NULL;
133 }
134 }
135 if ((r = getRelativeFN(exp)) == NULL) {
136 sprintf(rfaErrStr, "%s not within RFA subtree", exp);
137 return NULL;
138 }
139 return r;
140}
141
142
143char *realPath3 (dir, path1, path2)
144 char *dir, *path1, *path2;
145{
146 register char *s, *s1, *rp;
147 static char realp[MAXPATHLEN];
148 char givenp[MAXPATHLEN];
149
150 s = givenp;
151 strcpy(s, dir);
152 s += strlen(dir);
153 if (*path1) {
154 *(s++) = '/';
155 strcpy(s, path1);
156 s += strlen(path1);
157 }
158 if (*path2) {
159 *(s++) = '/';
160 strcpy(s, path2);
161 }
162
163 rp = realp;
164 *(rp++) = '/';
165
166 for (s = givenp; *s;) {
167 while(*s == '/')
168 s++;
169 if (*s == '.') {
170 s1 = s+1;
171 if (*(s1) == '/') {
172 s += 2;
173 continue;
174 }
175 if (*s1 == '.' && ((*(s1+1) == '/')||(*(s1-1) == '\0'))) {
176 if ((rp - 1) != realp) {
177 for( rp -= 2; *rp != '/'; rp--)
178 ;
179 rp++;
180 }
181 s += 3;
182 continue;
183 }
184 }
185 for (; *s && *s != '/'; s++, rp++)
186 *rp = *s;
187 *(rp++) = *(s++);
188 }
189 *rp = '\0';
190
191 return realp;
192}
193
194
195char *realPath (dir, path)
196 char *dir, *path;
197{
198 return realPath3("", dir, path);
199}
200
201
202/*--------------------------------------------------------------*/
203/* getRfaContext */
204/*--------------------------------------------------------------*/
205char *getRfaContext(cwd, fn)
206 char *cwd, *fn;
207{
208 char *rp;
209 char buf[MAXPATHLEN];
210
211 if (*fn == '@')
212 rp = realPath(fsBase, fn+1);
213 else
214 if (*fn == '/')
215 rp = realPath("/", fn);
216 else
217 if(commandMode) {
218 (void)getwd(buf);
219 rp = realPath(buf, fn);
220 } else
221 rp = realPath3(fsBase, cwd, fn);
222
223 if (strncmp(fsBase, rp, strlen(fsBase)))
224 return NULL;
225
226 /*--- extract realtive path ---*/
227 rp += strlen(fsBase);
228
229 return rp;
230}
231
232