diff options
| -rw-r--r-- | date.c | 19 | ||||
| -rw-r--r-- | main.c | 13 |
2 files changed, 19 insertions, 13 deletions
@@ -106,29 +106,28 @@ void print_date(Date date) { char *print_date_to_string(Date date) { char *ret = (char *)calloc(16, 1); char *wd = weekday_to_string(date.weekday); - sprintf(ret, "%s %i/%i/%i", - wd, - date.month, date.day, date.year); + snprintf(ret, 15, "%s %i/%i/%i", + wd, + date.month, date.day, date.year); return ret; } char *short_date_to_string(Date date) { char *ret = (char *)calloc(16, 1); - sprintf(ret, "%i/%i/%i", - date.month, date.day, date.year); + snprintf(ret, 15, "%i/%i/%i", + date.month, date.day, date.year); return ret; } Date today(void) { Date ret; time_t tm; - struct tm localtm; - struct tm *ltm = &localtm; + struct tm *ltm; - time(&tm); - ltm = localtime(&tm); + time(&tm); + ltm = localtime(&tm); - ret = extract_date(ltm); + ret = extract_date(ltm); return ret; } @@ -9,18 +9,25 @@ static char *read_file_to_buffer(char *filename) { if (fd < 0) return NULL; // if error opening file, return NULLptr size_t bufsize = BS; size_t bufread = 0; - char *buf = (char *)malloc(bufsize); + ssize_t r = 0; + char *buf = (char *)malloc(bufsize+1); char *newbuf = NULL; - while (read(fd, buf + bufread, BS) == BS) { + while ((r = read(fd, buf + bufread, BS)) == BS) { newbuf = (char *)malloc(bufsize + BS); - strncpy(newbuf, buf, bufsize); + memcpy(newbuf, buf, bufsize); free(buf); buf = newbuf; bufsize += BS; bufread += BS; } close(fd); + if (r < 0) { + fprintf(stderr, "Error: error occured while reading file \"%s\".\n", filename); + free(buf); + return NULL; + } + buf[bufread + (size_t)r + 1] = 0; return buf; } |
