aboutsummaryrefslogtreecommitdiff
path: root/coreutils/ln.c
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2000-10-04 09:34:35 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2000-10-04 09:34:35 +0000
commitaa3908d1b72c236264d91bed5db79ddec8e7b5a6 (patch)
tree082a9b0d3b631360adc236abd417d135526b445e /coreutils/ln.c
parentb520e083e0507f15e05230733a760e52dcd9f133 (diff)
downloadbusybox-w32-aa3908d1b72c236264d91bed5db79ddec8e7b5a6.tar.gz
busybox-w32-aa3908d1b72c236264d91bed5db79ddec8e7b5a6.tar.bz2
busybox-w32-aa3908d1b72c236264d91bed5db79ddec8e7b5a6.zip
Rewrite to use getopt and return instead of exit, a seperate function (fs_link) does the actual work.
Diffstat (limited to 'coreutils/ln.c')
-rw-r--r--coreutils/ln.c152
1 files changed, 69 insertions, 83 deletions
diff --git a/coreutils/ln.c b/coreutils/ln.c
index 8ef8d05de..38e9b6763 100644
--- a/coreutils/ln.c
+++ b/coreutils/ln.c
@@ -30,96 +30,82 @@
30#include <dirent.h> 30#include <dirent.h>
31#include <errno.h> 31#include <errno.h>
32 32
33static int symlinkFlag = FALSE; 33#define LN_SYMLINK 1
34static int removeoldFlag = FALSE; 34#define LN_FORCE 2
35static int followLinks = TRUE; 35#define LN_NODEREFERENCE 4
36 36
37extern int ln_main(int argc, char **argv) 37/*
38 * linkDestName is where the link points to,
39 * linkSrcName is the name of the link to be created.
40 */
41static int fs_link(const char *link_DestName, const char *link_SrcName, const int flag)
38{ 42{
39 char *linkName, *dirName=NULL; 43 int status;
40 int linkIntoDirFlag; 44 int srcIsDir;
41 int stopIt = FALSE; 45 char *srcName;
42 46
43 argc--; 47 if (link_DestName==NULL)
44 argv++; 48 return(FALSE);
45 49
46 /* Parse any options */ 50 srcName = (char *) malloc(strlen(link_SrcName)+strlen(link_DestName)+1);
47 while (argc > 0 && stopIt == FALSE) { 51
48 if (**argv == '-') { 52 if (link_SrcName==NULL)
49 while (*++(*argv)) 53 strcpy(srcName, link_DestName);
50 switch (**argv) { 54 else
51 case 's': 55 strcpy(srcName, link_SrcName);
52 symlinkFlag = TRUE; 56
53 break; 57 if (flag&LN_NODEREFERENCE)
54 case 'f': 58 srcIsDir = isDirectory(srcName, TRUE, NULL);
55 removeoldFlag = TRUE; 59 else
56 break; 60 srcIsDir = isDirectory(srcName, FALSE, NULL);
57 case 'n': 61
58 followLinks = FALSE; 62 if ((srcIsDir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) {
59 break; 63 strcat(srcName, "/");
60 case '-': 64 strcat(srcName, link_DestName);
61 stopIt = TRUE;
62 break;
63 default:
64 usage(ln_usage);
65 }
66 argc--;
67 argv++;
68 }
69 else
70 break;
71 }
72
73 if (argc < 2) {
74 usage(ln_usage);
75 } 65 }
76 66
77 linkName = argv[argc - 1]; 67 if (flag&LN_FORCE)
78 68 unlink(srcName);
79 linkIntoDirFlag = isDirectory(linkName, followLinks, NULL); 69
80 if ((argc >= 3) && linkIntoDirFlag == FALSE) { 70 if (flag&LN_SYMLINK)
81 errorMsg(not_a_directory, linkName); 71 status = symlink(link_DestName, srcName);
82 exit FALSE; 72 else
73 status = link(link_DestName, srcName);
74
75 if (status != 0) {
76 perror(srcName);
77 return(FALSE);
83 } 78 }
79 return(TRUE);
80}
84 81
85 if (linkIntoDirFlag == TRUE) 82extern int ln_main(int argc, char **argv)
86 dirName = linkName; 83{
87 84 int flag = 0;
88 while (argc-- >= 2) { 85 int opt;
89 int status; 86
90 87 /* Parse any options */
91 if (linkIntoDirFlag == TRUE) { 88 while ((opt=getopt(argc, argv, "sfn")) != -1) {
92 char *baseName = get_last_path_component(*argv); 89 switch(opt) {
93 linkName = (char *)xmalloc(strlen(dirName)+strlen(baseName)+2); 90 case 's':
94 strcpy(linkName, dirName); 91 flag |= LN_SYMLINK;
95 if(dirName[strlen(dirName)-1] != '/') 92 break;
96 strcat(linkName, "/"); 93 case 'f':
97 strcat(linkName,baseName); 94 flag |= LN_FORCE;
98 } 95 break;
99 96 case 'n':
100 if (removeoldFlag == TRUE) { 97 flag |= LN_NODEREFERENCE;
101 status = (unlink(linkName) && errno != ENOENT); 98 break;
102 if (status != 0) { 99 default:
103 perror(linkName); 100 usage(ln_usage);
104 exit FALSE;
105 }
106 }
107
108 if (symlinkFlag == TRUE)
109 status = symlink(*argv, linkName);
110 else
111 status = link(*argv, linkName);
112 if (status != 0) {
113 perror(linkName);
114 exit FALSE;
115 } 101 }
116
117 if (linkIntoDirFlag == TRUE)
118 free(linkName);
119
120 argv++;
121 } 102 }
122 return( TRUE); 103 while(optind<(argc-1)) {
104 if (fs_link(argv[optind], argv[argc-1], flag)==FALSE)
105 return(FALSE);
106 optind++;
107 }
108 return(TRUE);
123} 109}
124 110
125/* 111/*