diff options
| author | gonzo <gonzo@toniatuh.com> | 2023-02-05 12:28:46 +0100 |
|---|---|---|
| committer | gonzo <gonzo@toniatuh.com> | 2023-02-05 12:28:46 +0100 |
| commit | 536bd4261e565f34e7ef8fcb8ca26a0148898ae3 (patch) | |
| tree | 89df0964cc75061e30398663ec0343928138a5e8 | |
| parent | a565247c8e000482a4136a4e0f5bd113102a6d65 (diff) | |
| download | org-to-conky-536bd4261e565f34e7ef8fcb8ca26a0148898ae3.tar.gz | |
cleaned up warnings in main.c agenda_entry.* utils.h
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | agenda_entry.c | 26 | ||||
| -rw-r--r-- | agenda_entry.h | 2 | ||||
| -rw-r--r-- | main.c | 24 | ||||
| -rw-r--r-- | utils.h | 2 |
5 files changed, 32 insertions, 24 deletions
@@ -6,7 +6,7 @@ CC=clang --std=gnu99 CC=clang --std=c99 .endif -CFLAGS=-Wall -Wextra -Werror -Weverything -pedantic +CFLAGS=-Wall -Wextra -Werror -Weverything -Wpadded -pedantic LIBS= BLDFLAGS=-g RELFLAGS=-O2 diff --git a/agenda_entry.c b/agenda_entry.c index 8a68353..92fc798 100644 --- a/agenda_entry.c +++ b/agenda_entry.c @@ -14,6 +14,7 @@ entry read_agenda_entry(char *agenda) { char *cursor = agenda; char *lookahead, *tmp; char *nextentry = next_entry(cursor); + unsigned long size; if (nextentry == NULL) nextentry = agenda + strlen(agenda) - 1; @@ -27,17 +28,19 @@ entry read_agenda_entry(char *agenda) { lookahead = strstr(cursor, ":\n"); if (lookahead && (lookahead < nextentry)) { tmp = rstrchr(cursor, lookahead-1, ':'); - agenda_entry.tag = (char *)malloc(lookahead - tmp); - strncpy(agenda_entry.tag, tmp+1, lookahead - tmp - 1); - agenda_entry.tag[lookahead - tmp - 1] = '\0'; // here we're actually modifying lookahead-tmp cause the array is zero indexed + size = (unsigned long)labs(lookahead - tmp); + agenda_entry.tag = (char *)malloc(size); + strncpy(agenda_entry.tag, tmp+1, size - 1); + agenda_entry.tag[size - 1] = '\0'; // here we're actually modifying lookahead-tmp cause the array is zero indexed lookahead = tmp; } else { lookahead = strstr(cursor, "\n"); agenda_entry.tag = NULL; } - agenda_entry.title = (char *)malloc(lookahead - cursor + 1); - strncpy(agenda_entry.title, cursor, lookahead - cursor); - agenda_entry.title[lookahead - cursor] = '\0'; // here we're actually modifying lookahead-cursor cause the array is zero indexed + size = (unsigned long)(lookahead - cursor); + agenda_entry.title = (char *)malloc(size + 1); + strncpy(agenda_entry.title, cursor, size); + agenda_entry.title[size] = '\0'; // here we're actually modifying lookahead-cursor cause the array is zero indexed //find out if there is a deadline and extract it cursor = next_line(cursor); @@ -48,8 +51,9 @@ entry read_agenda_entry(char *agenda) { //there is a deadline or event is scheduled tmp = strstr(lookahead, ">"); lookahead = strstr(lookahead, "<"); - char *date = (char *)malloc(tmp - lookahead + 1); - strncpy(date, lookahead+1, tmp - lookahead - 1); + size = (unsigned long)labs(tmp - lookahead); + char *date = (char *)malloc(size + 1); + strncpy(date, lookahead+1, size - 1); agenda_entry.date = extract_date_from_string(date); free(date); } else { @@ -134,6 +138,12 @@ void merge_entry_array(entry *arr, int halfway, int n) { } } +void destroy_entry_array(entry *arr, int n) { + for (int i = 0; i < n; i++) + destroy_entry(arr[i]); + free(arr); +} + void print_entry(entry e) { char *tmp = print_date_to_string(e.date); printf("%s\n%s\n>%s\n", diff --git a/agenda_entry.h b/agenda_entry.h index ea9ea8a..bd0142d 100644 --- a/agenda_entry.h +++ b/agenda_entry.h @@ -25,5 +25,7 @@ void merge_entry_array(entry *arr, int halfway, int n); void insertion_sort_entry_array(entry *arr, int n); void merge_sort_entry_array(entry *arr, int n); void sort_entry_array(entry *arr, int n); +void destroy_entry_array(entry *arr, int n); + void print_entry(entry e); void destroy_entry(entry e); @@ -4,17 +4,11 @@ #include "agenda_entry.h" #define BS 1024 //blocksize for buffers -void destroy_entry_array(entry *array, int elements) { - for (int i = 0; i < elements; i++) - destroy_entry(array[i]); - free(array); -} - -char *read_file_to_buffer(char *filename) { - int fd = open(filename, O_RDONLY); +static char *read_file_to_buffer(char *filename) { + int fd = open(filename, O_RDONLY); if (fd < 0) return NULL; // if error opening file, return NULLptr - int bufsize = BS, - bufread = 0; + size_t bufsize = BS; + size_t bufread = 0; char *buf = (char *)malloc(bufsize); char *newbuf; @@ -30,15 +24,15 @@ char *read_file_to_buffer(char *filename) { return buf; } -entry *read_entries_to_array(char *buffer, int *entries) { - int numentries = 5, - idx = 0; +static entry *read_entries_to_array(char *buffer, int *entries) { + unsigned long numentries = 5; + int idx = 0; entry *ret = (entry *)malloc(numentries * sizeof(entry)); char *cursor = buffer; while (cursor) { - if (idx == numentries) { + if ((size_t)idx == numentries) { entry *newret = (entry *)malloc((numentries + 5) * sizeof(entry)); - memcpy(newret, ret, (idx)*sizeof(entry)); + memcpy(newret, ret, ((size_t)idx)*sizeof(entry)); free(ret); ret = newret; numentries += 5; @@ -1,5 +1,7 @@ #include <stddef.h> +char *rstrchr(const char *, char *, char); + char *rstrchr(const char *s, char *start, char c) { char *ret = start; while ((ret > s) && (*ret != c)) ret--; |
