Skip to content

Commit

Permalink
lib/, src/: Use str[n]cmp(3) instead of explicit byte comparisons
Browse files Browse the repository at this point in the history
This is simpler to read, IMO.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jul 30, 2024
1 parent 9a6e328 commit 3473796
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 124 deletions.
12 changes: 7 additions & 5 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>

Expand All @@ -49,7 +50,8 @@ login_name_max_size(void)
}


static bool is_valid_name (const char *name)
static bool
is_valid_name(const char *name)
{
if (allow_bad_names) {
return true;
Expand All @@ -66,9 +68,9 @@ static bool is_valid_name (const char *name)
*/
int numeric;

if ('\0' == *name ||
('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
'\0' == name[1])) ||
if (strcmp(name, "") == 0 ||
strcmp(name, ".") == 0 ||
strcmp(name, "..") == 0 ||
!((*name >= 'a' && *name <= 'z') ||
(*name >= 'A' && *name <= 'Z') ||
(*name >= '0' && *name <= '9') ||
Expand All @@ -86,7 +88,7 @@ static bool is_valid_name (const char *name)
*name == '_' ||
*name == '.' ||
*name == '-' ||
(*name == '$' && name[1] == '\0')
strcmp(name, "$") == 0
)) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/getrange.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "atoi/a2i/a2u.h"
#include "defines.h"
Expand Down Expand Up @@ -53,7 +54,7 @@ getrange(const char *range,
return 0; /* <long> */

case '-':
if ('\0' == *end)
if (strcmp(end, "") == 0)
return 0; /* <long>- */
parse_max:
if (!isdigit((unsigned char) *end))
Expand Down
10 changes: 6 additions & 4 deletions lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#ident "$Id$"

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
Expand Down Expand Up @@ -341,7 +342,8 @@ static bool user_in_group (const char *uname, const char *gname)
return is_on_list (groupdata->gr_mem, uname);
}

static int setup_user_limits (const char *uname)
static int
setup_user_limits(const char *uname)
{
FILE *fil;
char buf[1024];
Expand Down Expand Up @@ -412,11 +414,11 @@ static int setup_user_limits (const char *uname)
}
}
(void) fclose (fil);
if (limits[0] == '\0') {
if (strcmp(limits, "") == 0) {
/* no user specific limits */
if (deflimits[0] == '\0') { /* no default limits */
if (strcmp(deflimits, "") == 0) /* no defaults limits */
return 0;
}

strcpy (limits, deflimits); /* use the default limits */
}
return do_user_limits (limits, uname);
Expand Down
5 changes: 2 additions & 3 deletions lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

#include <config.h>

#ident "$Id$"

#include <string.h>
#include <assert.h>

#include "alloc/x/xmalloc.h"
Expand Down Expand Up @@ -224,7 +223,7 @@ bool is_on_list (char *const *list, const char *member)
* Empty list is special - 0 members, not 1 empty member. --marekm
*/

if ('\0' == *members) {
if (strcmp(members, "") == 0) {
*array = NULL;
free (members);
return array;
Expand Down
8 changes: 4 additions & 4 deletions lib/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ static int portcmp (const char *pattern, const char *port)
port++;
}

if (('\0' == *pattern) && ('\0' == *port)) {
if (strcmp(pattern, "") == 0 && strcmp(port, "") == 0)
return 0;
}

if (strcmp(orig, "SU") == 0)
return 1;

Expand Down Expand Up @@ -203,7 +203,7 @@ getportent(void)

cp = field;

if ('\0' == *cp) {
if (strcmp(field, "") == 0) {
port.pt_times = 0;
return &port;
}
Expand All @@ -214,7 +214,7 @@ getportent(void)
* Get the next comma separated entry
*/

for (j = 0; ('\0' != *cp) && (j < PORT_TIMES); j++) {
for (j = 0; strcmp(cp, "") == 0 && (j < PORT_TIMES); j++) {

/*
* Start off with no days of the week
Expand Down
6 changes: 4 additions & 2 deletions lib/prefix_flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ident "$Id$"

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "atoi/getnum.h"
Expand Down Expand Up @@ -50,7 +51,8 @@ static FILE* fp_grent = NULL;
*
* The audit, syslog, or locale files shall be open before
*/
extern const char* process_prefix_flag (const char* short_opt, int argc, char **argv)
extern const char *
process_prefix_flag(const char* short_opt, int argc, char **argv)
{
/*
* Parse the command line options.
Expand Down Expand Up @@ -96,7 +98,7 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
exit (EXIT_FAILURE);
}

if ( prefix[0] == '\0' || !strcmp(prefix, "/"))
if (strcmp(prefix, "") == 0 || strcmp(prefix, "/") == 0)
return ""; /* if prefix is "/" then we ignore the flag option */
/* should we prevent symbolic link from being used as a prefix? */

Expand Down
14 changes: 6 additions & 8 deletions lib/pwauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

Expand Down Expand Up @@ -47,10 +48,9 @@ static const char *PROMPT = gettext_noop ("%s's Password: ");
* compared.
*/

int pw_auth (const char *cipher,
const char *user,
int reason,
/*@null@*/const char *input)
int
pw_auth(const char *cipher, const char *user, int reason,
/*@null@*/const char *input)
{
int retval;
char prompt[1024];
Expand Down Expand Up @@ -101,10 +101,8 @@ int pw_auth (const char *cipher,
* the user could just hit <ENTER>, so it doesn't really
* matter.
*/

if ((NULL == cipher) || ('\0' == *cipher)) {
if (NULL == cipher || strcmp(cipher, "") == 0)
return 0;
}

#ifdef SKEY
/*
Expand Down Expand Up @@ -169,7 +167,7 @@ int pw_auth (const char *cipher,
* ...Re-prompt, with echo on.
* -- AR 8/22/1999
*/
if ((0 != retval) && ('\0' == input[0]) && use_skey) {
if (0 != retval && strcmp(input, "") == 0 && use_skey) {
erase_pass(clear);
clear = agetpass(prompt);
input = (clear == NULL) ? "" : clear;
Expand Down
6 changes: 4 additions & 2 deletions lib/salt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "getdef.h"
#include "shadowlog.h"


#if (defined CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY && \
CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY)
#define USE_XCRYPT_GENSALT 1
Expand Down Expand Up @@ -354,7 +355,8 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
* (if not NULL).
* * For the YESCRYPT method, this specifies the cost factor (if not NULL).
*/
/*@observer@*/const char *crypt_make_salt (/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg)
/*@observer@*/const char *
crypt_make_salt(/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg)
{
static char result[GENSALT_SETTING_SIZE];
size_t salt_len = MAX_SALT_SIZE;
Expand Down Expand Up @@ -417,7 +419,7 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
* Prepare DES setting for crypt_gensalt(), if result
* has not been filled with anything previously.
*/
if ('\0' == result[0]) {
if (strcmp(result, "") == 0) {
/* Avoid -Wunused-but-set-variable. */
salt_len = GENSALT_SETTING_SIZE - 1;
rounds = 0;
Expand Down
10 changes: 6 additions & 4 deletions lib/setupenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#ident "$Id$"

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <ctype.h>

#include "prototypes.h"
#include "defines.h"
Expand Down Expand Up @@ -166,7 +167,8 @@ static void read_env_file (const char *filename)
* variables.
*/

void setup_env (struct passwd *info)
void
setup_env(struct passwd *info)
{
#ifndef USE_PAM
const char *envf;
Expand Down Expand Up @@ -209,7 +211,7 @@ void setup_env (struct passwd *info)
* Create the SHELL environmental variable and export it.
*/

if ((NULL == info->pw_shell) || ('\0' == *info->pw_shell)) {
if (NULL == info->pw_shell || strcmp(info->pw_shell, "") == 0) {
free (info->pw_shell);
info->pw_shell = xstrdup (SHELL);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/sgetgrent.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ list(char *s)
return NULL;
}
}
if (!s || s[0] == '\0')
if (!s || strcmp(s, "") == 0)
break;
members[i++] = strsep(&s, ",");
}
Expand All @@ -63,7 +63,8 @@ list(char *s)
}


struct group *sgetgrent (const char *buf)
struct group *
sgetgrent(const char *buf)
{
static char *grpbuf = NULL;
static size_t size = 0;
Expand All @@ -89,7 +90,7 @@ struct group *sgetgrent (const char *buf)
for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++)
grpfields[i] = strsep(&cp, ":");

if (i < (NFIELDS - 1) || *grpfields[2] == '\0' || cp != NULL) {
if (i < (NFIELDS - 1) || strcmp(grpfields[2], "") == 0 || cp != NULL) {
return NULL;
}
grent.gr_name = grpfields[0];
Expand Down
7 changes: 5 additions & 2 deletions lib/sgetpwent.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ sgetpwent(const char *buf)
* There must be exactly NFIELDS colon separated fields or
* the entry is invalid. Also, the UID and GID must be non-blank.
*/

if (i != NFIELDS || *fields[2] == '\0' || *fields[3] == '\0')
if (i != NFIELDS)
return NULL;
if (strcmp(fields[2], "") == 0)
return NULL;
if (strcmp(fields[3], "") == 0)
return NULL;

/*
Expand Down
14 changes: 7 additions & 7 deletions lib/sgetspent.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ sgetspent(const char *string)
* incorrectly formatted number.
*/

if (fields[2][0] == '\0')
if (strcmp(fields[2], "") == 0)
spwd.sp_lstchg = -1;
else if (a2sl(&spwd.sp_lstchg, fields[2], NULL, 0, 0, LONG_MAX) == -1)
return NULL;
Expand All @@ -94,7 +94,7 @@ sgetspent(const char *string)
* Get the minimum period between password changes.
*/

if (fields[3][0] == '\0')
if (strcmp(fields[3], "") == 0)
spwd.sp_min = -1;
else if (a2sl(&spwd.sp_min, fields[3], NULL, 0, 0, LONG_MAX) == -1)
return NULL;
Expand All @@ -103,7 +103,7 @@ sgetspent(const char *string)
* Get the maximum number of days a password is valid.
*/

if (fields[4][0] == '\0')
if (strcmp(fields[4], "") == 0)
spwd.sp_max = -1;
else if (a2sl(&spwd.sp_max, fields[4], NULL, 0, 0, LONG_MAX) == -1)
return NULL;
Expand All @@ -126,7 +126,7 @@ sgetspent(const char *string)
* Get the number of days of password expiry warning.
*/

if (fields[5][0] == '\0')
if (strcmp(fields[5], "") == 0)
spwd.sp_warn = -1;
else if (a2sl(&spwd.sp_warn, fields[5], NULL, 0, 0, LONG_MAX) == -1)
return NULL;
Expand All @@ -136,7 +136,7 @@ sgetspent(const char *string)
* disabled.
*/

if (fields[6][0] == '\0')
if (strcmp(fields[6], "") == 0)
spwd.sp_inact = -1;
else if (a2sl(&spwd.sp_inact, fields[6], NULL, 0, 0, LONG_MAX) == -1)
return NULL;
Expand All @@ -146,7 +146,7 @@ sgetspent(const char *string)
* set to expire.
*/

if (fields[7][0] == '\0')
if (strcmp(fields[7], "") == 0)
spwd.sp_expire = -1;
else if (a2sl(&spwd.sp_expire, fields[7], NULL, 0, 0, LONG_MAX) == -1)
return NULL;
Expand All @@ -156,7 +156,7 @@ sgetspent(const char *string)
* to have anything other than a valid integer in it.
*/

if (fields[8][0] == '\0')
if (strcmp(fields[8], "") == 0)
spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
else if (str2ul(&spwd.sp_flag, fields[8]) == -1)
return NULL;
Expand Down
Loading

0 comments on commit 3473796

Please sign in to comment.