From bdfa15950d48605cc2aa1118fed1a77bb878678a Mon Sep 17 00:00:00 2001
From: Brandon Rodriguez <brodriguez8774@gmail.com>
Date: Mon, 11 Dec 2017 16:12:36 -0500
Subject: [PATCH] Mostly done. Still issues with read/write

---
 Final.h  |  3 ++-
 makefile |  3 ++-
 unjust.c | 54 ++++++++++++++++++++++++++++++++++++++----------------
 3 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/Final.h b/Final.h
index a71e325..34bfe22 100644
--- a/Final.h
+++ b/Final.h
@@ -1,6 +1,7 @@
+// Error #10: Needed to specify obj type for Field2.
 typedef struct Thing {
     int Field1;
-    Thing * Field2;
+    struct Thing* Field2;
     int fd;
     FILE * file;
 } Thing;
diff --git a/makefile b/makefile
index e612de8..699c075 100644
--- a/makefile
+++ b/makefile
@@ -1,2 +1,3 @@
 all:
-	gcc -Wall -Wpedantic -std=c99 Final1-fall-2017.c error.c -pthread
+	# Error #8: Missing "-g -o" and references nonexistant file instead of "unjust.c".
+	gcc -Wall -Wpedantic -std=c99 unjust.c error.c -pthread
diff --git a/unjust.c b/unjust.c
index 5d95c34..b857672 100644
--- a/unjust.c
+++ b/unjust.c
@@ -1,5 +1,7 @@
-$ ssh git@jazz.cs.wmich.edu begin 3240 FinalF17
-$ git clone git@jazz.cs.wmich.edu:3240/<YOUR NAME>/FinalF17
+
+// Error #1: Won't compile without this commented out.
+// $ ssh git@jazz.cs.wmich.edu begin 3240 FinalF17
+// $ git clone git@jazz.cs.wmich.edu:3240/<YOUR NAME>/FinalF17
 
 // Final Exam, December 11
 // This program will create a file "NEWFILE" and write a struct to it at
@@ -9,37 +11,54 @@ $ git clone git@jazz.cs.wmich.edu:3240/<YOUR NAME>/FinalF17
 // Write the struct (T2) to NEWFILE with raw I/O, read the file with a file stream
 // Into a new struct (T1), and print the integer stored in T1
 
+// Error #9: Missing some includes.
+#include <fcntl.h>
 #include <stdio.h>
-#include "error.c"
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>	// Error #13: Why would you ever include a .c file here? That's what headers are for.
 #include "apue.h"
-#include "final.h"
+#include "Final.h"  // Error #2: Typo. Should be upper case.
 #include <pthread.h>
 
 void * readThing(void * Thing);
 void * writeThing(void * Thing);
 
-void main ()
+// Error #9: Main should return an int, not void.
+// Error #10: Attempted us of argv without ever declaring it in main.
+int main (int argc, char* argv[])
 {
 Thing T1;
 Thing *T2; // Pointing at Things and Things
-int fd, offset;
+int fd; // Error #12: Offest never used.
 FILE * F;
 pthread_t writeThread;
 pthread_t readThread;
 
-T2=malloc(sizeof(Thing *));
+T2=malloc(sizeof(Thing)); // Error #5: Should malloc the size of the object, not a pointer.
+
+umask(0000); // everybody gets everything ? // Error #4: To set 777, umask should be set to 0. This will deny all permisions (I think).
 
-umask(0777); // everybody gets everything ?
+fd = open("NEWFILE", O_CREAT|O_TRUNC, 0777);// Error #3: Missing third arguement. Defaulting to 777 because umask, above.
+if (fd < 0) {
+    err_sys("Invalid open call");
+}
 
-fd = open("NEWFILE",O_CREAT|O_TRUNC|S_IRWXU,O_RDWR);
-T2->Field1=argv[1]; // enter 42 on command line
-F  = fopen("NEWFILE",rw);
+T2->Field1=atoi(argv[1]); // enter 42 on command line // Error #12: Did not atoi argv but assigning it to an int.
+F  = fopen("NEWFILE", "rw");	// Error #6: Missing quotes around rw.
 
 T2->fd=fd;
 T1.file=F;
 
+// Error #7: Pretty sure threads should be joined before the last three functions.
+// Otherwise it's possible that program might end before child threads execute.
+// Also, write thread should (probably) be joined before read thread, as well?
+// Or else race condition. Kind of defeats the purpose of threading but oh well.
+
 pthread_create(&writeThread,NULL,writeThing,(void *) T2);
+pthread_join(writeThread, (void*) NULL);
 pthread_create(&readThread,NULL,readThing,(void *) &T1);
+pthread_join(readThread, (void*) NULL);
 
 close(fd);
 
@@ -50,18 +69,21 @@ printf("%d\n",T1.Field1);
 return 0;
 }
 
-void * readThing(void * Thing)
+// Error #11: Applies to both thread methods. Gives object type name rather than variable name.
+void * readThing(void * aThing)
 {
-Thing * readThing = (Thing *)Thing;
+Thing * readThing = (Thing *)aThing;
 fseek(readThing->file,1000,SEEK_SET);
 fread(readThing,sizeof(Thing),1,readThing->file);
 pthread_exit(NULL);
 }
 
-void * writeThing(void * Thing)
+void * writeThing(void * aThing)
 {
-Thing * writeThing = (Thing *)Thing;
+Thing * writeThing = (Thing *)aThing;
 lseek(writeThing->fd,1000,SEEK_CUR);
-write(fd,writeThing,sizeof(Thing));
+write(writeThing->fd,writeThing,sizeof(Thing));
+// write(writeThing->)
 pthread_exit(NULL);
 }
+
-- 
GitLab