Skip to content

Commit

Permalink
lib/, src/: str2*(): Rename functions and reorder parameters
Browse files Browse the repository at this point in the history
This makes them compatible with liba2i's functions.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed Mar 29, 2024
1 parent b085c3f commit f39ac10
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 71 deletions.
4 changes: 2 additions & 2 deletions lib/atoi/str2i.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
#include "atoi/str2i.h"


extern inline int getlong(const char *restrict s, long *restrict n);
extern inline int getulong(const char *restrict s, unsigned long *restrict n);
extern inline int str2sl(long *restrict n, const char *restrict s);
extern inline int str2ul(unsigned long *restrict n, const char *restrict s);
12 changes: 6 additions & 6 deletions lib/atoi/str2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
#include "attr.h"


ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int getlong(const char *restrict s, long *restrict n);
ATTR_STRING(1) ATTR_ACCESS(write_only, 2)
inline int getulong(const char *restrict s, unsigned long *restrict n);
ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
inline int str2sl(long *restrict n, const char *restrict s);
ATTR_STRING(2) ATTR_ACCESS(write_only, 1)
inline int str2ul(unsigned long *restrict n, const char *restrict s);


inline int
getlong(const char *restrict s, long *restrict n)
str2sl(long *restrict n, const char *restrict s)
{
return a2sl(n, s, NULL, 0, LONG_MIN, LONG_MAX);
}


inline int
getulong(const char *restrict s, unsigned long *restrict n)
str2ul(unsigned long *restrict n, const char *restrict s)
{
return a2ul(n, s, NULL, 0, 0, ULONG_MAX);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/getdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ int getdef_num (const char *item, int dflt)
return dflt;
}

if ( (getlong(d->value, &val) == -1)
if ( (str2sl(&val, d->value) == -1)
|| (val > INT_MAX)
|| (val < -1)) {
fprintf (shadow_logfd,
Expand Down Expand Up @@ -281,7 +281,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
return dflt;
}

if ( (getlong(d->value, &val) == -1)
if ( (str2sl(&val, d->value) == -1)
|| (val < 0)
|| (val > INT_MAX)) {
fprintf (shadow_logfd,
Expand Down Expand Up @@ -316,7 +316,7 @@ long getdef_long (const char *item, long dflt)
return dflt;
}

if (getlong(d->value, &val) == -1 || val < -1) {
if (str2sl(&val, d->value) == -1 || val < -1) {
fprintf (shadow_logfd,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
Expand Down Expand Up @@ -348,7 +348,7 @@ unsigned long getdef_ulong (const char *item, unsigned long dflt)
return dflt;
}

if (getulong(d->value, &val) == -1) {
if (str2ul(&val, d->value) == -1) {
fprintf (shadow_logfd,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
Expand Down
6 changes: 3 additions & 3 deletions lib/idmapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
/* Gather up the ranges from the command line */
mapping = mappings;
for (idx = 0, argidx = 0; idx < ranges; idx++, argidx += 3, mapping++) {
if (getulong(argv[argidx + 0], &mapping->upper) == -1) {
if (str2ul(&mapping->upper, argv[argidx + 0]) == -1) {
free(mappings);
return NULL;
}
if (getulong(argv[argidx + 1], &mapping->lower) == -1) {
if (str2ul(&mapping->lower, argv[argidx + 1]) == -1) {
free(mappings);
return NULL;
}
if (getulong(argv[argidx + 2], &mapping->count) == -1) {
if (str2ul(&mapping->count, argv[argidx + 2]) == -1) {
free(mappings);
return NULL;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static int setrlimit_value (unsigned int resource,
limit = RLIM_INFINITY;
}
else {
/* We cannot use getlong here because it fails when there
/* We cannot use str2sl() here because it fails when there
* is more to the value than just this number!
* Also, we are limited to base 10 here (hex numbers will not
* work with the limit string parser as is anyway)
Expand Down Expand Up @@ -93,7 +93,7 @@ static int set_prio (const char *value)
{
long prio;

if ( (getlong(value, &prio) == -1)
if ( (str2sl(&prio, value) == -1)
|| (prio != (int) prio)) {
return 0;
}
Expand All @@ -108,7 +108,7 @@ static int set_umask (const char *value)
{
unsigned long mask;

if ( (getulong(value, &mask) == -1)
if ( (str2ul(&mask, value) == -1)
|| (mask != (mode_t) mask)) {
return 0;
}
Expand All @@ -123,7 +123,7 @@ static int check_logins (const char *name, const char *maxlogins)
{
unsigned long limit, count;

if (getulong(maxlogins, &limit) == -1) {
if (str2ul(&limit, maxlogins) == -1) {
return 0;
}

Expand Down Expand Up @@ -486,7 +486,7 @@ void setup_limits (const struct passwd *info)
if (strncmp (cp, "pri=", 4) == 0) {
long inc;

if ( (getlong(cp + 4, &inc) == 0)
if ( (str2sl(&inc, cp + 4) == 0)
&& (inc >= -20) && (inc <= 20)) {
errno = 0;
if ( (nice (inc) != -1)
Expand All @@ -504,7 +504,7 @@ void setup_limits (const struct passwd *info)
}
if (strncmp (cp, "ulimit=", 7) == 0) {
long blocks;
if ( (getlong(cp + 7, &blocks) == -1)
if ( (str2sl(&blocks, cp + 7) == -1)
|| (blocks != (int) blocks)
|| (set_filesize_limit (blocks) != 0)) {
SYSLOG ((LOG_WARN,
Expand All @@ -516,7 +516,7 @@ void setup_limits (const struct passwd *info)
if (strncmp (cp, "umask=", 6) == 0) {
unsigned long mask;

if ( (getulong(cp + 6, &mask) == -1)
if ( (str2ul(&mask, cp + 6) == -1)
|| (mask != (mode_t) mask)) {
SYSLOG ((LOG_WARN,
"Can't set umask value for user %s",
Expand Down
3 changes: 1 addition & 2 deletions lib/rlogin.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ do_rlogin(const char *remote_host, char *name, size_t namesize, char *term,
*cp = '\0';
cp++;

if (getulong(cp, &remote_speed) == -1) {
if (str2ul(&remote_speed, cp) == -1)
remote_speed = 9600;
}
}
for (i = 0;
( (speed_table[i].spd_baud != remote_speed)
Expand Down
14 changes: 7 additions & 7 deletions lib/sgetspent.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sgetspent(const char *string)

if (fields[2][0] == '\0') {
spwd.sp_lstchg = -1;
} else if ( (getlong(fields[2], &spwd.sp_lstchg) == -1)
} else if ( (str2sl(&spwd.sp_lstchg, fields[2]) == -1)
|| (spwd.sp_lstchg < 0)) {
return NULL;
}
Expand All @@ -109,7 +109,7 @@ sgetspent(const char *string)

if (fields[3][0] == '\0') {
spwd.sp_min = -1;
} else if ( (getlong(fields[3], &spwd.sp_min) == -1)
} else if ( (str2sl(&spwd.sp_min, fields[3]) == -1)
|| (spwd.sp_min < 0)) {
return NULL;
}
Expand All @@ -120,7 +120,7 @@ sgetspent(const char *string)

if (fields[4][0] == '\0') {
spwd.sp_max = -1;
} else if ( (getlong(fields[4], &spwd.sp_max) == -1)
} else if ( (str2sl(&spwd.sp_max, fields[4]) == -1)
|| (spwd.sp_max < 0)) {
return NULL;
}
Expand All @@ -145,7 +145,7 @@ sgetspent(const char *string)

if (fields[5][0] == '\0') {
spwd.sp_warn = -1;
} else if ( (getlong(fields[5], &spwd.sp_warn) == -1)
} else if ( (str2sl(&spwd.sp_warn, fields[5]) == -1)
|| (spwd.sp_warn < 0)) {
return NULL;
}
Expand All @@ -157,7 +157,7 @@ sgetspent(const char *string)

if (fields[6][0] == '\0') {
spwd.sp_inact = -1;
} else if ( (getlong(fields[6], &spwd.sp_inact) == -1)
} else if ( (str2sl(&spwd.sp_inact, fields[6]) == -1)
|| (spwd.sp_inact < 0)) {
return NULL;
}
Expand All @@ -169,7 +169,7 @@ sgetspent(const char *string)

if (fields[7][0] == '\0') {
spwd.sp_expire = -1;
} else if ( (getlong(fields[7], &spwd.sp_expire) == -1)
} else if ( (str2sl(&spwd.sp_expire, fields[7]) == -1)
|| (spwd.sp_expire < 0)) {
return NULL;
}
Expand All @@ -181,7 +181,7 @@ sgetspent(const char *string)

if (fields[8][0] == '\0') {
spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
} else if (getulong(fields[8], &spwd.sp_flag) == -1) {
} else if (str2ul(&spwd.sp_flag, fields[8]) == -1) {
return NULL;
}

Expand Down
14 changes: 7 additions & 7 deletions lib/shadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[2][0] == '\0') {
spwd.sp_lstchg = -1;
} else {
if (getlong(fields[2], &spwd.sp_lstchg) == -1)
if (str2sl(&spwd.sp_lstchg, fields[2]) == -1)
return 0;
if (spwd.sp_lstchg < 0)
return 0;
Expand All @@ -131,7 +131,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[3][0] == '\0') {
spwd.sp_min = -1;
} else {
if (getlong(fields[3], &spwd.sp_min) == -1)
if (str2sl(&spwd.sp_min, fields[3]) == -1)
return 0;
if (spwd.sp_min < 0)
return 0;
Expand All @@ -144,7 +144,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[4][0] == '\0') {
spwd.sp_max = -1;
} else {
if (getlong(fields[4], &spwd.sp_max) == -1)
if (str2sl(&spwd.sp_max, fields[4]) == -1)
return 0;
if (spwd.sp_max < 0)
return 0;
Expand All @@ -171,7 +171,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[5][0] == '\0') {
spwd.sp_warn = -1;
} else {
if (getlong(fields[5], &spwd.sp_warn) == -1)
if (str2sl(&spwd.sp_warn, fields[5]) == -1)
return 0;
if (spwd.sp_warn < 0)
return 0;
Expand All @@ -185,7 +185,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[6][0] == '\0') {
spwd.sp_inact = -1;
} else {
if (getlong(fields[6], &spwd.sp_inact) == -1)
if (str2sl(&spwd.sp_inact, fields[6]) == -1)
return 0;
if (spwd.sp_inact < 0)
return 0;
Expand All @@ -199,7 +199,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[7][0] == '\0') {
spwd.sp_expire = -1;
} else {
if (getlong(fields[7], &spwd.sp_expire) == -1)
if (str2sl(&spwd.sp_expire, fields[7]) == -1)
return 0;
if (spwd.sp_expire < 0)
return 0;
Expand All @@ -213,7 +213,7 @@ static struct spwd *my_sgetspent (const char *string)
if (fields[8][0] == '\0') {
spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
} else {
if (getulong(fields[8], &spwd.sp_flag) == -1)
if (str2ul(&spwd.sp_flag, fields[8]) == -1)
return 0;
if (spwd.sp_flag < 0)
return 0;
Expand Down
3 changes: 1 addition & 2 deletions lib/strtoday.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ long strtoday (const char *str)
}
if (isnum) {
long retdate;
if (getlong(str, &retdate) == -1) {
if (str2sl(&retdate, str) == -1)
return -2;
}
return retdate;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/subordinateio.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ subordinate_parse(const char *line)
if (i != SUBID_NFIELDS || *fields[0] == '\0' || *fields[1] == '\0' || *fields[2] == '\0')
return NULL;
range.owner = fields[0];
if (getulong(fields[1], &range.start) == -1)
if (str2ul(&range.start, fields[1]) == -1)
return NULL;
if (getulong(fields[2], &range.count) == -1)
if (str2ul(&range.count, fields[2]) == -1)
return NULL;

return &range;
Expand Down
16 changes: 8 additions & 8 deletions src/chage.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ static int new_fields (void)

SNPRINTF(buf, "%ld", mindays);
change_field (buf, sizeof buf, _("Minimum Password Age"));
if ( (getlong(buf, &mindays) == -1)
if ( (str2sl(&mindays, buf) == -1)
|| (mindays < -1)) {
return 0;
}

SNPRINTF(buf, "%ld", maxdays);
change_field (buf, sizeof buf, _("Maximum Password Age"));
if ( (getlong(buf, &maxdays) == -1)
if ( (str2sl(&maxdays, buf) == -1)
|| (maxdays < -1)) {
return 0;
}
Expand All @@ -201,14 +201,14 @@ static int new_fields (void)

SNPRINTF(buf, "%ld", warndays);
change_field (buf, sizeof buf, _("Password Expiration Warning"));
if ( (getlong(buf, &warndays) == -1)
if ( (str2sl(&warndays, buf) == -1)
|| (warndays < -1)) {
return 0;
}

SNPRINTF(buf, "%ld", inactdays);
change_field (buf, sizeof buf, _("Password Inactive"));
if ( (getlong(buf, &inactdays) == -1)
if ( (str2sl(&inactdays, buf) == -1)
|| (inactdays < -1)) {
return 0;
}
Expand Down Expand Up @@ -397,7 +397,7 @@ static void process_flags (int argc, char **argv)
break;
case 'I':
Iflg = true;
if ( (getlong(optarg, &inactdays) == -1)
if ( (str2sl(&inactdays, optarg) == -1)
|| (inactdays < -1)) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"),
Expand All @@ -410,7 +410,7 @@ static void process_flags (int argc, char **argv)
break;
case 'm':
mflg = true;
if ( (getlong(optarg, &mindays) == -1)
if ( (str2sl(&mindays, optarg) == -1)
|| (mindays < -1)) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"),
Expand All @@ -420,7 +420,7 @@ static void process_flags (int argc, char **argv)
break;
case 'M':
Mflg = true;
if ( (getlong(optarg, &maxdays) == -1)
if ( (str2sl(&maxdays, optarg) == -1)
|| (maxdays < -1)) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"),
Expand All @@ -434,7 +434,7 @@ static void process_flags (int argc, char **argv)
break;
case 'W':
Wflg = true;
if ( (getlong(optarg, &warndays) == -1)
if ( (str2sl(&warndays, optarg) == -1)
|| (warndays < -1)) {
fprintf (stderr,
_("%s: invalid numeric argument '%s'\n"),
Expand Down

0 comments on commit f39ac10

Please sign in to comment.