aboutsummaryrefslogtreecommitdiff
path: root/8.5/fsize.c
diff options
context:
space:
mode:
Diffstat (limited to '8.5/fsize.c')
-rw-r--r--8.5/fsize.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/8.5/fsize.c b/8.5/fsize.c
new file mode 100644
index 0000000..96dd683
--- /dev/null
+++ b/8.5/fsize.c
@@ -0,0 +1,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);
+}