Debian Codegraph @

terps/tads/tads2/msdos/ossrelpa.c

raw symbols SCIP
1 #if 0
2 // THIS FILE IS DEPRECATED. The MSDOS-specific oss_get_rel_path() has
3 // been promoted (as of TADS 3.1.1, March 2012) to a general-purpose
4 // osifc routine, os_get_rel_path(), so the implementation has been moved
5 // to tads2/osnoui.c.
6
7 #ifdef RCSID
8 static char RCSid[] =
9 "$Header$";
10 #endif
11
12 /*
13 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
14 *
15 * Please see the accompanying license file, LICENSE.TXT, for information
16 * on using and copying this software.
17 */
18 /*
19 Name
20 ossrelpa.c - DOS routine - get a relative path
21 Function
22
23 Notes
24
25 Modified
26 11/17/99 MJRoberts - Creation
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include "os.h"
33
34 /*
35 * Create the relative form of a path name. Given a filename and a
36 * working directory, get the name of the file with its path expressed
37 * relative to the working directory, if possible. Both the filename
38 * and working directory paths should be expressed as absolute paths.
39 *
40 * If the filename and working directory are on different drives, we'll
41 * simply return the filename unchanged, since it can't be expressed
42 * relative to the directory.
43 */
44 void oss_get_rel_path(char *result, size_t result_len,
45 const char *filename, const char *working_dir)
46 {
47 const char *fp;
48 const char *wp;
49 const char *fsep;
50 const char *wsep;
51 size_t rem;
52 char *rp;
53
54 /*
55 * Determine if the paths are on the same drive. If they're not, we
56 * can't express the filename relative to the path, so return the
57 * filename unchanged.
58 */
59 if (toupper(filename[0]) != toupper(working_dir[0]))
60 {
61 /* different drives - copy the filename unchanged */
62 strncpy(result, filename, result_len - 1);
63 result[result_len - 1] = '\0';
64 return;
65 }
66
67 /*
68 * They're on the same drive, so we can express the paths
69 * relatively. First, find out how much of the paths match, since
70 * we can simply lop off the common prefix.
71 */
72 for (fp = filename, wp = working_dir, fsep = 0, wsep = 0 ;
73 ((toupper(*fp) == toupper(*wp))
74 || ((*fp == '/' || *fp == '\\') && (*wp == '/' || *wp == '\\'))) ;
75 ++fp, ++wp)
76 {
77 /*
78 * if this is a separator character, note it - we want to keep
79 * track of the last separator in the common portion, since this
80 * is the end of the common path prefix
81 */
82 if (*fp == '\\' || *fp == '/' || *fp == '\0')
83 {
84 fsep = fp;
85 wsep = wp;
86 }
87
88 /* stop at the end of the strings */
89 if (*fp == '\0' || *wp == '\0')
90 break;
91 }
92
93 /* if we didn't find any separators, we can't relativize the paths */
94 if (wsep == 0 || fsep == 0)
95 {
96 /* nothing in common - return the filename unchanged */
97 strncpy(result, filename, result_len - 1);
98 result[result_len - 1] = '\0';
99 return;
100 }
101
102
103 /*
104 * If we reached the end of the working directory string, and we're
105 * at a path separator in the filename string, then the entire
106 * working directory prefix is common
107 */
108 if (*wp == '\0' && (*fp == '/' || *fp == '\\' || *fp == '\0'))
109 {
110 fsep = fp;
111 wsep = wp;
112 }
113
114 /* if we're at a path separator in the working directory, skip it */
115 if (*wsep != '\0')
116 ++wsep;
117
118 /* if we're at a path separator in the filename, skip it */
119 if (*fsep != '\0')
120 ++fsep;
121
122 /*
123 * Everything up to fsep and wsep can be dropped, because it's a
124 * common directory path prefix. We must now add the relative
125 * adjustment portion: add a ".." directory for each remaining
126 * directory in the working path, since we must move from the
127 * working diretory up to the common ancestor; then add the rest of
128 * the filename path.
129 */
130
131 /*
132 * first, set up to copy into the result buffer - leave space for
133 * null termination
134 */
135 rp = result;
136 rem = result_len - 1;
137
138 /*
139 * Add a ".." for each remaining directory in the working directory
140 * string
141 */
142 while (*wsep != '\0')
143 {
144 /* add ".." and a path separator */
145 if (rem > 3)
146 {
147 *rp++ = '.';
148 *rp++ = '.';
149 *rp++ = '\\';
150 rem -= 3;
151 }
152 else
153 {
154 /* no more room - give up */
155 rem = 0;
156 break;
157 }
158
159 /* scan to the next path separator */
160 while (*wsep != '\0' && *wsep != '/' && *wsep != '\\')
161 ++wsep;
162
163 /* if this is a path separator, skip it */
164 if (*wsep != '\0')
165 ++wsep;
166 }
167
168 /*
169 * Copy the remainder of the filename, or as much as will fit, and
170 * ensure that the result is properly null-terminated
171 */
172 strncpy(rp, fsep, rem);
173 rp[rem] = '\0';
174
175 /* if the result is empty, return "." to represent the current dir */
176 if (result[0] == '\0')
177 strcpy(rp, ".");
178 }
179
180 /* ------------------------------------------------------------------------ */
181 /*
182 * Test routine
183 */
184
185 #ifdef TEST
186
187 int main(int argc, char **argv)
188 {
189 char working_dir[OSFNMAX];
190 char filename[OSFNMAX];
191 char rel_path[OSFNMAX];
192
193 for (;;)
194 {
195 printf("Working dir> ");
196 if (gets(working_dir) == 0)
197 break;
198
199 printf("Filename > ");
200 if (gets(filename) == 0)
201 break;
202
203 oss_get_rel_path(rel_path, sizeof(rel_path), filename, working_dir);
204 printf("Relative = \"%s\"\n\n", rel_path);
205 }
206
207 return 0;
208 }
209
210 #endif
211
212 #endif // #if 0 - FILE DEPRECATED