Prefer other types to int in names.c
* src/names.c (uname_to_uid, gname_to_gid, handle_option) (make_file_name): Prefer bool for boolean. (struct name_elt, read_name_from_file): Prefer char for char. (handle_option): Invert sense of return value, for clarity. All uses changed. (merge_sort_sll, merge_sort, collect_and_sort_names): Don’t assume list length fits in int. Use intptr_t not idx_t, since the bound is the size of all memory rather than one array.
This commit is contained in:
110
src/names.c
110
src/names.c
@@ -300,9 +300,7 @@ static char const *const backup_file_table[] = {
|
|||||||
static void
|
static void
|
||||||
add_exclude_array (char const *const *fv, int opts)
|
add_exclude_array (char const *const *fv, int opts)
|
||||||
{
|
{
|
||||||
int i;
|
for (int i = 0; fv[i]; i++)
|
||||||
|
|
||||||
for (i = 0; fv[i]; i++)
|
|
||||||
add_exclude (excluded, fv[i], opts);
|
add_exclude (excluded, fv[i], opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -534,15 +532,16 @@ gid_to_gname (gid_t gid, char **gname)
|
|||||||
*gname = xstrdup (cached_gname);
|
*gname = xstrdup (cached_gname);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Given UNAME, set the corresponding UID and return 1, or else, return 0. */
|
/* Given UNAME, set the corresponding UID and return true,
|
||||||
int
|
or else, return false. */
|
||||||
|
bool
|
||||||
uname_to_uid (char const *uname, uid_t *uidp)
|
uname_to_uid (char const *uname, uid_t *uidp)
|
||||||
{
|
{
|
||||||
struct passwd *passwd;
|
struct passwd *passwd;
|
||||||
|
|
||||||
if (cached_no_such_uname
|
if (cached_no_such_uname
|
||||||
&& strcmp (uname, cached_no_such_uname) == 0)
|
&& strcmp (uname, cached_no_such_uname) == 0)
|
||||||
return 0;
|
return false;
|
||||||
|
|
||||||
if (!cached_uname
|
if (!cached_uname
|
||||||
|| uname[0] != cached_uname[0]
|
|| uname[0] != cached_uname[0]
|
||||||
@@ -557,22 +556,23 @@ uname_to_uid (char const *uname, uid_t *uidp)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
assign_string (&cached_no_such_uname, uname);
|
assign_string (&cached_no_such_uname, uname);
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*uidp = cached_uid;
|
*uidp = cached_uid;
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Given GNAME, set the corresponding GID and return 1, or else, return 0. */
|
/* Given GNAME, set the corresponding GID and return true,
|
||||||
int
|
or else, return false. */
|
||||||
|
bool
|
||||||
gname_to_gid (char const *gname, gid_t *gidp)
|
gname_to_gid (char const *gname, gid_t *gidp)
|
||||||
{
|
{
|
||||||
struct group *group;
|
struct group *group;
|
||||||
|
|
||||||
if (cached_no_such_gname
|
if (cached_no_such_gname
|
||||||
&& strcmp (gname, cached_no_such_gname) == 0)
|
&& strcmp (gname, cached_no_such_gname) == 0)
|
||||||
return 0;
|
return false;
|
||||||
|
|
||||||
if (!cached_gname
|
if (!cached_gname
|
||||||
|| gname[0] != cached_gname[0]
|
|| gname[0] != cached_gname[0]
|
||||||
@@ -587,11 +587,11 @@ gname_to_gid (char const *gname, gid_t *gidp)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
assign_string (&cached_no_such_gname, gname);
|
assign_string (&cached_no_such_gname, gname);
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*gidp = cached_gid;
|
*gidp = cached_gid;
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -656,7 +656,7 @@ struct name_elt /* A name_array element. */
|
|||||||
{
|
{
|
||||||
const char *name;/* File name */
|
const char *name;/* File name */
|
||||||
intmax_t line; /* Input line number */
|
intmax_t line; /* Input line number */
|
||||||
int term; /* File name terminator in the list */
|
char term; /* File name terminator in the list */
|
||||||
bool verbatim; /* Verbatim handling of file names: no white-space
|
bool verbatim; /* Verbatim handling of file names: no white-space
|
||||||
trimming, no option processing */
|
trimming, no option processing */
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
@@ -957,10 +957,10 @@ read_name_from_file (struct name_elt *ent)
|
|||||||
int c;
|
int c;
|
||||||
idx_t counter = 0;
|
idx_t counter = 0;
|
||||||
FILE *fp = ent->v.file.fp;
|
FILE *fp = ent->v.file.fp;
|
||||||
int term = ent->v.file.term;
|
char term = ent->v.file.term;
|
||||||
|
|
||||||
++ent->v.file.line;
|
++ent->v.file.line;
|
||||||
for (c = getc (fp); c != EOF && c != term; c = getc (fp))
|
while (! ((c = getc (fp)) < 0 || c == term))
|
||||||
{
|
{
|
||||||
if (counter == name_buffer_length)
|
if (counter == name_buffer_length)
|
||||||
name_buffer = xpalloc (name_buffer, &name_buffer_length, 1, -1, 1);
|
name_buffer = xpalloc (name_buffer, &name_buffer_length, 1, -1, 1);
|
||||||
@@ -982,7 +982,7 @@ read_name_from_file (struct name_elt *ent)
|
|||||||
return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
|
return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static bool
|
||||||
handle_option (const char *str, struct name_elt const *ent)
|
handle_option (const char *str, struct name_elt const *ent)
|
||||||
{
|
{
|
||||||
struct wordsplit ws;
|
struct wordsplit ws;
|
||||||
@@ -991,7 +991,7 @@ handle_option (const char *str, struct name_elt const *ent)
|
|||||||
while (*str && c_isspace (*str))
|
while (*str && c_isspace (*str))
|
||||||
++str;
|
++str;
|
||||||
if (*str != '-')
|
if (*str != '-')
|
||||||
return 1;
|
return false;
|
||||||
|
|
||||||
ws.ws_offs = 1;
|
ws.ws_offs = 1;
|
||||||
if (wordsplit (str, &ws, WRDSF_DEFFLAGS | WRDSF_DOOFFS) != WRDSE_OK)
|
if (wordsplit (str, &ws, WRDSF_DEFFLAGS | WRDSF_DOOFFS) != WRDSE_OK)
|
||||||
@@ -1007,7 +1007,7 @@ handle_option (const char *str, struct name_elt const *ent)
|
|||||||
more_options (argc, ws.ws_wordv, &loc);
|
more_options (argc, ws.ws_wordv, &loc);
|
||||||
memset (ws.ws_wordv, 0, argc * sizeof *ws.ws_wordv);
|
memset (ws.ws_wordv, 0, argc * sizeof *ws.ws_wordv);
|
||||||
wordsplit_free (&ws);
|
wordsplit_free (&ws);
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
@@ -1054,7 +1054,7 @@ read_next_name (struct name_elt *ent, struct name_elt *ret)
|
|||||||
{
|
{
|
||||||
if (unquote_option)
|
if (unquote_option)
|
||||||
unquote_string (name_buffer);
|
unquote_string (name_buffer);
|
||||||
if (handle_option (name_buffer, ent) == 0)
|
if (handle_option (name_buffer, ent))
|
||||||
{
|
{
|
||||||
name_list_adjust ();
|
name_list_adjust ();
|
||||||
return false;
|
return false;
|
||||||
@@ -1483,51 +1483,36 @@ label_notfound (void)
|
|||||||
|
|
||||||
Apart from the type 'struct name' and its 'next' member,
|
Apart from the type 'struct name' and its 'next' member,
|
||||||
this is a generic list-sorting function, but it's too painful to
|
this is a generic list-sorting function, but it's too painful to
|
||||||
make it both generic and portable
|
make it both generic and portable in C. */
|
||||||
in C. */
|
|
||||||
|
|
||||||
static struct name *
|
static struct name *
|
||||||
merge_sort_sll (struct name *list, int length,
|
merge_sort_sll (struct name *list, intptr_t length,
|
||||||
int (*compare) (struct name const*, struct name const*))
|
int (*compare) (struct name const *, struct name const *))
|
||||||
{
|
{
|
||||||
struct name *first_list;
|
|
||||||
struct name *second_list;
|
|
||||||
int first_length;
|
|
||||||
int second_length;
|
|
||||||
struct name *result;
|
|
||||||
struct name **merge_point;
|
|
||||||
struct name *cursor;
|
|
||||||
int counter;
|
|
||||||
|
|
||||||
if (length == 1)
|
if (length == 1)
|
||||||
return list;
|
return list;
|
||||||
|
|
||||||
if (length == 2)
|
if (length == 2)
|
||||||
{
|
{
|
||||||
if (compare (list, list->next) > 0)
|
if (compare (list, list->next) <= 0)
|
||||||
{
|
return list;
|
||||||
result = list->next;
|
struct name *r = list->next;
|
||||||
result->next = list;
|
r->next = list;
|
||||||
list->next = 0;
|
list->next = NULL;
|
||||||
return result;
|
return r;
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
first_list = list;
|
struct name *first_list = list, *cursor = list;
|
||||||
first_length = (length + 1) / 2;
|
intptr_t second_length = length >> 1, first_length = length - second_length;
|
||||||
second_length = length / 2;
|
for (intptr_t counter = first_length - 1; counter; counter--)
|
||||||
for (cursor = list, counter = first_length - 1;
|
cursor = cursor->next;
|
||||||
counter;
|
struct name *second_list = cursor->next;
|
||||||
cursor = cursor->next, counter--)
|
cursor->next = NULL;
|
||||||
continue;
|
|
||||||
second_list = cursor->next;
|
|
||||||
cursor->next = 0;
|
|
||||||
|
|
||||||
first_list = merge_sort_sll (first_list, first_length, compare);
|
first_list = merge_sort_sll (first_list, first_length, compare);
|
||||||
second_list = merge_sort_sll (second_list, second_length, compare);
|
second_list = merge_sort_sll (second_list, second_length, compare);
|
||||||
|
|
||||||
merge_point = &result;
|
struct name *result, **merge_point = &result;
|
||||||
while (first_list && second_list)
|
while (first_list && second_list)
|
||||||
if (compare (first_list, second_list) < 0)
|
if (compare (first_list, second_list) < 0)
|
||||||
{
|
{
|
||||||
@@ -1543,10 +1528,7 @@ merge_sort_sll (struct name *list, int length,
|
|||||||
merge_point = &second_list->next;
|
merge_point = &second_list->next;
|
||||||
second_list = cursor;
|
second_list = cursor;
|
||||||
}
|
}
|
||||||
if (first_list)
|
*merge_point = first_list ? first_list : second_list;
|
||||||
*merge_point = first_list;
|
|
||||||
else
|
|
||||||
*merge_point = second_list;
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -1554,14 +1536,16 @@ merge_sort_sll (struct name *list, int length,
|
|||||||
/* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
|
/* Sort doubly linked LIST of names, of given LENGTH, using COMPARE
|
||||||
to order names. Return the sorted list. */
|
to order names. Return the sorted list. */
|
||||||
static struct name *
|
static struct name *
|
||||||
merge_sort (struct name *list, int length,
|
merge_sort (struct name *list, intptr_t length,
|
||||||
int (*compare) (struct name const*, struct name const*))
|
int (*compare) (struct name const *, struct name const *))
|
||||||
{
|
{
|
||||||
struct name *head, *p, *prev;
|
struct name *head = merge_sort_sll (list, length, compare);
|
||||||
head = merge_sort_sll (list, length, compare);
|
|
||||||
/* Fixup prev pointers */
|
/* Fixup prev pointers. */
|
||||||
for (prev = NULL, p = head; p; prev = p, p = p->next)
|
struct name *prev = NULL;
|
||||||
|
for (struct name *p = head; p; prev = p, p = p->next)
|
||||||
p->prev = prev;
|
p->prev = prev;
|
||||||
|
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1724,7 +1708,7 @@ collect_and_sort_names (void)
|
|||||||
{
|
{
|
||||||
struct name *name;
|
struct name *name;
|
||||||
struct name *next_name, *prev_name = NULL;
|
struct name *next_name, *prev_name = NULL;
|
||||||
int num_names;
|
intptr_t num_names;
|
||||||
Hash_table *nametab;
|
Hash_table *nametab;
|
||||||
|
|
||||||
name_gather ();
|
name_gather ();
|
||||||
@@ -1932,7 +1916,7 @@ make_file_name (const char *directory_name, const char *name)
|
|||||||
{
|
{
|
||||||
idx_t dirlen = strlen (directory_name);
|
idx_t dirlen = strlen (directory_name);
|
||||||
idx_t namelen = strlen (name) + 1;
|
idx_t namelen = strlen (name) + 1;
|
||||||
int slash = dirlen && ! ISSLASH (directory_name[dirlen - 1]);
|
bool slash = dirlen && ! ISSLASH (directory_name[dirlen - 1]);
|
||||||
char *buffer = xmalloc (dirlen + slash + namelen);
|
char *buffer = xmalloc (dirlen + slash + namelen);
|
||||||
memcpy (buffer, directory_name, dirlen);
|
memcpy (buffer, directory_name, dirlen);
|
||||||
buffer[dirlen] = '/';
|
buffer[dirlen] = '/';
|
||||||
|
|||||||
Reference in New Issue
Block a user