Factor out strtobytes() helper function from syslogd.c

For use in logger.c

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-10-25 15:51:19 +02:00
parent f6e17bd6b3
commit 76b6981467
2 changed files with 32 additions and 29 deletions

View File

@ -31,6 +31,8 @@
#define SYSKLOGD_COMPAT_H_
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
/*
* The following macro is used to remove const cast-away warnings
@ -69,4 +71,33 @@ static inline char *getprogname(void)
return __progname;
}
static inline int strtobytes(char *arg)
{
int mod = 0, bytes;
size_t pos;
if (!arg)
return -1;
pos = strspn(arg, "0123456789");
if (arg[pos] != 0) {
if (arg[pos] == 'G')
mod = 3;
else if (arg[pos] == 'M')
mod = 2;
else if (arg[pos] == 'k')
mod = 1;
else
return -1;
arg[pos] = 0;
}
bytes = atoi(arg);
while (mod--)
bytes *= 1000;
return bytes;
}
#endif /* SYSKLOGD_COMPAT_H_ */

View File

@ -569,6 +569,7 @@ static char sccsid[] __attribute__((unused)) =
#endif
#include "config.h"
#include <paths.h>
#include "compat.h"
/*
* Linux uses EIO instead of EBADFD (mrn 12 May 96)
@ -2713,35 +2714,6 @@ void init(void)
logit("syslogd: restarted.\n");
}
static int strtobytes(char *arg)
{
int mod = 0, bytes;
size_t pos;
if (!arg)
return -1;
pos = strspn(arg, "0123456789");
if (arg[pos] != 0) {
if (arg[pos] == 'G')
mod = 3;
else if (arg[pos] == 'M')
mod = 2;
else if (arg[pos] == 'k')
mod = 1;
else
return -1;
arg[pos] = 0;
}
bytes = atoi(arg);
while (mod--)
bytes *= 1000;
return bytes;
}
/*
* Crack a configuration file line
*/