File: util.c

package info (click to toggle)
mpsql 2.1-2
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 3,528 kB
  • ctags: 4,886
  • sloc: ansic: 35,184; makefile: 3,761; sh: 44
file content (396 lines) | stat: -rw-r--r-- 10,884 bytes parent folder | download | duplicates (2)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*  -*- c -*-  */
/* -------------------------------------------------------------------- *
**  copyright (c) 1995 ipvr stuttgart and thomas harrer
** -------------------------------------------------------------------- *
**
**  libhelp
**
**  a comprehensive hypertext help system for OSF/Motif(tm) applications. 
**  based on libhtmlw from NCSA Mosaic(tm) version 2.4
**
**  written by thomas harrer
**  e-mail: Thomas.Harrer@rus.uni-stuttgart.de
**  
** -------------------------------------------------------------------- *
*h  $Id: util.c,v 1.10 1995/06/28 12:59:30 thomas Exp $
** -------------------------------------------------------------------- *
**
*h  module:		util.c
**
**  contents:		utilities for extracting components in urls and
**			path names and for classifying filenames.
**
**  interface:		procedures
**			* get_file
**			* get_anchor
**			* convert_newlines_to_spaces
**			* get_file_and_strip_path
**			* get_path
**			* file_type
**			
** -------------------------------------------------------------------- *
**  license and copying issues:
**
**  this software is free; you can redistribute it and/or modify it 
**  under terms similar to the gnu general public license (version 1 
**  or any later version published by the free software foundation). 
**  see the file Licence for more details.
**
**  this program 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.  
** -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- *
*g  include files
** -------------------------------------------------------------------- */
#include <sys/stat.h>
#include <ctype.h>
#include <dirent.h>

#include "helpp.h"
#include "util.h"

/* -------------------------------------------------------------------- *
*g  module identification
** -------------------------------------------------------------------- */
#ifdef RCSID
static char rcsid [] =
    "$Id: util.c,v 1.10 1995/06/28 12:59:30 thomas Exp $";
#endif /* RCSID */

/* -------------------------------------------------------------------- *
*g  local defs.
** -------------------------------------------------------------------- */
#define LOCAL "file:"
#define LOCAL_LEN 5

/* -------------------------------------------------------------------- *
*p  procedure-name:	get_file
**
**  purpose:		extracts file (plus opt. path) from url.
**			strips of prefix and anchor.
** -------------------------------------------------------------------- *
**  args:		filename and opt. anchor in a '\0' terminated
**			string.  format: "[file:]<file>[#<anchor>]".
**  return type:	char* (allocated). caller is responsible for
**			freeing the returned memory (if != NULL).
** -------------------------------------------------------------------- */
char*
get_file (/* i  */ char* url)
{
    char* filename = NULL;	/* filename to be returned.		*/
    char* ptr;			/* pointer for string manipulation.	*/

    execute ("get_file");

    /* we work arround URL's containing file: prefix.  */
    if (url) {

	if (0 == c_strncmp (url, LOCAL, LOCAL_LEN)) {
	    checked_strdup (filename, url + LOCAL_LEN);
	} else  {
	    checked_strdup (filename, url); /* scope: returned. */
	}

	ptr = filename;

	/* for absolute specified files we ignore the anchor.  */
	/* we neet this to access filesnames which contain '#' chars.  */
	if ((url[0] != '.') && (url[0] != '/')) {

	    /* else we strip off the optional anchor  */
	    while (*ptr != '\0') {
		if (*ptr == '#') {
		    *ptr = '\0';
		    break;
		}
		ptr++;
	    }
	}
    
	return filename;
    }
    return NULL;
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	get_anchor
**
**  purpose:		returns the anchor from the given url or an
**			empty string if there was no anchor.
** -------------------------------------------------------------------- *
**  args:		filename + opt anchor.
**  returns:		the anchor or "" (\0 terminated).
**			the application must not free the returned ptr.
** -------------------------------------------------------------------- */
char*
get_anchor (/* i  */ char* url)
{
    char* ptr = url;
    
    execute ("get_anchor");

    while (*ptr != '\0') {
	if (*ptr == '#') {
	    return (ptr + 1);
	}
	else ptr++;
    }
    return ptr;
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	convert_newlines_to_spaces
**
**  purpose:		removes leading whitespace and converts all
**			newlines with spaces.
**  and:		shows we can fluently deal with pointers :-)
** -------------------------------------------------------------------- *
**  args:		string str
**  return type:	char*
**  precondition:	\0 terminated string must be passed.
**  postcondition:	see purpose. side effect is, that non blank part
**			of str is copied to the beginning of string.
**  error handling.:	assumes \0 at the end of string.
** -------------------------------------------------------------------- */
char*
convert_newlines_to_spaces (/* io */ char*	str)
{
    if (str) {

	char* ptr = str;
    
	execute ("convert_newlines_to_spaces");
    
	/* we try to move to the first nonspace position.  */
	while ((*ptr != '\0') && (isspace (*ptr))) {
	    ptr++;
	}

	/* if we moved the position, now we move the string.  */
	if (ptr != str) {
	    /* is this destructive ??  */
	    c_memmove (str, ptr, (c_strlen (ptr) + 1));
	}
	
	/* in the result, we convert newlines to spaces.  */
	ptr = str;
	while (*ptr) {
	    if (*ptr == '\n') *ptr = ' ';
	    ptr++;
	}
    
	return str;
    }

    /* if we've got nothing, we return nothing.  */
    return NULL;    
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	get_file_and_strip_path
**
**  purpose:		strips the path component from a filename.
** -------------------------------------------------------------------- *
**  return-value:	returns a newly allocated string filename.
**			or null if ref does not contain a filename 
**			or ref is a directory.
**			the caller is responsible to free the returned 
**			string.
** -------------------------------------------------------------------- */
char*
get_file_and_strip_path (/* i  */ char* url)
{
    execute ("get_file_and_strip_path");
    
    if (url) {

	char* file = get_file (url); /* scope return  */

	if (file) {

	    /* here we need the filetype:  */
	    int type = file_type (file);

	    if (type & file_dir)  {

		checked_free (file);
		return NULL;
	    
	    } else {

		char* last = NULL;
		char* ptr = file;

		/* we extract the last element of the path.  */
		while (*ptr != '\0') {
		    if (*ptr == '/') {
			last = ptr;
		    }
		    ptr++;
		}

		/* now we move the last component to the 1st position. */
		if ((last) && (*last))  { 
		    last++;
		    c_memmove (file, last, (c_strlen (last) + 1));
		}
		return file;
	    }
	}
    }

    return NULL;
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	get_path
**
**  purpose:		returns the path component of filename
** -------------------------------------------------------------------- *
**  precondition:	file must be accessible (for reading).
**
**  return value:	path component of file in a newly allocated 
**			string or NULL if name of file
** -------------------------------------------------------------------- */
char*
get_path (/* i  */ char* filename)
{
    execute ("get_path");

    if (filename) {

	struct stat 	st;
	char* 		fn = NULL;
	
	/* we work arround URL's containing file: prefix.  */
	if (0 == c_strncmp (filename, LOCAL, LOCAL_LEN)) {
	    fn = filename + LOCAL_LEN;
	} else  {
	    fn = filename;
	}
	if (stat (fn, &st) >= 0) {

	    int 	len = c_strlen (fn);
	    char* 	path;	/* scope: returned. */
	
	    checked_malloc (path, len + 2, char);
	    c_strcpy (path, fn);

	    /* we apply some changes to the path.  */
	    if (S_ISDIR (st.st_mode)) {
		/* for directories we add "/" if not already at the end.  */
		if ((len > 0) && (path[len - 1] != '/'))
		    c_strcat (path, "/");
	    } else {
	    
		/* for all others: we take the string before the last '/'. */
		char* pos = NULL;
		char* ptr = path;
    
		while (*ptr) {	/* searches path!  */
		    if (*ptr == '/') pos = ptr;
		    ptr++;
		}

		if (pos)  {	/* changes path! */
		    *(pos + 1) = '\0';
		} else {
		    *ptr = '/';
		    *(ptr + 1) = '\0';
		}
	    }
	    return path;
	} else  {
	    return NULL;
	}

    } else  {
	return NULL;
    }
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	file_type
**
**  purpose:		returns the type of the file `filename'.
**			
**			possible values are:
**			
**			* file_html
**			* file_dir
**			* file_plain
**			* file_gif
**			* file_executable
**			* file_no_access
**			
**			possible more than one attributes belong to a file
**			the caller can test if attribute is true by
**			using the & (and) operator. e.g. 
**			
**			xxx = file_type ("?");
**			if (xxx & file_dir) printf ("? is a dir\n");
** -------------------------------------------------------------------- */
int
file_type (/* i  */ char* filename)
{

/* some local definitions. */
#define GIF_SUF ".gif"
#define GIF_SUF_LEN 4
#define HTML_SUF ".html"
#define HTML_SUF_LEN 5
#define HTMLGZ_SUF ".html.gz"
#define HTMLGZ_SUF_LEN 8

    execute ("file_type");

    if (filename) {

	int 		ftype = 0;	/* the return value. */
	struct stat 	st;		/* tmp. stat buffer. */

	/* we check only if file is accessible  */
	if (stat (filename, &st) < 0) return file_no_access;
	if (0 != access (filename, R_OK)) return file_no_access;

	if (S_ISREG (st.st_mode)) {

	    int len = c_strlen (filename);
	    int htmlpos = (len > HTML_SUF_LEN ? (len - HTML_SUF_LEN) : 0);
	    int gzpos = (len > HTMLGZ_SUF_LEN ? (len - HTMLGZ_SUF_LEN) : 0);
	    int gifpos = (len > GIF_SUF_LEN ? (len - GIF_SUF_LEN) : 0);
	
	    /* html file ?  */
	    if (0 == c_strcmp (filename + htmlpos, HTML_SUF)) {
		ftype |= file_html;
	    } else if (0 == c_strcmp (filename + gzpos, HTMLGZ_SUF)) {
		ftype |= file_html;
	    } else if (0 == c_strcmp (filename + gifpos, GIF_SUF)) {
		/* gif file ?  */
		ftype = file_gif;
	    } else {
		ftype |= file_plain;
	    }
	    /* executable ?  */
	    if (0 == access (filename, X_OK)) ftype |= file_executable;

	} else if (S_ISDIR (st.st_mode)) {
	    ftype = file_dir;
	}

	return ftype;
    } else {
	return file_no_access;
    }
}

/* -------------------------------------------------------------------- *
*l  emacs:
**  local variables:
**  mode:		c
**  outline-regexp:	"\*[HGPLT]"
**  comment-column:	32
**  eval:		(outline-minor-mode t)
**  end:
** -------------------------------------------------------------------- */