[PATCH v2 2/2] tree-wide: use xopen_as

Rasmus Villemoes rasmus.villemoes at prevas.dk
Thu Mar 4 21:02:49 UTC 2021


Typical saving per callsite (on x86-64) is 7 bytes, so net win at 3
conversions.

function                                             old     new   delta
xopen_as                                               -      18     +18
xargs_main                                           871     864      -7
vlock_main                                           409     402      -7
uniq_main                                            463     456      -7
ubi_tools_main                                      1298    1291      -7
split_main                                           581     574      -7
sort_main                                           1065    1058      -7
shuf_main                                            550     543      -7
reinitialize                                         206     199      -7
nc_main                                             1225    1218      -7
mkfs_minix_main                                     2865    2858      -7
mkfs_ext2_main                                      2405    2398      -7
fsck_minix_main                                     3145    3138      -7
acpid_main                                          1326    1319      -7
cpio_main                                            654     645      -9
close_dev_fd                                          28      19      -9
watchdog_main                                        335     321     -14
dd_main                                             1642    1628     -14
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/17 up/down: 18/-137)         Total: -119 bytes

Signed-off-by: Rasmus Villemoes <rasmus.villemoes at prevas.dk>
---
 archival/cpio.c         | 6 +++---
 coreutils/dd.c          | 4 ++--
 coreutils/shuf.c        | 2 +-
 coreutils/sort.c        | 2 +-
 coreutils/split.c       | 2 +-
 coreutils/uniq.c        | 2 +-
 findutils/xargs.c       | 2 +-
 loginutils/vlock.c      | 2 +-
 miscutils/less.c        | 2 +-
 miscutils/ubi_tools.c   | 2 +-
 miscutils/watchdog.c    | 4 ++--
 networking/nc_bloaty.c  | 2 +-
 util-linux/acpid.c      | 2 +-
 util-linux/fdisk.c      | 2 +-
 util-linux/fsck_minix.c | 2 +-
 util-linux/mkfs_ext2.c  | 2 +-
 util-linux/mkfs_minix.c | 2 +-
 17 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/archival/cpio.c b/archival/cpio.c
index 94303389e..0d25ead91 100644
--- a/archival/cpio.c
+++ b/archival/cpio.c
@@ -405,11 +405,11 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
 	}
 #if !ENABLE_FEATURE_CPIO_O
 	if (opt & OPT_FILE) { /* -F */
-		xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
+		xopen_as(cpio_filename, O_RDONLY, STDIN_FILENO);
 	}
 #else
 	if ((opt & (OPT_FILE|OPT_CREATE)) == OPT_FILE) { /* -F without -o */
-		xmove_fd(xopen(cpio_filename, O_RDONLY), STDIN_FILENO);
+		xopen_as(cpio_filename, O_RDONLY, STDIN_FILENO);
 	}
 	if (opt & OPT_PASSTHROUGH) {
 		pid_t pid;
@@ -459,7 +459,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
 		if (cpio_fmt[0] != 'n') /* we _require_ "-H newc" */
 			bb_show_usage();
 		if (opt & OPT_FILE) {
-			xmove_fd(xopen(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
+			xopen_as(cpio_filename, O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
 		}
  dump:
 		return cpio_o();
diff --git a/coreutils/dd.c b/coreutils/dd.c
index 24d7f0b84..6fd8b1daf 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -500,7 +500,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 		if (G.flags & FLAG_IDIRECT)
 			iflag |= O_DIRECT;
 #endif
-		xmove_fd(xopen(infile, iflag), ifd);
+		xopen_as(infile, iflag, ifd);
 	} else {
 		infile = bb_msg_standard_input;
 	}
@@ -515,7 +515,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 		if (G.flags & FLAG_ODIRECT)
 			oflag |= O_DIRECT;
 #endif
-		xmove_fd(xopen(outfile, oflag), ofd);
+		xopen_as(outfile, oflag, ofd);
 
 		if (seek && !(G.flags & FLAG_NOTRUNC)) {
 			size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
diff --git a/coreutils/shuf.c b/coreutils/shuf.c
index fdbd3e9b2..b9daaaf38 100644
--- a/coreutils/shuf.c
+++ b/coreutils/shuf.c
@@ -132,7 +132,7 @@ int shuf_main(int argc, char **argv)
 		shuffle_lines(lines, numlines);
 
 	if (opts & OPT_o)
-		xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
+		xopen_as(opt_o_str, O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
 
 	if (opts & OPT_n) {
 		unsigned maxlines;
diff --git a/coreutils/sort.c b/coreutils/sort.c
index b194847d1..899f2cc51 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -635,7 +635,7 @@ int sort_main(int argc UNUSED_PARAM, char **argv)
 #if ENABLE_FEATURE_SORT_BIG
 	/* Open output file _after_ we read all input ones */
 	if (option_mask32 & FLAG_o)
-		xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
+		xopen_as(str_o, O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
 #endif
 	{
 		int ch = (option_mask32 & FLAG_z) ? '\0' : '\n';
diff --git a/coreutils/split.c b/coreutils/split.c
index 3fcfd95f2..a8f60222a 100644
--- a/coreutils/split.c
+++ b/coreutils/split.c
@@ -148,7 +148,7 @@ int split_main(int argc UNUSED_PARAM, char **argv)
 			if (!remaining) {
 				if (!pfx)
 					bb_simple_error_msg_and_die("suffixes exhausted");
-				xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1);
+				xopen_as(pfx, O_WRONLY | O_CREAT | O_TRUNC, 1);
 				pfx = next_file(pfx, suffix_len);
 				remaining = cnt;
 			}
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index e1594286f..b26b8653f 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -80,7 +80,7 @@ int uniq_main(int argc UNUSED_PARAM, char **argv)
 				// Won't work with "uniq - FILE" and closed stdin:
 				//close(STDOUT_FILENO);
 				//xopen(output, O_WRONLY | O_CREAT | O_TRUNC);
-				xmove_fd(xopen(output, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
+				xopen_as(output, O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
 			}
 		}
 	}
diff --git a/findutils/xargs.c b/findutils/xargs.c
index e2b3527f3..ceeb4bb67 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -633,7 +633,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
 
 #if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
 	if (opt_a)
-		xmove_fd(xopen(opt_a, O_RDONLY), 0);
+		xopen_as(opt_a, O_RDONLY, 0);
 #endif
 
 	/* -E ""? You may wonder why not just omit -E?
diff --git a/loginutils/vlock.c b/loginutils/vlock.c
index 334b7d2ad..b4f3e012d 100644
--- a/loginutils/vlock.c
+++ b/loginutils/vlock.c
@@ -91,7 +91,7 @@ int vlock_main(int argc UNUSED_PARAM, char **argv)
 
 	/* Revert stdin/out to our controlling tty
 	 * (or die if we have none) */
-	xmove_fd(xopen(CURRENT_TTY, O_RDWR), STDIN_FILENO);
+	xopen_as(CURRENT_TTY, O_RDWR, STDIN_FILENO);
 	xdup2(STDIN_FILENO, STDOUT_FILENO);
 
 #ifdef __linux__
diff --git a/miscutils/less.c b/miscutils/less.c
index 223c2558d..cb2fecccd 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -1048,7 +1048,7 @@ static void buffer_lineno(int lineno)
 static void open_file_and_read_lines(void)
 {
 	if (filename) {
-		xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
+		xopen_as(filename, O_RDONLY, STDIN_FILENO);
 #if ENABLE_FEATURE_LESS_FLAGS
 		num_lines = REOPEN_AND_COUNT;
 #endif
diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c
index 69ead7a13..a2a6b03d7 100644
--- a/miscutils/ubi_tools.c
+++ b/miscutils/ubi_tools.c
@@ -323,7 +323,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
 			if (!*argv)
 				bb_show_usage();
 			if (NOT_LONE_DASH(*argv)) /* mtd-utils supports "-" as stdin */
-				xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
+				xopen_as(*argv, O_RDONLY, STDIN_FILENO);
 
 			if (!(opts & OPTION_s)) {
 				struct stat st;
diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c
index 0ed10bcf1..600a4e636 100644
--- a/miscutils/watchdog.c
+++ b/miscutils/watchdog.c
@@ -71,7 +71,7 @@ static void shutdown_on_signal(int sig UNUSED_PARAM)
 static void watchdog_open(const char* device)
 {
 	/* Use known fd # - avoid needing global 'int fd' */
-	xmove_fd(xopen(device, O_WRONLY), 3);
+	xopen_as(device, O_WRONLY, 3);
 
 	/* If the watchdog driver can do something other than cause a reboot
 	 * on a timeout, then it's possible this program may be starting from
@@ -81,7 +81,7 @@ static void watchdog_open(const char* device)
 	 */
 	shutdown_watchdog();
 
-	xmove_fd(xopen(device, O_WRONLY), 3);
+	xopen_as(device, O_WRONLY, 3);
 }
 
 int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c
index 25b95246f..9a5f96209 100644
--- a/networking/nc_bloaty.c
+++ b/networking/nc_bloaty.c
@@ -885,7 +885,7 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
 	}
 #if ENABLE_NC_EXTRA
 	if (o_ofile)
-		xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), ofd);
+		xopen_as(str_o, O_WRONLY | O_CREAT | O_TRUNC, ofd);
 #endif
 
 	if (cnt_l != 0) {
diff --git a/util-linux/acpid.c b/util-linux/acpid.c
index 00613f8e3..fa05afc21 100644
--- a/util-linux/acpid.c
+++ b/util-linux/acpid.c
@@ -285,7 +285,7 @@ int acpid_main(int argc UNUSED_PARAM, char **argv)
 		/* No -d "Debug", we log to log file.
 		 * This includes any output from children.
 		 */
-		xmove_fd(xopen(opt_logfile, O_WRONLY | O_CREAT | O_APPEND), STDOUT_FILENO);
+		xopen_as(opt_logfile, O_WRONLY | O_CREAT | O_APPEND, STDOUT_FILENO);
 		xdup2(STDOUT_FILENO, STDERR_FILENO);
 		/* Also, acpid's messages (but not children) will go to syslog too */
 		openlog(applet_name, LOG_PID, LOG_DAEMON);
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index c50ceead1..fc66dbcd5 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -549,7 +549,7 @@ static void
 close_dev_fd(void)
 {
 	/* Not really closing, but making sure it is open, and to harmless place */
-	xmove_fd(xopen(bb_dev_null, O_RDONLY), dev_fd);
+	xopen_as(bb_dev_null, O_RDONLY, dev_fd);
 }
 
 /* Return partition name */
diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c
index 40b86d01b..ac67fb0fd 100644
--- a/util-linux/fsck_minix.c
+++ b/util-linux/fsck_minix.c
@@ -1244,7 +1244,7 @@ int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
 		if (!isatty(0) || !isatty(1))
 			die("need terminal for interactive repairs");
 	}
-	xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
+	xopen_as(device_name, OPT_repair ? O_RDWR : O_RDONLY, dev_fd);
 
 	/*sync(); paranoia? */
 	read_superblock();
diff --git a/util-linux/mkfs_ext2.c b/util-linux/mkfs_ext2.c
index fcf374b2d..526f3b4f7 100644
--- a/util-linux/mkfs_ext2.c
+++ b/util-linux/mkfs_ext2.c
@@ -255,7 +255,7 @@ int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv)
 	argv += optind; // argv[0] -- device
 
 	// open the device, check the device is a block device
-	xmove_fd(xopen(argv[0], O_WRONLY), fd);
+	xopen_as(argv[0], O_WRONLY, fd);
 	xfstat(fd, &st, argv[0]);
 	if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_F))
 		bb_error_msg_and_die("%s: not a block device", argv[0]);
diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c
index 3c72e5419..461822a40 100644
--- a/util-linux/mkfs_minix.c
+++ b/util-linux/mkfs_minix.c
@@ -655,7 +655,7 @@ int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
 	if (find_mount_point(G.device_name, 0))
 		bb_simple_error_msg_and_die("can't format mounted filesystem");
 
-	xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
+	xopen_as(G.device_name, O_RDWR, dev_fd);
 
 	G.total_blocks = get_volume_size_in_bytes(dev_fd, argv[1], 1024, /*extend:*/ 1) / 1024;
 
-- 
2.29.2



More information about the busybox mailing list