Skip to content

Commit

Permalink
lib/, src/, po/: get[u]long(): Move functions to lib/atoi/str2i.h
Browse files Browse the repository at this point in the history
And make them inline.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar authored and hallyn committed Mar 29, 2024
1 parent dc12e87 commit 27e236c
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 84 deletions.
4 changes: 2 additions & 2 deletions lib/Makefile.am
Expand Up @@ -31,6 +31,8 @@ libshadow_la_SOURCES = \
agetpass.h \
alloc.c \
alloc.h \
atoi/str2i.c \
atoi/str2i.h \
atoi/strtoi.c \
atoi/strtoi.h \
atoi/strtou_noneg.c \
Expand Down Expand Up @@ -74,11 +76,9 @@ libshadow_la_SOURCES = \
getdate.y \
getdef.c \
getdef.h \
getlong.c \
getgr_nam_gid.c \
getrange.c \
gettime.c \
getulong.c \
groupio.c \
groupmem.c \
groupio.h \
Expand Down
12 changes: 12 additions & 0 deletions lib/atoi/str2i.c
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2007-2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#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);
58 changes: 58 additions & 0 deletions lib/atoi/str2i.h
@@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2007-2009, Nicolas François
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_ATOI_STR2I_H_
#define SHADOW_INCLUDE_LIB_ATOI_STR2I_H_


#include <config.h>

#include <stdlib.h>
#include <errno.h>

#include "atoi/str2i.h"
#include "atoi/strtou_noneg.h"
#include "attr.h"


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


inline int
getlong(const char *restrict s, long *restrict n)
{
char *endp;
long val;

errno = 0;
val = strtol(s, &endp, 0);
if (('\0' == *s) || ('\0' != *endp) || (0 != errno))
return -1;

*n = val;
return 0;
}


inline int
getulong(const char *restrict s, unsigned long *restrict n)
{
char *endp;
unsigned long val;

errno = 0;
val = strtoul_noneg(s, &endp, 0);
if (('\0' == *s) || ('\0' != *endp) || (0 != errno))
return -1;

*n = val;
return 0;
}


#endif // include guard
1 change: 1 addition & 0 deletions lib/getdef.c
Expand Up @@ -23,6 +23,7 @@
#endif

#include "alloc.h"
#include "atoi/str2i.h"
#include "getdef.h"
#include "shadowlog_internal.h"
#include "string/sprintf.h"
Expand Down
36 changes: 0 additions & 36 deletions lib/getlong.c

This file was deleted.

37 changes: 0 additions & 37 deletions lib/getulong.c

This file was deleted.

2 changes: 2 additions & 0 deletions lib/idmapping.c
Expand Up @@ -14,6 +14,7 @@
#include <strings.h>

#include "alloc.h"
#include "atoi/str2i.h"
#include "prototypes.h"
#include "string/stpeprintf.h"
#include "idmapping.h"
Expand All @@ -24,6 +25,7 @@
#include "shadowlog.h"
#include "sizeof.h"


struct map_range *get_map_ranges(int ranges, int argc, char **argv)
{
struct map_range *mappings, *mapping;
Expand Down
4 changes: 4 additions & 0 deletions lib/limits.c
Expand Up @@ -29,7 +29,11 @@
#include "getdef.h"
#include "shadowlog.h"
#include <sys/resource.h>

#include "atoi/str2i.h"
#include "memzero.h"


#ifndef LIMITS_FILE
#define LIMITS_FILE "/etc/limits"
#endif
Expand Down
8 changes: 0 additions & 8 deletions lib/prototypes.h
Expand Up @@ -149,10 +149,6 @@ extern int get_gid (const char *gidstr, gid_t *gid);
/* getgr_nam_gid.c */
extern /*@only@*//*@null@*/struct group *getgr_nam_gid (/*@null@*/const char *grname);

/* getlong.c */
ATTR_ACCESS(write_only, 2)
extern int getlong(const char *restrict numstr, long *restrict result);

/* get_pid.c */
extern int get_pid (const char *pidstr, pid_t *pid);
extern int get_pidfd_from_fd(const char *pidfdstr);
Expand All @@ -169,10 +165,6 @@ extern time_t gettime (void);
/* get_uid.c */
extern int get_uid (const char *uidstr, uid_t *uid);

/* getulong.c */
ATTR_ACCESS(write_only, 2)
extern int getulong(const char *restrict numstr, unsigned long *restrict result);

/* fputsx.c */
ATTR_ACCESS(write_only, 1, 2)
extern /*@null@*/char *fgetsx(/*@returned@*/char *restrict, int, FILE *restrict);
Expand Down
4 changes: 4 additions & 0 deletions lib/rlogin.c
Expand Up @@ -18,6 +18,10 @@
#include <stdio.h>
#include <pwd.h>
#include <netdb.h>

#include "atoi/str2i.h"


static struct {
int spd_name;
int spd_baud;
Expand Down
1 change: 1 addition & 0 deletions lib/sgetspent.c
Expand Up @@ -19,6 +19,7 @@
#include <sys/types.h>
#include <string.h>

#include "atoi/str2i.h"
#include "prototypes.h"
#include "shadowlog_internal.h"
#include "defines.h"
Expand Down
2 changes: 2 additions & 0 deletions lib/shadow.c
Expand Up @@ -19,6 +19,8 @@
#include "defines.h"
#include <stdio.h>

#include "atoi/str2i.h"


static FILE *shadow;

Expand Down
2 changes: 2 additions & 0 deletions lib/strtoday.c
Expand Up @@ -13,9 +13,11 @@

#ident "$Id$"

#include "atoi/str2i.h"
#include "prototypes.h"
#include "getdate.h"


/*
* strtoday() now uses get_date() (borrowed from GNU shellutils)
* which can handle many date formats, for example:
Expand Down
1 change: 1 addition & 0 deletions lib/subordinateio.c
Expand Up @@ -19,6 +19,7 @@
#include <string.h>

#include "alloc.h"
#include "atoi/str2i.h"
#include "string/sprintf.h"


Expand Down
1 change: 0 additions & 1 deletion po/POTFILES.in
Expand Up @@ -25,7 +25,6 @@ lib/fputsx.c
lib/get_gid.c
lib/get_uid.c
lib/getdef.c
lib/getlong.c
lib/getgr_nam_gid.c
lib/getrange.c
lib/groupio.c
Expand Down
1 change: 1 addition & 0 deletions src/chage.c
Expand Up @@ -27,6 +27,7 @@
#include <pwd.h>

#include "alloc.h"
#include "atoi/str2i.h"
#include "defines.h"
#include "memzero.h"
#include "prototypes.h"
Expand Down
3 changes: 3 additions & 0 deletions src/chgpasswd.c
Expand Up @@ -16,11 +16,13 @@
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef ACCT_TOOLS_SETUID
#ifdef USE_PAM
#include "pam_defs.h"
#endif /* USE_PAM */
#endif /* ACCT_TOOLS_SETUID */
#include "atoi/str2i.h"
#include "defines.h"
#include "nscd.h"
#include "sssd.h"
Expand All @@ -33,6 +35,7 @@
#include "exitcodes.h"
#include "shadowlog.h"


/*
* Global variables
*/
Expand Down
3 changes: 3 additions & 0 deletions src/chpasswd.c
Expand Up @@ -16,9 +16,11 @@
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef USE_PAM
#include "pam_defs.h"
#endif /* USE_PAM */
#include "atoi/str2i.h"
#include "defines.h"
#include "nscd.h"
#include "sssd.h"
Expand All @@ -30,6 +32,7 @@
#include "exitcodes.h"
#include "shadowlog.h"


#define IS_CRYPT_METHOD(str) ((crypt_method != NULL && strcmp(crypt_method, str) == 0) ? true : false)

/*
Expand Down
2 changes: 2 additions & 0 deletions src/faillog.c
Expand Up @@ -19,6 +19,7 @@
#include <time.h>
#include <assert.h>

#include "atoi/str2i.h"
#include "defines.h"
#include "faillog.h"
#include "memzero.h"
Expand All @@ -29,6 +30,7 @@
#include "string/strftime.h"



/* local function prototypes */
NORETURN static void usage (int status);
static void print_one (/*@null@*/const struct passwd *pw, bool force);
Expand Down
2 changes: 2 additions & 0 deletions src/lastlog.c
Expand Up @@ -23,6 +23,7 @@
#include <net/if.h>
#endif

#include "atoi/str2i.h"
#include "defines.h"
#include "prototypes.h"
#include "getdef.h"
Expand All @@ -33,6 +34,7 @@
#include "string/strftime.h"



/*
* Needed for MkLinux DR1/2/2.1 - J.
*/
Expand Down
1 change: 1 addition & 0 deletions src/newusers.c
Expand Up @@ -31,6 +31,7 @@
#include <string.h>

#include "alloc.h"
#include "atoi/str2i.h"
#ifdef ACCT_TOOLS_SETUID
#ifdef USE_PAM
#include "pam_defs.h"
Expand Down
2 changes: 2 additions & 0 deletions src/passwd.c
Expand Up @@ -22,6 +22,7 @@

#include "agetpass.h"
#include "alloc.h"
#include "atoi/str2i.h"
#include "defines.h"
#include "getdef.h"
#include "memzero.h"
Expand All @@ -36,6 +37,7 @@
#include "time/day_to_str.h"



/*
* exit status values
*/
Expand Down
1 change: 1 addition & 0 deletions src/useradd.c
Expand Up @@ -37,6 +37,7 @@
#include <unistd.h>

#include "alloc.h"
#include "atoi/str2i.h"
#include "chkname.h"
#include "defines.h"
#include "faillog.h"
Expand Down

0 comments on commit 27e236c

Please sign in to comment.