aboutsummaryrefslogtreecommitdiff
path: root/8.5/fsize.c
blob: 96dd68384f198e47429eb5130c6a5985acf8d397 (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
#include <sys/stat.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include "fsize.h"

#define MAX_PATH	1024

static void dirwalk(char *name);
static void error(char *fmt, ...);

void fsize(char *name)
{
	struct stat buf;

	if (stat(name, &buf) == -1) {
		error("fsize: can't access %s", name);
		return;
	}

	if ((buf.st_mode & S_IFMT) == S_IFDIR)
		dirwalk(name);

	printf("%10ld %4d %4d %ld %s\n", buf.st_size, buf.st_uid,
	     buf.st_gid, buf.st_ctim.tv_sec, name);
}

static void dirwalk(char *dir)
{
	char path[MAX_PATH];
	DIR *dfd;
	struct dirent *dp;

	if ((dfd = opendir(dir)) == NULL) {
		error("dirwalk: can't open %s", dir);
		return;
	}

	while ((dp = readdir(dfd)) != NULL) {
		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
			continue;

		
		if (strlen(dp->d_name) + strlen(dir) + 2 > MAX_PATH) {
			error("dirwalk: name %s/%s too long", dir, path);
		} else {
			sprintf(path, "%s/%s", dir, dp->d_name);
			fsize(path);
		}
	}

	closedir(dfd);
}

static void error(char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	fprintf(stderr, "error: ");
	vfprintf(stderr,fmt, args);
	putc('\n', stderr);
	va_end(args);
}