* libmisc/list.c: Avoid assignments in comparisons, assignments

with post increments (x++), use of integers as booleans, and
	explicitly mark blocks with brackets.
This commit is contained in:
nekral-guest
2008-05-24 14:09:35 +00:00
parent d99423405c
commit 0219d72f48
2 changed files with 42 additions and 20 deletions

View File

@ -7,6 +7,9 @@
* src/passwd.c: Declare the check_selinux_access prototype and * src/passwd.c: Declare the check_selinux_access prototype and
avoid name clashes (change_user -> changed_user; change_uid -> avoid name clashes (change_user -> changed_user; change_uid ->
changed_uid; access -> requested_access) changed_uid; access -> requested_access)
* libmisc/list.c: Avoid assignments in comparisons, assignments
with post increments (x++), use of integers as booleans, and
explicitly mark blocks with brackets.
2008-05-23 Nicolas François <nicolas.francois@centraliens.net> 2008-05-23 Nicolas François <nicolas.francois@centraliens.net>

View File

@ -55,9 +55,11 @@ char **add_list (char **list, const char *member)
* pointer if it is present. * pointer if it is present.
*/ */
for (i = 0; list[i] != (char *) 0; i++) for (i = 0; list[i] != (char *) 0; i++) {
if (strcmp (list[i], member) == 0) if (strcmp (list[i], member) == 0) {
return list; return list;
}
}
/* /*
* Allocate a new list pointer large enough to hold all the * Allocate a new list pointer large enough to hold all the
@ -72,11 +74,12 @@ char **add_list (char **list, const char *member)
* is returned to the invoker. * is returned to the invoker.
*/ */
for (i = 0; list[i] != (char *) 0; i++) for (i = 0; list[i] != (char *) 0; i++) {
tmp[i] = list[i]; tmp[i] = list[i];
}
tmp[i++] = xstrdup (member); tmp[i] = xstrdup (member);
tmp[i] = (char *) 0; tmp[i+1] = (char *) 0;
return tmp; return tmp;
} }
@ -99,12 +102,15 @@ char **del_list (char **list, const char *member)
* pointer if it is not present. * pointer if it is not present.
*/ */
for (i = j = 0; list[i] != (char *) 0; i++) for (i = j = 0; list[i] != (char *) 0; i++) {
if (strcmp (list[i], member)) if (strcmp (list[i], member) != 0) {
j++; j++;
}
}
if (j == i) if (j == i) {
return list; return list;
}
/* /*
* Allocate a new list pointer large enough to hold all the * Allocate a new list pointer large enough to hold all the
@ -119,9 +125,12 @@ char **del_list (char **list, const char *member)
* is returned to the invoker. * is returned to the invoker.
*/ */
for (i = j = 0; list[i] != (char *) 0; i++) for (i = j = 0; list[i] != (char *) 0; i++) {
if (strcmp (list[i], member)) if (strcmp (list[i], member) != 0) {
tmp[j++] = list[i]; tmp[j] = list[i];
j++;
}
}
tmp[j] = (char *) 0; tmp[j] = (char *) 0;
@ -138,8 +147,11 @@ char **dup_list (char *const *list)
tmp = (char **) xmalloc ((i + 1) * sizeof (char *)); tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
i = 0; i = 0;
while (*list) while (*list) {
tmp[i++] = xstrdup (*list++); tmp[i] = xstrdup (*list);
i++;
list++;
}
tmp[i] = (char *) 0; tmp[i] = (char *) 0;
return tmp; return tmp;
@ -148,8 +160,9 @@ char **dup_list (char *const *list)
int is_on_list (char *const *list, const char *member) int is_on_list (char *const *list, const char *member)
{ {
while (*list) { while (*list) {
if (strcmp (*list, member) == 0) if (strcmp (*list, member) == 0) {
return 1; return 1;
}
list++; list++;
} }
return 0; return 0;
@ -176,11 +189,14 @@ char **comma_to_list (const char *comma)
* Count the number of commas in the list * Count the number of commas in the list
*/ */
for (cp = members, i = 0;; i++) for (cp = members, i = 0;; i++) {
if ((cp2 = strchr (cp, ','))) cp2 = strchr (cp, ',');
if (NULL != cp2) {
cp = cp2 + 1; cp = cp2 + 1;
else } else {
break; break;
}
}
/* /*
* Add 2 - one for the ending NULL, the other for the last item * Add 2 - one for the ending NULL, the other for the last item
@ -198,7 +214,7 @@ char **comma_to_list (const char *comma)
* Empty list is special - 0 members, not 1 empty member. --marekm * Empty list is special - 0 members, not 1 empty member. --marekm
*/ */
if (!*members) { if ('\0' == *members) {
*array = (char *) 0; *array = (char *) 0;
return array; return array;
} }
@ -210,8 +226,10 @@ char **comma_to_list (const char *comma)
for (cp = members, i = 0;; i++) { for (cp = members, i = 0;; i++) {
array[i] = cp; array[i] = cp;
if ((cp2 = strchr (cp, ','))) { cp2 = strchr (cp, ',');
*cp2++ = '\0'; if (NULL != cp2) {
*cp2 = '\0';
cp2++;
cp = cp2; cp = cp2;
} else { } else {
array[i + 1] = (char *) 0; array[i + 1] = (char *) 0;
@ -225,3 +243,4 @@ char **comma_to_list (const char *comma)
return array; return array;
} }