Skip to content

Commit

Permalink
contrib/, lib/, src/: Use streq() instead of its pattern
Browse files Browse the repository at this point in the history
Except for the added includes, and the removal of redundant parentheses,
this patch can be approximated with the following semantic patch:

	$ cat ~/tmp/spatch/streq.sp;
	@@
	expression a, b;
	@@

	- strcmp(a, b) == 0
	+ streq(a, b)

	@@
	expression a, b;
	@@

	- 0 == strcmp(a, b)
	+ streq(a, b)

	@@
	expression a, b;
	@@

	- !strcmp(a, b)
	+ streq(a, b)

	$ find contrib/ lib* src/ -type f \
	| xargs spatch --sp-file ~/tmp/spatch/streq.sp --in-place;
	$ git restore lib/string/strcmp/streq.h;

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Nov 17, 2024
1 parent 929e61d commit a5a0a56
Show file tree
Hide file tree
Showing 52 changed files with 320 additions and 247 deletions.
4 changes: 3 additions & 1 deletion contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
#include <sys/stat.h>
#include <syslog.h>

#include "string/strcmp/streq.h"


#define IMMEDIATE_CHANGE /* Expire newly created password, must be changed
* immediately upon next login */
Expand Down Expand Up @@ -315,7 +317,7 @@ main (void)
#ifdef HAVE_GETUSERSHELL
setusershell ();
while ((sh = getusershell ()) != NULL)
if (!strcmp (shell, sh))
if (streq(shell, sh))
ok = 1;
endusershell ();
#endif
Expand Down
11 changes: 7 additions & 4 deletions lib/chowndir.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

#include <sys/types.h>
#include <sys/stat.h>
#include "prototypes.h"
#include "defines.h"
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#include "defines.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"


static int chown_tree_at (int at_fd,
const char *path,
uid_t old_uid,
Expand Down Expand Up @@ -56,8 +59,8 @@ static int chown_tree_at (int at_fd,
/*
* Skip the "." and ".." entries
*/
if ( (strcmp (ent->d_name, ".") == 0)
|| (strcmp (ent->d_name, "..") == 0)) {
if ( streq(ent->d_name, ".")
|| streq(ent->d_name, "..")) {
continue;
}

Expand Down
7 changes: 3 additions & 4 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "sssd.h"
#include "string/memset/memzero.h"
#include "string/sprintf/snprintf.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -830,10 +831,8 @@ int commonio_sort_wrt (struct commonio_db *shadow,
if (NULL == spw_ptr->eptr) {
continue;
}
if (strcmp (name, shadow->ops->getname (spw_ptr->eptr))
== 0) {
if (streq(name, shadow->ops->getname(spw_ptr->eptr)))
break;
}
}
if (NULL == spw_ptr) {
continue;
Expand Down Expand Up @@ -1034,7 +1033,7 @@ static /*@dependent@*/ /*@null@*/struct commonio_entry *next_entry_by_name (
for (p = pos; NULL != p; p = p->next) {
ep = p->eptr;
if ( (NULL != ep)
&& (strcmp (db->ops->getname (ep), name) == 0)) {
&& streq(db->ops->getname(ep), name)) {
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "defines.h"
#include "getdef.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcpy/strtcpy.h"
#include "string/strtok/stpsep.h"

Expand Down Expand Up @@ -51,7 +52,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def)
STRTCPY(buf, cons);
pbuf = &buf[0];
while ((s = strtok (pbuf, ":")) != NULL) {
if (strcmp (s, tty) == 0) {
if (streq(s, tty)) {
return true;
}

Expand All @@ -76,7 +77,7 @@ static bool is_listed (const char *cfgin, const char *tty, bool def)

while (fgets (buf, sizeof (buf), fp) != NULL) {
stpsep(buf, "\n");
if (strcmp (buf, tty) == 0) {
if (streq(buf, tty)) {
(void) fclose (fp);
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/copydir.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#endif /* WITH_ATTR */
#include "shadowlog.h"
#include "string/sprintf/xasprintf.h"
#include "string/strcmp/streq.h"


static /*@null@*/const char *src_orig;
Expand Down Expand Up @@ -314,8 +315,8 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
/*
* Skip the "." and ".." entries
*/
if (strcmp(ent->d_name, ".") == 0 ||
strcmp(ent->d_name, "..") == 0)
if (streq(ent->d_name, ".") ||
streq(ent->d_name, ".."))
{
continue;
}
Expand Down
21 changes: 11 additions & 10 deletions lib/getdate.y
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "attr.h"
#include "getdate.h"
#include "string/strchr/stpspn.h"
#include "string/strcmp/streq.h"


/* Some old versions of bison generate parsers that use bcopy.
Expand Down Expand Up @@ -630,12 +631,12 @@ static int LookupWord (char *buff)
if (isupper (*p))
*p = tolower (*p);

if (strcmp (buff, "am") == 0 || strcmp (buff, "a.m.") == 0)
if (streq(buff, "am") || streq(buff, "a.m."))
{
yylval.Meridian = MERam;
return tMERIDIAN;
}
if (strcmp (buff, "pm") == 0 || strcmp (buff, "p.m.") == 0)
if (streq(buff, "pm") || streq(buff, "p.m."))
{
yylval.Meridian = MERpm;
return tMERIDIAN;
Expand All @@ -662,25 +663,25 @@ static int LookupWord (char *buff)
return tp->type;
}
}
else if (strcmp (buff, tp->name) == 0)
else if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
}
}

for (tp = TimezoneTable; tp->name; tp++)
if (strcmp (buff, tp->name) == 0)
if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
}

if (strcmp (buff, "dst") == 0)
if (streq(buff, "dst"))
return tDST;

for (tp = UnitsTable; tp->name; tp++)
if (strcmp (buff, tp->name) == 0)
if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
Expand All @@ -692,7 +693,7 @@ static int LookupWord (char *buff)
{
stpcpy(&buff[i], "");
for (tp = UnitsTable; tp->name; tp++)
if (strcmp (buff, tp->name) == 0)
if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
Expand All @@ -701,7 +702,7 @@ static int LookupWord (char *buff)
}

for (tp = OtherTable; tp->name; tp++)
if (strcmp (buff, tp->name) == 0)
if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
Expand All @@ -711,7 +712,7 @@ static int LookupWord (char *buff)
if (buff[1] == '\0' && isalpha (*buff))
{
for (tp = MilitaryTable; tp->name; tp++)
if (strcmp (buff, tp->name) == 0)
if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
Expand All @@ -727,7 +728,7 @@ static int LookupWord (char *buff)
stpcpy(p, "");
if (0 != i)
for (tp = TimezoneTable; NULL != tp->name; tp++)
if (strcmp (buff, tp->name) == 0)
if (streq(buff, tp->name))
{
yylval.Number = tp->value;
return tp->type;
Expand Down
5 changes: 3 additions & 2 deletions lib/getdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "string/sprintf/xasprintf.h"
#include "string/strchr/stpspn.h"
#include "string/strchr/strrspn.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -419,7 +420,7 @@ static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name, cons
*/

for (ptr = def_table; NULL != ptr->name; ptr++) {
if (strcmp (ptr->name, name) == 0) {
if (streq(ptr->name, name)) {
return ptr;
}
}
Expand All @@ -429,7 +430,7 @@ static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name, cons
*/

for (ptr = knowndef_table; NULL != ptr->name; ptr++) {
if (strcmp (ptr->name, name) == 0) {
if (streq(ptr->name, name)) {
goto out;
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/groupio.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

#include "alloc/calloc.h"
#include "alloc/malloc.h"
#include "prototypes.h"
#include "defines.h"
#include "commonio.h"
#include "defines.h"
#include "getdef.h"
#include "groupio.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"


static /*@null@*/struct commonio_entry *merge_group_entries (
Expand Down Expand Up @@ -263,8 +264,8 @@ static int group_open_hook (void)
struct group *g2 = gr2->eptr;
if (NULL != g1 &&
NULL != g2 &&
0 == strcmp (g1->gr_name, g2->gr_name) &&
0 == strcmp (g1->gr_passwd, g2->gr_passwd) &&
streq(g1->gr_name, g2->gr_name) &&
streq(g1->gr_passwd, g2->gr_passwd) &&
g1->gr_gid == g2->gr_gid) {
/* Both group entries refer to the same
* group. It is a split group. Merge the
Expand Down Expand Up @@ -332,7 +333,7 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
for (i=0; NULL != gptr2->gr_mem[i]; i++) {
char **pmember = gptr1->gr_mem;
while (NULL != *pmember) {
if (0 == strcmp(*pmember, gptr2->gr_mem[i])) {
if (streq(*pmember, gptr2->gr_mem[i])) {
break;
}
pmember++;
Expand All @@ -355,7 +356,7 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
for (i=0; NULL != gptr2->gr_mem[i]; i++) {
char **pmember = new_members;
while (NULL != *pmember) {
if (0 == strcmp(*pmember, gptr2->gr_mem[i])) {
if (streq(*pmember, gptr2->gr_mem[i])) {
break;
}
pmember++;
Expand Down
3 changes: 2 additions & 1 deletion lib/gshadow.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "alloc/x/xrealloc.h"
#include "defines.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -203,7 +204,7 @@ void endsgent (void)
setsgent ();

while ((sgrp = getsgent ()) != NULL) {
if (strcmp (name, sgrp->sg_name) == 0) {
if (streq(name, sgrp->sg_name)) {
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/hushed.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "getdef.h"
#include "prototypes.h"
#include "string/sprintf/snprintf.h"
#include "string/strcmp/streq.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -74,8 +75,8 @@ bool hushed (const char *username)
}
for (found = false; !found && (fgets (buf, sizeof buf, fp) == buf);) {
stpsep(buf, "\n");
found = (strcmp (buf, pw->pw_shell) == 0) ||
(strcmp (buf, pw->pw_name) == 0);
found = streq(buf, pw->pw_shell) ||
streq(buf, pw->pw_name);
}
(void) fclose (fp);
return found;
Expand Down
17 changes: 9 additions & 8 deletions lib/idmapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#if HAVE_SYS_CAPABILITY_H
#include <sys/prctl.h>
#include <sys/capability.h>
#endif

#include "alloc/calloc.h"
#include "alloc/x/xmalloc.h"
#include "atoi/a2i/a2u.h"
#include "prototypes.h"
#include "string/sprintf/stpeprintf.h"
#include "idmapping.h"
#if HAVE_SYS_CAPABILITY_H
#include <sys/prctl.h>
#include <sys/capability.h>
#endif
#include "prototypes.h"
#include "shadowlog.h"
#include "sizeof.h"
#include "string/sprintf/stpeprintf.h"
#include "string/strcmp/streq.h"


struct map_range *
Expand Down Expand Up @@ -133,9 +134,9 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
struct __user_cap_header_struct hdr = {_LINUX_CAPABILITY_VERSION_3, 0};
struct __user_cap_data_struct data[2] = {{0}};

if (strcmp(map_file, "uid_map") == 0) {
if (streq(map_file, "uid_map")) {
cap = CAP_SETUID;
} else if (strcmp(map_file, "gid_map") == 0) {
} else if (streq(map_file, "gid_map")) {
cap = CAP_SETGID;
} else {
fprintf(log_get_logfd(), _("%s: Invalid map file %s specified\n"), log_get_progname(), map_file);
Expand Down
3 changes: 2 additions & 1 deletion lib/isexpired.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "adds.h"
#include "defines.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"

#ident "$Id$"

Expand Down Expand Up @@ -67,7 +68,7 @@ int isexpired (const struct passwd *pw, /*@null@*/const struct spwd *sp)
* returns sp_lstchg==0 (must change password) instead of -1!
*/
if ( (0 == sp->sp_lstchg)
&& (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0)) {
&& (streq(pw->pw_passwd, SHADOW_PASSWD_STRING))) {
return 1;
}

Expand Down
Loading

0 comments on commit a5a0a56

Please sign in to comment.