diff --git a/archival/bbunzip.c b/archival/bbunzip.c index e16e6b083..3c3cc2886 100644 --- a/archival/bbunzip.c +++ b/archival/bbunzip.c @@ -105,8 +105,7 @@ int bbunpack(char **argv, if (new_name == filename) filename[strlen(filename)] = '.'; } - if (unlink(del) < 0) - bb_perror_msg_and_die("cannot remove %s", del); + xunlink(del); #if 0 /* Currently buggy - wrong name: "a.gz: 261% - replaced with a.gz" */ /* Extreme bloat for gunzip compat */ diff --git a/editors/patch.c b/editors/patch.c index 2c908040f..4d1425edc 100644 --- a/editors/patch.c +++ b/editors/patch.c @@ -260,12 +260,9 @@ int patch_main(int argc, char **argv) } if ((dest_cur_line == 0) || (dest_beg_line == 0)) { /* The new patched file is empty, remove it */ - if (unlink(new_filename) == -1) { - bb_perror_msg_and_die("cannot remove file %s", new_filename); - } - if (unlink(original_filename) == -1) { - bb_perror_msg_and_die("cannot remove original file %s", new_filename); - } + xunlink(new_filename); + if (strcmp(new_filename, original_filename) != 0) + xunlink(original_filename); } } } diff --git a/include/libbb.h b/include/libbb.h index 91715c16a..b56352626 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -293,14 +293,15 @@ extern void sig_pause(void); -extern void xsetgid(gid_t gid); -extern void xsetuid(uid_t uid); -extern void xchdir(const char *path); -extern void xsetenv(const char *key, const char *value); -extern int xopen(const char *pathname, int flags); -extern int xopen3(const char *pathname, int flags, int mode); -extern off_t xlseek(int fd, off_t offset, int whence); -extern off_t fdlength(int fd); +void xsetgid(gid_t gid); +void xsetuid(uid_t uid); +void xchdir(const char *path); +void xsetenv(const char *key, const char *value); +void xunlink(const char *pathname); +int xopen(const char *pathname, int flags); +int xopen3(const char *pathname, int flags, int mode); +off_t xlseek(int fd, off_t offset, int whence); +off_t fdlength(int fd); int xsocket(int domain, int type, int protocol); diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index b08f92d81..c18a1d998 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -122,6 +122,12 @@ int xopen3(const char *pathname, int flags, int mode) return ret; } +void xunlink(const char *pathname) +{ + if (unlink(pathname)) + bb_perror_msg_and_die("cannot remove file '%s'", pathname); +} + // Turn on nonblocking I/O on a fd int ndelay_on(int fd) {