File: filedr.c

package info (click to toggle)
lam 7.1.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 54,668 kB
  • ctags: 17,034
  • sloc: ansic: 156,264; sh: 9,976; cpp: 7,699; makefile: 5,590; perl: 476; fortran: 260; asm: 83
file content (471 lines) | stat: -rw-r--r-- 9,268 bytes parent folder | download | duplicates (9)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: filedr.c,v 6.11 2003/01/13 19:15:06 jsquyres Exp $
 *
 *	Function:	- file system diagnostic tool (file doctor)
 *			- tests the remote file system functions
 *
 *	Examples:	  filedr -c -t -rw -g ps -l 30
 *			  filedr -ro -f junk -m 664
 *			  filedr -v -wr -f junk -l 1000
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <netdb.h>
#include <unistd.h>

#include <args.h>
#include <etc_misc.h>
#include <lamdebug.h>
#include <freq.h>
#include <net.h>
#include <terror.h>
#include <typical.h>

/*
 * local functions
 */
static void		help();		/* print help */
static void		fdr_close();	/* close file */
static void		fdr_init();	/* init flags based on options */
static void 		fdr_open();	/* open file */
static void 		fdr_read();	/* read data from file */
static void		fdr_write();	/* write data into file */
static int		otod();		/* converts octal string to dec */

/*
 * local variables
 */
static char		*usage =
"filedr [-chtv] -ro/wo/rw/wr [-f filename] [-g flags] [-m modes] [-l length]\n";

static int		fl_close;	/* close file */
static int		fl_rdwr;	/* do read then write */
static int		fl_read;	/* file read flag */
static int		fl_stdfile;	/* stdin, stdout, stderr */
static int		fl_time;	/* timing results */
static int		fl_verbose;	/* verbose flag */
static int		fl_write;	/* file write flag */
static int		fl_wrrd;	/* do write then read */

static int		flags;		/* open file with flags	*/
static int		modes;		/* open file with modes */
static int		numbytes;	/* num of bytes to rd/wr */
static int		handle;		/* file handle/udesc */
static char		*filename;	/* file name */
static char		*buffer;	/* read-write buffer */

/*
 *	main
 */
int
main(argc, argv)

int			argc;
char			*argv[];

{
/*
 * Parse command line.
 */
	validopts("chtvN");
	followed("fgml");
	twoletter("row");
	twoletter("wor");

	if (do_args(&argc, argv) || (errno = (argc == 1) ? 0 : EUSAGE)) {
		fprintf(stderr, usage);
		exit(errno);
	}
/*
 * Check for help request.
 */
	if (opt_taken('h')) {
		help();
		kexit(0);
	}
/*
 * Attach to kernel.
 */
	if (kinit(0)) {
	  char hostname[MAXHOSTNAMELEN];
	  gethostname(hostname, MAXHOSTNAMELEN);

	  show_help(NULL, "no-lamd", "filedr", hostname, NULL);
	  exit(0);
	}
/*
 * Initialize flags based on selected options.
 */
	fdr_init();
/*
 * Perform specified file operations.
 */
	if (!fl_stdfile) {
		fdr_open();
	}

	if (numbytes > 0) {

		if (fl_read || fl_rdwr) {
			fdr_read();
/*
 * Close and open file again.
 */
			if (fl_rdwr) {

				if ((!fl_stdfile) && fl_close) {
	    				fdr_close();
					flags &= ~LAM_O_CREAT;
					fdr_open();
            			}

				fdr_write();
			}
		} 

		if (fl_write || fl_wrrd) {
			fdr_write();

			if (fl_wrrd) {
/* 
 * Close and open file again.
 */
				if ((!fl_stdfile) && fl_close) {
					fdr_close();
					flags &= ~LAM_O_CREAT;
					fdr_open();
				}

				fdr_read();
			}
		}
	}

	if ((!fl_stdfile) && fl_close) {
		fdr_close();
	}

	return(0);
}

/*
 *	fdr_open
 *
 *	Function:	- opens non-stdio file
 */
static void
fdr_open()

{
	double		timeval;

	if (fl_time) timeval = ttime();

	VERBOSE("opening %s ...\n", filename);
	if ((handle = lam_rfopen(filename, flags, modes)) < 0)
			lamfail("filedr (lam_rfopen)"); 

	if (fl_time) timeval = ttime() - timeval;

	if (fl_time) {
		printf("opened %s in %f seconds\n", filename, timeval);
	}
} 

/*
 *	fdr_close
 *
 *	Function:	- closes file
 */
static void
fdr_close()

{
	double		timeval;

	if (fl_time) timeval = ttime();

	VERBOSE("closing %s ...\n", filename);
	if (lam_rfclose(handle)) lamfail("filedr (lam_rfclose)");

	if (fl_time) timeval = ttime() - timeval;

	if (fl_time) {
                printf("closed %s in %f seconds\n", filename, timeval);
        }
}

/*
 *	fdr_read()
 *
 *	Function:	- reads data from file
 */
static void
fdr_read()

{
	double		timeval;
	int		i;
	int		ret_bytes;
/*
 * Read numbytes amount of data.
 */
	buffer = malloc ((unsigned) numbytes);
	if (buffer == 0) lamfail("filedr (malloc)");

	for (i = 0; i < numbytes; i++) {
		buffer[i] = ' ';
	}

	VERBOSE("reading %d bytes from %s ...\n", numbytes, filename);
	if (fl_time) timeval = ttime();
	ret_bytes = lam_rfread(handle, buffer, numbytes);
	if (ret_bytes < 0) lamfail("filedr (lam_rfread)");
	if (fl_time) timeval = ttime() - timeval;

        if (fl_time) {
		printf("read %d bytes in %f seconds\n", ret_bytes, timeval);
        }

	free(buffer);
}

/*
 *	fdr_write
 *
 *	Function:	- writes data to a file
 */
static void
fdr_write()

{
	double		timeval;
	int 		i;
	int		ret_bytes;

        buffer = malloc ((unsigned) numbytes);
        if (buffer == NULL) lamfail("filedr (malloc)");

	for (i = 0; i < numbytes; i++) {
		buffer[i] = (char) (i % 256);
	}

	VERBOSE("writing %d bytes to %s ...\n", numbytes, filename);
	if (fl_time) timeval = ttime();
	ret_bytes = lam_rfwrite(handle, buffer, numbytes);
	if (ret_bytes < 0) lamfail("filedr (lam_rfwrite)");
	if (fl_time) timeval = ttime() - timeval;

	if (fl_time) {
        	printf("wrote %d bytes in %f seconds\n", ret_bytes, timeval);
        }

	free(buffer);
}
	
/*
 *	help
 *
 *	Function:	- prints useful information on this command
 */
static void
help()

{
	printf("\nSynopsis:\tfiledr <options>\n");
	printf("\nDescription:\tTest and time remote file system access.\n");
	printf("\nOptions:\t-h\t\tPrint this message.\n");
	printf("\t\t-c\t\tDo not close the file.\n");
	printf("\t\t-t\t\tTime file operations.\n");
	printf("\t\t-v\t\tBe verbose.\n");
	printf("\t\t-ro\t\tRead-only the file. (default)\n");
	printf("\t\t-wo\t\tWrite-only the file.\n");
	printf("\t\t-rw\t\tRead then write the file.\n");
	printf("\t\t-wr\t\tWrite then read the file.\n");
	printf("\t\t-f <filename>\tWork on this file.\n");
	printf("\t\t-g <flags>\tOpen file with flags.\n");
	printf("\t\t-m <modes>\tCreate with permission modes (octal).\n");
	printf("\t\t-l <#bytes>\tSet transfer length.\n");
	printf("\nOpen Flags:\tstring of characters from the set:\n");
	printf("\t\t\tp,c,t,x,e\n");
	printf("\t\tfor:\n");
	printf("\t\t\tappend, create, trunc, excl, reuse\n");
}

/*
 *	fdr_init
 *
 *	Function:	- sets option flags and parameters
 */
static void
fdr_init()

{
	char 	*str_flags;
	char 	*str_modes;
	int 	other_chr;
/*
 * Check for switches.
 */
	fl_close = !opt_taken('c');
	fl_time = opt_taken('t');

	fl_verbose = opt_taken('v');
/*
 * Check for parameters.
 */
	fl_stdfile = FALSE;
	fl_read = FALSE;
	fl_write = FALSE;
	fl_rdwr = FALSE;
	fl_wrrd = FALSE;
	flags = 0;

	other_chr = get_2nd_let('r');

	if (other_chr == 'o') {
		fl_read = TRUE;
		flags = flags | LAM_O_RDONLY;
	} else if (other_chr == 'w') {
		fl_rdwr = TRUE;
		flags = flags | LAM_O_RDWR;
	} else {
		other_chr = get_2nd_let('w');

		if (other_chr == 'o') {
			fl_write = TRUE;
			flags = flags | LAM_O_WRONLY;
		} else if (other_chr == 'r') {
			fl_wrrd = TRUE;
			flags = flags | LAM_O_RDWR;
		} else {
			fprintf(stderr, usage);
			kexit(EUSAGE);
		}
	}

	if (opt_taken('f')) {
		filename = getparam('f');

		if (strcmp(filename, "stdin") == 0) {
			handle = 0;
			fl_stdfile = TRUE;
		} else if (strcmp(filename, "stdout") == 0) {
			handle = 1;
			fl_stdfile = TRUE;
		} else if (strcmp(filename, "stderr") == 0) {
			handle = 2;
			fl_stdfile = TRUE;
		} else {
			handle = -1;
		}
	}

	else {
		filename = "testfile";
		VERBOSE("operating on default file 'testfile'\n");
	}

	if (opt_taken('g')) {
		str_flags = getparam('g');
		if (strchr(str_flags,'p') != NULL)
				flags = flags | LAM_O_APPEND;
		else if (strchr(str_flags,'c') != NULL)
				flags = flags | LAM_O_CREAT;
		else if (strchr(str_flags,'t') != NULL)
				flags = flags | LAM_O_TRUNC;
		else if (strchr(str_flags,'x') != NULL)
				flags = flags | LAM_O_EXCL;
		else if (strchr(str_flags,'e') != NULL)
				flags = flags | LAM_O_REUSE;
		else {
			fprintf(stderr, usage);
			kexit(EUSAGE);
		}
	}
	
	if (opt_taken('m')) {
		str_modes = getparam('m');

		if ((modes = otod(str_modes)) == -1) {
			fprintf(stderr, "filedr: modes must be in octal\n");
			kexit(EUSAGE);
		}
	} else {
		modes = 0644;
	}

	if (opt_taken('l')) {
		if (intparam('l', &numbytes)) lamfail("filedr (intparam)");
	} else {
		numbytes = 1;
	}
}

/*
 *	otod
 *
 *	Function:	- converts a string of octal digits into decimal number
 *			- input string must be unsigned and may begin
 *			  with a zero
 */
static int 
otod(s)

char			*s;

{
	int		dec;
	int		mult;
	int		n;
	char		c;
	char		*p;

	if (s[0] == '0') {
		s++;
	}

	n = strlen(s);

	if (n < 1) {
		return(0);
	}

	p = s + n - 1;
	dec = 0;
	mult = 1;

	while (p + 1 != s) {
		c = *p;

		if ((c >= '0') && (c <= '7')) {
			n = c - '0';
		} else {
			return(-1);
		}

		dec = dec + (n * mult);
		mult *= 8;
		p = p-1;
	}

	return(dec);
}