aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/update_passwd.c7
-rw-r--r--libbb/wfopen.c16
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,
137 fchown(new_fd, sb.st_uid, sb.st_gid); 137 fchown(new_fd, sb.st_uid, sb.st_gid);
138 } 138 }
139 errno = 0; 139 errno = 0;
140 new_fp = fdopen(new_fd, "w"); 140 new_fp = xfdopen_for_write(new_fd);
141 if (!new_fp) {
142 bb_perror_nomsg();
143 close(new_fd);
144 goto unlink_new;
145 }
146 141
147 /* Backup file is "/etc/passwd-" */ 142 /* Backup file is "/etc/passwd-" */
148 *sfx_char = '-'; 143 *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)
38{ 38{
39 return xfopen(path, "w"); 39 return xfopen(path, "w");
40} 40}
41
42static FILE* xfdopen_helper(unsigned fd_and_rw_bit)
43{
44 FILE* fp = fdopen(fd_and_rw_bit >> 1, fd_and_rw_bit & 1 ? "w" : "r");
45 if (!fp)
46 bb_error_msg_and_die(bb_msg_memory_exhausted);
47 return fp;
48}
49FILE* FAST_FUNC xfdopen_for_read(int fd)
50{
51 return xfdopen_helper(fd << 1);
52}
53FILE* FAST_FUNC xfdopen_for_write(int fd)
54{
55 return xfdopen_helper((fd << 1) + 1);
56}