diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-05-15 18:20:57 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-05-15 18:21:50 +0200 |
| commit | ddfe378a403b04c3701772ed8520b73e32b1e8c0 (patch) | |
| tree | 571ad5eee9a985f7b58941bb321cbc0943e04eca /sdep.c | |
| parent | 62c6e6f3f44545a410e2c89f6ba529468dd72d61 (diff) | |
| download | sdep-ddfe378a403b04c3701772ed8520b73e32b1e8c0.tar.gz sdep-ddfe378a403b04c3701772ed8520b73e32b1e8c0.zip | |
Replaced strncpy with hand-written function suggested by NRK
Diffstat (limited to 'sdep.c')
| -rw-r--r-- | sdep.c | 30 |
1 files changed, 23 insertions, 7 deletions
| @@ -56,6 +56,7 @@ static char *format_line(Event, Options, char *); | |||
| 56 | static int is_space(char); | 56 | static int is_space(char); |
| 57 | static void read_input(Options, EventList *); | 57 | static void read_input(Options, EventList *); |
| 58 | static Options read_op(int, char *[]); | 58 | static Options read_op(int, char *[]); |
| 59 | static void str_copy(char *, char *, int); | ||
| 59 | static char *str_trim(char *); | 60 | static char *str_trim(char *); |
| 60 | static void write_output(Options, Event *, int); | 61 | static void write_output(Options, Event *, int); |
| 61 | 62 | ||
| @@ -63,12 +64,12 @@ static void write_output(Options, Event *, int); | |||
| 63 | static void | 64 | static void |
| 64 | add_event(struct tm t, char *text, EventList *evlist) | 65 | add_event(struct tm t, char *text, EventList *evlist) |
| 65 | { | 66 | { |
| 66 | int l = strlen(text)+1; | 67 | size_t l = strlen(text)+1; |
| 67 | EventNode *next = malloc(sizeof(EventNode)); | 68 | EventNode *next = malloc(sizeof(EventNode)); |
| 68 | 69 | ||
| 69 | next->ev.time = t; | 70 | next->ev.time = t; |
| 70 | next->ev.text = malloc(l); | 71 | next->ev.text = malloc(l); |
| 71 | strncpy(next->ev.text, text, l); | 72 | str_copy(next->ev.text, text, l); |
| 72 | next->ev.text = str_trim(next->ev.text); | 73 | next->ev.text = str_trim(next->ev.text); |
| 73 | next->next = NULL; | 74 | next->next = NULL; |
| 74 | 75 | ||
| @@ -149,9 +150,13 @@ events_in_range(EventList *evlist, Options op, Event *sel) | |||
| 149 | static char * | 150 | static char * |
| 150 | format_line(Event ev, Options op, char *out) | 151 | format_line(Event ev, Options op, char *out) |
| 151 | { | 152 | { |
| 153 | size_t l; | ||
| 154 | |||
| 152 | strftime(out, MAXLEN, op.format_out, &ev.time); | 155 | strftime(out, MAXLEN, op.format_out, &ev.time); |
| 153 | strncat(out, op.separator, MAXLEN - strlen(out)); | 156 | l = strlen(out); |
| 154 | strncat(out, ev.text, MAXLEN - strlen(out)); | 157 | |
| 158 | str_copy(out+l, op.separator, MAXLEN - l); | ||
| 159 | str_copy(out+l, ev.text, MAXLEN - l); | ||
| 155 | 160 | ||
| 156 | return out; | 161 | return out; |
| 157 | } | 162 | } |
| @@ -183,8 +188,8 @@ read_op(int argc, char *argv[]) | |||
| 183 | * This changes the way other options are read */ | 188 | * This changes the way other options are read */ |
| 184 | for (i = 1; i < argc; i++) { | 189 | for (i = 1; i < argc; i++) { |
| 185 | if (argv[i][0] == '+') { | 190 | if (argv[i][0] == '+') { |
| 186 | strncpy(op.format_in, &argv[i][1], MAXLEN); | 191 | str_copy(op.format_in, &argv[i][1], MAXLEN); |
| 187 | strncpy(op.format_out, &argv[i][1], MAXLEN); | 192 | str_copy(op.format_out, &argv[i][1], MAXLEN); |
| 188 | } | 193 | } |
| 189 | } | 194 | } |
| 190 | 195 | ||
| @@ -196,7 +201,7 @@ read_op(int argc, char *argv[]) | |||
| 196 | puts(default_format); | 201 | puts(default_format); |
| 197 | exit(0); | 202 | exit(0); |
| 198 | } else if (!strcmp(argv[i], "-s")) { | 203 | } else if (!strcmp(argv[i], "-s")) { |
| 199 | strncpy(op.separator, argv[++i], MAXLEN); | 204 | str_copy(op.separator, argv[++i], MAXLEN); |
| 200 | } else if (!strcmp(argv[i], "-f")) { | 205 | } else if (!strcmp(argv[i], "-f")) { |
| 201 | if (i+1 >= argc || | 206 | if (i+1 >= argc || |
| 202 | strptime(argv[i+1], op.format_in, &op.from) == NULL) | 207 | strptime(argv[i+1], op.format_in, &op.from) == NULL) |
| @@ -222,6 +227,17 @@ read_op(int argc, char *argv[]) | |||
| 222 | return op; | 227 | return op; |
| 223 | } | 228 | } |
| 224 | 229 | ||
| 230 | /* | ||
| 231 | * Copy up to n characters of the string str to dst and append '\0'. | ||
| 232 | * Suggested by NRK. | ||
| 233 | */ | ||
| 234 | static void | ||
| 235 | str_copy(char *dst, char *src, int n) | ||
| 236 | { | ||
| 237 | if (memccpy(dst, src, '\0', n) == NULL) | ||
| 238 | dst[n-1] = '\0'; | ||
| 239 | } | ||
| 240 | |||
| 225 | static char * | 241 | static char * |
| 226 | str_trim(char *t) | 242 | str_trim(char *t) |
| 227 | { | 243 | { |
