annotate toys/ln.c @ 562:4d802d438983

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