diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/update_passwd.c | 7 | ||||
-rw-r--r-- | libbb/wfopen.c | 16 |
2 files changed, 17 insertions, 6 deletions
diff --git a/libbb/update_passwd.c b/libbb/update_passwd.c index ba773fcb2..301893be1 100644 --- a/libbb/update_passwd.c +++ b/libbb/update_passwd.c @@ -137,12 +137,7 @@ int FAST_FUNC update_passwd(const char *filename, fchown(new_fd, sb.st_uid, sb.st_gid); } errno = 0; - new_fp = fdopen(new_fd, "w"); - if (!new_fp) { - bb_perror_nomsg(); - close(new_fd); - goto unlink_new; - } + new_fp = xfdopen_for_write(new_fd); /* Backup file is "/etc/passwd-" */ *sfx_char = '-'; diff --git a/libbb/wfopen.c b/libbb/wfopen.c index 1cb871ef5..deec79a28 100644 --- a/libbb/wfopen.c +++ b/libbb/wfopen.c @@ -38,3 +38,19 @@ FILE* FAST_FUNC xfopen_for_write(const char *path) { return xfopen(path, "w"); } + +static FILE* xfdopen_helper(unsigned fd_and_rw_bit) +{ + FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r"); + if (!fp) + bb_error_msg_and_die(bb_msg_memory_exhausted); + return fp; +} +FILE* FAST_FUNC xfdopen_for_read(int fd) +{ + return xfdopen_helper(fd << 1); +} +FILE* FAST_FUNC xfdopen_for_write(int fd) +{ + return xfdopen_helper((fd << 1) + 1); +} |