2 |
7u83 |
1 |
/*
|
|
|
2 |
Crown Copyright (c) 1997
|
|
|
3 |
|
|
|
4 |
This TenDRA(r) Computer Program is subject to Copyright
|
|
|
5 |
owned by the United Kingdom Secretary of State for Defence
|
|
|
6 |
acting through the Defence Evaluation and Research Agency
|
|
|
7 |
(DERA). It is made available to Recipients with a
|
|
|
8 |
royalty-free licence for its use, reproduction, transfer
|
|
|
9 |
to other parties and amendment for any purpose not excluding
|
|
|
10 |
product development provided that any such use et cetera
|
|
|
11 |
shall be deemed to be acceptance of the following conditions:-
|
|
|
12 |
|
|
|
13 |
(1) Its Recipients shall ensure that this Notice is
|
|
|
14 |
reproduced upon any copies or amended versions of it;
|
|
|
15 |
|
|
|
16 |
(2) Any amended version of it shall be clearly marked to
|
|
|
17 |
show both the nature of and the organisation responsible
|
|
|
18 |
for the relevant amendment or amendments;
|
|
|
19 |
|
|
|
20 |
(3) Its onward transfer from a recipient to another
|
|
|
21 |
party shall be deemed to be that party's acceptance of
|
|
|
22 |
these conditions;
|
|
|
23 |
|
|
|
24 |
(4) DERA gives no warranty or assurance as to its
|
|
|
25 |
quality or suitability for any purpose and DERA accepts
|
|
|
26 |
no liability whatsoever in relation to any use to which
|
|
|
27 |
it may be put.
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**** file-name.c --- File name manipulation routines.
|
|
|
32 |
*
|
|
|
33 |
** Author: Steve Folkes <smf@hermes.mod.uk>
|
|
|
34 |
*
|
|
|
35 |
**** Commentary:
|
|
|
36 |
*
|
|
|
37 |
* This file implements the file name manipulation facility specified in the
|
|
|
38 |
* file "file-name.h". See that file for more details.
|
|
|
39 |
*
|
|
|
40 |
**** Change Log:
|
|
|
41 |
* $Log: file-name.c,v $
|
|
|
42 |
* Revision 1.1.1.1 1998/01/17 15:57:18 release
|
|
|
43 |
* First version to be checked into rolling release.
|
|
|
44 |
*
|
|
|
45 |
* Revision 1.3 1995/07/05 11:58:40 smf
|
|
|
46 |
* Marked possible unused function as such. Reduced strength of checks.
|
|
|
47 |
*
|
|
|
48 |
* Revision 1.2 1994/12/12 11:45:37 smf
|
|
|
49 |
* Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
|
|
|
50 |
* OSSG C Coding Standards.
|
|
|
51 |
*
|
|
|
52 |
* Revision 1.1.1.1 1994/07/25 16:06:14 smf
|
|
|
53 |
* Initial import of os-interface shared files.
|
|
|
54 |
*
|
|
|
55 |
**/
|
|
|
56 |
|
|
|
57 |
/****************************************************************************/
|
|
|
58 |
|
|
|
59 |
#include "file-name.h"
|
|
|
60 |
#include "cstring.h"
|
|
|
61 |
|
|
|
62 |
/*--------------------------------------------------------------------------*/
|
|
|
63 |
|
|
|
64 |
CStringP
|
|
|
65 |
file_name_basename PROTO_N ((path))
|
|
|
66 |
PROTO_T (CStringP path)
|
|
|
67 |
{
|
|
|
68 |
CStringP last = cstring_find_reverse (path, '/');
|
|
|
69 |
|
|
|
70 |
if (last) {
|
|
|
71 |
return (cstring_duplicate (last + 1));
|
|
|
72 |
} else {
|
|
|
73 |
return (cstring_duplicate (path));
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
CStringP
|
|
|
78 |
file_name_dirname PROTO_N ((path))
|
|
|
79 |
PROTO_T (CStringP path)
|
|
|
80 |
{
|
|
|
81 |
CStringP last = cstring_find_reverse (path, '/');
|
|
|
82 |
|
|
|
83 |
if (last) {
|
|
|
84 |
return (cstring_duplicate_prefix (path, (unsigned) (last - path)));
|
|
|
85 |
} else {
|
|
|
86 |
return (cstring_duplicate (path));
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
CStringP
|
|
|
91 |
file_name_expand PROTO_N ((dir, name, suffix))
|
|
|
92 |
PROTO_T (CStringP dir X
|
|
|
93 |
CStringP name X
|
|
|
94 |
CStringP suffix)
|
|
|
95 |
{
|
|
|
96 |
unsigned dir_length = cstring_length (dir);
|
|
|
97 |
unsigned name_length = cstring_length (name);
|
|
|
98 |
unsigned suf_length = (suffix ? (cstring_length (suffix)) : 0);
|
|
|
99 |
unsigned suf_extra = (unsigned) (suffix ? 1 : 0);
|
|
|
100 |
unsigned extra;
|
|
|
101 |
unsigned length;
|
|
|
102 |
CStringP path;
|
|
|
103 |
CStringP tmp;
|
|
|
104 |
|
|
|
105 |
if ((dir_length > 0) && (dir [dir_length - 1] != '/')) {
|
|
|
106 |
extra = 1;
|
|
|
107 |
} else {
|
|
|
108 |
extra = 0;
|
|
|
109 |
}
|
|
|
110 |
length = (dir_length + extra + name_length + suf_extra + suf_length + 1);
|
|
|
111 |
path = ALLOCATE_VECTOR (char, length);
|
|
|
112 |
tmp = path;
|
|
|
113 |
if (dir_length > 0) {
|
|
|
114 |
(void) memcpy ((GenericP) tmp, (GenericP) dir, (SizeT) dir_length);
|
|
|
115 |
tmp += dir_length;
|
|
|
116 |
if (dir [dir_length - 1] != '/') {
|
|
|
117 |
tmp [0] = '/';
|
|
|
118 |
tmp ++;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
(void) memcpy ((GenericP) tmp, (GenericP) name, (SizeT) name_length);
|
|
|
122 |
tmp += name_length;
|
|
|
123 |
if (suffix) {
|
|
|
124 |
tmp [0] = '.';
|
|
|
125 |
tmp ++;
|
|
|
126 |
(void) memcpy ((GenericP) tmp, (GenericP) suffix, (SizeT) suf_length);
|
|
|
127 |
tmp += suf_length;
|
|
|
128 |
}
|
|
|
129 |
tmp [0] = '\0';
|
|
|
130 |
return (path);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
BoolT
|
|
|
134 |
file_name_is_basename PROTO_N ((path))
|
|
|
135 |
PROTO_T (CStringP path)
|
|
|
136 |
{
|
|
|
137 |
return (!cstring_contains (path, '/'));
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
BoolT
|
|
|
141 |
file_name_is_absolute PROTO_N ((path))
|
|
|
142 |
PROTO_T (CStringP path)
|
|
|
143 |
{
|
|
|
144 |
return (path [0] == '/');
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
void
|
|
|
148 |
file_name_populate PROTO_N ((path))
|
|
|
149 |
PROTO_T (CStringP path)
|
|
|
150 |
{
|
|
|
151 |
#ifdef FS_MKDIR
|
|
|
152 |
CStringP new_path = cstring_duplicate (path);
|
|
|
153 |
CStringP tmp = cstring_find (new_path, '/');
|
|
|
154 |
|
|
|
155 |
if (tmp) {
|
|
|
156 |
do {
|
|
|
157 |
*tmp = '\0';
|
|
|
158 |
(void) mkdir (new_path, 0755);
|
|
|
159 |
*tmp = '/';
|
|
|
160 |
} while ((tmp = cstring_find (tmp + 1, '/')) != NIL (CStringP));
|
|
|
161 |
}
|
|
|
162 |
DEALLOCATE (new_path);
|
|
|
163 |
#else
|
|
|
164 |
UNUSED (path);
|
|
|
165 |
#endif /* defined (FS_MKDIR) */
|
|
|
166 |
}
|