summaryrefslogtreecommitdiffstats
path: root/basic/subr.c
blob: cf220b7ec373454fd41c340259c9a7546ddb9774 (plain)
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/*
 *	@(#)subr.c	1.6 03/12/29 Connectathon Testsuite
 *	1.6 Lachman ONC Test Suite source
 *
 * Useful subroutines shared by all tests
 */

#if defined (DOS) || defined (WIN32)
/* If Dos, Windows or Win32 */
#define DOSorWIN32
#endif

#ifdef DOSorWIN32
#include <io.h>
#include <direct.h>
#else
#include <sys/param.h>
#include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/timeb.h>

#ifdef DOSorWIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef STDARG
#include <stdarg.h>
#endif

#include "../tests.h"

char *Myname;
int Dflag = 0;

static void chdrive ARGS_((char *path));

/*
 * Build a directory tree "lev" levels deep
 * with "files" number of files in each directory
 * and "dirs" fan out.  Starts at the current directory.
 * "fname" and "dname" are the base of the names used for
 * files and directories.
 */
void
dirtree(lev, files, dirs, fname, dname, totfiles, totdirs)
	int lev;
	int files;
	int dirs;
	char *fname;
	char *dname;
	int *totfiles;
	int *totdirs;
{
	int fd;
	int f, d;
	char name[MAXPATHLEN];

	if (lev-- == 0) {
		return;
	}
	for ( f = 0; f < files; f++) {
		if (Dflag == 0) 
			sprintf(name, "%s%d", fname, f);
		else
			sprintf(name, "%s%d.%d", fname, lev, f);
		if ((fd = creat(name, CHMOD_RW)) < 0) {
			error("creat %s failed", name);
			exit(1);
		}
		(*totfiles)++;
		if (close(fd) < 0) {
			error("close %d failed", fd);
			exit(1);
		}
	}
	for ( d = 0; d < dirs; d++) {
		if (Dflag == 0) 
			sprintf(name, "%s%d", dname, d);
		else
			sprintf(name, "%s%d.%d", dname, lev, d);
		if (unix_mkdir(name, 0777) < 0) {
			error("mkdir %s failed", name);
			exit(1);
		}
		(*totdirs)++;
		if (unix_chdir(name) < 0) {
			error("chdir %s failed", name);
			exit(1);
		}
		dirtree(lev, files, dirs, fname, dname, totfiles, totdirs);
		if (unix_chdir("..") < 0) {
			error("chdir .. failed");
			exit(1);
		}
	}
}

/*
 * Remove a directory tree starting at the current directory.
 * "fname" and "dname" are the base of the names used for
 * files and directories to be removed - don't remove anything else!
 * "files" and "dirs" are used with fname and dname to generate
 * the file names to remove.
 *
 * This routine will fail if, say after removing known files,
 * the directory is not empty.
 *
 * This is used to test the unlink function and to clean up after tests.
 */
void
rmdirtree(lev, files, dirs, fname, dname, totfiles, totdirs, ignore)
	int lev;
	int files;
	int dirs;
	char *fname;
	char *dname;
	int *totfiles;		/* total removed */
	int *totdirs;		/* total removed */
	int ignore;
{
	int f, d;
	char name[MAXPATHLEN];

	if (lev-- == 0) {
		return;
	}
	for ( f = 0; f < files; f++) {
		if (Dflag == 0) 
			sprintf(name, "%s%d", fname, f);
		else
			sprintf(name, "%s%d.%d", fname, lev, f);
		if (unlink(name) < 0 && !ignore) {
			error("unlink %s failed", name);
			exit(1);
		}
		(*totfiles)++;
	}
	for ( d = 0; d < dirs; d++) {
		if (Dflag == 0) 
			sprintf(name, "%s%d", dname, d);
		else
			sprintf(name, "%s%d.%d", dname, lev, d);
		if (unix_chdir(name) < 0) {
			if (ignore)
				continue;
			error("chdir %s failed", name);
			exit(1);
		}
		rmdirtree(lev, files, dirs, fname, dname, totfiles, totdirs, ignore);
		if (unix_chdir("..") < 0) {
			error("chdir .. failed");
			exit(1);
		}
		if (rmdir(name) < 0) {
			error("rmdir %s failed", name);
			exit(1);
		}
		(*totdirs)++;
	}
}

#ifdef STDARG
void
error(char *str, ...)
{
	int oerrno;
	char *ret;
	char path[MAXPATHLEN];
	va_list ap;

	oerrno = errno;

	va_start(ap, str);
	if ((ret = getcwd(path, sizeof(path))) == NULL)
		fprintf(stderr, "%s: getcwd failed\n", Myname);
	else
		fprintf(stderr, "\t%s: (%s) ", Myname, path);
	vfprintf(stderr, str, ap);
	va_end(ap);

	if (oerrno) {
		errno = oerrno;
		perror(" ");
	} else {
		fprintf(stderr, "\n");
	}
	fflush(stderr);
	if (ret == NULL)
		exit(1);
}
#else
/* VARARGS */
error(str, ar1, ar2, ar3, ar4, ar5, ar6, ar7, ar8, ar9)
	char *str;
{
	int oerrno;
	char *ret;
	char path[MAXPATHLEN];

	oerrno = errno;
	if ((ret = getcwd(path, sizeof(path))) == NULL)
		fprintf(stderr, "%s: getcwd failed\n", Myname);
	else
		fprintf(stderr, "\t%s: (%s) ", Myname, path);

	fprintf(stderr, str, ar1, ar2, ar3, ar4, ar5, ar6, ar7, ar8, ar9);
	if (oerrno) {
		errno = oerrno;
		perror(" ");
	} else {
		fprintf(stderr, "\n");
	}
	fflush(stderr);
	if (ret == NULL)
		exit(1);
}
#endif /* STDARG */

static struct timeval ts, te;

/*
 * save current time in struct ts
 */
void
starttime()
{

	gettimeofday(&ts, (struct timezone *)0);
}

/*
 * sets the struct tv to the difference in time between
 * current time and the time in struct ts.
 */
void
endtime(tv)
	struct timeval *tv;
{

	gettimeofday(&te, (struct timezone *)0);
	if (te.tv_usec < ts.tv_usec) {
		te.tv_sec--;
		te.tv_usec += 1000000;
	}
	tv->tv_usec = te.tv_usec - ts.tv_usec;
	tv->tv_sec = te.tv_sec - ts.tv_sec;
#ifdef DOS
	/*
	 * DOS uses time since midnight, so it could go negative if the
	 * test spans midnight.  If that happens, add a day.
	 */
	if (tv->tv_sec < 0)
		tv->tv_sec += 24 * 3600;
#endif
}

/*
 * Set up and move to a test directory
 */
void
testdir(dir)
	char *dir;
{
	struct stat statb;
	char str[MAXPATHLEN];

	/*
	 *  If dir is non-NULL, use that dir.  If NULL, first
	 *  check for env variable NFSTESTDIR.  If that is not
	 *  set, use the compiled-in TESTDIR.
	 */
	if (dir == NULL)
		if ((dir = getenv("NFSTESTDIR")) == NULL)
			dir = TESTDIR;

	if (stat(dir, &statb) == 0) {
		sprintf(str, "rm -r %s", dir);
#ifdef WIN16
		if (rmdir(dir) < 0) {
#else
		if (system(str) != 0) {
#endif
			error("can't remove old test directory %s", dir);
			exit(1);
		}
	}

	if (unix_mkdir(dir, 0777) < 0) {
		error("can't create test directory %s", dir);
		exit(1);
	}
	if (unix_chdir(dir) < 0) {
		error("can't chdir to test directory %s", dir);
		exit(1);
	}
}

/*
 * Move to a test directory
 */
int
mtestdir(dir)
	char *dir;
{
	/*
	 *  If dir is non-NULL, use that dir.  If NULL, first
	 *  check for env variable NFSTESTDIR.  If that is not
	 *  set, use the compiled-in TESTDIR.
	 */
	if (dir == NULL)
		if ((dir = getenv("NFSTESTDIR")) == NULL)
			dir = TESTDIR;

	if (unix_chdir(dir) < 0) {
		error("can't chdir to test directory %s", dir);
		return(-1);
	}
	return(0);
}

/*
 *  get parameter at parm, convert to int, and make sure that
 *  it is at least min.
 */
long
getparm(parm, min, label)
	char *parm; 
	long min;
	char *label;
{
	long val;

	val = atol(parm);
	if (val < min) {
		error("Illegal %s parameter %ld, must be at least %ld",
			label, val, min);
		exit(1);
	}
	return(val);
}

/*
 *  exit point for successful test
 */
void
complete()
{

	fprintf(stdout, "\t%s ok.\n", Myname);
	chdrive(Myname);
	exit(0);
}

/*
 * Change to drive specified in path
 */
int
unix_chdir(path)
char *path;
{
	chdrive(path);
	return chdir(path);
}

#ifndef DOSorWIN32

static void
chdrive(path)
	char *path;
{
}

int
unix_mkdir(path, mode)
	char *path;
	mode_t mode;
{
	return mkdir(path, mode);
}

#endif /* DOSorWIN32 */


#ifdef NEED_STRERROR
/*
 * Hack replacement for strerror().  This could be made to include useful
 * error strings, but it will do for the time being.
 */
char *
strerror(errval)
	int errval;			/* errno value */
{
	static char buf[1024];

	sprintf(buf, "error %d", errval);
	return (buf);
}
#endif /* NEED_STRERROR */

/***********************************************************/
/* The following routines were ADDED specifically for      */
/* 			DOS AND WIN32.			   */
/***********************************************************/

#ifdef DOSorWIN32

/*
 * Return file statistics for the path specified
 */

int
lstat(char * path, struct stat * buf)
{
	return stat(path, buf);
}

int
unix_mkdir(const char * path, int mode)
{
	mode = mode;			/* keep lint, compiler happy */
	return mkdir(path);
}

#endif /* DOSorWIN32 */

/************************************************************/
/* The following routines were ADDED specifically for WIN32.*/
/************************************************************/

#ifdef WIN32

/*
 * Change to drive specified in path
 */
static void
chdrive(char * path)
{
	int desireddrive;

	if (path[1] == ':') {
		desireddrive = toupper(path[0]) - ('A' - 1);
		if (_chdrive(desireddrive)) {
			error("can't change to drive %c:", path[0]);
			exit(1);
		}
	}
}

void
gettimeofday(struct timeval *TV, struct timezone *TimeZone)
{
	struct _timeb  dostime;

	_ftime(&dostime);
	TV->tv_sec = dostime.time;
	TV->tv_usec = dostime.millitm * 1000L;
	TimeZone = TimeZone;    /* shut up compiler/lint */
}

int
statfs(char * path, struct statfs * buf)
{
	char *p = (char *) buf;
	int i;
	unsigned drive;
	
	unsigned    sect_per_clust;
	unsigned    bytes_per_sect;
	unsigned    free_clust;
	unsigned    clust;
	char rootpath[MAXPATHLEN];
	
	for (i = 0; i < sizeof(*buf); i++)
		*p++ = (char) -1;
	buf->f_type = 0;        /* that's what the man page says */
	if (path[1] == ':')
		drive = toupper(path[0]) - ('A' - 1);
	else
		drive = _getdrive();
	
	// GetDiskFreeSpace must have the #$%^&* root!
	// be simple-minded: must be "d:\<whatever>"
	strcpy(rootpath, path);
	p = strtok(rootpath, "\\");
	*p++ = '\\';
	*p = '\0';
	if (! GetDiskFreeSpace(rootpath, &sect_per_clust, &bytes_per_sect,
			 &free_clust, &clust)) {
		printf("GetDiskFreeSpace failed\n");
		return -1;
	}
	buf->f_bsize = bytes_per_sect;
	buf->f_blocks = clust * sect_per_clust;
	buf->f_bfree = free_clust * sect_per_clust;
	buf->f_bavail = buf->f_bfree;

	return 0;
}

/***************************************************************
DIRENT emulation for Win32
***************************************************************/
char pattern[MAXNAMLEN];
struct _finddata_t findtst;
long findhandle;
int maxentry;
int currententry;
int diropen = 0;
struct dirent *dirlist;
DIR dirst;

static void copynametolower(char *dest, char *src);
static void findt_to_dirent(struct dirent *d);
static int win32_findfirst(char *pattern);
static int win32_findnext(void);

int
win32_findfirst(char *pattern)
{
	findhandle = _findfirst(pattern, &findtst);
	return findhandle == -1;
}

int
win32_findnext(void)
{
	return _findnext(findhandle, &findtst);
}

int
win32_findclose(void)
{
	return _findclose(findhandle);
}

DIR *
opendir(char * dirname)
{
	int i;

	strcpy(pattern, dirname);
	strcat(pattern, "\\*.*");
	if (diropen)
		return NULL;
	diropen = 1;
	dirlist = (struct dirent *) malloc(512 * sizeof(struct dirent));
	if (dirlist == NULL)
		return NULL;

	if (win32_findfirst(pattern))
		return NULL;
	findt_to_dirent(&dirlist[0]);
	for (i = 1; ! win32_findnext(); i++) {
		findt_to_dirent(&dirlist[i]);
	}
	win32_findclose();

	maxentry = i - 1;
	currententry = 0;
	return &dirst;
}

void
rewinddir(DIR * dirp)
{
	int i;
	unsigned int attributes = _A_NORMAL|_A_RDONLY|_A_HIDDEN|_A_SUBDIR;

	dirp = dirp;    /* shut up compiler */

	if (win32_findfirst(pattern)) {
		error("rewind failed");
		exit(1);
	}
	findt_to_dirent(&dirlist[0]);
	for (i = 1; ! win32_findnext(); i++) {
		findt_to_dirent(&dirlist[i]);
	}
	win32_findclose();

	maxentry = i - 1;
	currententry = 0;
}

long
telldir(DIR * dirp)
{
	dirp = dirp;    /* keep compiler happy */
	return (long) currententry;
}

void
seekdir(DIR * dirp, long loc)
{
	dirp = dirp;    /* keep compiler happy */
	if (loc <= (long) maxentry)
		currententry = (int) loc;
	/* else seekdir silently fails */
}

struct dirent *
readdir(DIR * dirp)
{
	dirp = dirp;    /* shut up compiler */
	if (currententry > maxentry)
		return (struct dirent *) NULL;
	else {
		return &dirlist[currententry++];
	}
}

void
findt_to_dirent(struct dirent * d)
{
	copynametolower(d->d_name, findtst.name);
}

static void
copynametolower(char * dest, char * src)
{
	int i;
	for (i = 0; dest[i] = (char) tolower((int) src[i]); i++) {
		/* null body */
	}
}

void
closedir(DIR * dirp)
{
	dirp = dirp;    /* keep compiler happy */
	diropen = 0;
}


#endif /* WIN32 */

/***********************************************************/
/* The following routines were ADDED specifically for DOS  */
/***********************************************************/

#if defined DOS

/*
 * Change to drive specified in path
 */

static void
chdrive(path)
	char *path;
{
	int desireddrive, drive;
	if (path[1] == ':') {
		desireddrive = toupper(path[0]) - ('A' - 1);
		_dos_setdrive(desireddrive, &drive);
		_dos_getdrive(&drive);
		if (drive != desireddrive) {
			error("can't change to drive %c:", path[0]);
			exit(1);
		}
	}
}


void
gettimeofday(struct timeval *TV, struct timezone *TimeZone)
{
	struct dostime_t dostime;

	_dos_gettime(&dostime);
	TV->tv_sec = dostime.hour * 3600L
		+ dostime.minute * 60L
		+ dostime.second;
	TV->tv_usec = dostime.hsecond * 10000L;
	TimeZone = TimeZone;	/* shut up compiler/lint */
}

int
statfs(path, buf)
	char *path;
	struct statfs *buf;
{
	char *p = (char *) buf;
	int i;
	unsigned drive;
	struct diskfree_t diskspace;
	
	for (i = 0; i < sizeof(*buf); i++)
		*p++ = (char) -1;
	buf->f_type = 0;	/* that's what the man page says */
	if (path[1] == ':')
		drive = toupper(path[0]) - ('A' - 1);
	else
		_dos_getdrive(&drive);
	if (_dos_getdiskfree(drive, &diskspace))
		return -1;
	buf->f_bsize = diskspace.bytes_per_sector;
	buf->f_blocks = (long) diskspace.total_clusters
		* diskspace.sectors_per_cluster;
	buf->f_bfree = (long) diskspace.avail_clusters
		* diskspace.sectors_per_cluster;
	buf->f_bavail = buf->f_bfree;
	return 0;
}

/***************************************************************
DIRENT emulation for DOS
***************************************************************/
char pattern[MAXNAMLEN];
struct find_t findtst;
int maxentry;
int currententry;
int diropen = 0;
struct dirent *dirlist;
DIR dirst;

static void copynametolower(char *dest, char *src);
static void findt_to_dirent(struct find_t *f, struct dirent *d);

DIR *
opendir(dirname)
	char *dirname;
{
	int i;
	unsigned int attributes = _A_NORMAL|_A_RDONLY|_A_HIDDEN|_A_SUBDIR;

	strcpy(pattern, dirname);
	strcat(pattern, "\\*.*");
	if (diropen)
		return NULL;
	diropen = 1;
	dirlist = (struct dirent *) malloc(512 * sizeof(struct dirent));
	if (dirlist == NULL)
		return NULL;
	if (_dos_findfirst(pattern, attributes, &findtst))
		return NULL;
	findt_to_dirent(&findtst, &dirlist[0]);
	for (i = 1; ! _dos_findnext(&findtst); i++) {
		findt_to_dirent(&findtst, &dirlist[i]);
	}
	maxentry = i - 1;
	currententry = 0;
	return &dirst;
}

void
rewinddir(dirp)
	DIR *dirp;
{
	int i;
	unsigned int attributes = _A_NORMAL|_A_RDONLY|_A_HIDDEN|_A_SUBDIR;

	dirp = dirp;	/* shut up compiler */
	if (_dos_findfirst(pattern, attributes, &findtst)) {
		error("rewind failed");
		exit(1);
	}
	findt_to_dirent(&findtst, &dirlist[0]);
	for (i = 1; ! _dos_findnext(&findtst); i++) {
		findt_to_dirent(&findtst, &dirlist[i]);
	}
	maxentry = i - 1;
	currententry = 0;
}

long
telldir(dirp)
	DIR *dirp;
{
	dirp = dirp;	/* keep compiler happy */
	return (long) currententry;
}

void
seekdir(dirp, loc)
	DIR *dirp;
	long loc;
{
	dirp = dirp;	/* keep compiler happy */
	if (loc <= (long) maxentry)
		currententry = (int) loc;
	/* else seekdir silently fails */
}

struct dirent *
readdir(dirp)
	DIR *dirp;
{
	dirp = dirp;	/* shut up compiler */
	if (currententry > maxentry)
		return (struct dirent *) NULL;
	else {
		return &dirlist[currententry++];
	}
}

void
findt_to_dirent(f, d)
	struct find_t *f;
	struct dirent *d;
{
	copynametolower(d->d_name, f->name);
}

static void
copynametolower(dest, src)
	char *dest;
	char *src;
{
	int i;
	for (i = 0; dest[i] = (char) tolower((int) src[i]); i++) {
		/* null body */
	}
}

void
closedir(dirp)
	DIR *dirp;
{
	dirp = dirp;	/* keep compiler happy */
	diropen = 0;
}

#endif /* DOS */