comparison toys/posix/ln.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children 1e1abf1113f2
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* ln.c - Create filesystem links
2 *
3 * ln.c - Create filesystem links
4 * 2 *
5 * Copyright 2012 Andre Renaud <andre@bluewatersys.com> 3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
6 * 4 *
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/ln.html 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/ln.html
8 6
9 USE_LN(NEWTOY(ln, "<1nfs", TOYFLAG_BIN)) 7 USE_LN(NEWTOY(ln, "<1nfs", TOYFLAG_BIN))
10 8
11 config LN 9 config LN
12 bool "ln" 10 bool "ln"
13 default y 11 default y
14 help 12 help
15 usage: ln [-sf] [FROM...] TO 13 usage: ln [-sf] [FROM...] TO
16 14
17 Create a link between FROM and TO. 15 Create a link between FROM and TO.
18 With only one argument, create link in current directory. 16 With only one argument, create link in current directory.
19 17
20 -s Create a symbolic link 18 -s Create a symbolic link
21 -f Force the creation of the link, even if TO already exists 19 -f Force the creation of the link, even if TO already exists
22 -n Symlink at destination treated as file 20 -n Symlink at destination treated as file
23 */ 21 */
24 22
25 #define FOR_ln 23 #define FOR_ln
26 #include "toys.h" 24 #include "toys.h"
27 25
28 void ln_main(void) 26 void ln_main(void)
29 { 27 {
30 char *dest = toys.optargs[--toys.optc], *new; 28 char *dest = toys.optargs[--toys.optc], *new;
31 struct stat buf; 29 struct stat buf;
32 int i; 30 int i;
33 31
34 // With one argument, create link in current directory. 32 // With one argument, create link in current directory.
35 if (!toys.optc) { 33 if (!toys.optc) {
36 toys.optc++; 34 toys.optc++;
37 dest="."; 35 dest=".";
38 } 36 }
39 37
40 // Is destination a directory? 38 // Is destination a directory?
41 if (((toys.optflags&FLAG_n) ? lstat : stat)(dest, &buf) 39 if (((toys.optflags&FLAG_n) ? lstat : stat)(dest, &buf)
42 || !S_ISDIR(buf.st_mode)) 40 || !S_ISDIR(buf.st_mode))
43 { 41 {
44 if (toys.optc>1) error_exit("'%s' not a directory"); 42 if (toys.optc>1) error_exit("'%s' not a directory");
45 buf.st_mode = 0; 43 buf.st_mode = 0;
46 } 44 }
47 45
48 for (i=0; i<toys.optc; i++) { 46 for (i=0; i<toys.optc; i++) {
49 int rc; 47 int rc;
50 char *try = toys.optargs[i]; 48 char *try = toys.optargs[i];
51 49
52 if (S_ISDIR(buf.st_mode)) { 50 if (S_ISDIR(buf.st_mode)) {
53 new = strrchr(try, '/'); 51 new = strrchr(try, '/');
54 if (!new) new = try; 52 if (!new) new = try;
55 new = xmsprintf("%s/%s", dest, new); 53 new = xmsprintf("%s/%s", dest, new);
56 } else new = dest; 54 } else new = dest;
57 /* Silently unlink the existing target. If it doesn't exist, 55 /* Silently unlink the existing target. If it doesn't exist,
58 * then we just move on */ 56 * then we just move on */
59 if (toys.optflags & FLAG_f) unlink(new); 57 if (toys.optflags & FLAG_f) unlink(new);
60 58
61 rc = (toys.optflags & FLAG_s) ? symlink(try, new) : link(try, new); 59 rc = (toys.optflags & FLAG_s) ? symlink(try, new) : link(try, new);
62 if (rc) 60 if (rc)
63 perror_exit("cannot create %s link from '%s' to '%s'", 61 perror_exit("cannot create %s link from '%s' to '%s'",
64 (toys.optflags & FLAG_s) ? "symbolic" : "hard", try, new); 62 (toys.optflags & FLAG_s) ? "symbolic" : "hard", try, new);
65 if (new != dest) free(new); 63 if (new != dest) free(new);
66 } 64 }
67 } 65 }