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

Replace fgetsx() by fgets(3) and fputsx() by fputs(3) #1056

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
11f9734
lib/, src/: Reduce scope of local variables
alejandro-colomar Dec 10, 2024
0cb53ee
lib/string/strcmp/: strprefix(): Add API
alejandro-colomar Jul 26, 2024
416a4df
lib/, src/: Use s=strprefix(s,p)?:s instead of its pattern
alejandro-colomar Dec 10, 2024
70bfb77
lib/, src/: Use strprefix() instead of its pattern
alejandro-colomar Dec 10, 2024
8756b3a
lib/env.c: sanitize_env(): Use !strprefix() instead of its pattern
alejandro-colomar Dec 10, 2024
99a02a1
lib/: Use strprefix() instead of its pattern
alejandro-colomar Dec 10, 2024
fe82e6d
lib/: Use !strprefix() instead of its pattern
alejandro-colomar Dec 10, 2024
09a331c
src/check_subid_range.c: main(): Remove local variable
alejandro-colomar Dec 10, 2024
0b8d02a
lib/, src/: Use strprefix() instead of its pattern
alejandro-colomar Dec 10, 2024
7c229c8
lib/, src/: Use str[n]cmp(3) instead of explicit byte comparisons
alejandro-colomar Jul 4, 2024
d174814
contrib/, lib/, src/: Use consistent style using strchr(3) in conditi…
alejandro-colomar Jul 4, 2024
8657193
contrib/, lib/, src/: Consistently use sizeof() as if it were a function
alejandro-colomar Jul 23, 2024
c8c777b
lib/, src/: Remove useless casts in fgets(3)
alejandro-colomar Jul 21, 2024
eccf426
lib/, src/: Consistently use NULL with fgets(3)
alejandro-colomar Jul 21, 2024
debf7e3
lib/, po/: Remove fgetsx() and fputsx()
alejandro-colomar Jul 21, 2024
fb154cc
lib/: Use getline(3) instead of its pattern
alejandro-colomar Jul 22, 2024
62af460
lib/gshadow.c: fgetsgent(): Don't use static variables
alejandro-colomar Jul 22, 2024
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
37 changes: 19 additions & 18 deletions contrib/adduser.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#include <syslog.h>

#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


#define IMMEDIATE_CHANGE /* Expire newly created password, must be changed
Expand Down Expand Up @@ -203,7 +204,7 @@ main (void)
printf ("\nLogin to add (^C to quit): ");
fflush (stdout);

safeget (usrname, sizeof (usrname));
safeget(usrname, sizeof(usrname));

if (!strlen (usrname))
{
Expand All @@ -224,7 +225,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 All @@ -238,10 +239,10 @@ main (void)

printf ("\nFull Name [%s]: ", usrname);
fflush (stdout);
safeget (person, sizeof (person));
safeget(person, sizeof(person));
if (!strlen (person))
{
bzero (person, sizeof (person));
bzero(person, sizeof(person));
strcpy (person, usrname);
};

Expand All @@ -252,7 +253,7 @@ main (void)
bad = 0;
printf ("GID [%d]: ", DEFAULT_GROUP);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
if (!strlen (foo))
group = DEFAULT_GROUP;
else if (isdigit (*foo))
Expand Down Expand Up @@ -293,7 +294,7 @@ main (void)
printf ("\nIf home dir ends with a / then '%s' will be appended to it\n", usrname);
printf ("Home Directory [%s/%s]: ", DEFAULT_HOME, usrname);
fflush (stdout);
safeget (dir, sizeof (dir));
safeget(dir, sizeof(dir));
if (!strlen(dir)) /* hit return */
sprintf(dir, "%s/%s", DEFAULT_HOME, usrname);
else if (dir[strlen (dir) - 1] == '/')
Expand All @@ -307,7 +308,7 @@ main (void)

printf ("\nShell [%s]: ", DEFAULT_SHELL);
fflush (stdout);
safeget (shell, sizeof (shell));
safeget(shell, sizeof(shell));
if (!strlen (shell))
strcpy(shell, DEFAULT_SHELL);
else
Expand Down Expand Up @@ -339,31 +340,31 @@ main (void)
#endif
printf ("\nMin. Password Change Days [%d]: ", DEFAULT_MIN_PASS);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
if (strlen (foo) > 1)
min_pass = DEFAULT_MIN_PASS;
else
min_pass = atoi (foo);

printf ("Max. Password Change Days [%d]: ", DEFAULT_MAX_PASS);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
if (strlen (foo) > 1)
max_pass = atoi (foo);
else
max_pass = DEFAULT_MAX_PASS;

printf ("Password Warning Days [%d]: ", DEFAULT_WARN_PASS);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
warn_pass = atoi (foo);
if (warn_pass == 0)

warn_pass = DEFAULT_WARN_PASS;

printf ("Days after Password Expiry for Account Locking [%d]: ", DEFAULT_USER_DIE);
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));
user_die = atoi (foo);
if (user_die == 0)
user_die = DEFAULT_USER_DIE;
Expand All @@ -387,9 +388,9 @@ main (void)
min_pass, max_pass, warn_pass, user_die);
printf ("\nIs this correct? [y/N]: ");
fflush (stdout);
safeget (foo, sizeof (foo));
safeget(foo, sizeof(foo));

done = bad = correct = (foo[0] == 'y' || foo[0] == 'Y');
done = bad = correct = (strprefix(foo, "y") || strprefix(foo, "Y"));

if (bad != 1)
printf ("\nUser [%s] not added\n", usrname);
Expand All @@ -400,7 +401,7 @@ main (void)

*environ = NULL;

bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
Comment on lines -403 to +404
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thesamesam

If you apply the semantic patch accompanied with the corresponding commit message, and then git diff -w against the actual commit, you should be able to ignore these unrelated whitespace changes. I've written recommendations for how to better review each commit.

I hope that helps reviewing, while at the same time allowing me to apply some house style while doing the changes.

sprintf (cmd, "%s -g %d -d %s -s %s -c \"%s\" -m -k /etc/skel %s",
USERADD_PATH, group, dir, shell, person, usrname);
printf ("Calling useradd to add new user:\n%s\n", cmd);
Expand All @@ -418,7 +419,7 @@ main (void)
*/
setuid (0);

bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));

/* Chage runs suid root. => we need ruid root to run it with
* anything other than chage -l
Expand All @@ -438,7 +439,7 @@ main (void)
/* I want to add a user completely with one easy command --chris */

#ifdef HAVE_QUOTAS
bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s -p %s -u %s", EDQUOTA_PATH, QUOTA_DEFAULT, usrname);
printf ("%s\n", cmd);
if (system (cmd))
Expand All @@ -452,7 +453,7 @@ main (void)
printf ("\nDefault quota set.\n");
#endif /* HAVE_QUOTAS */

bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s %s", PASSWD_PATH, usrname);
if (system (cmd))
{
Expand All @@ -462,7 +463,7 @@ main (void)
#endif
}
#ifdef IMMEDIATE_CHANGE
bzero (cmd, sizeof (cmd));
bzero(cmd, sizeof(cmd));
sprintf (cmd, "%s -e %s", PASSWD_PATH, usrname);
if (system (cmd))
{
Expand Down
3 changes: 2 additions & 1 deletion lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ libshadow_la_SOURCES = \
find_new_uid.c \
find_new_sub_gids.c \
find_new_sub_uids.c \
fputsx.c \
fs/readlink/areadlink.c \
fs/readlink/areadlink.h \
fs/readlink/readlinknul.c \
Expand Down Expand Up @@ -206,6 +205,8 @@ libshadow_la_SOURCES = \
string/strchr/strrspn.h \
string/strcmp/streq.c \
string/strcmp/streq.h \
string/strcmp/strprefix.c \
string/strcmp/strprefix.h \
string/strcpy/stpecpy.c \
string/strcpy/stpecpy.h \
string/strcpy/strncat.c \
Expand Down
49 changes: 15 additions & 34 deletions lib/commonio.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <utime.h>

#include "alloc/malloc.h"
#include "alloc/reallocf.h"
#include "atoi/getnum.h"
#include "commonio.h"
#include "defines.h"
Expand All @@ -37,6 +37,7 @@
#include "string/memset/memzero.h"
#include "string/sprintf/snprintf.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strtok/stpsep.h"


Expand Down Expand Up @@ -187,7 +188,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
errno = EINVAL;
return 0;
}
len = read (fd, buf, sizeof (buf) - 1);
len = read(fd, buf, sizeof(buf) - 1);
close (fd);
if (len <= 0) {
if (log) {
Expand Down Expand Up @@ -524,7 +525,7 @@ static void add_one_entry (struct commonio_db *db,

static bool name_is_nis (const char *name)
{
return (('+' == name[0]) || ('-' == name[0]));
return strprefix(name, "+") || strprefix(name, "-");
}


Expand Down Expand Up @@ -571,11 +572,9 @@ static void add_one_entry_nis (struct commonio_db *db,
}
#endif /* KEEP_NIS_AT_END */

/* Initial buffer size, as well as increment if not sufficient
(for reading very long lines in group files). */
#define BUFLEN 4096

int commonio_open (struct commonio_db *db, int mode)
int
commonio_open(struct commonio_db *db, int mode)
{
char *buf;
char *line;
Expand Down Expand Up @@ -637,31 +636,12 @@ int commonio_open (struct commonio_db *db, int mode)
return 0;
}

buflen = BUFLEN;
buf = MALLOC(buflen, char);
if (NULL == buf)
goto cleanup_errno;

while (db->ops->fgets (buf, buflen, db->fp) == buf) {
buf = NULL;
while (getline(&buf, &buflen, db->fp) != -1) {
struct commonio_entry *p;

while ( (strrchr (buf, '\n') == NULL)
&& (feof (db->fp) == 0)) {
size_t len;

buflen += BUFLEN;
buf = REALLOCF(buf, buflen, char);
if (NULL == buf)
goto cleanup_errno;

len = strlen (buf);
if (db->ops->fgets (buf + len,
(int) (buflen - len),
db->fp) == NULL) {
goto cleanup_buf;
}
}
stpsep(buf, "\n");
if (stpsep(buf, "\n") == NULL)
goto cleanup_buf;

line = strdup (buf);
if (NULL == line) {
Expand Down Expand Up @@ -723,6 +703,7 @@ int commonio_open (struct commonio_db *db, int mode)
return 0;
}


/*
* Sort given db according to cmp function (usually compares uids)
*/
Expand Down Expand Up @@ -775,7 +756,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
entries[n] = ptr;
n++;
}
qsort (entries, n, sizeof (struct commonio_entry *), cmp);
qsort(entries, n, sizeof(struct commonio_entry *), cmp);

/* Take care of the head and tail separately */
db->head = entries[0];
Expand Down Expand Up @@ -877,9 +858,9 @@ static int write_all (const struct commonio_db *db)
return -1;
}
} else if (NULL != p->line) {
if (db->ops->fputs (p->line, db->fp) == EOF) {
if (fputs(p->line, db->fp) == EOF)
return -1;
}

if (putc ('\n', db->fp) == EOF) {
return -1;
}
Expand Down Expand Up @@ -913,7 +894,7 @@ int commonio_close (struct commonio_db *db)
goto fail;
}

memzero (&sb, sizeof sb);
memzero(&sb, sizeof(sb));
if (NULL != db->fp) {
if (fstat (fileno (db->fp), &sb) != 0) {
(void) fclose (db->fp);
Expand Down
9 changes: 0 additions & 9 deletions lib/commonio.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ struct commonio_ops {
*/
int (*put) (const void *, FILE *);

/*
* fgets and fputs (can be replaced by versions that
* understand line continuation conventions).
*/
ATTR_ACCESS(write_only, 1, 2)
/*@null@*/char *(*fgets)(/*@returned@*/char *restrict s, int n,
FILE *restrict stream);
int (*fputs) (const char *, FILE *);

/*
* open_hook and close_hook.
* If non NULL, these functions will be called after the database
Expand Down
7 changes: 3 additions & 4 deletions lib/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "getdef.h"
#include "prototypes.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"
#include "string/strcpy/strtcpy.h"
#include "string/strtok/stpsep.h"

Expand Down Expand Up @@ -75,7 +76,7 @@ is_listed(const char *cfgin, const char *tty, bool def)
* See if this tty is listed in the console file.
*/

while (fgets (buf, sizeof (buf), fp) != NULL) {
while (fgets(buf, sizeof(buf), fp) != NULL) {
stpsep(buf, "\n");
if (streq(buf, tty)) {
(void) fclose (fp);
Expand Down Expand Up @@ -103,9 +104,7 @@ is_listed(const char *cfgin, const char *tty, bool def)

bool console (const char *tty)
{
if (strncmp (tty, "/dev/", 5) == 0) {
tty += 5;
}
tty = strprefix(tty, "/dev/") ?: tty;

return is_listed ("CONSOLE", tty, true);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/copydir.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "shadowlog.h"
#include "string/sprintf/xasprintf.h"
#include "string/strcmp/streq.h"
#include "string/strcmp/strprefix.h"


static /*@null@*/const char *src_orig;
Expand Down Expand Up @@ -576,7 +577,7 @@ static int copy_symlink (const struct path_info *src, const struct path_info *ds
* create a link to the corresponding entry in the dst_orig
* directory.
*/
if (strncmp(oldlink, src_orig, strlen(src_orig)) == 0) {
if (strprefix(oldlink, src_orig)) {
char *dummy;

xasprintf(&dummy, "%s%s", dst_orig, oldlink + strlen(src_orig));
Expand Down Expand Up @@ -759,7 +760,7 @@ static int copy_file (const struct path_info *src, const struct path_info *dst,
char buf[8192];
ssize_t cnt;

cnt = read (ifd, buf, sizeof buf);
cnt = read(ifd, buf, sizeof(buf));
if (cnt < 0) {
if (errno == EINTR) {
continue;
Expand Down
4 changes: 3 additions & 1 deletion lib/encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "prototypes.h"
#include "defines.h"
#include "shadowlog_internal.h"
#include "string/strcmp/strprefix.h"


/*@exposed@*//*@null@*/char *pw_encrypt (const char *clear, const char *salt)
{
Expand All @@ -35,7 +37,7 @@

/* Some crypt() do not return NULL if the algorithm is not
* supported, and return a DES encrypted password. */
if ((NULL != salt) && (salt[0] == '$') && (strlen (cp) <= 13))
if ((NULL != salt) && strprefix(salt, "$") && (strlen (cp) <= 13))
{
/*@observer@*/const char *method;
switch (salt[1])
Expand Down
Loading
Loading