2
* path.c --- path string functions
6
* Copyright (C) 1996-1999 Hirotsugu Kakugawa.
9
* This file is part of the VFlib Library. This library is free
10
* software; you can redistribute it and/or modify it under the terms of
11
* the GNU Library General Public License as published by the Free
12
* Software Foundation; either version 2 of the License, or (at your
13
* option) any later version. This library is distributed in the hope
14
* that it will be useful, but WITHOUT ANY WARRANTY; without even the
15
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16
* PURPOSE. See the GNU Library General Public License for more details.
17
* You should have received a copy of the GNU Library General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35
#include <sys/types.h>
36
#include <sys/param.h>
41
# include <sys/stat.h>
44
#include "VFlib-3_6.h"
52
Private int cons_path(char *path, int n, char *dir, char *file, char *ext);
53
Private char *vf_path_runtime_file2(char *root, char *subdir, char *file,
59
vf_path_core_subst_ext(char *f, char *ext)
60
/* "/opt/font/cmr10.300pk" & "vf" => "cmr10.vf" */
61
/* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
65
if ((f == NULL) || (ext == NULL))
68
if ((b = vf_path_base(f)) == NULL){
69
vf_error = VF_ERR_NO_MEMORY;
72
if ((p = (char*)malloc(strlen(b) + 1 + strlen(ext) + 1)) == NULL){
73
vf_error = VF_ERR_NO_MEMORY;
79
if ((e = vf_index(p, '.')) != NULL)
91
/* "/opt/font/cmr10.300pk" => "cmr10.300pk" */
92
/* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
97
dl = strlen(vf_directory_delimiter);
99
for (s = f; *s != '\0'; s++){
100
if (strncmp(s, vf_directory_delimiter, dl) == 0)
108
vf_path_base_core(char *f)
109
/* "/opt/font/cmr10.300pk" => "cmr10" */
110
/* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
114
if ((core = vf_path_base(f)) == NULL)
116
if ((p = vf_index(core, '.')) != NULL)
125
vf_path_absolute(char *f)
127
if (strncmp(f, vf_directory_delimiter,
128
strlen(vf_directory_delimiter)) == 0){
134
&& isalpha(f[0]) && (f[1] == ':')
135
&& ((f[2] == '/') || (f[2] == '\\'))){
144
vf_path_terminated_by_delim(char *f)
148
dlen = strlen(vf_directory_delimiter);
150
if ((index = strlen(f) - dlen) < 0)
152
if (strcmp(&f[index], vf_directory_delimiter) != 0)
159
vf_path_terminated_by_2delims(char *f)
163
dlen = strlen(vf_directory_delimiter);
165
if ((index = strlen(f) - 2*dlen) < 0)
167
if (strncmp(&f[index], vf_directory_delimiter, dlen) != 0)
169
if (strncmp(&f[index+dlen], vf_directory_delimiter, dlen) != 0)
176
vf_path_del_terminating_2delims(char *f)
180
if (vf_path_terminated_by_2delims(f) == TRUE){
181
dlen = strlen(vf_directory_delimiter);
182
f[strlen(f)-2*dlen] = '\0';
187
vf_path_cons_path(char *path, int n, char *dir, char *file)
189
return cons_path(path, n, dir, file, NULL);
193
vf_path_cons_path2(char *path, int n, char *dir, char *file, char *ext)
195
return cons_path(path, n, dir, file, ext);
199
cons_path(char *path, int n, char *dir, char *file, char *ext)
204
if (vf_path_absolute(file)){
205
strncpy(path, file, n);
206
if ((r -= strlen(file)) < 0)
209
strncat(path, ext, r);
212
strncpy(path, dir, r);
213
if ((r -= strlen(dir)) < 0)
215
if (vf_path_terminated_by_2delims(path) == TRUE)
216
vf_path_del_terminating_2delims(path);
217
strncat(path, vf_directory_delimiter, r);
218
if ((r -= strlen(vf_directory_delimiter)) < 0)
220
strncat(path, file, r);
221
if ((r -= strlen(file)) < 0)
224
strncat(path, ext, r);
231
vf_path_concat(char *path, int n, char *f)
233
if (vf_path_terminated_by_2delims(path) == TRUE)
234
vf_path_del_terminating_2delims(path);
235
strncat(path, vf_directory_delimiter, n);
236
strncat(path, f, n - strlen(path));
245
vf_path_file_read_ok(char *f)
250
if (stat(f, &st) < 0)
252
if ((st.st_mode & S_IFMT) != S_IFREG)
256
if (access(f, R_OK) < 0)
263
vf_path_directory_read_ok(char *f)
268
if (stat(f, &st) < 0)
270
if ((st.st_mode & S_IFMT) != S_IFDIR)
274
if (access(f, R_OK) < 0)
282
vf_path_runtime_dir(char *subdir, char *envname)
283
/* return a absolute directory name of SUBDIR under the default
284
runtime dir. Runtime dir can be overridden by ""
285
It can be overridden by an env var ENVNAME if
287
/* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
289
char *root, *dir, *s;
292
if ((root = getenv(VF_ENV_DIR_RUNTIME_LIB)) == NULL)
293
root = DIR_RUNTIME_LIB;
295
if ((envname != NULL) && ((s = getenv(envname)) != NULL)){
303
sd_len = strlen(subdir);
304
dir_len = strlen(root) + strlen(vf_directory_delimiter) + sd_len + 4;
305
ALLOCN_IF_ERR(dir, char, dir_len){
309
if ((subdir == NULL) || (strcmp(subdir, "") == 0)){
310
strncpy(dir, root, dir_len);
312
vf_path_cons_path(dir, dir_len, root, subdir);
319
vf_path_find_runtime_file(char *subdir, char *file, char *envname)
320
/* return a absolute directory name of SUBDIR under the default
321
runtime dir. Runtime dir can be overridden by ""
322
It can be overridden by an env var ENVNAME if
324
/* IMPORTANT: CALLER MUST RELEASE RETURNED DATA OBJECT */
328
if ((root = getenv(VF_ENV_DIR_RUNTIME_SITE_LIB)) == NULL)
329
root = DIR_RUNTIME_SITE_LIB;
330
p = vf_path_runtime_file2(root, subdir, file, envname);
334
if ((root = getenv(VF_ENV_DIR_RUNTIME_LIB)) == NULL)
335
root = DIR_RUNTIME_LIB;
336
p = vf_path_runtime_file2(root, subdir, file, envname);
341
vf_path_runtime_file2(char *root, char *subdir, char *file, char *envname)
346
if ((envname != NULL) && ((s = getenv(envname)) != NULL)){
354
sd_len = strlen(subdir);
355
dir_len = strlen(root) + sd_len + strlen(file)
356
+ 2 * strlen(vf_directory_delimiter) + 8;
357
ALLOCN_IF_ERR(dir, char, dir_len){
361
if ((subdir == NULL) || (strcmp(subdir, "") == 0)){
362
vf_path_cons_path(dir, dir_len, root, file);
364
vf_path_cons_path(dir, dir_len, root, subdir);
365
vf_path_concat(dir, dir_len, file);
367
if (vf_path_file_read_ok(dir) == FALSE){