#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);
}