Skip to content

Commit

Permalink
Make functions static. Add a malloc() in http
Browse files Browse the repository at this point in the history
  • Loading branch information
Babkock committed Dec 18, 2023
1 parent ee0e1bd commit a3deb7b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
15 changes: 9 additions & 6 deletions network/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -75,9 +75,11 @@ void cerror(FILE *stream, char *cause, char *errno, char *shortm, char *longm) {
fprintf(stream, "<hr color=\"white\"/><em>Web Server</em></body></html>\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);
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions point/bitfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand All @@ -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: ");
Expand Down
2 changes: 1 addition & 1 deletion point/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions point/linkpop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions point/linkpush.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions thread/async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <string>
#include <thread>

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");
}
Expand Down
4 changes: 2 additions & 2 deletions thread/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(*jobMutex);
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>
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;
}
Expand All @@ -26,3 +26,4 @@ int main(void) {
th.join();
return 0;
}

0 comments on commit a3deb7b

Please sign in to comment.