aboutsummaryrefslogtreecommitdiff
path: root/ln.c
diff options
context:
space:
mode:
authorerik <erik@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-02-08 19:58:47 +0000
committererik <erik@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-02-08 19:58:47 +0000
commita18125005d67c38a8ad7fb454571bb996664ad96 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /ln.c
parent8ef3b44285a8ce5b83bed9cf32ce5f40b30ba72f (diff)
downloadbusybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.tar.gz
busybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.tar.bz2
busybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.zip
Some formatting updates (ran the code through indent)
-Erik git-svn-id: svn://busybox.net/trunk/busybox@357 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'ln.c')
-rw-r--r--ln.c150
1 files changed, 76 insertions, 74 deletions
diff --git a/ln.c b/ln.c
index f20b340ea..bc51cb0d5 100644
--- a/ln.c
+++ b/ln.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini ln implementation for busybox 3 * Mini ln implementation for busybox
3 * 4 *
@@ -30,15 +31,16 @@
30#include <stdio.h> 31#include <stdio.h>
31#include <dirent.h> 32#include <dirent.h>
32#include <errno.h> 33#include <errno.h>
33#include <sys/param.h> /* for PATH_MAX */ 34#include <sys/param.h> /* for PATH_MAX */
34 35
35static const char ln_usage[] = 36static const char ln_usage[] =
36 "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n" 37 "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n"
37 "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n" 38 "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
38 "Options:\n" 39 "Options:\n"
39 "\t-s\tmake symbolic links instead of hard links\n" 40 "\t-s\tmake symbolic links instead of hard links\n"
40 "\t-f\tremove existing destination files\n" 41
41 "\t-n\tno dereference symlinks - treat like normal file\n"; 42 "\t-f\tremove existing destination files\n"
43 "\t-n\tno dereference symlinks - treat like normal file\n";
42 44
43static int symlinkFlag = FALSE; 45static int symlinkFlag = FALSE;
44static int removeoldFlag = FALSE; 46static int removeoldFlag = FALSE;
@@ -46,83 +48,83 @@ static int followLinks = TRUE;
46 48
47extern int ln_main(int argc, char **argv) 49extern int ln_main(int argc, char **argv)
48{ 50{
49 char *linkName; 51 char *linkName;
50 int linkIntoDirFlag; 52 int linkIntoDirFlag;
51 53
52 if (argc < 3) { 54 if (argc < 3) {
53 usage (ln_usage); 55 usage(ln_usage);
54 } 56 }
55 argc--;
56 argv++;
57
58 /* Parse any options */
59 while (**argv == '-') {
60 while (*++(*argv))
61 switch (**argv) {
62 case 's':
63 symlinkFlag = TRUE;
64 break;
65 case 'f':
66 removeoldFlag = TRUE;
67 break;
68 case 'n':
69 followLinks = FALSE;
70 break;
71 default:
72 usage (ln_usage);
73 }
74 argc--; 57 argc--;
75 argv++; 58 argv++;
76 }
77
78 linkName = argv[argc - 1];
79
80 if (strlen(linkName) > PATH_MAX) {
81 fprintf(stderr, name_too_long, "ln");
82 exit FALSE;
83 }
84 59
85 linkIntoDirFlag = isDirectory(linkName, TRUE); 60 /* Parse any options */
86 61 while (**argv == '-') {
87 if ((argc > 3) && !linkIntoDirFlag) { 62 while (*++(*argv))
88 fprintf(stderr, not_a_directory, "ln", linkName); 63 switch (**argv) {
89 exit FALSE; 64 case 's':
90 } 65 symlinkFlag = TRUE;
66 break;
67 case 'f':
68 removeoldFlag = TRUE;
69 break;
70 case 'n':
71 followLinks = FALSE;
72 break;
73 default:
74 usage(ln_usage);
75 }
76 argc--;
77 argv++;
78 }
91 79
92 while (argc-- >= 2) { 80 linkName = argv[argc - 1];
93 char srcName[PATH_MAX + 1];
94 int nChars, status;
95 81
96 if (strlen(*argv) > PATH_MAX) { 82 if (strlen(linkName) > PATH_MAX) {
97 fprintf(stderr, name_too_long, "ln"); 83 fprintf(stderr, name_too_long, "ln");
98 exit FALSE; 84 exit FALSE;
99 } 85 }
100 86
101 if (followLinks == FALSE) { 87 linkIntoDirFlag = isDirectory(linkName, TRUE);
102 strcpy(srcName, *argv);
103 } else {
104 /* Warning! This can silently truncate if > PATH_MAX, but
105 I don't think that there can be one > PATH_MAX anyway. */
106 nChars = readlink(*argv, srcName, PATH_MAX);
107 srcName[nChars] = '\0';
108 }
109 88
110 if (removeoldFlag == TRUE) { 89 if ((argc > 3) && !linkIntoDirFlag) {
111 status = ( unlink(linkName) && errno != ENOENT ); 90 fprintf(stderr, not_a_directory, "ln", linkName);
112 if (status != 0) {
113 perror(linkName);
114 exit FALSE; 91 exit FALSE;
115 }
116 } 92 }
117 93
118 if (symlinkFlag == TRUE) 94 while (argc-- >= 2) {
119 status = symlink(*argv, linkName); 95 char srcName[PATH_MAX + 1];
120 else 96 int nChars, status;
121 status = link(*argv, linkName); 97
122 if (status != 0) { 98 if (strlen(*argv) > PATH_MAX) {
123 perror(linkName); 99 fprintf(stderr, name_too_long, "ln");
124 exit FALSE; 100 exit FALSE;
101 }
102
103 if (followLinks == FALSE) {
104 strcpy(srcName, *argv);
105 } else {
106 /* Warning! This can silently truncate if > PATH_MAX, but
107 I don't think that there can be one > PATH_MAX anyway. */
108 nChars = readlink(*argv, srcName, PATH_MAX);
109 srcName[nChars] = '\0';
110 }
111
112 if (removeoldFlag == TRUE) {
113 status = (unlink(linkName) && errno != ENOENT);
114 if (status != 0) {
115 perror(linkName);
116 exit FALSE;
117 }
118 }
119
120 if (symlinkFlag == TRUE)
121 status = symlink(*argv, linkName);
122 else
123 status = link(*argv, linkName);
124 if (status != 0) {
125 perror(linkName);
126 exit FALSE;
127 }
125 } 128 }
126 } 129 exit TRUE;
127 exit TRUE;
128} 130}