summaryrefslogtreecommitdiffstats
path: root/cpukit/libfs/src/nfsclient/src/dirutils.c
blob: 357aab96dfbb309eabec1132b86a539797ffce8c (plain) (blame)
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
/* very crude and basic fs utilities for testing the NFS */

/* Till Straumann, <strauman@slac.stanford.edu>, 10/2002 */

/*
 * Authorship
 * ----------
 * This software (NFS-2 client implementation for RTEMS) was created by
 *     Till Straumann <strauman@slac.stanford.edu>, 2002-2007,
 * 	   Stanford Linear Accelerator Center, Stanford University.
 *
 * Acknowledgement of sponsorship
 * ------------------------------
 * The NFS-2 client implementation for RTEMS was produced by
 *     the Stanford Linear Accelerator Center, Stanford University,
 * 	   under Contract DE-AC03-76SFO0515 with the Department of Energy.
 *
 * Government disclaimer of liability
 * ----------------------------------
 * Neither the United States nor the United States Department of Energy,
 * nor any of their employees, makes any warranty, express or implied, or
 * assumes any legal liability or responsibility for the accuracy,
 * completeness, or usefulness of any data, apparatus, product, or process
 * disclosed, or represents that its use would not infringe privately owned
 * rights.
 *
 * Stanford disclaimer of liability
 * --------------------------------
 * Stanford University makes no representations or warranties, express or
 * implied, nor assumes any liability for the use of this software.
 *
 * Stanford disclaimer of copyright
 * --------------------------------
 * Stanford University, owner of the copyright, hereby disclaims its
 * copyright and all other rights in this software.  Hence, anyone may
 * freely use it for any purpose without restriction.
 *
 * Maintenance of notices
 * ----------------------
 * In the interest of clarity regarding the origin and status of this
 * SLAC software, this and all the preceding Stanford University notices
 * are to remain affixed to any copy or derivative of this software made
 * or distributed by the recipient and are to be affixed to any copy of
 * software made or distributed by the recipient that contains a copy or
 * derivative of this software.
 *
 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef __vxworks
#include <vxWorks.h>
#endif
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h> /* PATH_MAX */

#include <inttypes.h> /* PRI* */

#if SIZEOF_MODE_T == 8
#define PRIomode_t PRIo64
#elif SIZEOF_MODE_T == 4
#define PRIomode_t PRIo32
#else
#error "unsupport size of mode_t"
#endif

#if SIZEOF_OFF_T == 8
#define PRIdoff_t PRIo64
#elif SIZEOF_OFF_T == 4
#define PRIdoff_t PRIo32
#else
#error "unsupported size of off_t"
#endif

#ifdef HAVE_CEXP
#include <cexpHelp.h>
#endif

#ifndef __vxworks
int
pwd(void)
{
char buf[PATH_MAX];

	if ( !getcwd(buf,PATH_MAX)) {
		perror("getcwd");
		return -1;
	} else {
		printf("%s\n",buf);
	}
	return 0;
}

static int
ls_r(char *path, char *chpt, char *name, struct stat *buf)
{
char *t;
	sprintf(chpt, "/%s", name);
	if (lstat(path,buf)) {
		fprintf(stderr,"stat(%s): %s\n", path, strerror(errno));
		return -1;
	}
	switch ( buf->st_mode & S_IFMT ) {
		case S_IFSOCK:
		case S_IFIFO:	t = "|"; break;

		default:
		case S_IFREG:
		case S_IFBLK:
		case S_IFCHR:
						t = "";  break;
		case S_IFDIR:
						t = "/"; break;
		case S_IFLNK:
						t = "@"; break;
	}

	printf("%10li, %10" PRIdoff_t "b, %5i.%-5i 0%04" PRIomode_t " %s%s\n",
				buf->st_ino,
				buf->st_size,
				buf->st_uid,
				buf->st_gid,
				buf->st_mode & ~S_IFMT,
				name,
				t);
	*chpt = 0;
	return 0;
}

int
ls(char *dir, char *opts)
{
struct dirent	*de;
char			path[PATH_MAX+1];
char			*chpt;
DIR				*dp  = 0;
int				rval = -1;
struct stat		buf;

	if ( !dir )
		dir = ".";

	strncpy(path, dir, PATH_MAX);
	path[PATH_MAX] = 0;
	chpt = path+strlen(path);

	if ( !(dp=opendir(dir)) ) {
		perror("opendir");
		goto cleanup;
	}

	while ( (de = readdir(dp)) ) {
		ls_r(path, chpt, de->d_name, &buf);
	}

	rval = 0;

cleanup:
	if (dp)
		closedir(dp);
	return rval;
}
#endif

#if 0
		fprintf(stderr, "usage: cp(""from"",[""to""[,""-f""]]\n");
		fprintf(stderr, "          ""to""==NULL -> stdout\n");
		fprintf(stderr, "          ""-f""       -> overwrite existing file\n");
#endif

int
cp(char *from, char *to, char *opts)
{
struct stat	st;
int			rval  = -1;
int			fd    = -1;
FILE		*fst  = 0;
FILE		*tst  = 0;
int			flags = O_CREAT | O_WRONLY | O_TRUNC | O_EXCL;

	if (from) {

	if ((fd=open(from,O_RDONLY,0)) < 0) {
		fprintf(stderr,
				"Opening %s for reading: %s\n",
				from,
				strerror(errno));
		goto cleanup;
	}

	if (fstat(fd, &st)) {
		fprintf(stderr,
				"rstat(%s): %s\n",
				from,
				strerror(errno));
		goto cleanup;
	}


	if (!S_ISREG(st.st_mode)) {
		fprintf(stderr,"Refuse to copy a non-regular file\n");
		errno = EINVAL;
		goto cleanup;
	}
	/* Now create a stream -- I experienced occasional weirdness
	 * when circumventing the streams attached to fildno(stdin)
	 * by reading/writing to the underlying fd's directly ->
	 * for now we always go through buffered I/O...
	 */
	if ( !(fst=fdopen(fd,"r")) ) {
		fprintf(stderr,
				"Opening input stream [fdopen()] failed: %s\n",
				strerror(errno));
		goto cleanup;
	}
	/* at this point, we have a stream and don't need 'fd' anymore */
	fd = -1;

	} else {
		fst			= stdin;
		st.st_mode	= 0644;
	}

	if (opts && strchr(opts,'f'))
		flags &= ~ O_EXCL;

	if (to) {
		if ( (fd=open(to,flags,st.st_mode)) < 0 ) {
			fprintf(stderr,
					"Opening %s for writing: %s\n",
					to,
					strerror(errno));
			goto cleanup;
		}
		if ( !(tst=fdopen(fd, "w")) ) {
			fprintf(stderr,
					"Opening output stream [fdopen()] failed: %s\n",
					strerror(errno));
			goto cleanup;
		}
		/* at this point we have a stream and don't need 'fd' anymore */
		fd = -1;
	} else {
		tst = stdout;
	}

	/* clear old errors */
	clearerr(fst);
	clearerr(tst);

	/* use macro versions on register vars; stdio is already buffered,
	 * there's nothing to be gained by reading/writing blocks into
	 * a secondary buffer...
	 */
	{
	register int ch;
	register FILE *f = fst;
	register FILE *t = tst;
		while ( EOF != (ch = getc(f)) && EOF != putc(ch, t) )
			/* nothing else */;
	}

	if ( ferror(fst) ) {
		fprintf(stderr,"Read error: %s\n",strerror(errno));
		goto cleanup;
	}
	if ( ferror(tst) ) {
		fprintf(stderr,"Write error: %s\n",strerror(errno));
		goto cleanup;
	}

	rval = 0;

cleanup:

	if ( fd >= 0 )
		close(fd);

	if ( fst ) {
		if ( from )
			fclose(fst);
		else
			clearerr(fst);
	}
	if ( tst ) {
		if ( to )
			fclose(tst);
		else {
			/* flush stdout */
			fflush(tst);
			clearerr(tst);
		}
	}

	return rval;
}

int
ln(char *to, char *name, char *opts)
{
	if (!to) {
		fprintf(stderr,"ln: need 'to' argument\n");
		return -1;
	}
	if (!name) {
		if ( !(name = strrchr(to,'/')) ) {
			fprintf(stderr,
					"ln: 'unable to link %s to %s\n",
					to,to);
			return -1;
		}
		name++;
	}
	if (opts || strchr(opts,'s')) {
		if (symlink(name,to)) {
			fprintf(stderr,"symlink: %s\n",strerror(errno));
			return -1;
		}
	} else {
		if (link(name,to)) {
			fprintf(stderr,"hardlink: %s\n",strerror(errno));
			return -1;
		}
	}
	return 0;
}

int
rm(char *path)
{
	return unlink(path);
}

int
cd(char *path)
{
	return chdir(path);
}

#ifdef HAVE_CEXP
static CexpHelpTabRec _cexpHelpTabDirutils[] __attribute__((unused)) = {
	HELP(
"copy a file: cp(""from"",[""to""[,""-f""]])\n\
                 from = NULL <-- stdin\n\
                 to   = NULL --> stdout\n\
                 option -f: overwrite existing file\n",
		int,
		cp, (char *from, char *to, char *options)
		),
	HELP(
"list a directory: ls([""dir""])\n",
		int,
		ls, (char *dir)
		),
	HELP(
"remove a file\n",
		int,
		rm, (char *path)
		),
	HELP(
"change the working directory\n",
		int,
		cd, (char *path)
		),
	HELP(
"create a link: ln(""to"",""name"",""[-s]""\n\
                   -s creates a symlink\n",
		int,
		ln, (char *to, char *name, char *options)
		),
	HELP("",,0,)
};
#endif