summaryrefslogtreecommitdiffstats
path: root/tools/dirprt.c
blob: a8b50ea59b0ce0d59b02e8c9daa5f65555b25221 (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
/*
 *	@(#)dirprt.c	1.6 2002/12/13 Connectathon Testsuite
 *	1.2 Lachman ONC Test Suite source
 */

#ifdef SOLARIS2X
/* 
 * XXX if this is lacking, i-numbers get printed as zero.  Not clear why;
 * should investigate sometime.
 */
#define _FILE_OFFSET_BITS	64
#endif

#include <sys/param.h>
#ifndef major
#include <sys/types.h>
#endif
#ifdef use_directs
#include <sys/dir.h>
#else
#include <dirent.h>
#endif
#include <stdio.h>
#include <ctype.h>

#ifndef ARGS_
#ifdef __STDC__
#define ARGS_(x) x
#else
#define ARGS_(x) ()
#endif
#endif

static DIR *my_opendir ARGS_((char *));
static void print ARGS_((char *));

main(argc, argv)
	int argc;
	char *argv[];
{
#if defined (AIX)
	fprintf(stderr, "dirprt is not supported on this platform.\n");
	exit(1);
#else
	argv++;
	argc--;
	while (argc--) {
		print(*argv++);
	}
#endif /* AIX */
}

#if !defined(AIX)

static void
print(dir)
	char *dir;
{
	DIR *dirp;
#ifdef use_directs
	struct direct *dp;
#else
	struct dirent *dp;
#endif

	dirp = my_opendir(dir);
	if (dirp == NULL) {
		perror(dir);
		return;
	}
	while ((dp = readdir(dirp)) != NULL) {
#if defined(SVR3) || defined(SVR4) || defined(LINUX)
		printf("%5ld %5ld %5d %s\n", (long)telldir(dirp),
		       (long)dp->d_ino,
		       dp->d_reclen, dp->d_name);
#else
		printf("%5ld %5d %5d %5d %s\n", (long)telldir(dirp), 
		       dp->d_fileno,
		       dp->d_reclen, dp->d_namlen, dp->d_name);
#endif
	}
	closedir(dirp);
}

#include <sys/stat.h>

/*
 * open a directory.
 */
static DIR *
my_opendir(name)
	char *name;
{
	struct stat sb;

	if (stat(name, &sb) == -1) {
		printf("stat failed\n");
		return (NULL);
	}
	if ((sb.st_mode & S_IFMT) != S_IFDIR) {
		printf("not a directory\n");
		return (NULL);
	}
	printf("%s mode %o dir %o\n", name, (int)sb.st_mode, S_IFDIR);
	return(opendir(name));
}

#endif /* AIX */