completed homework 4
authorkremlin <ian@kremlin.cc>
Thu, 25 Sep 2014 23:04:29 +0000 (19:04 -0400)
committerkremlin <ian@kremlin.cc>
Thu, 25 Sep 2014 23:04:29 +0000 (19:04 -0400)
assgn4/Makefile [new file with mode: 0644]
assgn4/hw4.c [new file with mode: 0644]

diff --git a/assgn4/Makefile b/assgn4/Makefile
new file mode 100644 (file)
index 0000000..e907691
--- /dev/null
@@ -0,0 +1,7 @@
+.PHONY: all
+
+CC=/usr/bin/cc
+CFLAGS= -Wall -Wextra -Werror -Wno-format -std=c89 
+
+all: hw4.c
+       $(CC) -o hw4.bin $(CFLAGS) hw4.c
diff --git a/assgn4/hw4.c b/assgn4/hw4.c
new file mode 100644 (file)
index 0000000..81b7a79
--- /dev/null
@@ -0,0 +1,80 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <pwd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+int main(int argc, char *argv[]) {
+
+       struct stat    file_stat;
+       struct passwd *file_pd;
+
+       int argc_decr;
+       mode_t *ft;
+
+       extern int errno;
+
+       argc_decr = argc; /* K&R 117(ยง5.10) warns against decrementing argc directly */
+
+       if(argc <= 1 || argc >= 5) {
+
+               printf("USAGE: %s <file> [-o, -c]\n", argv[0]);
+               return 1;
+
+       } else if(stat(argv[1], &file_stat)) {
+
+               if(errno == ENOENT)
+                       printf("provided file does not exist.\n");
+               else if(errno == EACCES)
+                       printf("improper permissions to read from file.\n");
+               else if(errno == ENAMETOOLONG)
+                       printf("provided path is too long.\n");
+               else if(errno == ENOTDIR)
+                       printf("provided path contains a non-existant directory.\n");
+               else if(errno == EOVERFLOW)
+                       printf("provided file cannot be properly contained into a stat buf.\n");
+               else if(errno == ELOOP)
+                       printf("provided file points to an irresolvable symlink chain.\n");
+
+               return errno;
+       }
+
+       printf("file size in bytes: %llu\n", file_stat.st_size);
+
+       while(argc_decr > 2) {
+
+               if(!strncmp(argv[argc_decr - 1], "-o", 5)) {
+
+                       if((file_pd = getpwuid(file_stat.st_uid)) != NULL)
+                               printf("file owned by user %s.\n", file_pd->pw_name);
+
+                       else
+                               printf("file owned by uid %d.\n", file_stat.st_uid);
+
+               } else if(!strncmp(argv[argc_decr - 1], "-t", 5)) {
+
+                       printf("file is a ");
+                       ft = &(file_stat.st_mode);
+
+                       if     (S_ISBLK(*ft))  printf("block device.\n");
+                       else if(S_ISCHR(*ft))  printf("character device.\n");
+                       else if(S_ISFIFO(*ft)) printf("fifo pipe.\n");
+                       else if(S_ISREG(*ft))  printf("regular file.\n");
+                       else if(S_ISDIR(*ft))  printf("directory.\n");
+                       else if(S_ISLNK(*ft))  printf("symbolic link.\n");
+                               
+                               
+               } else {
+
+                       printf("provided argument is invalid, must be '-t' or '-o'.\n");
+                       return 1;
+               }
+
+               argc_decr--;
+       }
+
+       return 0;
+}