File: appSystem.c

package info (click to toggle)
ted 2.6-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,928 kB
  • ctags: 8,734
  • sloc: ansic: 71,878; makefile: 2,363; sh: 159
file content (362 lines) | stat: -rw-r--r-- 7,364 bytes parent folder | download
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
/************************************************************************/
/*									*/
/*  Consult the operating system about certain things.			*/
/*									*/
/*  0)  mkdir() is not declared on linux, suppress warning.		*/
/*  1)  Note that we use shell commands to consult the file system.	*/
/*	this is the easiest way.					*/
/*									*/
/************************************************************************/

#   include	"config.h"

#   include	<stdlib.h>
#   include	<string.h>

#   include	<sys/types.h>
#   include	<pwd.h>
#   include	<unistd.h>
#   include	<sys/stat.h>
#   include	<time.h>
#   include	<errno.h>

#   include	<appSystem.h>

#   include	<debugon.h>

    /*  0  */
#   ifdef __linux__
    extern int mkdir( const char * name, mode_t mode );
#   endif

#   define	USE_STAT	1
#   define	USE_ACCESS	1
#   define	USE_GETCWD	1

#   if  HAVE_DIRENT_H
#	define	USE_OPENDIR	1
#	include	<dirent.h>
#   endif

/************************************************************************/
/*									*/
/*  Determine the home directory of the user.				*/
/*									*/
/************************************************************************/

int appHomeDirectory(	char *		home,
			int		len )
    {
    struct passwd *	pwd;
    int			l;

    pwd= getpwuid( getuid() );
    if  ( ! pwd )
	{ LXDEB(getuid(),pwd); return -1;	}

    if  ( ! pwd->pw_dir )
	{ XDEB(pwd->pw_dir);	}

    l= strlen( pwd->pw_dir );
    if  ( l > len )
	{ SLDEB(pwd->pw_dir,len); return -1;	}

    strcpy( home, pwd->pw_dir ); return l;
    }

/************************************************************************/
/*									*/
/*  Check whether a directory exists.					*/
/*									*/
/************************************************************************/

#if USE_STAT
int appTestDirectory(	const char *	dir )
    {
    struct stat	st;

    if  ( stat( dir, &st ) )
	{
	if  ( errno != ENOENT )
	    { SSDEB(dir,strerror(errno)); }

	return -1;
	}

    if  ( ! S_ISDIR( st.st_mode ) )
	{ return -1;	}

    return 0;
    }
#else
int appTestDirectory(	const char *	dir )
    {
    char	scratch[1001];

    sprintf( scratch, "test -d '%s'", dir );

    if  ( system( scratch ) )
	{ return -1;	}

    return 0;
    }
#endif

#if USE_ACCESS
int appTestFileWritable( const char *	file )
    {
    if  ( access( file, W_OK ) )
	{ return -1;	}

    return 0;
    }
#else
int appTestFileWritable( const char *	file )
    {
    char	scratch[1001];

    sprintf( scratch, "test -w '%s'", file );

    if  ( system( scratch ) )
	{ return -1;	}

    return 0;
    }
#endif

#if USE_ACCESS
int appTestFileReadable( const char *	file )
    {
    if  ( access( file, R_OK ) )
	{ return -1;	}

    return 0;
    }
#else
int appTestFileReadable( const char *	file )
    {
    char	scratch[1001];

    sprintf( scratch, "test -w '%s'", file );

    if  ( system( scratch ) )
	{ return -1;	}

    return 0;
    }
#endif

#if USE_STAT
int appTestFileExists(	const char *	file )
    {
    struct stat	st;

    if  ( stat( file, &st ) )
	{
	if  ( errno != ENOENT )
	    { SSDEB(file,strerror(errno)); }

	return -1;
	}

    if  ( ! S_ISREG( st.st_mode ) )
	{ return -1;	}

    return 0;
    }
#else
int appTestFileExists( const char *	file )
    {
    char	scratch[1001];

    sprintf( scratch, "test -f '%s'", file );

    if  ( system( scratch ) )
	{ return -1;	}

    return 0;
    }
#endif

/************************************************************************/
/*									*/
/*  Make a Directory.							*/
/*									*/
/************************************************************************/

int appMakeDirectory(	const char *	dir )
    {
    if  ( mkdir( dir, 0777 ) )
	{ SSDEB(dir,strerror(errno)); return -1;	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Make a Directory.							*/
/*									*/
/************************************************************************/

long appGetTimestamp( void )
    {
    long	now;

    now= time( (long *)0 ); sleep( 1 );

    return now;
    }

/************************************************************************/
/*									*/
/*  Translate a file name to an absolute name.				*/
/*									*/
/************************************************************************/
int appAbsoluteName(	char *		absolute,
			int		len,
			const char *	filename )
    {
    int		rellen;
    int		abslen;

    rellen= strlen( filename );

    if  ( filename[0] == '/' )
	{
	if  ( rellen > len )
	    { SLDEB(filename,len); return -1;	}

	strcpy( absolute, filename ); return rellen;
	}

    if  ( rellen > len- 2 )
	{ SLDEB(filename,len); return -1;	}

#   if USE_GETCWD
    if  ( ! getcwd( absolute, len- rellen- 2 ) )
	{ LDEB(len); SSDEB(filename,strerror(errno)); return -1; }

    abslen= strlen( absolute );

#   else
    {
    FILE *	f= popen( "pwd", "r" );

    if  ( ! f )
	{ XDEB(f); return -1;	}

    if  ( ! fgets( absolute, len- rellen- 2, f ) )
	{ LDEB(1); pclose( f ); return -1;	}

    abslen= strlen( absolute );
    if  ( abslen < 1 || absolute[abslen -1] != '\n' )
	{ SDEB(absolute); return -1;	}

    absolute[abslen--]= '\0';

    pclose( f );
    }
#   endif

    absolute[abslen]= '/';

    strcpy( absolute+ abslen+ 1, filename );

    return abslen+ 1+ rellen;
    }

/************************************************************************/
/*									*/
/*  Remove a file.							*/
/*									*/
/************************************************************************/

int appRemoveFile(	const char *	filename )
    {
    if  ( remove( filename ) )
	{ SSDEB(filename,strerror(errno)); return -1;	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Call a function for all files matching a certain criterion.		*/
/*									*/
/************************************************************************/

#   define	FILEL		1000

#if USE_OPENDIR
int appForAllFiles(	const char *		dir,
			const char *		ext,
			void *			through,
			FILE_CALLBACK		callback )
    {
    int			rval= 0;
    DIR *		d= opendir( dir );
    struct dirent *	de;

    if  ( ! d )
	{ SSDEB(dir,strerror(errno)); return -1;	}

    de= readdir( d );
    while( de )
	{
	char *	dot= strrchr( de->d_name, '.' );

	if  ( dot					&&
	      ! strcmp( dot+ 1, ext )			)
	    {
	    char	scratch[FILEL+1];

	    sprintf( scratch, "%s/%s", dir, de->d_name );

	    if  ( (*callback)( scratch, through )	)
		{ SDEB(scratch); rval= -1; break;	}
	    }

	de= readdir( d );
	}

    if  ( closedir( d ) )
	{ SSDEB(dir,strerror(errno)); rval= -1;	}

    return rval;
    }
#else

int appForAllFiles(	const char *		dir,
			const char *		ext,
			void *			through,
			FILE_CALLBACK		callback )
    {
    int			rval= 0;
    char		scratch[FILEL+1];
    char *		format= "( ls '%s'/*.'%s' ) 2>/dev/null";
    FILE *		names;

    sprintf( scratch, format, dir, ext );
    names= popen( scratch, "r" );
    if  ( ! names )
	{ SXDEB(scratch,names); return -1;	}

    while( fgets( scratch, FILEL, names ) )
	{
	int	len= strlen( scratch );

	if  ( len > 0 && scratch[len -1] == '\n' )
	    { scratch[len -1]= '\0';		}
	else{ SLDEB(scratch,len); continue;	}

	if  ( (*callback)( scratch, through ) )
	    { SDEB(scratch); rval= -1; break;	}
	}

    if  ( pclose( names ) )
	{ LDEB(1); rval= -1;	}

    return rval;
    }

#endif