aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-02-13 22:57:30 -0600
committerRob Landley <rob@landley.net>2012-02-13 22:57:30 -0600
commit20d25e8b9d175a1b750e726b37a10d00f253fc70 (patch)
treed3ad41b9a3a1c6fd63134eae0af231ecf89a915d /toys
parentb07a3d3f1f5ff041e755110a451bb6c75a8a7231 (diff)
downloadtoybox-20d25e8b9d175a1b750e726b37a10d00f253fc70.tar.gz
Add ln from Andre Renaud.
Diffstat (limited to 'toys')
-rw-r--r--toys/ln.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/toys/ln.c b/toys/ln.c
new file mode 100644
index 00000000..9dd413f5
--- /dev/null
+++ b/toys/ln.c
@@ -0,0 +1,48 @@
+/* vi: set sw=4 ts=4:
+ *
+ * ln.c - Create filesystem links
+ *
+ * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
+ *
+ * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html
+
+USE_LN(NEWTOY(ln, "fs", TOYFLAG_BIN))
+
+config LN
+ bool "ln"
+ default n
+ help
+ usage: ln [-s] [-f] file1 file2
+
+ Create a link from file2 to file1
+
+ -s Create a symbolic link
+ -f Force the creation of the link, even if file2 already exists
+*/
+
+#include "toys.h"
+
+#define FLAG_s 1
+#define FLAG_f 2
+
+void ln_main(void)
+{
+ char *file1 = toys.optargs[0], *file2 = toys.optargs[1];
+
+ /* FIXME: How do we print out the usage info? */
+ if (!file1 || !file2)
+ perror_exit("Usage: ln [-s] [-f] file1 file2");
+ /* Silently unlink the existing target. If it doesn't exist,
+ * then we just move on */
+ if (toys.optflags & FLAG_f)
+ unlink(file2);
+ if (toys.optflags & FLAG_s) {
+ if (symlink(file1, file2))
+ perror_exit("cannot create symbolic link from %s to %s",
+ file1, file2);
+ } else {
+ if (link(file1, file2))
+ perror_exit("cannot create hard link from %s to %s",
+ file1, file2);
+ }
+}