comparison toys/posix/ln.c @ 653:2986aa63a021

Move commands into "posix", "lsb", and "other" menus/directories.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 14:25:22 -0500
parents toys/ln.c@94a2905dd325
children 6df4ccc0acbe
comparison
equal deleted inserted replaced
652:2d7c56913fda 653:2986aa63a021
1 /* vi: set sw=4 ts=4:
2 *
3 * ln.c - Create filesystem links
4 *
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
6 *
7 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html
8
9 USE_LN(NEWTOY(ln, "<1nfs", TOYFLAG_BIN))
10
11 config LN
12 bool "ln"
13 default y
14 help
15 usage: ln [-sf] [FROM...] TO
16
17 Create a link between FROM and TO.
18 With only one argument, create link in current directory.
19
20 -s Create a symbolic link
21 -f Force the creation of the link, even if TO already exists
22 -n Symlink at destination treated as file
23 */
24
25 #include "toys.h"
26
27 #define FLAG_s 1
28 #define FLAG_f 2
29 #define FLAG_n 4
30
31 void ln_main(void)
32 {
33 char *dest = toys.optargs[--toys.optc], *new;
34 struct stat buf;
35 int i;
36
37 // With one argument, create link in current directory.
38 if (!toys.optc) {
39 toys.optc++;
40 dest=".";
41 }
42
43 // Is destination a directory?
44 if (((toys.optflags&FLAG_n) ? lstat : stat)(dest, &buf)
45 || !S_ISDIR(buf.st_mode))
46 {
47 if (toys.optc>1) error_exit("'%s' not a directory");
48 buf.st_mode = 0;
49 }
50
51 for (i=0; i<toys.optc; i++) {
52 int rc;
53 char *try = toys.optargs[i];
54
55 if (S_ISDIR(buf.st_mode)) {
56 new = strrchr(try, '/');
57 if (!new) new = try;
58 new = xmsprintf("%s/%s", dest, new);
59 } else new = dest;
60 /* Silently unlink the existing target. If it doesn't exist,
61 * then we just move on */
62 if (toys.optflags & FLAG_f) unlink(new);
63
64 rc = (toys.optflags & FLAG_s) ? symlink(try, new) : link(try, new);
65 if (rc)
66 perror_exit("cannot create %s link from '%s' to '%s'",
67 (toys.optflags & FLAG_s) ? "symbolic" : "hard", try, new);
68 if (new != dest) free(new);
69 }
70 }