Skip to content

Commit a349dfd

Browse files
lib/: Reimplement all_digits() in terms of streq(stpspn()), and open-code it
Signed-off-by: Alejandro Colomar <[email protected]>
1 parent 8821d3f commit a349dfd

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

lib/chkname.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "defines.h"
3535
#include "chkname.h"
36+
#include "string/strchr/stpspn.h"
3637
#include "string/strcmp/streq.h"
3738

3839

@@ -69,7 +70,11 @@ is_valid_name(const char *name)
6970
*
7071
* Also do not allow fully numeric names or just "." or "..".
7172
*/
72-
int numeric;
73+
74+
if (streq(stpspn(name, "0123456789"), "")) {
75+
errno = EINVAL;
76+
return false;
77+
}
7378

7479
if ('\0' == *name ||
7580
('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
@@ -84,8 +89,6 @@ is_valid_name(const char *name)
8489
return false;
8590
}
8691

87-
numeric = isdigit(*name);
88-
8992
while (!streq(++name, "")) {
9093
if (!((*name >= 'a' && *name <= 'z') ||
9194
(*name >= 'A' && *name <= 'Z') ||
@@ -99,12 +102,6 @@ is_valid_name(const char *name)
99102
errno = EINVAL;
100103
return false;
101104
}
102-
numeric &= isdigit(*name);
103-
}
104-
105-
if (numeric) {
106-
errno = EINVAL;
107-
return false;
108105
}
109106

110107
return true;

lib/strtoday.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
long strtoday (const char *str)
3636
{
3737
time_t t;
38-
bool isnum = true;
3938
const char *s = str;
4039

4140
/*
@@ -54,14 +53,9 @@ long strtoday (const char *str)
5453
s++;
5554
}
5655
s = stpspn(s, " ");
57-
while (isnum && !streq(s, "")) {
58-
if (!isdigit (*s)) {
59-
isnum = false;
60-
}
61-
s++;
62-
}
63-
if (isnum) {
56+
if (streq(stpspn(s, "0123456789"), "")) {
6457
long retdate;
58+
6559
if (str2sl(&retdate, str) == -1)
6660
return -2;
6761
return retdate;

lib/subordinateio.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "alloc/reallocf.h"
2424
#include "atoi/str2i/str2u.h"
2525
#include "string/sprintf/snprintf.h"
26+
#include "string/strchr/stpspn.h"
2627
#include "string/strcmp/streq.h"
2728

2829

@@ -926,22 +927,12 @@ int list_owner_ranges(const char *owner, enum subid_type id_type, struct subid_r
926927
return count;
927928
}
928929

929-
static bool all_digits(const char *str)
930-
{
931-
int i;
932-
933-
for (i = 0; str[i] != '\0'; i++)
934-
if (!isdigit(str[i]))
935-
return false;
936-
return true;
937-
}
938-
939930
static int append_uids(uid_t **uids, const char *owner, int n)
940931
{
941932
int i;
942933
uid_t owner_uid;
943934

944-
if (all_digits(owner)) {
935+
if (streq(stpspn(owner, "0123456789"), "")) {
945936
i = sscanf(owner, "%d", &owner_uid);
946937
if (i != 1) {
947938
// should not happen

0 commit comments

Comments
 (0)