From b0c04fdc3d1724086346d38b2d2ce7aa171143b7 Mon Sep 17 00:00:00 2001 From: aspiring_aurelian Date: Fri, 8 Dec 2023 08:22:43 -0600 Subject: Successful refactor --- main.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 06fdcce..752b4a2 100644 --- a/main.c +++ b/main.c @@ -7,15 +7,26 @@ #include #include -int f1_newer(char *f1, char *f2) { +typedef struct thread_data { + char *f1; + char *f2; +} thread_data; + +void *f1_newer(void *arg) { + thread_data *files = (thread_data *) arg; + char *f1 = files->f1; + char *f2 = files->f2; struct stat f1_attr; struct stat f2_attr; stat(f1, &f1_attr); stat(f2, &f2_attr); - if (difftime(f1_attr.st_mtime, f2_attr.st_mtime) <= 0) { - return 0; + int diff = difftime(f1_attr.st_mtime, f2_attr.st_mtime); + int ret = 0; + if (diff <= 0) { + pthread_exit(&ret); } - return 1; + ret = 1; + pthread_exit(&ret); } void *create_make() { @@ -30,17 +41,25 @@ void *create_make() { int main() { - pthread_t f_thread; + thread_data files; + files.f1 = "main.c"; + files.f2 = "./bin/debug"; + + pthread_t f_thread, check_thread; pthread_create(&f_thread, NULL, create_make, NULL); + pthread_create(&check_thread, NULL, f1_newer, (void *)&files); mkdir("./bin", 0777); char *source = "main.c"; char *bin = "./bin/debug"; + void *newer; pthread_join(f_thread, NULL); + pthread_join(check_thread, &newer); + int recomp = *(int *) newer; - if (f1_newer(source, bin)) { + if (recomp) { pid_t make_pid; if ((make_pid = fork()) == 0) { printf("Recompiling...\n"); @@ -51,7 +70,7 @@ int main() { pid_t x_pid; if ((x_pid = fork()) == 0) { - execl("./bin/debug", "debug", NULL); + execl("./bin/tmp", "tmp", NULL); } waitpid(x_pid, NULL, 0); -- cgit v1.2.3 From 32b0dd534ff3ab63d51f6e3c5c5a83692aa4fac4 Mon Sep 17 00:00:00 2001 From: aspiring_aurelian Date: Fri, 8 Dec 2023 08:29:15 -0600 Subject: Ignore a.out --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0e9ae81..00f8cba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin tmp.c +a.out -- cgit v1.2.3 From 467ccefc2e5b76906f3e5194cc23511087aa9344 Mon Sep 17 00:00:00 2001 From: aspiring_aurelian Date: Fri, 8 Dec 2023 08:29:27 -0600 Subject: Add comments --- main.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 752b4a2..1cc6b57 100644 --- a/main.c +++ b/main.c @@ -7,13 +7,13 @@ #include #include -typedef struct thread_data { +typedef struct f_comp_data { char *f1; char *f2; -} thread_data; +} f_comp_data; void *f1_newer(void *arg) { - thread_data *files = (thread_data *) arg; + f_comp_data *files = (f_comp_data *) arg; char *f1 = files->f1; char *f2 = files->f2; struct stat f1_attr; @@ -41,18 +41,21 @@ void *create_make() { int main() { - thread_data files; + /* Struct to hold files */ + f_comp_data files; files.f1 = "main.c"; files.f2 = "./bin/debug"; + /* Threads to: + * create makefile + * check age of source vs. bin + */ pthread_t f_thread, check_thread; pthread_create(&f_thread, NULL, create_make, NULL); pthread_create(&check_thread, NULL, f1_newer, (void *)&files); mkdir("./bin", 0777); - char *source = "main.c"; - char *bin = "./bin/debug"; void *newer; pthread_join(f_thread, NULL); -- cgit v1.2.3