1 /* dir.c */
2 /* COPYRIGHT (C) 2000 THE VICTORIA UNIVERSITY OF MANCHESTER and John Levon
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307, USA.
16  */
17 /* for directory functions */
18 /*
19  * $Log: dir.c,v $
20  * Revision 1.3  2001/01/31 15:38:06  movement
21  * pre11 - fix netscape, add metapost support, credit foggy
22  *
23  * Revision 1.3  2001/01/29 20:57:28  moz
24  * Make netscape work again, metapost support.
25  *
26  * Revision 1.2  2000/12/06 20:56:01  moz
27  * GPL stuff.
28  *
29  * Revision 1.1.1.1  2000/08/21 01:05:30  moz
30  *
31  *
32  * Revision 1.1.1.1  2000/07/19 22:45:30  moz
33  * CVS Import
34  *
35  * Revision 1.9  2000/03/09 23:18:06  moz
36  * Create "false" ../ entry for unreadable dirs.
37  *
38  * Revision 1.8  2000/02/17 22:16:27  moz
39  * Compiler fixes.
40  *
41  * Revision 1.7  1999/11/15 02:08:04  moz
42  * Name change.
43  *
44  * Revision 1.6  1999/05/19 17:06:22  moz
45  * 1.0 Checkin.
46  *
47  * Revision 1.5  1999/05/03 06:20:33  moz
48  * Show fig only for all relevant dialogs.
49  *
50  * Revision 1.4  1999/04/23 01:04:59  moz
51  * FIG files only.
52  *
53  * Revision 1.3  1999/04/12 19:05:28  moz
54  * New function to strip off directory.
55  *
56  * Revision 1.2  1999/04/04 03:13:41  moz
57  * Core-dump fix on overflowing string.
58  *
59  * Revision 1.1  1999/04/04 03:11:23  moz
60  * Initial revision
61  *
62  */
63 
64 #include <dirent.h>
65 #include <sys/types.h>
66 #include <sys/stat.h>
67 #include <string.h>
68 #include "include/figurine.h"
69 #include "include/extern.h"
70 
71 /* returns filename part  */
72 /* e.g. /home/moz/man becomes */
73 /* man */
74 char *
strip_dirname(char * fname)75 strip_dirname(char *fname)
76 {
77 	char *a=fname+strlen(fname);
78 
79 	while (a>=fname && *a!='/')
80 		a--;
81 
82 	return a+1;
83 }
84 
85 /* replace a file name with chosen extension  */
86 void
add_export_extension(int type)87 add_export_extension(int type)
88 {
89 	char newext[10];
90 	char s[FIGURINE_PATH_MAX];
91 	char *cp;
92 
93 	switch (type)
94 		{
95 		case EXPORT_LATEX_BOX:
96 			strcpy(newext,".box");
97 			break;
98 
99 		case EXPORT_LATEX_PICTURE:
100 			strcpy(newext,".latex");
101 			break;
102 
103 		case EXPORT_EPIC:
104 			strcpy(newext,".epic");
105 			break;
106 
107 		case EXPORT_EEPIC:
108 			strcpy(newext,".eepic");
109 			break;
110 
111 		case EXPORT_EEPICEMU:
112 			strcpy(newext,".eepicemu");
113 			break;
114 
115 		case EXPORT_PICTEX:
116 			strcpy(newext,".pictex");
117 			break;
118 
119 		case EXPORT_IBMGL:
120 			strcpy(newext,".hgl");
121 			break;
122 
123 		case EXPORT_EPS:
124 			strcpy(newext,".eps");
125 			break;
126 
127 		case EXPORT_PS:
128 			strcpy(newext,".ps");
129 			break;
130 
131 		case EXPORT_PSLATEX:
132 			strcpy(newext,".pstex");
133 			break;
134 
135 		case EXPORT_TEXTYL:
136 			strcpy(newext,".textyl");
137 			break;
138 
139 		case EXPORT_TPIC:
140 			strcpy(newext,".tpic");
141 			break;
142 
143 		case EXPORT_PIC:
144 			strcpy(newext,".pic");
145 			break;
146 
147 		case EXPORT_METAFONT:
148 			strcpy(newext,".mf");
149 			break;
150 
151 		case EXPORT_METAPOST:
152 			strcpy(newext,".mp");
153 			break;
154 
155 		case EXPORT_MULTIMETAPOST:
156 			strcpy(newext,".mmp");
157 			break;
158 
159 		case EXPORT_AUTOCAD:
160 			strcpy(newext,".sld");
161 			break;
162 
163 		case EXPORT_PCX:
164 			strcpy(newext,".pcx");
165 			break;
166 
167 		case EXPORT_PNG:
168 			strcpy(newext,".png");
169 			break;
170 
171 		case EXPORT_GIF:
172 			strcpy(newext,".gif");
173 			break;
174 
175 		case EXPORT_JPEG:
176 			strcpy(newext,".jpg");
177 			break;
178 
179 		case EXPORT_TIFF:
180 			strcpy(newext,".tif");
181 			break;
182 
183 		case EXPORT_PPM:
184 			strcpy(newext,".ppm");
185 			break;
186 
187 		case EXPORT_XBM:
188 			strcpy(newext,".xbm");
189 			break;
190 
191 		case EXPORT_XPM:
192 			strcpy(newext,".xpm");
193 			break;
194 
195 		default:
196 			strcpy(newext,".tex");
197 		};
198 
199 	stk_get_text_entry(exportfile_entry,s);
200 
201 	cp = s+strlen(s);
202 
203 	while (cp>s && *cp!='/' && *cp!='.')
204 		cp--;
205 
206 	if (*cp=='.')
207 		{
208 		*cp = '\0';
209 		strcat(s,newext);
210 		};
211 
212 	if (*cp=='/' && cp!=(s+strlen(s)-1))
213 		strcat(s,newext);
214 
215 	stk_set_text_entry(exportfile_entry,s);
216 
217 }
218 
219 /* strip last directory from a path name  */
220 void
strip_end_dir(char * path)221 strip_end_dir(char *path)
222 {
223 	char *a=&path[strlen(path)];
224 
225 	if (path==NULL)
226 		return;
227 
228 	if (strlen(path)>1 && *(a-1)=='/')
229 		{
230 		a--;
231 		*a='\0';
232 		};
233 
234 	if (streq("/",path))
235 		return;
236 
237 	if (is_directory(path))
238 		{
239 		while (a!=path && *a!='/')
240 			a--;
241 		};
242 
243 	if (a==path)
244 		{
245 		*path='/';
246 		*(path+1)='\0';
247 		}
248 	else
249 		*(a+1)='\0';
250 
251 }
252 
253 Boolean
is_directory(char * dir)254 is_directory(char *dir)
255 {
256 	struct stat st;
257 
258 	if (streq(dir,""))
259 		return FALSE;
260 
261 	if (!stat(dir,&st))
262 		return (st.st_mode & S_IFDIR);
263 	else
264 		return FALSE;
265 }
266 
267 int
stringcomp(char ** a,char ** b)268 stringcomp(char **a, char **b)
269 {
270 	return (strcmp(*a,*b));
271 }
272 
273 /* fill in a dialog with the cwd contents  */
274 /* Window l is dirs  */
275 /* Window r is files  */
276 /* if figonly, mask is *.fig  */
277 void
fill_in_file(Window l,Window r,Boolean figonly)278 fill_in_file(Window l, Window r, Boolean figonly)
279 {
280 	DIR *dir;
281 	char *files[500]; /* MAX no. of files displayed */
282 	char *dirs[500]; /* MAX no. of dirs displayed  */
283 	char cd[FIGURINE_PATH_MAX];
284 	uint fn = 0;
285 	uint dn = 0;
286 	uint i;
287 	struct dirent *entry;
288 	struct stat st;
289 
290 	stk_clear_textlist(l);
291 	stk_clear_textlist(r);
292 
293 	strncpy(cd,state.cwd,FIGURINE_PATH_MAX);
294 	dir = opendir(cd);
295 
296 	/* if not accessible, just add parent ../ */
297 	if (dir==NULL)
298 		{
299 		stk_add_textlist_member(l, "../");
300 		return;
301 		};
302 
303 	while ((entry=readdir(dir))!=NULL)
304 		{
305 	if (!stat(entry->d_name,&st))
306 		{
307 		if (is_directory(entry->d_name))
308 			{
309 			if (dn<500 && !streq(entry->d_name,"."))
310 				{
311 				dirs[dn] = (char *)malloc(strlen(entry->d_name)+1+1);
312 				strcpy(dirs[dn], entry->d_name);
313 				strcat(dirs[dn], "/");
314 				dn++;
315 				};
316 			}
317 		else
318 			{
319 			if (fn<500)
320 				{
321 					if (!figonly || (strlen(entry->d_name)>3 &&
322 						 streq(&entry->d_name[strlen(entry->d_name)-4],".fig")))
323 						{
324 						files[fn] = (char *)malloc(strlen(entry->d_name)+1);
325 						strcpy(files[fn], entry->d_name);
326 						fn++;
327 						};
328 				};
329 			};
330 		};
331 	};
332 
333 	/* sort them */
334 	qsort(&files, fn, sizeof(char *),  (int (*)(const void *, const void *))stringcomp);
335 	qsort(&dirs, dn, sizeof(char *),  (int (*)(const void *, const void *))stringcomp);
336 
337 	for (i=0; i<fn; i++)
338 	stk_add_textlist_member(r, files[i]);
339 
340 	for (i=0; i<dn; i++)
341 	stk_add_textlist_member(l, dirs[i]);
342 
343 	for (i=0; i<fn; i++)
344 		free(files[i]);
345 
346 	for (i=0; i<dn; i++)
347 		free(dirs[i]);
348 
349 }
350