aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-05-15 18:20:57 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2023-05-15 18:21:50 +0200
commitddfe378a403b04c3701772ed8520b73e32b1e8c0 (patch)
tree571ad5eee9a985f7b58941bb321cbc0943e04eca
parent62c6e6f3f44545a410e2c89f6ba529468dd72d61 (diff)
downloadsdep-ddfe378a403b04c3701772ed8520b73e32b1e8c0.tar.gz
sdep-ddfe378a403b04c3701772ed8520b73e32b1e8c0.zip
Replaced strncpy with hand-written function suggested by NRK
-rw-r--r--sdep.c30
1 files changed, 23 insertions, 7 deletions
diff --git a/sdep.c b/sdep.c
index 9279c39..fe67b95 100644
--- a/sdep.c
+++ b/sdep.c
@@ -56,6 +56,7 @@ static char *format_line(Event, Options, char *);
56static int is_space(char); 56static int is_space(char);
57static void read_input(Options, EventList *); 57static void read_input(Options, EventList *);
58static Options read_op(int, char *[]); 58static Options read_op(int, char *[]);
59static void str_copy(char *, char *, int);
59static char *str_trim(char *); 60static char *str_trim(char *);
60static void write_output(Options, Event *, int); 61static void write_output(Options, Event *, int);
61 62
@@ -63,12 +64,12 @@ static void write_output(Options, Event *, int);
63static void 64static void
64add_event(struct tm t, char *text, EventList *evlist) 65add_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)
149static char * 150static char *
150format_line(Event ev, Options op, char *out) 151format_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 */
234static void
235str_copy(char *dst, char *src, int n)
236{
237 if (memccpy(dst, src, '\0', n) == NULL)
238 dst[n-1] = '\0';
239}
240
225static char * 241static char *
226str_trim(char *t) 242str_trim(char *t)
227{ 243{

Generated with cgit - Back to sebastiano.tronto.net