aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-12 18:03:52 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-12 18:03:52 +0200
commitef7899f01c6c20b7d18e1b668459a632a989f0b7 (patch)
treea8fb944779a08c590e924cb0b5fd8e2edb5836bf
parentaf7821da81932a990ecc64a6381a90f3d6eaf5d2 (diff)
downloadxutil-ef7899f01c6c20b7d18e1b668459a632a989f0b7.tar.gz
flip: Rewrite in C
-rwxr-xr-xetc/flip3
-rw-r--r--etc/flip.c31
2 files changed, 31 insertions, 3 deletions
diff --git a/etc/flip b/etc/flip
deleted file mode 100755
index 614bc67..0000000
--- a/etc/flip
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/usr/bin/perl
-
-exec (shift @ARGV, pop @ARGV, @ARGV);
diff --git a/etc/flip.c b/etc/flip.c
new file mode 100644
index 0000000..dcd6bc0
--- /dev/null
+++ b/etc/flip.c
@@ -0,0 +1,31 @@
+/*
+ * flip -- put last argument after first argument
+ * $ cc -O2 -o flip flip.c
+ */
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+ int i;
+
+ if(argc-1 == 0){
+ fprintf(stderr, "usage: %s command [arg ...]\n", argv[0]);
+ return 1;
+ }
+
+ /* Move last argument after first argument. */
+ if(argc-1 > 2){
+ for(i = argc; i >= 3; i--)
+ argv[i] = argv[i-1];
+ argv[2] = strdup(argv[argc]);
+ argv[argc] = NULL;
+ }
+
+ execvp(argv[1], argv+1);
+}