File: Sysvarcstuf

package info (click to toggle)
arc 5.21q-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 412 kB
  • ctags: 311
  • sloc: ansic: 4,124; makefile: 86
file content (374 lines) | stat: -rw-r--r-- 9,334 bytes parent folder | download | duplicates (6)
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
#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -rw-r--r--  1 hyc           539 Jul 22 16:30 README
# -rw-r--r--  1 hyc           787 Jul 22 15:10 getwd.c
# -rw-r--r--  1 hyc           280 Jul 22 16:14 rename.c
# -rw-r--r--  1 hyc          2353 Jul 22 15:55 scandir.3
# -rw-r--r--  1 hyc          1875 Jul 22 15:55 scandir.c
# -rw-r--r--  1 hyc           397 Jul 22 15:10 utimes.c
#
echo 'x - README'
if test -f README; then echo 'shar: not overwriting README'; else
sed 's/^X//' << '________This_Is_The_END________' > README
XThe enclosed files should be sufficient for bringing up ARC on a Sys V R3
Xsystem. As Jon mentions, Doug Gwyn's directory routines are needed for
XSys V R2. The enclosed copy of scandir is new, as far as I can tell, and
XI've removed the (unneeded) ftw.? files. Also added a rename() routine,
Xcourtesy of Janet Walz. (And an addition from Rich Salz.)
X
X[see comp.sources.unix, volume 9, for gwyn-dir-lib...]
X
XThanks again to Jon Zeeff, Janet Walz, and Rich Salz for their help.
X  -- Howard Chu
X	hyc@umix.cc.umich.edu
X	{uunet,rutgers}!umix!hyc
________This_Is_The_END________
if test `wc -c < README` -ne      539; then
	echo 'shar: README was damaged during transit (should have been      539 bytes)'
fi
fi		; : end of overwriting check
echo 'x - getwd.c'
if test -f getwd.c; then echo 'shar: not overwriting getwd.c'; else
sed 's/^X//' << '________This_Is_The_END________' > getwd.c
X/*
X * 4.2bsd getwd simulation for Sys V.3
X */
X
X#include <stdio.h>
X
X#define SYSV3
X
X#define MAXWD 1024	     /* limited by 4.2 getwd(2) */
X
X#ifdef SYSV3
X
Xchar *getcwd();
X
Xchar *
Xgetwd(path)
Xchar *path;
X{
X    return(getcwd(path,MAXWD));
X}
X
X#else
X
X/*
X * 4.2bsd getwd simulation for Sys V.2
X */
X
X#include <stdio.h>
X
X#define MAXWD 1024	     /* limited by 4.2 getwd(2) */
X
Xchar *
Xgetwd(path)
Xchar *path;
X{
X     char *nlp;
X     FILE *fp;
X     FILE *popen();
X     char *strrchr();
X
X	putenv("IFS= \t\n");
X     fp = popen("PATH=/bin:/usr/bin pwd", "r");
X     if (fp == NULL)
X	     return 0;
X     if (fgets(path, MAXWD, fp) == NULL) {
X	     (void) pclose(fp);
X	     return 0;
X     }
X     if ((nlp = strrchr(path, '\n')) != NULL)
X	     *nlp = '\0';
X     (void) pclose(fp);
X     return path;
X}
X#endif
X
________This_Is_The_END________
if test `wc -c < getwd.c` -ne      787; then
	echo 'shar: getwd.c was damaged during transit (should have been      787 bytes)'
fi
fi		; : end of overwriting check
echo 'x - rename.c'
if test -f rename.c; then echo 'shar: not overwriting rename.c'; else
sed 's/^X//' << '________This_Is_The_END________' > rename.c
X/*
X * substitute for BSD/SVR3 rename() system call, from
X * Janet Walz, walz@mimsy.umd.edu & Rich Salz, rsalz@pineapple.bbn.com
X */
X
Xint rename(oldname,newname)
Xchar *oldname,*newname;
X{
X	(void)unlink(newname);
X	if(link(oldname,newname))
X		return(-1);
X	return(unlink(oldname));
X}
________This_Is_The_END________
if test `wc -c < rename.c` -ne      280; then
	echo 'shar: rename.c was damaged during transit (should have been      280 bytes)'
fi
fi		; : end of overwriting check
echo 'x - scandir.3'
if test -f scandir.3; then echo 'shar: not overwriting scandir.3'; else
sed 's/^X//' << '________This_Is_The_END________' > scandir.3
X.TH SCANDIR 3
X.SH NAME
Xscandir, alphasort \- scan a directory
X.SH SYNOPSIS
X.nf
X.ft B
X#include <sys/types.h>
X#include <sys/dirent.h>
X
Xint
Xscandir(name, list, selector, sorter)
X.in +4n
Xchar *name;
Xstruct dirent ***list;
Xint (*selector)();
Xint (*sorter)();
X.in -4n
X
Xint
Xalphasort(d1, d2)
X.in +4n
Xstruct dirent **d1;
Xstruct dirent **d2;
X.in -4n
X.ft R
X.fi
X.SH DESCRIPTION
X.I Scandir
Xreads the directory
X.I name
Xand builds a NULL\-terminated array of pointers to the entries found
Xin that directory.
XThis array is put into the location pointed to by the
X.I list
Xparameter.
X.PP
XIf the
X.I selector
Xparameter is non\-NULL, it is taken to be a pointer to a function called
Xwith each entry, to determine whether or not it should be included in
Xthe returned list.
XIf the parameter is NULL, all entries are included.
X.PP
XAs an added feature, the entries can be sorted (with
X.IR qsort (3))
Xbefore the list is returned.
XIf the
X.I sorter
Xparameter is non\-NULL, it is passed to qsort to use as the comparison
Xfunction.
XThe
X.I alphasort
Xroutine is provided to sort the array alphabetically.
X.PP
XThe array pointed to by
X.I list
Xand the items it points to are all space obtained through
X.IR malloc (3),
Xand their storage can be reclaimed as shown in the example below.
X.SH "EXAMPLE"
XHere is a small
X.IR ls (1)\-like
Xprogram:
X.ne 50
X.RS
X.nf
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/dir.h>
X
Xextern int alphasort();
X
Xstatic int
Xfilesonly(e)
X	struct dirent *e;
X{
X	struct stat sb;
X
X	return(stat(e->d_name, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFREG);
X}
X
Xmain(ac, av)
X	int ac;
X	char *av[];
X{
X	register int i;
X	register int j;
X	struct dirent **list;
X
X	if (ac != 2) {
X		fprintf(stderr, "usage: %s dirname\n", av[0]);
X		exit(1);
X	}
X	if (chdir(av[1]) < 0) {
X		perror(av[1]);
X		exit(1);
X	}
X	if ((i = scandir(".", &list, filesonly, alphasort)) < 0) {
X		perror("Error reading directory");
X		exit(1);
X	}
X	for (j = 0; j < i; j++)
X		printf("%s\n", list[j]->d_name);
X	for (j = 0; j < i; j++)
X		free((char *)list[j]);
X	free((char *)list);
X	exit(0);
X}
X.fi
X.RE
X.SH "SEE ALSO"
Xdirectory(3), qsort(3)
X.SH DIAGNOSTICS
XReturns the number of entries in the ``list,'' or \-1 if the directory
Xcould not be opened or a memory allocation failed.
X.SH BUGS
XThe routine can be slightly wasteful of space.
________This_Is_The_END________
if test `wc -c < scandir.3` -ne     2353; then
	echo 'shar: scandir.3 was damaged during transit (should have been     2353 bytes)'
fi
fi		; : end of overwriting check
echo 'x - scandir.c'
if test -f scandir.c; then echo 'shar: not overwriting scandir.c'; else
sed 's/^X//' << '________This_Is_The_END________' > scandir.c
X/*
X**  SCANDIR
X**  Scan a directory, collecting all (selected) items into a an array.
X*/
X#include <stdio.h>
X#include <sys/types.h>
X#include <dirent.h>
X
X#ifdef	RCSID
Xstatic char RCS[] = "$Header: /cvsroot/arc/arc/Sysvarcstuf,v 1.2 2003/10/31 02:22:36 highlandsun Exp $";
X#endif	/* RCSID */
X
X/* Initial guess at directory size. */
X#define INITIAL_SIZE	20
X
X/* A convenient shorthand. */
Xtypedef struct dirent	 ENTRY;
X	    
X#define DIRSIZ(d) (sizeof(struct dirent) + strlen(d->d_name) + 1) 
X
X/* Linked in later. */
Xextern char		*malloc();
Xextern char		*realloc();
Xextern char		*strcpy();
X
X
Xint
Xscandir(Name, List, Selector, Sorter)
X    char		  *Name;
X    ENTRY		***List;
X    int			 (*Selector)();
X    int			 (*Sorter)();
X{
X    register ENTRY	 **names;
X    register ENTRY	  *E;
X    register DIR	  *Dp;
X    register int	   i;
X    register int	   size;
X
X    /* Get initial list space and open directory. */
X    size = INITIAL_SIZE;
X    if ((names = (ENTRY **)malloc(size * sizeof names[0])) == NULL
X     || (Dp = opendir(Name)) == NULL)
X	return(-1);
X
X    /* Read entries in the directory. */
X    for (i = 0; E = readdir(Dp); )
X	if (Selector == NULL || (*Selector)(E)) {
X	    /* User wants them all, or he wants this one. */
X	    if (++i >= size) {
X		size <<= 1;
X		names = (ENTRY **)realloc((char *)names, size * sizeof names[0]);
X		if (names == NULL) {
X		    closedir(Dp);
X		    return(-1);
X		}
X	    }
X
X	    /* Copy the entry. */
X	    if ((names[i - 1] = (ENTRY *)malloc(DIRSIZ(E))) == NULL) { 
X		closedir(Dp);
X		return(-1);
X	    }
X	    names[i - 1]->d_ino = E->d_ino;
X	    names[i - 1]->d_reclen = E->d_reclen;
X	 /*   names[i - 1]->d_namlen = E->d_namlen; */
X	    (void)strcpy(names[i - 1]->d_name, E->d_name);
X	}
X
X    /* Close things off. */
X    names[i] = NULL;
X    *List = names;
X    closedir(Dp);
X
X    /* Sort? */
X    if (i && Sorter)
X	qsort((char *)names, i, sizeof names[0], Sorter);
X
X    return(i);
X}
________This_Is_The_END________
if test `wc -c < scandir.c` -ne     1875; then
	echo 'shar: scandir.c was damaged during transit (should have been     1875 bytes)'
fi
fi		; : end of overwriting check
echo 'x - utimes.c'
if test -f utimes.c; then echo 'shar: not overwriting utimes.c'; else
sed 's/^X//' << '________This_Is_The_END________' > utimes.c
X
X/* bsd utimes emulation for Sys V */
X/* by Jon Zeeff */
X
X#include <sys/types.h>
X
Xstruct utimbuf {
X     time_t  actime;
X     time_t  modtime;
X};
X
Xstruct timeval {
X     long    tv_sec;
X     long    tv_usec;
X};
X
Xutimes(path,tvp)
Xchar *path;
Xstruct timeval tvp[2];
X{
X
Xstruct utimbuf times;
X
Xtimes.actime = (time_t) tvp[0].tv_sec;
Xtimes.modtime = (time_t) tvp[1].tv_sec;
X
Xreturn utime(path,times);
X
X}
________This_Is_The_END________
if test `wc -c < utimes.c` -ne      397; then
	echo 'shar: utimes.c was damaged during transit (should have been      397 bytes)'
fi
fi		; : end of overwriting check
exit 0