[Toybox] [patch] add install

Strake strake888 at gmail.com
Tue Aug 13 18:02:33 PDT 2013


>From f147c9618914a6dbfe41eea04329e7243629d1fe Mon Sep 17 00:00:00 2001
From: Strake <strake888 at gmail.com>
Date: Wed, 14 Aug 2013 00:52:40 +0000
Subject: [PATCH] add install

---
 lib/lib.h              |  3 ++
 lib/pending.c          | 11 +++++++
 toys/pending/install.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 toys/pending/install.c

diff --git a/lib/lib.h b/lib/lib.h
index f66a70c..12a8c98 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -202,3 +202,6 @@ unsigned long get_int_value(const char *numstr,
unsigned lowrange, unsigned high
 // grep helper functions
 char  *astrcat (char *, char *);
 char *xastrcat (char *, char *);
+
+// pending
+int xsspawnv (char **);
diff --git a/lib/pending.c b/lib/pending.c
index fad1c65..89ed260 100644
--- a/lib/pending.c
+++ b/lib/pending.c
@@ -102,3 +102,14 @@ char *xastrcat (char *x, char *y) {
   if (!x) error_exit ("xastrcat");
   return x;
 }
+
+int xsspawnv (char **argu) {
+  pid_t pid;
+  int c;
+
+  pid = fork ();
+  if (pid < 0) perror_exit ("failed to fork");
+  if (!pid) xexec (argu);
+  if (waitpid (pid, &c, 0) < 0) perror_exit ("failed to wait");
+  return c;
+}
diff --git a/toys/pending/install.c b/toys/pending/install.c
new file mode 100644
index 0000000..cde5bac
--- /dev/null
+++ b/toys/pending/install.c
@@ -0,0 +1,86 @@
+/* install.c - install files
+ *
+ * Copyright 2013 CE Strake <strake888 at gmail.com>
+ *
+ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
+
+USE_INSTALL(NEWTOY(install, "Dcdg:m:o:t:[!dt]", TOYFLAG_BIN))
+
+config INSTALL
+  bool "install"
+  default n
+  help
+    usage: install [-D] [-m mode] [-g grp] [-o usr] (src tgt | src...
dir | -t dir src... | -d dir...)
+
+    forms:
+      install src tgt           install src as tgt
+      install -t dir src...     install all srcs into dir
+      install -d dir...         mk all dirs
+
+    flags:
+      -D         create all needed directories
+      -m mode    chmod mode
+      -g grp     chgrp grp
+      -o usr     chown usr
+*/
+
+#define FOR_install
+#include "toys.h"
+
+GLOBALS(
+  char *tArgu;
+  char *oArgu;
+  char *mArgu;
+  char *gArgu;
+)
+
+static void chxs (char *path) {
+  if (toys.optflags & FLAG_m) xsspawnv ((char *[]){ "chmod", "--",
TT.mArgu, path, 0 });
+  if (toys.optflags & FLAG_g) xsspawnv ((char *[]){ "chown", "--",
TT.gArgu, path, 0 });
+  if (toys.optflags & FLAG_o) xsspawnv ((char *[]){ "chgrp", "--",
TT.oArgu, path, 0 });
+}
+
+void install_main () {
+  int ii;
+
+  if (!(toys.optflags & (FLAG_t | FLAG_d))) {
+    if (toys.optc < 2) error_exit ("too few arguments");
+    if (toys.optc > 2) {
+      toys.optflags |= FLAG_t;
+      TT.tArgu = toys.optargs[toys.optc - 1];
+      toys.optargs[--toys.optc] = 0;
+    }
+    else {
+      if (toys.optflags & FLAG_D) {
+        char *p;
+        p = strrchr (toys.optargs[1], '/');
+        if (p) p[0] = 0;
+        xmkpath (toys.optargs[1], -1);
+        p[0] = '/';
+      }
+      xexec ((char *[]){ "cp", "--", toys.optargs[0], toys.optargs[1], 0 });
+    }
+  }
+
+  if (toys.optflags & FLAG_d) {
+    for (ii = 0; ii < toys.optc; ii++) {
+      xmkpath (toys.optargs[ii], -1);
+      chxs (toys.optargs[ii]);
+    }
+  }
+
+  if (toys.optflags & FLAG_t) {
+    struct stat st;
+
+    if (toys.optflags & FLAG_D) xmkpath (TT.tArgu, -1);
+    xstat (TT.tArgu, &st);
+    if (!S_ISDIR(st.st_mode)) error_exit ("not a directory: %s", TT.tArgu);
+    for (ii = 0; ii < toys.optc; ii++) {
+      char *path;
+      path = xmsprintf ("%s/%s", TT.tArgu, basename (toys.optargs[ii]));
+      xsspawnv ((char *[]){ "cp", "--", toys.optargs[ii], path, 0 });
+      chxs (path);
+      free (path);
+    }
+  }
+}
-- 
1.8.3.4


More information about the Toybox mailing list