Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify string handling #1048

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ main (void)
printf ("That name is in use, choose another.\n");
done = 0;
}
else if (strchr (usrname, ' ') != NULL)
else if (!!strchr(usrname, ' '))
{
printf ("No spaces in username!!\n");
done = 0;
Expand Down
21 changes: 11 additions & 10 deletions lib/addgrps.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@
#include "prototypes.h"
#include "defines.h"

#include <stdio.h>
#include <grp.h>
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <string.h>

#include "alloc/malloc.h"
#include "alloc/reallocf.h"
#include "shadowlog.h"

#ident "$Id$"

#define SEP ",:"
/*
* Add groups with names from LIST (separated by commas or colons)
* to the supplementary group set. Silently ignore groups which are
* already there. Warning: uses strtok().
* already there.
*/
int add_groups (const char *list)
int
add_groups(const char *list)
{
GETGROUPS_T *grouplist;
size_t i;
int ngroups;
bool added;
char *token;
char *g, *p;
char buf[1024];
int ret;
FILE *shadow_logfd = log_get_logfd();
Expand Down Expand Up @@ -71,13 +72,13 @@ int add_groups (const char *list)
}

added = false;
for (token = strtok (buf, SEP); NULL != token; token = strtok (NULL, SEP)) {
p = buf;
while (NULL != (g = strsep(&p, ",:"))) {
struct group *grp;

grp = getgrnam (token); /* local, no need for xgetgrnam */
grp = getgrnam(g); /* local, no need for xgetgrnam */
if (NULL == grp) {
fprintf (shadow_logfd, _("Warning: unknown group %s\n"),
token);
fprintf(shadow_logfd, _("Warning: unknown group %s\n"), g);
continue;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/chkname.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/param.h>
#include <unistd.h>

Expand Down Expand Up @@ -70,9 +71,9 @@ 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 @@ -92,7 +93,7 @@ is_valid_name(const char *name)
*name == '_' ||
*name == '.' ||
*name == '-' ||
(*name == '$' && name[1] == '\0')
strcmp(name, "$") == 0
))
{
errno = EINVAL;
Expand Down
10 changes: 5 additions & 5 deletions lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* under "cfgin" in config (directly or indirectly). Fallback to default if
* something is bad.
*/
static bool is_listed (const char *cfgin, const char *tty, bool def)
static bool
is_listed(const char *cfgin, const char *tty, bool def)
{
FILE *fp;
char buf[1024], *s;
Expand All @@ -48,14 +49,13 @@ static bool is_listed (const char *cfgin, const char *tty, bool def)

if (*cons != '/') {
char *pbuf;

STRTCPY(buf, cons);
pbuf = &buf[0];
while ((s = strtok (pbuf, ":")) != NULL) {
pbuf = buf;
while (NULL != (s = strsep(&pbuf, ":"))) {
if (strcmp (s, tty) == 0) {
return true;
}

pbuf = NULL;
}
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ void set_env (int argc, char *const *argv)
* but... I feel better with that silly precaution. -j.
*/

void sanitize_env (void)
void
sanitize_env(void)
{
char **envp = environ;
const char *const *bad;
Expand All @@ -225,9 +226,9 @@ void sanitize_env (void)
if (strncmp (*cur, *bad, strlen (*bad)) != 0) {
continue;
}
if (strchr (*cur, '/') == NULL) {
if (strchr(*cur, '/') == NULL)
continue; /* OK */
}

for (move = cur; NULL != *move; move++) {
*move = *(move + 1);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/fields.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ int valid_field (const char *field, const char *illegal)
* prompt the user with the name of the field being changed and the
* current value.
*/
void change_field (char *buf, size_t maxsize, const char *prompt)
void
change_field(char *buf, size_t maxsize, const char *prompt)
{
char newf[200];
char *cp;
Expand All @@ -91,7 +92,7 @@ void change_field (char *buf, size_t maxsize, const char *prompt)
* makes it possible to change the field to empty, by
* entering a space. --marekm
*/
stpcpy(strrspn(newf, " \t\n"), "");
stpcpy(strrspn(newf, " \t"), "");
cp = stpspn(newf, " \t");
strcpy (buf, cp);
}
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
12 changes: 5 additions & 7 deletions lib/motd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ident "$Id$"

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

#include "defines.h"
#include "getdef.h"
Expand All @@ -26,7 +27,8 @@
* it to the user's terminal at login time. The MOTD_FILE configuration
* option is a colon-delimited list of filenames.
*/
void motd (void)
void
motd(void)
{
FILE *fp;
char *motdlist;
Expand All @@ -41,12 +43,8 @@ void motd (void)

motdlist = xstrdup (motdfile);

for (mb = motdlist; ;mb = NULL) {
motdfile = strtok (mb, ":");
if (NULL == motdfile) {
break;
}

mb = motdlist;
while (NULL != (motdfile = strsep(&mb, ":"))) {
fp = fopen (motdfile, "r");
if (NULL != fp) {
while ((c = getc (fp)) != EOF) {
Expand Down
7 changes: 4 additions & 3 deletions lib/obscure.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

#include "attr.h"
#include "prototypes.h"
Expand Down Expand Up @@ -50,7 +51,8 @@ static bool palindrome (MAYBE_UNUSED const char *old, const char *new)
* more than half of the characters are different ones.
*/

static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new)
static bool
similar(/*@notnull@*/const char *old, /*@notnull@*/const char *new)
{
int i, j;

Expand All @@ -65,9 +67,8 @@ static bool similar (/*@notnull@*/const char *old, /*@notnull@*/const char *new)
}

for (i = j = 0; ('\0' != new[i]) && ('\0' != old[i]); i++) {
if (strchr (new, old[i]) != NULL) {
if (!!strchr(new, old[i]))
j++;
}
}

if (i >= j * 2) {
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
Loading
Loading