File: path.c

package info (click to toggle)
vflib3 3.6.14.dfsg-3+nmu3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 11,536 kB
  • ctags: 3,710
  • sloc: ansic: 35,811; sh: 10,357; asm: 3,290; makefile: 962; lisp: 123; perl: 109; awk: 43
file content (376 lines) | stat: -rw-r--r-- 8,169 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * path.c  --- path string functions
 *
 */
/*
 * Copyright (C) 1996-1999  Hirotsugu Kakugawa. 
 * All rights reserved.
 *
 * This file is part of the VFlib Library.  This library is free
 * software; you can redistribute it and/or modify it under the terms of
 * the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  This library is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_STRINGS_H
# include <strings.h>
#endif
#include <sys/types.h>
#include <sys/param.h>
#if HAVE_SYS_STAT_H
#  ifdef __linux__
#    define __USE_BSD
#  endif
#  include <sys/stat.h>
#endif

#include "VFlib-3_6.h"
#include "VFsys.h"
#include "path.h"
#include "vflpaths.h"
#include "consts.h"
#include "str.h"


Private int   cons_path(char *path, int n, char *dir, char *file, char *ext);
Private char *vf_path_runtime_file2(char *root, char *subdir, char *file,
				    char *envname);



Glocal char*
vf_path_core_subst_ext(char *f, char *ext) 
     /* "/opt/font/cmr10.300pk" & "vf" => "cmr10.vf" */
     /* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
{
  char  *b, *p, *e;

  if ((f == NULL) || (ext == NULL))
    return NULL;

  if ((b = vf_path_base(f)) == NULL){
    vf_error = VF_ERR_NO_MEMORY;
    return NULL;
  }
  if ((p = (char*)malloc(strlen(b) + 1 + strlen(ext) + 1)) == NULL){
    vf_error = VF_ERR_NO_MEMORY;
    return NULL;
  }
  strcpy(p, b);
  vf_free(b);

  if ((e = vf_index(p, '.')) != NULL)
    *e = '\0';
  if (*ext != '.')
    strcat(p, ".");
  strcat(p, ext);

  return p;
}


Glocal char*
vf_path_base(char *f)   
     /* "/opt/font/cmr10.300pk" => "cmr10.300pk" */
     /* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
{
  char  *s, *b;
  int   dl; 

  dl = strlen(vf_directory_delimiter);
  b = f;
  for (s = f; *s != '\0'; s++){
    if (strncmp(s, vf_directory_delimiter, dl) == 0)
      b = &s[dl];
  }
  return vf_strdup(b); 
}


Glocal char*
vf_path_base_core(char *f)
     /* "/opt/font/cmr10.300pk" => "cmr10" */
     /* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
{
  char  *p, *core;

  if ((core = vf_path_base(f)) == NULL)
    return NULL;
  if ((p = vf_index(core, '.')) != NULL)
    *p = '\0';
  return core;
}




Glocal int
vf_path_absolute(char *f)
{
  if (strncmp(f, vf_directory_delimiter,
	      strlen(vf_directory_delimiter)) == 0){
    return TRUE;
  }

#ifdef MSDOS
  if ((strlen(f) >= 3) 
      && isalpha(f[0]) && (f[1] == ':') 
      && ((f[2] == '/') || (f[2] == '\\'))){
    return TRUE;
  }
#endif /*MSDOS*/

  return FALSE;
}

Glocal int
vf_path_terminated_by_delim(char *f)
{
  int   dlen, index;

  dlen = strlen(vf_directory_delimiter);

  if ((index = strlen(f) - dlen) < 0)
    return FALSE;
  if (strcmp(&f[index], vf_directory_delimiter) != 0)
    return FALSE;

  return TRUE;  
}

Glocal int
vf_path_terminated_by_2delims(char *f)
{
  int   dlen, index;

  dlen = strlen(vf_directory_delimiter);

  if ((index = strlen(f) - 2*dlen) < 0)
    return FALSE;
  if (strncmp(&f[index],      vf_directory_delimiter, dlen) != 0)
    return FALSE;
  if (strncmp(&f[index+dlen], vf_directory_delimiter, dlen) != 0)
    return FALSE;

  return TRUE;  
}

Glocal void
vf_path_del_terminating_2delims(char *f)
{
  int   dlen;

  if (vf_path_terminated_by_2delims(f) == TRUE){
    dlen = strlen(vf_directory_delimiter);
    f[strlen(f)-2*dlen] = '\0';
  }
}

Glocal int
vf_path_cons_path(char *path, int n, char *dir, char *file)
{
  return cons_path(path, n, dir, file, NULL);
}

Glocal int
vf_path_cons_path2(char *path, int n, char *dir, char *file, char *ext)
{
  return cons_path(path, n, dir, file, ext);
}

Private int
cons_path(char *path, int n, char *dir, char *file, char *ext)
{
  int  r;

  r = n;
  if (vf_path_absolute(file)){
    strncpy(path, file, n);
    if ((r -= strlen(file)) < 0)
      return -1;
    if (ext != NULL)
      strncat(path, ext, r);
    return 0;
  } else {
    strncpy(path, dir, r);
    if ((r -= strlen(dir)) < 0)
      return -1;
    if (vf_path_terminated_by_2delims(path) == TRUE)
      vf_path_del_terminating_2delims(path);
    strncat(path, vf_directory_delimiter, r);
    if ((r -= strlen(vf_directory_delimiter)) < 0)
      return -1;
    strncat(path, file, r);
    if ((r -= strlen(file)) < 0)
      return -1;
    if (ext != NULL)
      strncat(path, ext, r);
  }

  return 0;
}

Glocal int
vf_path_concat(char *path, int n, char *f)
{
  if (vf_path_terminated_by_2delims(path) == TRUE)
    vf_path_del_terminating_2delims(path);
  strncat(path, vf_directory_delimiter, n);
  strncat(path, f, n - strlen(path));

  return 0;
}




Glocal int
vf_path_file_read_ok(char *f)
{
#if HAVE_SYS_STAT_H
  struct stat  st;

  if (stat(f, &st) < 0)
    return FALSE;
  if ((st.st_mode & S_IFMT) != S_IFREG)
    return FALSE;
#endif

  if (access(f, R_OK) < 0)
    return FALSE;

  return TRUE;
}

Glocal int
vf_path_directory_read_ok(char *f)
{
#if HAVE_SYS_STAT_H
  struct stat  st;

  if (stat(f, &st) < 0)
    return FALSE;
  if ((st.st_mode & S_IFMT) != S_IFDIR)
    return FALSE;
#endif

  if (access(f, R_OK) < 0)
    return FALSE;

  return TRUE;
}


Glocal char*
vf_path_runtime_dir(char *subdir, char *envname)
     /* return a absolute directory name of SUBDIR under the default 
	runtime dir. Runtime dir can be overridden by ""
	It can be overridden by an env var ENVNAME if 
	it is defined. */
     /* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
{
  char  *root, *dir, *s;
  int    sd_len, dir_len;

  if ((root = getenv(VF_ENV_DIR_RUNTIME_LIB)) == NULL)
    root = DIR_RUNTIME_LIB;

  if ((envname != NULL) && ((s = getenv(envname)) != NULL)){
    root = s;
    subdir = NULL;
  }

  if (subdir == NULL)
    sd_len = 0;
  else
    sd_len = strlen(subdir);
  dir_len = strlen(root) + strlen(vf_directory_delimiter) + sd_len + 4;
  ALLOCN_IF_ERR(dir, char, dir_len){
    return NULL;
  }

  if ((subdir == NULL) || (strcmp(subdir, "") == 0)){
    strncpy(dir, root, dir_len);
  } else {
    vf_path_cons_path(dir, dir_len, root, subdir);
  }

  return  dir;
}

Glocal char*
vf_path_find_runtime_file(char *subdir, char *file, char *envname)
     /* return a absolute directory name of SUBDIR under the default 
	runtime dir. Runtime dir can be overridden by ""
	It can be overridden by an env var ENVNAME if 
	it is defined. */
     /* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
{
  char *root, *p;

  if ((root = getenv(VF_ENV_DIR_RUNTIME_SITE_LIB)) == NULL)
    root = DIR_RUNTIME_SITE_LIB;
  p = vf_path_runtime_file2(root, subdir, file, envname);
  if (p != NULL)
    return p;

  if ((root = getenv(VF_ENV_DIR_RUNTIME_LIB)) == NULL)
    root = DIR_RUNTIME_LIB;
  p = vf_path_runtime_file2(root, subdir, file, envname);
  return p;
}

Private char*
vf_path_runtime_file2(char *root, char *subdir, char *file, char *envname)
{
  char  *dir, *s;
  int    sd_len, dir_len;

  if ((envname != NULL) && ((s = getenv(envname)) != NULL)){
    root = s;
    subdir = NULL;
  }

  if (subdir == NULL)
    sd_len = 0;
  else
    sd_len = strlen(subdir);
  dir_len = strlen(root) + sd_len + strlen(file) 
            + 2 * strlen(vf_directory_delimiter) + 8;
  ALLOCN_IF_ERR(dir, char, dir_len){
    return NULL;
  }

  if ((subdir == NULL) || (strcmp(subdir, "") == 0)){
    vf_path_cons_path(dir, dir_len, root, file);
  } else {
    vf_path_cons_path(dir, dir_len, root, subdir);
    vf_path_concat(dir, dir_len, file);
  }
  if (vf_path_file_read_ok(dir) == FALSE){
    vf_free(dir);
    dir = NULL;
  }

  return  dir;
}


/*EOF*/