diff options
author | sinanmohd <sinan@firemail.cc> | 2023-05-19 13:17:33 +0530 |
---|---|---|
committer | sinanmohd <sinan@firemail.cc> | 2023-05-19 18:45:33 +0530 |
commit | f6901aab1fb07f8ee807afb7b76a3c69ff6a9370 (patch) | |
tree | 3dd990b5e3b45568b1f6c1a006e69f8a8b51ea9c /8.5 | |
parent | 68f15b3002235095d9649ba423069b492cf63746 (diff) |
8.5: initial commit
Diffstat (limited to '8.5')
-rw-r--r-- | 8.5/Makefile | 12 | ||||
-rw-r--r-- | 8.5/fsize.c | 68 | ||||
-rw-r--r-- | 8.5/fsize.h | 1 | ||||
-rw-r--r-- | 8.5/main.c | 12 |
4 files changed, 93 insertions, 0 deletions
diff --git a/8.5/Makefile b/8.5/Makefile new file mode 100644 index 0000000..fc7953f --- /dev/null +++ b/8.5/Makefile @@ -0,0 +1,12 @@ +OBJECTS = main.o fsize.o +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +fsize: $(OBJECTS) + $(CC) $(CFLAGS) -o fsize $(OBJECTS) + +main.o: fsize.h + +.PHONY: clean +clean: + rm -f fsize $(OBJECTS) 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); +} diff --git a/8.5/fsize.h b/8.5/fsize.h new file mode 100644 index 0000000..8844d5e --- /dev/null +++ b/8.5/fsize.h @@ -0,0 +1 @@ +void fsize(char *name); diff --git a/8.5/main.c b/8.5/main.c new file mode 100644 index 0000000..4a9ed19 --- /dev/null +++ b/8.5/main.c @@ -0,0 +1,12 @@ +#include "fsize.h" + +int main(int argc, char *argv[]) +{ + if (argc == 1) + fsize("."); + else + while (--argc > 0) + fsize(*++argv); + + return 0; +} |