[pacman-dev] [PATCH 2/2] Fix some errors that cppcheck gave back

Laszlo Papp djszapi2 at gmail.com
Fri Oct 23 16:54:18 EDT 2009


	Fix some warning that cppcheck gave back

	* opendir(path)) == (DIR *)-1 is maybe the result of miss
	understanding of the man page, if the opendir wasn't successful
	it gives back NULL instead of '(DIR *)-1'.

	* return(PM_ERR_NOT_A_DIR); was established instead of hard coding
	numbers for return statement.

	* The ambiguity while cycle with EINTR condition was refactored
	for a do {} while () cycle to be easier to read/understand

Signed-off-by: Laszlo Papp <djszapi at archlinux.us>
---
 lib/libalpm/util.c |   11 ++++++-----
 src/pacman/util.c  |    4 ++--
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index d910809..ad3dc2c 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -212,8 +212,9 @@ int _alpm_lckmk()
 	_alpm_makepath(dir);
 	FREE(dir);
 
-	while((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000)) == -1
-			&& errno == EINTR);
+	do {
+		fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000);
+	} while(fd == -1 && errno == EINTR);
 	if(fd > 0) {
 		FILE *f = fdopen(fd, "w");
 		fprintf(f, "%ld\n", (long)getpid());
@@ -315,7 +316,7 @@ int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int
 
 		st = archive_entry_stat(entry);
 		entryname = archive_entry_pathname(entry);
-		
+
 		if(S_ISREG(st->st_mode)) {
 			archive_entry_set_perm(entry, 0644);
 		} else if(S_ISDIR(st->st_mode)) {
@@ -389,8 +390,8 @@ int _alpm_rmrf(const char *path)
 				}
 			}
 		} else {
-			if((dirp = opendir(path)) == (DIR *)-1) {
-				return(1);
+			if((dirp = opendir(path)) == NULL) {
+				return(PM_ERR_NOT_A_DIR);
 			}
 			for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
 				if(dp->d_ino) {
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 1143bef..e3dbfbb 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -122,8 +122,8 @@ int rmrf(const char *path)
 			return(1);
 		}
 
-		if((dirp = opendir(path)) == (DIR *)-1) {
-			return(1);
+		if((dirp = opendir(path)) == NULL) {
+			return(PM_ERR_NOT_A_DIR);
 		}
 		for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
 			if(dp->d_ino) {
-- 
1.6.5



More information about the pacman-dev mailing list