Skip to content

Commit a2c70df

Browse files
lib/, src/: Use str[n]cmp(3) instead of explicit byte comparisons
This is simpler to read, IMO. Signed-off-by: Alejandro Colomar <[email protected]>
1 parent e7dc5f2 commit a2c70df

28 files changed

+148
-123
lines changed

lib/chkname.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdbool.h>
2929
#include <stddef.h>
3030
#include <stdint.h>
31+
#include <string.h>
3132
#include <sys/param.h>
3233
#include <unistd.h>
3334

@@ -70,9 +71,9 @@ is_valid_name(const char *name)
7071
*/
7172
int numeric;
7273

73-
if ('\0' == *name ||
74-
('.' == *name && (('.' == name[1] && '\0' == name[2]) ||
75-
'\0' == name[1])) ||
74+
if (strcmp(name, "") == 0 ||
75+
strcmp(name, ".") == 0 ||
76+
strcmp(name, "..") == 0 ||
7677
!((*name >= 'a' && *name <= 'z') ||
7778
(*name >= 'A' && *name <= 'Z') ||
7879
(*name >= '0' && *name <= '9') ||
@@ -92,7 +93,7 @@ is_valid_name(const char *name)
9293
*name == '_' ||
9394
*name == '.' ||
9495
*name == '-' ||
95-
(*name == '$' && name[1] == '\0')
96+
strcmp(name, "$") == 0
9697
))
9798
{
9899
errno = EINVAL;

lib/getrange.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <ctype.h>
1111
#include <stdlib.h>
12+
#include <string.h>
1213

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

5556
case '-':
56-
if ('\0' == *end)
57+
if (strcmp(end, "") == 0)
5758
return 0; /* <long>- */
5859
parse_max:
5960
if (!isdigit((unsigned char) *end))

lib/limits.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#ident "$Id$"
2121

22+
#include <string.h>
2223
#include <sys/types.h>
2324
#include <sys/stat.h>
2425
#include <stdio.h>
@@ -341,7 +342,8 @@ static bool user_in_group (const char *uname, const char *gname)
341342
return is_on_list (groupdata->gr_mem, uname);
342343
}
343344

344-
static int setup_user_limits (const char *uname)
345+
static int
346+
setup_user_limits(const char *uname)
345347
{
346348
FILE *fil;
347349
char buf[1024];
@@ -412,11 +414,11 @@ static int setup_user_limits (const char *uname)
412414
}
413415
}
414416
(void) fclose (fil);
415-
if (limits[0] == '\0') {
417+
if (strcmp(limits, "") == 0) {
416418
/* no user specific limits */
417-
if (deflimits[0] == '\0') { /* no default limits */
419+
if (strcmp(deflimits, "") == 0) /* no defaults limits */
418420
return 0;
419-
}
421+
420422
strcpy (limits, deflimits); /* use the default limits */
421423
}
422424
return do_user_limits (limits, uname);

lib/list.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
#include <config.h>
1010

11-
#ident "$Id$"
12-
11+
#include <string.h>
1312
#include <assert.h>
1413

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

227-
if ('\0' == *members) {
226+
if (strcmp(members, "") == 0) {
228227
*array = NULL;
229228
free (members);
230229
return array;

lib/port.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ static int portcmp (const char *pattern, const char *port)
4343
port++;
4444
}
4545

46-
if (('\0' == *pattern) && ('\0' == *port)) {
46+
if (strcmp(pattern, "") == 0 && strcmp(port, "") == 0)
4747
return 0;
48-
}
48+
4949
if (strcmp(orig, "SU") == 0)
5050
return 1;
5151

@@ -203,7 +203,7 @@ getportent(void)
203203

204204
cp = field;
205205

206-
if ('\0' == *cp) {
206+
if (strcmp(field, "") == 0) {
207207
port.pt_times = 0;
208208
return &port;
209209
}
@@ -214,7 +214,7 @@ getportent(void)
214214
* Get the next comma separated entry
215215
*/
216216

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

219219
/*
220220
* Start off with no days of the week

lib/prefix_flag.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ident "$Id$"
1111

1212
#include <stdio.h>
13+
#include <string.h>
1314
#include <assert.h>
1415

1516
#include "atoi/getnum.h"
@@ -50,7 +51,8 @@ static FILE* fp_grent = NULL;
5051
*
5152
* The audit, syslog, or locale files shall be open before
5253
*/
53-
extern const char* process_prefix_flag (const char* short_opt, int argc, char **argv)
54+
extern const char *
55+
process_prefix_flag(const char* short_opt, int argc, char **argv)
5456
{
5557
/*
5658
* Parse the command line options.
@@ -96,7 +98,7 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
9698
exit (EXIT_FAILURE);
9799
}
98100

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

lib/pwauth.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fcntl.h>
1717
#include <signal.h>
1818
#include <stdio.h>
19+
#include <string.h>
1920
#include <sys/types.h>
2021
#include <unistd.h>
2122

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

50-
int pw_auth (const char *cipher,
51-
const char *user,
52-
int reason,
53-
/*@null@*/const char *input)
51+
int
52+
pw_auth(const char *cipher, const char *user, int reason,
53+
/*@null@*/const char *input)
5454
{
5555
int retval;
5656
char prompt[1024];
@@ -101,10 +101,8 @@ int pw_auth (const char *cipher,
101101
* the user could just hit <ENTER>, so it doesn't really
102102
* matter.
103103
*/
104-
105-
if ((NULL == cipher) || ('\0' == *cipher)) {
104+
if (NULL == cipher || strcmp(cipher, "") == 0)
106105
return 0;
107-
}
108106

109107
#ifdef SKEY
110108
/*
@@ -169,7 +167,7 @@ int pw_auth (const char *cipher,
169167
* ...Re-prompt, with echo on.
170168
* -- AR 8/22/1999
171169
*/
172-
if ((0 != retval) && ('\0' == input[0]) && use_skey) {
170+
if (0 != retval && strcmp(input, "") == 0 && use_skey) {
173171
erase_pass(clear);
174172
clear = agetpass(prompt);
175173
input = (clear == NULL) ? "" : clear;

lib/salt.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "getdef.h"
2828
#include "shadowlog.h"
2929

30+
3031
#if (defined CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY && \
3132
CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY)
3233
#define USE_XCRYPT_GENSALT 1
@@ -354,7 +355,8 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
354355
* (if not NULL).
355356
* * For the YESCRYPT method, this specifies the cost factor (if not NULL).
356357
*/
357-
/*@observer@*/const char *crypt_make_salt (/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg)
358+
/*@observer@*/const char *
359+
crypt_make_salt(/*@null@*//*@observer@*/const char *meth, /*@null@*/void *arg)
358360
{
359361
static char result[GENSALT_SETTING_SIZE];
360362
size_t salt_len = MAX_SALT_SIZE;
@@ -417,7 +419,7 @@ static /*@observer@*/const char *gensalt (size_t salt_size)
417419
* Prepare DES setting for crypt_gensalt(), if result
418420
* has not been filled with anything previously.
419421
*/
420-
if ('\0' == result[0]) {
422+
if (strcmp(result, "") == 0) {
421423
/* Avoid -Wunused-but-set-variable. */
422424
salt_len = GENSALT_SETTING_SIZE - 1;
423425
rounds = 0;

lib/setupenv.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
#ident "$Id$"
1717

1818
#include <assert.h>
19+
#include <ctype.h>
20+
#include <stdio.h>
21+
#include <string.h>
1922
#include <sys/types.h>
2023
#include <sys/stat.h>
21-
#include <stdio.h>
22-
#include <ctype.h>
2324

2425
#include "prototypes.h"
2526
#include "defines.h"
@@ -166,7 +167,8 @@ static void read_env_file (const char *filename)
166167
* variables.
167168
*/
168169

169-
void setup_env (struct passwd *info)
170+
void
171+
setup_env(struct passwd *info)
170172
{
171173
#ifndef USE_PAM
172174
const char *envf;
@@ -209,7 +211,7 @@ void setup_env (struct passwd *info)
209211
* Create the SHELL environmental variable and export it.
210212
*/
211213

212-
if ((NULL == info->pw_shell) || ('\0' == *info->pw_shell)) {
214+
if (NULL == info->pw_shell || strcmp(info->pw_shell, "") == 0) {
213215
free (info->pw_shell);
214216
info->pw_shell = xstrdup (SHELL);
215217
}

lib/sgetgrent.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ list(char *s)
5454
return NULL;
5555
}
5656
}
57-
if (!s || s[0] == '\0')
57+
if (!s || strcmp(s, "") == 0)
5858
break;
5959
members[i++] = strsep(&s, ",");
6060
}
@@ -63,7 +63,8 @@ list(char *s)
6363
}
6464

6565

66-
struct group *sgetgrent (const char *buf)
66+
struct group *
67+
sgetgrent(const char *buf)
6768
{
6869
static char *grpbuf = NULL;
6970
static size_t size = 0;
@@ -89,7 +90,7 @@ struct group *sgetgrent (const char *buf)
8990
for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++)
9091
grpfields[i] = strsep(&cp, ":");
9192

92-
if (i < (NFIELDS - 1) || *grpfields[2] == '\0' || cp != NULL) {
93+
if (i < (NFIELDS - 1) || strcmp(grpfields[2], "") == 0 || cp != NULL) {
9394
return NULL;
9495
}
9596
grent.gr_name = grpfields[0];

lib/sgetpwent.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ sgetpwent(const char *buf)
7575
* There must be exactly NFIELDS colon separated fields or
7676
* the entry is invalid. Also, the UID and GID must be non-blank.
7777
*/
78-
79-
if (i != NFIELDS || *fields[2] == '\0' || *fields[3] == '\0')
78+
if (i != NFIELDS)
79+
return NULL;
80+
if (strcmp(fields[2], "") == 0)
81+
return NULL;
82+
if (strcmp(fields[3], "") == 0)
8083
return NULL;
8184

8285
/*

lib/sgetspent.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ sgetspent(const char *string)
8585
* incorrectly formatted number.
8686
*/
8787

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

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

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

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

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

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

159-
if (fields[8][0] == '\0')
159+
if (strcmp(fields[8], "") == 0)
160160
spwd.sp_flag = SHADOW_SP_FLAG_UNSET;
161161
else if (str2ul(&spwd.sp_flag, fields[8]) == -1)
162162
return NULL;

0 commit comments

Comments
 (0)