Skip to content

Commit

Permalink
Add feature for usr.sbin/newsyslog.c: accept human unit suffix for si…
Browse files Browse the repository at this point in the history
…ze fileds in configuration file.

Use `expand_number(3)` function instead of parsing manually.
  • Loading branch information
Rin0913 committed Nov 2, 2024
1 parent d0f9b0b commit 51e2e6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion usr.sbin/newsyslog/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CONFS= newsyslog.conf
PROG= newsyslog
MAN= newsyslog.8 newsyslog.conf.5
SRCS= newsyslog.c ptimes.c
LIBADD= sbuf
LIBADD= sbuf util

HAS_TESTS=
SUBDIR.${MK_TESTS}+= tests
Expand Down
18 changes: 16 additions & 2 deletions usr.sbin/newsyslog/newsyslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <libgen.h>
#include <libutil.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
Expand Down Expand Up @@ -1335,8 +1336,21 @@ parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
badline("malformed line (missing fields):\n%s",
errline);
*parse = '\0';
if (isdigitch(*q))
working->trsize = atoi(q);
if (isdigitch(*q)) {
size_t last_digit = q[strlen(q) - 1];
if ('0' <= last_digit && last_digit <= '9')
working->trsize = atoi(q);
else {
uint64_t trsize = 0;
if (expand_number(q, &trsize) == 0)
working->trsize = trsize / 1024;
else {
working->trsize = -1;
warnx("Invalid value of '%s' for 'size' in line:\n%s",
q, errline);
}
}
}
else if (strcmp(q, "*") == 0)
working->trsize = -1;
else {
Expand Down

0 comments on commit 51e2e6f

Please sign in to comment.