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

with post increments (x++), use of integers as booleans, and
	explicitly mark blocks with brackets.
	* libmisc/copydir.c: Likewise.
	* libmisc/fields.c: Add comments.
	* libmisc/copydir.c: Mark function whose return value is not
	checked as such.

	* libmisc/copydir.c (remove_tree): Make sure unlink is successful
	when removing files.
This commit is contained in:
nekral-guest 2008-05-24 15:19:02 +00:00
parent cda1f9a23d
commit 9c41a8ad38
3 changed files with 51 additions and 25 deletions

View File

@ -1,3 +1,8 @@
2008-05-24 Nicolas François <nicolas.francois@centraliens.net>
* libmisc/copydir.c (remove_tree): Make sure unlink is successful
when removing files.
2008-05-24 Nicolas François <nicolas.francois@centraliens.net> 2008-05-24 Nicolas François <nicolas.francois@centraliens.net>
* libmisc/pwdcheck.c: Simply passwd_check since it's never used * libmisc/pwdcheck.c: Simply passwd_check since it's never used
@ -15,6 +20,11 @@
* libmisc/list.c: Avoid assignments in comparisons, assignments * libmisc/list.c: Avoid assignments in comparisons, assignments
with post increments (x++), use of integers as booleans, and with post increments (x++), use of integers as booleans, and
explicitly mark blocks with brackets. explicitly mark blocks with brackets.
* libmisc/fields.c: Likewise.
* libmisc/copydir.c: Likewise.
* libmisc/fields.c: Add comments.
* libmisc/copydir.c: Mark function whose return value is not
checked as such.
2008-05-23 Nicolas François <nicolas.francois@centraliens.net> 2008-05-23 Nicolas François <nicolas.francois@centraliens.net>

View File

@ -250,7 +250,7 @@ int copy_tree (const char *src_root, const char *dst_root,
} }
} }
} }
closedir (dir); (void) closedir (dir);
if (set_orig) { if (set_orig) {
src_orig = 0; src_orig = 0;
@ -412,7 +412,7 @@ static int copy_symlink (const char *src, const char *dst,
return -1; return -1;
} }
oldlink[len] = '\0'; /* readlink() does not NUL-terminate */ oldlink[len] = '\0'; /* readlink() does not NUL-terminate */
if (!strncmp (oldlink, src_orig, strlen (src_orig))) { if (strncmp (oldlink, src_orig, strlen (src_orig)) == 0) {
snprintf (dummy, sizeof dummy, "%s%s", snprintf (dummy, sizeof dummy, "%s%s",
dst_orig, dst_orig,
oldlink + strlen (src_orig)); oldlink + strlen (src_orig));
@ -533,7 +533,7 @@ static int copy_file (const char *src, const char *dst,
(uid == -1) ? statp->st_uid : (uid_t) uid, (uid == -1) ? statp->st_uid : (uid_t) uid,
(gid == -1) ? statp->st_gid : (gid_t) gid) != 0) (gid == -1) ? statp->st_gid : (gid_t) gid) != 0)
|| (chmod (dst, statp->st_mode & 07777) != 0)) { || (chmod (dst, statp->st_mode & 07777) != 0)) {
close (ifd); (void) close (ifd);
return -1; return -1;
} }
@ -543,7 +543,7 @@ static int copy_file (const char *src, const char *dst,
} }
} }
close (ifd); (void) close (ifd);
if (futimes (ofd, mt) != 0) { if (futimes (ofd, mt) != 0) {
return -1; return -1;
@ -622,19 +622,22 @@ int remove_tree (const char *root)
* Recursively delete this directory. * Recursively delete this directory.
*/ */
if (remove_tree (new_name)) { if (remove_tree (new_name) != 0) {
err = -1; err = -1;
break; break;
} }
if (rmdir (new_name)) { if (rmdir (new_name) != 0) {
err = -1;
break;
}
} else {
if (unlink (new_name) != 0) {
err = -1; err = -1;
break; break;
} }
continue;
} }
unlink (new_name);
} }
closedir (dir); (void) closedir (dir);
return err; return err;
} }

View File

@ -38,6 +38,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "prototypes.h" #include "prototypes.h"
/* /*
* valid_field - insure that a field contains all legal characters * valid_field - insure that a field contains all legal characters
* *
@ -53,15 +54,22 @@ int valid_field (const char *field, const char *illegal)
const char *cp; const char *cp;
int err = 0; int err = 0;
for (cp = field; *cp && !strchr (illegal, *cp); cp++); /* For each character of field, search if it appears in the list
* of illegal characters. */
for (cp = field; '\0' != *cp; cp++) {
if (strchr (illegal, *cp) != NULL) {
err = -1;
break;
}
}
if (*cp) { if (0 == err) {
err = -1; /* Search if there are some non-printable characters */
} else { for (cp = field; '\0' != *cp; cp++) {
for (cp = field; *cp && isprint (*cp); cp++); if (!isprint (*cp)) {
err = 1;
if (*cp) { break;
err = 1; }
} }
} }
@ -74,25 +82,28 @@ int valid_field (const char *field, const char *illegal)
* prompt the user with the name of the field being changed and the * prompt the user with the name of the field being changed and the
* current value. * 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 newf[200];
char *cp; char *cp;
if (maxsize > sizeof (newf)) if (maxsize > sizeof (newf)) {
maxsize = sizeof (newf); maxsize = sizeof (newf);
}
printf ("\t%s [%s]: ", prompt, buf); printf ("\t%s [%s]: ", prompt, buf);
fflush (stdout); (void) fflush (stdout);
if (fgets (newf, maxsize, stdin) != newf) if (fgets (newf, (int) maxsize, stdin) != newf) {
return; return;
}
if (!(cp = strchr (newf, '\n'))) cp = strchr (newf, '\n');
if (NULL == cp) {
return; return;
}
*cp = '\0'; *cp = '\0';
if (newf[0]) { if ('\0' != newf[0]) {
/* /*
* Remove leading and trailing whitespace. This also * Remove leading and trailing whitespace. This also
* makes it possible to change the field to empty, by * makes it possible to change the field to empty, by
@ -100,11 +111,13 @@ void change_field (char *buf, size_t maxsize, const char *prompt)
*/ */
while (--cp >= newf && isspace (*cp)); while (--cp >= newf && isspace (*cp));
*++cp = '\0'; cp++;
*cp = '\0';
cp = newf; cp = newf;
while (*cp && isspace (*cp)) while (('\0' != *cp) && isspace (*cp)) {
cp++; cp++;
}
strncpy (buf, cp, maxsize - 1); strncpy (buf, cp, maxsize - 1);
buf[maxsize - 1] = '\0'; buf[maxsize - 1] = '\0';