aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/ln.c114
-rw-r--r--coreutils/mkdir.c119
-rw-r--r--coreutils/rmdir.c44
3 files changed, 188 insertions, 89 deletions
diff --git a/coreutils/ln.c b/coreutils/ln.c
index 3e87b579e..cd3cb4e45 100644
--- a/coreutils/ln.c
+++ b/coreutils/ln.c
@@ -1,52 +1,100 @@
1/*
2 * Mini ln implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1#include "internal.h" 22#include "internal.h"
2#include <stdio.h> 23#include <stdio.h>
3#include <sys/stat.h> 24#include <dirent.h>
4#include <sys/param.h>
5#include <errno.h> 25#include <errno.h>
6 26
7const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n" 27
28static const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n"
8"\n" 29"\n"
9"\tAdd a new name that refers to the same file as \"original-name\"\n" 30"\tAdd a new name that refers to the same file as \"original-name\"\n"
10"\n" 31"\n"
11"\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n" 32"\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n"
12"\t-f:\tRemove existing destination files.\n"; 33"\t-f:\tRemove existing destination files.\n";
13 34
14int 35
15ln_fn(const struct FileInfo * i) 36static int symlinkFlag = FALSE;
37static int removeoldFlag = FALSE;
38static const char *destName;
39
40
41extern int ln_main(int argc, char **argv)
16{ 42{
17 int status = 0; 43 int status;
18 char d[PATH_MAX]; 44 char newdestName[NAME_MAX];
19 const char * destination = i->destination;
20 45
21 if ( !i->makeSymbolicLink && (i->stat.st_mode & S_IFMT) == S_IFDIR ) { 46 if (argc < 3) {
22 fprintf(stderr, "Please use \"ln -s\" to link directories.\n"); 47 fprintf(stderr, "Usage: %s", ln_usage);
23 return 1; 48 exit (FALSE);
24 } 49 }
50 argc--;
51 argv++;
25 52
26 /* 53 /* Parse any options */
27 * If the destination is a directory, create a file within it. 54 while (**argv == '-') {
28 */ 55 while (*++(*argv))
29 if ( is_a_directory(i->destination) ) { 56 switch (**argv) {
30 destination = join_paths( 57 case 's':
31 d 58 symlinkFlag = TRUE;
32 ,i->destination 59 break;
33 ,&i->source[i->directoryLength]); 60 case 'f':
34 } 61 removeoldFlag = TRUE;
62 break;
63 default:
64 fprintf(stderr, "Usage: %s\n", ln_usage);
65 exit(FALSE);
66 }
67 argc--;
68 argv++;
69 }
35 70
36 if ( i->force )
37 status = ( unlink(destination) && errno != ENOENT );
38 71
39 if ( status == 0 ) { 72 destName = argv[argc - 1];
40 if ( i->makeSymbolicLink )
41 status = symlink(i->source, destination);
42 else
43 status = link(i->source, destination);
44 }
45 73
46 if ( status != 0 ) { 74 if ((argc > 3) && !(isDirectory(destName))) {
47 name_and_error(destination); 75 fprintf(stderr, "%s: not a directory\n", destName);
48 return 1; 76 exit (FALSE);
77 }
78
79 while (argc-- >= 2) {
80 strcpy(newdestName, destName);
81 strcat(newdestName, (*argv)+(strlen(*(++argv))));
82
83 if (removeoldFlag==TRUE ) {
84 status = ( unlink(newdestName) && errno != ENOENT );
85 if ( status != 0 ) {
86 perror(newdestName);
87 exit( FALSE);
88 }
49 } 89 }
90 if ( symlinkFlag==TRUE)
91 status = symlink(*argv, newdestName);
50 else 92 else
51 return 0; 93 status = link(*argv, newdestName);
94 if ( status != 0 ) {
95 perror(newdestName);
96 exit( FALSE);
97 }
98 }
99 exit( TRUE);
52} 100}
diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c
index 8f1fa04db..61d35d5cd 100644
--- a/coreutils/mkdir.c
+++ b/coreutils/mkdir.c
@@ -1,58 +1,85 @@
1/*
2 * Mini mkdir implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1#include "internal.h" 22#include "internal.h"
23#include <stdio.h>
2#include <errno.h> 24#include <errno.h>
3#include <sys/param.h> 25#include <sys/param.h>
4 26
5const char mkdir_usage[] = "mkdir [-m mode] directory [directory ...]\n" 27const char mkdir_usage[] = "Usage: mkdir [OPTION] DIRECTORY...\n"
6"\tCreate directories.\n" 28"Create the DIRECTORY(ies), if they do not already exist\n\n"
7"\n" 29"-m\tset permission mode (as in chmod), not rwxrwxrwx - umask\n"
8"\t-m mode:\tSpecifiy the mode for the new directory\n" 30"-p\tno error if existing, make parent directories as needed\n";
9"\t\tunder the argument directory.";
10 31
11/*make directories skipping the last part of the path. Used here and by untar*/
12int mkdir_until(const char *fpath, const struct FileInfo * fi)
13{
14 char path[PATH_MAX];
15 char * s = path;
16 32
17 strcpy(path, fpath); 33static int parentFlag = FALSE;
18 if ( s[0] == '\0' && s[1] == '\0' ) { 34static int permFlag = FALSE;
19 usage(mkdir_usage); 35static mode_t mode = 0777;
20 return 1; 36
21 }
22 s++;
23 while ( *s != '\0' ) {
24 if ( *s == '/' ) {
25 int status;
26
27 *s = '\0';
28 status = mkdir(path, (fi?fi->orWithMode:0700) );
29 *s = '/';
30
31 if ( status != 0 ) {
32 if ( errno != EEXIST ) {
33 name_and_error(fpath);
34 return 1;
35 }
36 }
37
38 }
39 s++;
40 }
41 return 0;
42}
43 37
44int 38extern int mkdir_main(int argc, char **argv)
45mkdir_fn(const struct FileInfo * i)
46{ 39{
47 if ( i->makeParentDirectories ) { 40 argc--;
48 if(mkdir_until(i->source, i)) return 1; 41 argv++;
49 } 42
43 /* Parse any options */
44 while (argc > 1 && **argv == '-') {
45 while (*++(*argv))
46 switch (**argv) {
47 case 'm':
48 permFlag = TRUE;
49 break;
50 case 'p':
51 parentFlag = TRUE;
52 break;
53 default:
54 fprintf(stderr, "%s\n", mkdir_usage);
55 exit(FALSE);
56 }
57 argc--;
58 argv++;
59 }
60
50 61
51 if ( mkdir(i->source, i->orWithMode) != 0 && errno != EEXIST ) { 62 if (argc < 1) {
52 name_and_error(i->source); 63 fprintf(stderr, "%s\n", mkdir_usage);
53 return 1; 64 exit (FALSE);
65 }
66
67 while (--argc > 0) {
68 struct stat statBuf;
69 if (stat(*(++argv), &statBuf) != ENOENT) {
70 fprintf(stderr, "%s: File exists\n", *argv);
71 return( FALSE);
72 }
73 if (parentFlag == TRUE)
74 createPath(*argv, mode);
75 else {
76 if (mkdir (*argv, mode) != 0) {
77 perror(*argv);
78 exit( FALSE);
79 }
54 } 80 }
55 else 81 }
56 return 0; 82 exit( TRUE);
57} 83}
58 84
85
diff --git a/coreutils/rmdir.c b/coreutils/rmdir.c
index 069e68546..b4da03f12 100644
--- a/coreutils/rmdir.c
+++ b/coreutils/rmdir.c
@@ -1,17 +1,41 @@
1/*
2 * Mini rmdir implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1#include "internal.h" 22#include "internal.h"
23#include <stdio.h>
2#include <errno.h> 24#include <errno.h>
3 25
4const char rmdir_usage[] = "rmdir directory [directory ...]\n"
5"\n"
6"\tDelete directories.\n";
7 26
8extern int 27extern int rmdir_main(int argc, char **argv)
9rmdir_fn(const struct FileInfo * i)
10{ 28{
11 if ( rmdir(i->source) != 0 && errno != ENOENT && !i->force ) { 29 if ( argc==1 || **(argv+1) == '-' ) {
12 name_and_error(i->source); 30 fprintf(stderr, "Usage: rmdir [OPTION]... DIRECTORY...\nRemove the DIRECTORY(ies), if they are empty.");
13 return 1; 31 exit(FALSE);
32 }
33
34 while (--argc > 0) {
35 if ( rmdir(*(++argv)) == -1 ) {
36 fprintf(stderr, "%s: %s\n", *argv, strerror(errno));
37 exit(FALSE);
14 } 38 }
15 else 39 }
16 return 0; 40 exit(TRUE);
17} 41}