From a3deb7be93120964b59fd9756663cd44a551ff25 Mon Sep 17 00:00:00 2001 From: Tanner Babcock Date: Mon, 18 Dec 2023 01:51:28 -0600 Subject: [PATCH] Make functions static. Add a malloc() in http --- network/http.c | 15 +++++++++------ point/bitfield.c | 8 ++++---- point/hashmap.c | 2 +- point/linkpop.c | 6 +++--- point/linkpush.c | 4 ++-- thread/async.cpp | 4 ++-- thread/mutex.cpp | 4 ++-- thread/thread.cpp | 3 ++- 8 files changed, 25 insertions(+), 21 deletions(-) diff --git a/network/http.c b/network/http.c index 14e7e68..2342785 100644 --- a/network/http.c +++ b/network/http.c @@ -41,7 +41,7 @@ mime_type types[] = { {".", "text/plain"} }; -void format_size(char *buf, struct stat *stat) { +static void format_size(char *buf, struct stat *stat) { bzero(buf, strlen(buf)); if (S_ISDIR(stat->st_mode)) { sprintf(buf, "%s", "DIR"); @@ -58,12 +58,12 @@ void format_size(char *buf, struct stat *stat) { } } -void error(char *msg) { +static void error(char *msg) { perror(msg); exit(1); } -void cerror(FILE *stream, char *cause, char *errno, char *shortm, char *longm) { +static void cerror(FILE *stream, char *cause, char *errno, char *shortm, char *longm) { fprintf(stream, "HTTP/1.1 %s %s\n", errno, shortm); fprintf(stream, "Content-Type: text/html\n"); fprintf(stream, "\n"); @@ -75,9 +75,11 @@ void cerror(FILE *stream, char *cause, char *errno, char *shortm, char *longm) { fprintf(stream, "
Web Server\n"); } -void serve_file(FILE *stream, struct stat *stat, char *filename) { +static void serve_file(FILE *stream, struct stat *stat, char *filename) { char *p; - char filetype[BUFSIZE]; + //char filetype[BUFSIZE]; + char *filetype; + filetype = (char *)malloc(BUFSIZE); for (int z = 0; z < sizeof(types); z++) { if (strstr(filename, types[z].extension)) { strcpy(filetype, types[z].type); @@ -99,9 +101,10 @@ void serve_file(FILE *stream, struct stat *stat, char *filename) { fwrite(p, 1, stat->st_size, stream); munmap(p, stat->st_size); close(fd); + free(filetype); } -void serve_directory(int cfd, char *filename) { +static void serve_directory(int cfd, char *filename) { char buf[BBUFSIZE]; sprintf(buf, "HTTP/1.1 200 OK\r\n%s%s%s%s%s%s%s%s", diff --git a/point/bitfield.c b/point/bitfield.c index 7bac4ae..a495e1d 100644 --- a/point/bitfield.c +++ b/point/bitfield.c @@ -30,15 +30,15 @@ const char *months[] = { "December" }; -void print_big_date(DateBig *date) { +static void print_big_date(DateBig *date) { printf("%s %d, %d\n\n", months[(date->month)-1], date->day, date->year); } -void print_date(DateSmall *date) { +static void print_date(DateSmall *date) { printf("%s %d, %d\n\n", months[(date->month)-1], date->day, date->year); } -int make_big_date(DateBig *out) { +static int make_big_date(DateBig *out) { unsigned int m, d, y; printf("Making big date\n"); printf("Enter month: "); @@ -65,7 +65,7 @@ int make_big_date(DateBig *out) { return 0; } -int make_date(DateSmall *out) { +static int make_date(DateSmall *out) { unsigned int m, d, y; printf("Making small date\n"); printf("Enter month: "); diff --git a/point/hashmap.c b/point/hashmap.c index 4bee489..cc5311a 100644 --- a/point/hashmap.c +++ b/point/hashmap.c @@ -9,7 +9,7 @@ typedef struct { int value; } item; -item *linear_search(item *items, size_t size, const char *key) { +static item *linear_search(item *items, size_t size, const char *key) { for (size_t i = 0; i < size; i++) { if (strcmp(items[i].key, key) == 0) { return &items[i]; diff --git a/point/linkpop.c b/point/linkpop.c index 33d58c6..70a96e9 100644 --- a/point/linkpop.c +++ b/point/linkpop.c @@ -9,7 +9,7 @@ typedef struct node { } node_t; /* Print the linked list */ -void print_list(node_t *h) { +static void print_list(node_t *h) { node_t *current; for (current = h; current != NULL; current = current->next) { @@ -18,7 +18,7 @@ void print_list(node_t *h) { } /* New push function, inserts at beginning instead of end */ -int push(node_t **h, int val) { +static int push(node_t **h, int val) { /* This **h is a double pointer */ if (*h == NULL) { return 1; @@ -38,7 +38,7 @@ int push(node_t **h, int val) { } /* Remove from beginning of list, and return stored value */ -int pop(node_t **h) { +static int pop(node_t **h) { int ret = -1; node_t *next = NULL; diff --git a/point/linkpush.c b/point/linkpush.c index afbea9b..596f989 100644 --- a/point/linkpush.c +++ b/point/linkpush.c @@ -8,7 +8,7 @@ typedef struct node { struct node *next; } node_t; -void print_list(node_t *h) { +static void print_list(node_t *h) { node_t *current = h; while (current != NULL) { @@ -18,7 +18,7 @@ void print_list(node_t *h) { } /* Push a node to the end of the list with value val */ -void push(node_t *h, int v) { +static void push(node_t *h, int v) { node_t *current = h; while (current->next != NULL) current = current->next; diff --git a/thread/async.cpp b/thread/async.cpp index b8c11d9..92832a4 100644 --- a/thread/async.cpp +++ b/thread/async.cpp @@ -6,11 +6,11 @@ #include #include -void callback(const std::string& data) { +static void callback(const std::string& data) { std::cout << "Callback called with: " << data << std::endl; } -void task(int time) { +static void task(int time) { std::this_thread::sleep_for(std::chrono::seconds(time)); callback("async task done"); } diff --git a/thread/mutex.cpp b/thread/mutex.cpp index 0f25f43..a8d404b 100644 --- a/thread/mutex.cpp +++ b/thread/mutex.cpp @@ -34,7 +34,7 @@ struct tcout { }; std::mutex tcout::mutex; -void addJobs(void) { +static void addJobs(void) { static int num = 0; job current = { num++ }; std::unique_lock lock(*jobMutex); @@ -46,7 +46,7 @@ void addJobs(void) { /* Pass the parameter by giving another argument * to the thread constructor */ -void work(int seconds) { +static void work(int seconds) { job current; threadsRunning++; while (true) { diff --git a/thread/thread.cpp b/thread/thread.cpp index ef6f432..8388720 100644 --- a/thread/thread.cpp +++ b/thread/thread.cpp @@ -4,7 +4,7 @@ #include using namespace std; -void print(int n, const string &str) { +static void print(int n, const string &str) { string msg = to_string(n) + " : " + str; cout << msg << endl; } @@ -26,3 +26,4 @@ int main(void) { th.join(); return 0; } +