1 /* -*- Mode: C++-C -*-
2 *
3 *
4 * Copyright 1994 Christopher B. Liebman
5 *
6 * Permission to use, copy, modify, distribute, and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and that
9 * both that copyright notice and this permission notice appear in
10 * supporting documentation, and that the name Christopher B. Liebman not
11 * be used in advertising or publicity pertaining to distribution of this
12 * software without specific, written prior permission.
13 *
14 * THIS SOFTWARE IS PROVIDED `AS-IS'. CHRISTOPHER B. LIEBMAN, DISCLAIMS
15 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
16 * LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
17 * PARTICULAR PURPOSE, OR NONINFRINGEMENT. IN NO EVENT SHALL CHRISTOPHER
18 * B. LIEBMAN, BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING SPECIAL,
19 * INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA, OR
20 * PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
21 * WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 *
24 *
25 *
26 * Author : Chris Liebman
27 * Created On : Sat Jan 29 16:45:48 1994
28 * Last Modified By: Chris Liebman
29 * Last Modified On: Mon Jan 31 22:32:22 1994
30 * Update Count : 16
31 * Status : Unknown, Use with caution!
32 *
33 * HISTORY
34 *
35 * PURPOSE
36 * Path functions.
37 */
38
39 #ifndef lint
40 static char *RCSid = "$Id: path.c,v 1.2 1994/02/23 13:17:02 liebman Exp $";
41 #endif
42
43 #include "faces.h"
44 #include <sys/stat.h>
45
46 char**
PathParse(str)47 PathParse(str)
48 char* str;
49 {
50 char** path;
51
52 path = StringParse(str, ":");
53
54 return path;
55 }
56
57 int
PathEnumerate(file,paths,func,data)58 PathEnumerate(file, paths, func, data)
59 char* file;
60 char** paths;
61 int (*func) P_((char* file, char* path, void* data));
62 void* data;
63 {
64 static char* buffer = NULL;
65 static int buffer_size = 0;
66 struct stat buf;
67 int ret = 0;
68 int path_len;
69 int file_len;
70 int len;
71
72 if (buffer == NULL)
73 {
74 buffer_size = 1024;
75 buffer = XtMalloc(buffer_size);
76 }
77
78 if (paths == NULL)
79 {
80 fprintf(stderr, "PathEnumerate: NULL paths!\n");
81 return 0;
82 }
83
84 file_len = strlen(file);
85
86 while(!ret && *paths != NULL)
87 {
88 if (TheFacesResources.path_by_chdir)
89 {
90 if (chdir(*paths) != -1)
91 {
92 ret = func(file, *paths, data);
93 }
94 }
95 else
96 {
97 if (stat(*paths, &buf) != -1 && (buf.st_mode & S_IFDIR))
98 {
99 path_len = strlen(*paths);
100
101 /*
102 * We need path+/+file+NUL
103 */
104
105 len = path_len + file_len + 2;
106
107 if (len > buffer_size)
108 {
109 buffer_size = len;
110 buffer = XtRealloc(buffer, buffer_size);
111 }
112
113 sprintf(buffer, "%s/%s", *paths, file);
114
115 ret = func(buffer, *paths, data);
116 }
117 }
118
119 /*
120 * Go to the next path.
121 */
122
123 ++paths;
124 }
125
126 return ret;
127 }
128
129