aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-05 22:58:32 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-05 22:58:32 +0000
commit2b69c40e8060934c115922c012737bd471956f09 (patch)
treec92657a42b94419ab9579e8c52639c301764ec40
parentcc8ed39b240180b58810784f844e253263594ac3 (diff)
downloadbusybox-w32-2b69c40e8060934c115922c012737bd471956f09.tar.gz
busybox-w32-2b69c40e8060934c115922c012737bd471956f09.tar.bz2
busybox-w32-2b69c40e8060934c115922c012737bd471956f09.zip
More stuff works.
-Erik
-rw-r--r--Makefile4
-rw-r--r--applets/busybox.c4
-rw-r--r--busybox.c4
-rw-r--r--busybox.def.h11
-rw-r--r--cat.c8
-rw-r--r--chgrp.c89
-rw-r--r--chown.c156
-rw-r--r--chroot.c73
-rw-r--r--clear.c26
-rw-r--r--console-tools/clear.c26
-rw-r--r--coreutils/cat.c8
-rw-r--r--coreutils/chgrp.c89
-rw-r--r--coreutils/chown.c156
-rw-r--r--coreutils/chroot.c73
-rw-r--r--internal.h109
-rw-r--r--utility.c744
-rw-r--r--utility.h73
17 files changed, 488 insertions, 1165 deletions
diff --git a/Makefile b/Makefile
index bef25a3ae..62c4ac6fe 100644
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ CFLAGS=-Wall -O2 -fomit-frame-pointer -fno-builtin -D_GNU_SOURCE
14# For debugging only 14# For debugging only
15#CFLAGS=-Wall -g -D_GNU_SOURCE 15#CFLAGS=-Wall -g -D_GNU_SOURCE
16LIBRARIES=-lc 16LIBRARIES=-lc
17OBJECTS=$(shell ./busybox.obj) utility.o 17OBJECTS=$(shell ./busybox.sh) utility.o
18 18
19CFLAGS+= -DBB_VER='"$(VERSION)"' 19CFLAGS+= -DBB_VER='"$(VERSION)"'
20CFLAGS+= -DBB_BT='"$(BUILDTIME)"' 20CFLAGS+= -DBB_BT='"$(BUILDTIME)"'
@@ -36,3 +36,5 @@ distclean: clean
36 - rm -f busybox 36 - rm -f busybox
37 37
38force: 38force:
39
40$(OBJECTS): busybox.def.h Makefile
diff --git a/applets/busybox.c b/applets/busybox.c
index b7b2b6009..08ba88d65 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -16,14 +16,12 @@ static const struct Applet applets[] = {
16#ifdef BB_CAT //bin 16#ifdef BB_CAT //bin
17 {"cat", cat_more_main}, 17 {"cat", cat_more_main},
18#endif 18#endif
19#ifdef BB_CHGRP //bin
20 {"chgrp", chgrp_main},
21#endif
22#ifdef BB_CHMOD //bin 19#ifdef BB_CHMOD //bin
23 {"chmod", chmod_main}, 20 {"chmod", chmod_main},
24#endif 21#endif
25#ifdef BB_CHOWN //bin 22#ifdef BB_CHOWN //bin
26 {"chown", chown_main}, 23 {"chown", chown_main},
24 {"chgrp", chown_main},
27#endif 25#endif
28#ifdef BB_CHROOT //sbin 26#ifdef BB_CHROOT //sbin
29 {"chroot", chroot_main}, 27 {"chroot", chroot_main},
diff --git a/busybox.c b/busybox.c
index b7b2b6009..08ba88d65 100644
--- a/busybox.c
+++ b/busybox.c
@@ -16,14 +16,12 @@ static const struct Applet applets[] = {
16#ifdef BB_CAT //bin 16#ifdef BB_CAT //bin
17 {"cat", cat_more_main}, 17 {"cat", cat_more_main},
18#endif 18#endif
19#ifdef BB_CHGRP //bin
20 {"chgrp", chgrp_main},
21#endif
22#ifdef BB_CHMOD //bin 19#ifdef BB_CHMOD //bin
23 {"chmod", chmod_main}, 20 {"chmod", chmod_main},
24#endif 21#endif
25#ifdef BB_CHOWN //bin 22#ifdef BB_CHOWN //bin
26 {"chown", chown_main}, 23 {"chown", chown_main},
24 {"chgrp", chown_main},
27#endif 25#endif
28#ifdef BB_CHROOT //sbin 26#ifdef BB_CHROOT //sbin
29 {"chroot", chroot_main}, 27 {"chroot", chroot_main},
diff --git a/busybox.def.h b/busybox.def.h
index ad6480a25..7d49ed0c2 100644
--- a/busybox.def.h
+++ b/busybox.def.h
@@ -6,11 +6,10 @@
6//#define BB_BLOCK_DEVICE 6//#define BB_BLOCK_DEVICE
7#define BB_BUSYBOX 7#define BB_BUSYBOX
8#define BB_CAT 8#define BB_CAT
9#define BB_CHGRP
10//#define BB_CHMOD 9//#define BB_CHMOD
11//#define BB_CHOWN 10#define BB_CHOWN
12//#define BB_CHROOT 11#define BB_CHROOT
13//#define BB_CLEAR 12#define BB_CLEAR
14//#define BB_CP 13//#define BB_CP
15//#define BB_DATE 14//#define BB_DATE
16//#define BB_DD 15//#define BB_DD
@@ -19,7 +18,7 @@
19//#define BB_DMESG 18//#define BB_DMESG
20//#define BB_DUTMP 19//#define BB_DUTMP
21//#define BB_DYADIC 20//#define BB_DYADIC
22//#define BB_FALSE 21#define BB_FALSE
23//#define BB_FDFLUSH 22//#define BB_FDFLUSH
24//#define BB_FIND 23//#define BB_FIND
25//#define BB_FINDMOUNT 24//#define BB_FINDMOUNT
@@ -56,7 +55,7 @@
56//#define BB_SYNC 55//#define BB_SYNC
57//#define BB_TAR 56//#define BB_TAR
58//#define BB_TOUCH 57//#define BB_TOUCH
59//#define BB_TRUE 58#define BB_TRUE
60//#define BB_UMOUNT 59//#define BB_UMOUNT
61//#define BB_UPDATE 60//#define BB_UPDATE
62//#define BB_UTILITY 61//#define BB_UTILITY
diff --git a/cat.c b/cat.c
index 12faf55ab..0f2460eb7 100644
--- a/cat.c
+++ b/cat.c
@@ -31,7 +31,7 @@ extern int cat_more_main(int argc, char **argv)
31 31
32 if (argc < 2) { 32 if (argc < 2) {
33 fprintf(stderr, "Usage: %s %s", *argv, cat_usage); 33 fprintf(stderr, "Usage: %s %s", *argv, cat_usage);
34 return 1; 34 return(FALSE);
35 } 35 }
36 argc--; 36 argc--;
37 argv++; 37 argv++;
@@ -39,8 +39,8 @@ extern int cat_more_main(int argc, char **argv)
39 while (argc-- > 0) { 39 while (argc-- > 0) {
40 file = fopen(*argv, "r"); 40 file = fopen(*argv, "r");
41 if (file == NULL) { 41 if (file == NULL) {
42 name_and_error(*argv); 42 perror(*argv);
43 return 1; 43 return(FALSE);
44 } 44 }
45 while ((c = getc(file)) != EOF) 45 while ((c = getc(file)) != EOF)
46 putc(c, stdout); 46 putc(c, stdout);
@@ -50,5 +50,5 @@ extern int cat_more_main(int argc, char **argv)
50 argc--; 50 argc--;
51 argv++; 51 argv++;
52 } 52 }
53 return 0; 53 return(TRUE);
54} 54}
diff --git a/chgrp.c b/chgrp.c
deleted file mode 100644
index 038c665dd..000000000
--- a/chgrp.c
+++ /dev/null
@@ -1,89 +0,0 @@
1/*
2 * Mini chgrp 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
22#include "internal.h"
23#include <grp.h>
24#include <stdio.h>
25
26const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n"
27 "Change the group membership of each FILE to GROUP.\n"
28 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
29
30int chgrp_main(int argc, char **argv)
31{
32 const char *cp;
33 int gid;
34 struct group *grp;
35 struct stat statBuf;
36
37 if (argc < 2) {
38 fprintf(stderr, "Usage: %s %s", *argv, chgrp_usage);
39 return 1;
40 }
41 argc--;
42 argv++;
43
44 cp = argv[1];
45 if (isDecimal(*cp)) {
46 gid = 0;
47 while (isDecimal(*cp))
48 gid = gid * 10 + (*cp++ - '0');
49 if (*cp) {
50 fprintf(stderr, "Bad gid value\n");
51 return -1;
52 }
53 } else {
54 grp = getgrnam(cp);
55 if (grp == NULL) {
56 fprintf(stderr, "Unknown group name\n");
57 return -1;
58 }
59 gid = grp->gr_gid;
60 }
61 argc--;
62 argv++;
63 while (argc-- > 1) {
64 argv++;
65 if ((stat(*argv, &statBuf) < 0) ||
66 (chown(*argv, statBuf.st_uid, gid) < 0)) {
67 perror(*argv);
68 }
69 }
70 return 1;
71}
72
73
74
75
76
77
78
79
80
81#if 0
82int
83recursive(const char *fileName, BOOL followLinks, const char *pattern,
84 int (*fileAction) (const char *fileName,
85 const struct stat * statbuf),
86 int (*dirAction) (const char *fileName,
87 const struct stat * statbuf))
88
89#endif
diff --git a/chown.c b/chown.c
index a611f92f1..fc0c2424f 100644
--- a/chown.c
+++ b/chown.c
@@ -1,63 +1,125 @@
1#include "internal.h" 1/*
2#include <pwd.h> 2 * Mini chown/chgrp implementation for busybox
3#include <grp.h> 3 *
4#include <string.h> 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
5#include <stdio.h> 22#include <stdio.h>
23#include <grp.h>
24#include <pwd.h>
25#include "internal.h"
6 26
7const char chown_usage[] = "chown [-R] user-name file [file ...]\n"
8"\n\tThe group list is kept in the file /etc/groups.\n\n"
9"\t-R:\tRecursively change the mode of all files and directories\n"
10"\t\tunder the argument directory.";
11 27
12int 28static int uid=-1;
13parse_user_name(const char * s, struct FileInfo * i) 29static int gid=0;
14{ 30static int chownApp;
15 struct passwd * p; 31static char* invocationName=NULL;
16 char * dot = strchr(s, '.');
17 32
18 if (! dot )
19 dot = strchr(s, ':');
20 33
21 if ( dot ) 34const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
22 *dot = '\0'; 35 "Change the group membership of each FILE to GROUP.\n"
36 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
37const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n"
38 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
39 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
23 40
24 if ( (p = getpwnam(s)) == 0 ) { 41
25 fprintf(stderr, "%s: no such user.\n", s); 42
26 return 1; 43static int fileAction(const char *fileName)
27 } 44{
28 i->userID = p->pw_uid; 45 struct stat statBuf;
29 46 if ((stat(fileName, &statBuf) < 0) ||
30 if ( dot ) { 47 (chown(fileName,
31 struct group * g = getgrnam(++dot); 48 ((chownApp==TRUE)? uid: statBuf.st_uid),
32 if ( g == 0 ) { 49 gid) < 0)) {
33 fprintf(stderr, "%s: no such group.\n", dot); 50 perror(fileName);
34 return 1; 51 return( TRUE);
35 } 52 }
36 i->groupID = g->gr_gid; 53 return( FALSE);
37 i->changeGroupID = 1;
38 }
39 return 0;
40} 54}
41 55
42extern int 56int chown_main(int argc, char **argv)
43chown_main(struct FileInfo * i, int argc, char * * argv)
44{ 57{
45 int status; 58 struct group *grp;
59 struct passwd *pwd;
60 int recursiveFlag=FALSE;
61 char *groupName;
46 62
47 while ( argc >= 3 && strcmp("-R", argv[1]) == 0 ) {
48 i->recursive = 1;
49 argc--;
50 argv++;
51 }
52 63
53 if ( (status = parse_user_name(argv[1], i)) != 0 ) 64 chownApp = (strcmp(*argv, "chown")==0)? TRUE : FALSE;
54 return status;
55 65
56 argv++; 66 if (argc < 2) {
67 fprintf(stderr, "Usage: %s %s", *argv,
68 (chownApp==TRUE)? chown_usage : chgrp_usage);
69 return( FALSE);
70 }
71 invocationName=*argv;
72 argc--;
73 argv++;
74
75 /* Parse options */
76 while (**argv == '-') {
77 while (*++(*argv)) switch (**argv) {
78 case 'R':
79 recursiveFlag = TRUE;
80 break;
81 default:
82 fprintf(stderr, "Unknown option: %c\n", **argv);
83 return( FALSE);
84 }
57 argc--; 85 argc--;
86 argv++;
87 }
88
89 /* Find the selected group */
90 groupName = strchr(*argv, '.');
91 if ( chownApp==TRUE && groupName )
92 *groupName++ = '\0';
93 else
94 groupName = *argv;
95 grp = getgrnam(groupName);
96 if (grp == NULL) {
97 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
98 return( FALSE);
99 }
100 gid = grp->gr_gid;
58 101
59 i->changeUserID = 1; 102 /* Find the selected user (if appropriate) */
60 i->complainInPostProcess = 1; 103 if (chownApp==TRUE) {
104 pwd = getpwnam(*argv);
105 if (pwd == NULL) {
106 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
107 return( FALSE);
108 }
109 uid = pwd->pw_uid;
110 }
61 111
62 return monadic_main(i, argc, argv); 112 /* Ok, ready to do the deed now */
113 if (argc <= 1) {
114 fprintf(stderr, "%s: too few arguments", invocationName);
115 return( FALSE);
116 }
117 while (argc-- > 1) {
118 argv++;
119 if (recursiveFlag==TRUE)
120 recursiveAction( *argv, TRUE, fileAction, fileAction);
121 else
122 fileAction( *argv);
123 }
124 return(TRUE);
63} 125}
diff --git a/chroot.c b/chroot.c
index ca0bfcf3f..d39549496 100644
--- a/chroot.c
+++ b/chroot.c
@@ -1,32 +1,65 @@
1/*
2 * Mini chroot 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 <unistd.h> 24#include <unistd.h>
4 25
5 26
6const char chroot_usage[] = "chroot directory [command]\n" 27static const char chroot_usage[] = "NEWROOT [COMMAND...]\n"
7 "Run a command with special root directory.\n"; 28"Run COMMAND with root directory set to NEWROOT.\n";
29
30
8 31
9extern int 32int chroot_main(int argc, char **argv)
10chroot_main (struct FileInfo *i, int argc, char **argv)
11{ 33{
12 char *prog; 34 if (argc < 2) {
35 fprintf(stderr, "Usage: %s %s", *argv, chroot_usage);
36 return( FALSE);
37 }
38 argc--;
39 argv++;
13 40
14 if (chroot (argv[1])) 41 fprintf(stderr, "new root: %s\n", *argv);
15 { 42
16 name_and_error ("cannot chroot to that directory"); 43 if (chroot (*argv) || (chdir ("/"))) {
17 return 1; 44 perror("cannot chroot");
45 return( FALSE);
18 } 46 }
19 if (argc > 2) 47
20 { 48 argc--;
21 execvp (argv[2], argv + 2); 49 argv++;
50 if (argc >= 1) {
51 fprintf(stderr, "command: %s\n", *argv);
52 execvp (*argv, argv);
22 } 53 }
23 else 54 else {
24 { 55 char *prog;
25 prog = getenv ("SHELL"); 56 prog = getenv ("SHELL");
26 if (!prog) 57 if (!prog)
27 prog = "/bin/sh"; 58 prog = "/bin/sh";
28 execlp (prog, prog, NULL); 59 fprintf(stderr, "no command. running: %s\n", prog);
60 execlp (prog, prog, NULL);
29 } 61 }
30 name_and_error ("cannot exec"); 62 perror("cannot exec");
31 return 1; 63 return(FALSE);
32} 64}
65
diff --git a/clear.c b/clear.c
index 21a890c9e..c0c94d06e 100644
--- a/clear.c
+++ b/clear.c
@@ -1,12 +1,30 @@
1/*
2 * Mini clear 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 24
4const char clear_usage[] = "clear\n"
5"\n"
6"\tClears the screen.\n";
7 25
8extern int 26extern int
9clear_main(struct FileInfo * i, int argc, char * * argv) 27clear_main(int argc, char** argv)
10{ 28{
11 printf("\033[H\033[J"); 29 printf("\033[H\033[J");
12 return 0; 30 return 0;
diff --git a/console-tools/clear.c b/console-tools/clear.c
index 21a890c9e..c0c94d06e 100644
--- a/console-tools/clear.c
+++ b/console-tools/clear.c
@@ -1,12 +1,30 @@
1/*
2 * Mini clear 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 24
4const char clear_usage[] = "clear\n"
5"\n"
6"\tClears the screen.\n";
7 25
8extern int 26extern int
9clear_main(struct FileInfo * i, int argc, char * * argv) 27clear_main(int argc, char** argv)
10{ 28{
11 printf("\033[H\033[J"); 29 printf("\033[H\033[J");
12 return 0; 30 return 0;
diff --git a/coreutils/cat.c b/coreutils/cat.c
index 12faf55ab..0f2460eb7 100644
--- a/coreutils/cat.c
+++ b/coreutils/cat.c
@@ -31,7 +31,7 @@ extern int cat_more_main(int argc, char **argv)
31 31
32 if (argc < 2) { 32 if (argc < 2) {
33 fprintf(stderr, "Usage: %s %s", *argv, cat_usage); 33 fprintf(stderr, "Usage: %s %s", *argv, cat_usage);
34 return 1; 34 return(FALSE);
35 } 35 }
36 argc--; 36 argc--;
37 argv++; 37 argv++;
@@ -39,8 +39,8 @@ extern int cat_more_main(int argc, char **argv)
39 while (argc-- > 0) { 39 while (argc-- > 0) {
40 file = fopen(*argv, "r"); 40 file = fopen(*argv, "r");
41 if (file == NULL) { 41 if (file == NULL) {
42 name_and_error(*argv); 42 perror(*argv);
43 return 1; 43 return(FALSE);
44 } 44 }
45 while ((c = getc(file)) != EOF) 45 while ((c = getc(file)) != EOF)
46 putc(c, stdout); 46 putc(c, stdout);
@@ -50,5 +50,5 @@ extern int cat_more_main(int argc, char **argv)
50 argc--; 50 argc--;
51 argv++; 51 argv++;
52 } 52 }
53 return 0; 53 return(TRUE);
54} 54}
diff --git a/coreutils/chgrp.c b/coreutils/chgrp.c
deleted file mode 100644
index 038c665dd..000000000
--- a/coreutils/chgrp.c
+++ /dev/null
@@ -1,89 +0,0 @@
1/*
2 * Mini chgrp 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
22#include "internal.h"
23#include <grp.h>
24#include <stdio.h>
25
26const char chgrp_usage[] = "chgrp [OPTION]... GROUP FILE...\n"
27 "Change the group membership of each FILE to GROUP.\n"
28 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
29
30int chgrp_main(int argc, char **argv)
31{
32 const char *cp;
33 int gid;
34 struct group *grp;
35 struct stat statBuf;
36
37 if (argc < 2) {
38 fprintf(stderr, "Usage: %s %s", *argv, chgrp_usage);
39 return 1;
40 }
41 argc--;
42 argv++;
43
44 cp = argv[1];
45 if (isDecimal(*cp)) {
46 gid = 0;
47 while (isDecimal(*cp))
48 gid = gid * 10 + (*cp++ - '0');
49 if (*cp) {
50 fprintf(stderr, "Bad gid value\n");
51 return -1;
52 }
53 } else {
54 grp = getgrnam(cp);
55 if (grp == NULL) {
56 fprintf(stderr, "Unknown group name\n");
57 return -1;
58 }
59 gid = grp->gr_gid;
60 }
61 argc--;
62 argv++;
63 while (argc-- > 1) {
64 argv++;
65 if ((stat(*argv, &statBuf) < 0) ||
66 (chown(*argv, statBuf.st_uid, gid) < 0)) {
67 perror(*argv);
68 }
69 }
70 return 1;
71}
72
73
74
75
76
77
78
79
80
81#if 0
82int
83recursive(const char *fileName, BOOL followLinks, const char *pattern,
84 int (*fileAction) (const char *fileName,
85 const struct stat * statbuf),
86 int (*dirAction) (const char *fileName,
87 const struct stat * statbuf))
88
89#endif
diff --git a/coreutils/chown.c b/coreutils/chown.c
index a611f92f1..fc0c2424f 100644
--- a/coreutils/chown.c
+++ b/coreutils/chown.c
@@ -1,63 +1,125 @@
1#include "internal.h" 1/*
2#include <pwd.h> 2 * Mini chown/chgrp implementation for busybox
3#include <grp.h> 3 *
4#include <string.h> 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
5#include <stdio.h> 22#include <stdio.h>
23#include <grp.h>
24#include <pwd.h>
25#include "internal.h"
6 26
7const char chown_usage[] = "chown [-R] user-name file [file ...]\n"
8"\n\tThe group list is kept in the file /etc/groups.\n\n"
9"\t-R:\tRecursively change the mode of all files and directories\n"
10"\t\tunder the argument directory.";
11 27
12int 28static int uid=-1;
13parse_user_name(const char * s, struct FileInfo * i) 29static int gid=0;
14{ 30static int chownApp;
15 struct passwd * p; 31static char* invocationName=NULL;
16 char * dot = strchr(s, '.');
17 32
18 if (! dot )
19 dot = strchr(s, ':');
20 33
21 if ( dot ) 34const char chgrp_usage[] = "[OPTION]... GROUP FILE...\n"
22 *dot = '\0'; 35 "Change the group membership of each FILE to GROUP.\n"
36 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
37const char chown_usage[] = "[OPTION]... OWNER[.[GROUP] FILE...\n"
38 "Change the owner and/or group of each FILE to OWNER and/or GROUP.\n"
39 "\n\tOptions:\n" "\t-R\tchange files and directories recursively\n";
23 40
24 if ( (p = getpwnam(s)) == 0 ) { 41
25 fprintf(stderr, "%s: no such user.\n", s); 42
26 return 1; 43static int fileAction(const char *fileName)
27 } 44{
28 i->userID = p->pw_uid; 45 struct stat statBuf;
29 46 if ((stat(fileName, &statBuf) < 0) ||
30 if ( dot ) { 47 (chown(fileName,
31 struct group * g = getgrnam(++dot); 48 ((chownApp==TRUE)? uid: statBuf.st_uid),
32 if ( g == 0 ) { 49 gid) < 0)) {
33 fprintf(stderr, "%s: no such group.\n", dot); 50 perror(fileName);
34 return 1; 51 return( TRUE);
35 } 52 }
36 i->groupID = g->gr_gid; 53 return( FALSE);
37 i->changeGroupID = 1;
38 }
39 return 0;
40} 54}
41 55
42extern int 56int chown_main(int argc, char **argv)
43chown_main(struct FileInfo * i, int argc, char * * argv)
44{ 57{
45 int status; 58 struct group *grp;
59 struct passwd *pwd;
60 int recursiveFlag=FALSE;
61 char *groupName;
46 62
47 while ( argc >= 3 && strcmp("-R", argv[1]) == 0 ) {
48 i->recursive = 1;
49 argc--;
50 argv++;
51 }
52 63
53 if ( (status = parse_user_name(argv[1], i)) != 0 ) 64 chownApp = (strcmp(*argv, "chown")==0)? TRUE : FALSE;
54 return status;
55 65
56 argv++; 66 if (argc < 2) {
67 fprintf(stderr, "Usage: %s %s", *argv,
68 (chownApp==TRUE)? chown_usage : chgrp_usage);
69 return( FALSE);
70 }
71 invocationName=*argv;
72 argc--;
73 argv++;
74
75 /* Parse options */
76 while (**argv == '-') {
77 while (*++(*argv)) switch (**argv) {
78 case 'R':
79 recursiveFlag = TRUE;
80 break;
81 default:
82 fprintf(stderr, "Unknown option: %c\n", **argv);
83 return( FALSE);
84 }
57 argc--; 85 argc--;
86 argv++;
87 }
88
89 /* Find the selected group */
90 groupName = strchr(*argv, '.');
91 if ( chownApp==TRUE && groupName )
92 *groupName++ = '\0';
93 else
94 groupName = *argv;
95 grp = getgrnam(groupName);
96 if (grp == NULL) {
97 fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
98 return( FALSE);
99 }
100 gid = grp->gr_gid;
58 101
59 i->changeUserID = 1; 102 /* Find the selected user (if appropriate) */
60 i->complainInPostProcess = 1; 103 if (chownApp==TRUE) {
104 pwd = getpwnam(*argv);
105 if (pwd == NULL) {
106 fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
107 return( FALSE);
108 }
109 uid = pwd->pw_uid;
110 }
61 111
62 return monadic_main(i, argc, argv); 112 /* Ok, ready to do the deed now */
113 if (argc <= 1) {
114 fprintf(stderr, "%s: too few arguments", invocationName);
115 return( FALSE);
116 }
117 while (argc-- > 1) {
118 argv++;
119 if (recursiveFlag==TRUE)
120 recursiveAction( *argv, TRUE, fileAction, fileAction);
121 else
122 fileAction( *argv);
123 }
124 return(TRUE);
63} 125}
diff --git a/coreutils/chroot.c b/coreutils/chroot.c
index ca0bfcf3f..d39549496 100644
--- a/coreutils/chroot.c
+++ b/coreutils/chroot.c
@@ -1,32 +1,65 @@
1/*
2 * Mini chroot 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 <unistd.h> 24#include <unistd.h>
4 25
5 26
6const char chroot_usage[] = "chroot directory [command]\n" 27static const char chroot_usage[] = "NEWROOT [COMMAND...]\n"
7 "Run a command with special root directory.\n"; 28"Run COMMAND with root directory set to NEWROOT.\n";
29
30
8 31
9extern int 32int chroot_main(int argc, char **argv)
10chroot_main (struct FileInfo *i, int argc, char **argv)
11{ 33{
12 char *prog; 34 if (argc < 2) {
35 fprintf(stderr, "Usage: %s %s", *argv, chroot_usage);
36 return( FALSE);
37 }
38 argc--;
39 argv++;
13 40
14 if (chroot (argv[1])) 41 fprintf(stderr, "new root: %s\n", *argv);
15 { 42
16 name_and_error ("cannot chroot to that directory"); 43 if (chroot (*argv) || (chdir ("/"))) {
17 return 1; 44 perror("cannot chroot");
45 return( FALSE);
18 } 46 }
19 if (argc > 2) 47
20 { 48 argc--;
21 execvp (argv[2], argv + 2); 49 argv++;
50 if (argc >= 1) {
51 fprintf(stderr, "command: %s\n", *argv);
52 execvp (*argv, argv);
22 } 53 }
23 else 54 else {
24 { 55 char *prog;
25 prog = getenv ("SHELL"); 56 prog = getenv ("SHELL");
26 if (!prog) 57 if (!prog)
27 prog = "/bin/sh"; 58 prog = "/bin/sh";
28 execlp (prog, prog, NULL); 59 fprintf(stderr, "no command. running: %s\n", prog);
60 execlp (prog, prog, NULL);
29 } 61 }
30 name_and_error ("cannot exec"); 62 perror("cannot exec");
31 return 1; 63 return(FALSE);
32} 64}
65
diff --git a/internal.h b/internal.h
index e658d3b7d..75e5503cf 100644
--- a/internal.h
+++ b/internal.h
@@ -1,3 +1,26 @@
1/*
2 * Busybox main header file
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 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
21 * Permission has been granted to redistribute this code under the GPL.
22 *
23 */
1#ifndef _INTERNAL_H_ 24#ifndef _INTERNAL_H_
2#define _INTERNAL_H_ 25#define _INTERNAL_H_
3 26
@@ -13,8 +36,8 @@
13typedef int BOOL; 36typedef int BOOL;
14#define STDIN 0 37#define STDIN 0
15#define STDOUT 1 38#define STDOUT 1
16#define FALSE ((BOOL) 0) 39#define FALSE ((BOOL) 1)
17#define TRUE ((BOOL) 1) 40#define TRUE ((BOOL) 0)
18 41
19#define PATH_LEN 1024 42#define PATH_LEN 1024
20#define BUF_SIZE 8192 43#define BUF_SIZE 8192
@@ -129,60 +152,36 @@ parse_mode(
129 152
130extern int parse_user_name(const char* string, struct FileInfo * i); 153extern int parse_user_name(const char* string, struct FileInfo * i);
131 154
132extern const char block_device_usage[];
133extern const char chgrp_usage[];
134extern const char chmod_usage[];
135extern const char chown_usage[];
136extern const char chroot_usage[];
137extern const char clear_usage[];
138extern const char cp_usage[];
139extern const char date_usage[];
140extern const char dd_usage[];
141extern const char df_usage[];
142extern const char dmesg_usage[];
143extern const char dutmp_usage[];
144extern const char false_usage[];
145extern const char fdflush_usage[];
146extern const char find_usage[];
147extern const char grep_usage[];
148extern const char halt_usage[];
149extern const char init_usage[];
150extern const char kill_usage[];
151extern const char length_usage[];
152extern const char ln_usage[];
153extern const char loadkmap_usage[];
154extern const char losetup_usage[];
155extern const char ls_usage[];
156extern const char math_usage[];
157extern const char makedevs_usage[];
158extern const char mkdir_usage[];
159extern const char mknod_usage[];
160extern const char mkswap_usage[];
161extern const char mnc_usage[];
162extern const char more_usage[];
163extern const char mount_usage[];
164extern const char mt_usage[];
165extern const char mv_usage[];
166extern const char printf_usage[];
167extern const char pwd_usage[];
168extern const char reboot_usage[];
169extern const char rm_usage[];
170extern const char rmdir_usage[];
171extern const char scan_partitions_usage[];
172extern const char sleep_usage[];
173extern const char tar_usage[];
174extern const char swapoff_usage[];
175extern const char swapon_usage[];
176extern const char sync_usage[];
177extern const char touch_usage[];
178extern const char tput_usage[];
179extern const char true_usage[];
180extern const char tryopen_usage[];
181extern const char umount_usage[];
182extern const char update_usage[];
183extern const char zcat_usage[];
184extern const char gzip_usage[];
185 155
156/*
157 * A chunk of data.
158 * Chunks contain data which is allocated as needed, but which is
159 * not freed until all of the data needs freeing, such as at
160 * the beginning of the next command.
161 */
162typedef struct chunk CHUNK;
163#define CHUNK_INIT_SIZE 4
164
165struct chunk {
166 CHUNK *next;
167 char data[CHUNK_INIT_SIZE]; /* actually of varying length */
168};
169
170const char *modeString(int mode);
171const char *timeString(time_t timeVal);
172BOOL isDirectory(const char *name);
173BOOL isDevice(const char *name);
174BOOL copyFile(const char *srcName, const char *destName, BOOL setModes);
175const char *buildName(const char *dirName, const char *fileName);
176BOOL makeString(int argc, const char **argv, char *buf, int bufLen);
177char *getChunk(int size);
178char *chunkstrdup(const char *str);
179void freeChunks(void);
180int fullWrite(int fd, const char *buf, int len);
181int fullRead(int fd, char *buf, int len);
182int recursiveAction(const char *fileName, BOOL followLinks,
183 int (*fileAction) (const char *fileName),
184 int (*dirAction) (const char *fileName));
186 185
187 186
188#endif 187#endif
diff --git a/utility.c b/utility.c
index 68259710b..a516355b6 100644
--- a/utility.c
+++ b/utility.c
@@ -22,7 +22,16 @@
22 * 22 *
23 */ 23 */
24 24
25#include "utility.h" 25#include "internal.h"
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29#include <fcntl.h>
30//#include <sys/types.h>
31//#include <sys/stat.h>
32#include <dirent.h>
33#include <time.h>
34#include <utime.h>
26 35
27#if 0 36#if 0
28 37
@@ -54,13 +63,6 @@ join_paths(char * buffer, const char * a, const char * b)
54 63
55static CHUNK * chunkList; 64static CHUNK * chunkList;
56 65
57extern void
58name_and_error(const char * name)
59{
60 fprintf(stderr, "%s: %s\n", name, strerror(errno));
61}
62
63
64 66
65/* 67/*
66 * Return the standard ls-like mode string from a file mode. 68 * Return the standard ls-like mode string from a file mode.
@@ -130,36 +132,6 @@ modeString(int mode)
130 132
131 133
132/* 134/*
133 * Get the time string to be used for a file.
134 * This is down to the minute for new files, but only the date for old files.
135 * The string is returned from a static buffer, and so is overwritten for
136 * each call.
137 */
138const char *
139timeString(time_t timeVal)
140{
141 time_t now;
142 char * str;
143 static char buf[26];
144
145 time(&now);
146
147 str = ctime(&timeVal);
148
149 strcpy(buf, &str[4]);
150 buf[12] = '\0';
151
152 if ((timeVal > now) || (timeVal < now - 365*24*60*60L))
153 {
154 strcpy(&buf[7], &str[20]);
155 buf[11] = '\0';
156 }
157
158 return buf;
159}
160
161
162/*
163 * Return TRUE if a fileName is a directory. 135 * Return TRUE if a fileName is a directory.
164 * Nonexistant files return FALSE. 136 * Nonexistant files return FALSE.
165 */ 137 */
@@ -325,624 +297,6 @@ buildName(const char * dirName, const char * fileName)
325 297
326 298
327/* 299/*
328 * Expand the wildcards in a fileName wildcard pattern, if any.
329 * Returns an argument list with matching fileNames in sorted order.
330 * The expanded names are stored in memory chunks which can later all
331 * be freed at once. The returned list is only valid until the next
332 * call or until the next command. Returns zero if the name is not a
333 * wildcard, or returns the count of matched files if the name is a
334 * wildcard and there was at least one match, or returns -1 if either
335 * no fileNames matched or there was an allocation error.
336 */
337int
338expandWildCards(const char * fileNamePattern, const char *** retFileTable)
339{
340 const char * last;
341 const char * cp1;
342 const char * cp2;
343 const char * cp3;
344 char * str;
345 DIR * dirp;
346 struct dirent * dp;
347 int dirLen;
348 int newFileTableSize;
349 char ** newFileTable;
350 char dirName[PATH_LEN];
351
352 static int fileCount;
353 static int fileTableSize;
354 static char ** fileTable;
355
356 /*
357 * Clear the return values until we know their final values.
358 */
359 fileCount = 0;
360 *retFileTable = NULL;
361
362 /*
363 * Scan the file name pattern for any wildcard characters.
364 */
365 cp1 = strchr(fileNamePattern, '*');
366 cp2 = strchr(fileNamePattern, '?');
367 cp3 = strchr(fileNamePattern, '[');
368
369 /*
370 * If there are no wildcard characters then return zero to
371 * indicate that there was actually no wildcard pattern.
372 */
373 if ((cp1 == NULL) && (cp2 == NULL) && (cp3 == NULL))
374 return 0;
375
376 /*
377 * There are wildcards in the specified filename.
378 * Get the last component of the file name.
379 */
380 last = strrchr(fileNamePattern, '/');
381
382 if (last)
383 last++;
384 else
385 last = fileNamePattern;
386
387 /*
388 * If any wildcards were found before the last filename component
389 * then return an error.
390 */
391 if ((cp1 && (cp1 < last)) || (cp2 && (cp2 < last)) ||
392 (cp3 && (cp3 < last)))
393 {
394 fprintf(stderr,
395 "Wildcards only implemented for last file name component\n");
396
397 return -1;
398 }
399
400 /*
401 * Assume at first that we are scanning the current directory.
402 */
403 dirName[0] = '.';
404 dirName[1] = '\0';
405
406 /*
407 * If there was a directory given as part of the file name then
408 * copy it and null terminate it.
409 */
410 if (last != fileNamePattern)
411 {
412 memcpy(dirName, fileNamePattern, last - fileNamePattern);
413 dirName[last - fileNamePattern - 1] = '\0';
414
415 if (dirName[0] == '\0')
416 {
417 dirName[0] = '/';
418 dirName[1] = '\0';
419 }
420 }
421
422 /*
423 * Open the directory containing the files to be checked.
424 */
425 dirp = opendir(dirName);
426
427 if (dirp == NULL)
428 {
429 perror(dirName);
430
431 return -1;
432 }
433
434 /*
435 * Prepare the directory name for use in making full path names.
436 */
437 dirLen = strlen(dirName);
438
439 if (last == fileNamePattern)
440 {
441 dirLen = 0;
442 dirName[0] = '\0';
443 }
444 else if (dirName[dirLen - 1] != '/')
445 {
446 dirName[dirLen++] = '/';
447 dirName[dirLen] = '\0';
448 }
449
450 /*
451 * Find all of the files in the directory and check them against
452 * the wildcard pattern.
453 */
454 while ((dp = readdir(dirp)) != NULL)
455 {
456 /*
457 * Skip the current and parent directories.
458 */
459 if ((strcmp(dp->d_name, ".") == 0) ||
460 (strcmp(dp->d_name, "..") == 0))
461 {
462 continue;
463 }
464
465 /*
466 * If the file name doesn't match the pattern then skip it.
467 */
468 if (!match(dp->d_name, last))
469 continue;
470
471 /*
472 * This file name is selected.
473 * See if we need to reallocate the file name table.
474 */
475 if (fileCount >= fileTableSize)
476 {
477 /*
478 * Increment the file table size and reallocate it.
479 */
480 newFileTableSize = fileTableSize + EXPAND_ALLOC;
481
482 newFileTable = (char **) realloc((char *) fileTable,
483 (newFileTableSize * sizeof(char *)));
484
485 if (newFileTable == NULL)
486 {
487 fprintf(stderr, "Cannot allocate file list\n");
488 closedir(dirp);
489
490 return -1;
491 }
492
493 fileTable = newFileTable;
494 fileTableSize = newFileTableSize;
495 }
496
497 /*
498 * Allocate space for storing the file name in a chunk.
499 */
500 str = getChunk(dirLen + strlen(dp->d_name) + 1);
501
502 if (str == NULL)
503 {
504 fprintf(stderr, "No memory for file name\n");
505 closedir(dirp);
506
507 return -1;
508 }
509
510 /*
511 * Save the file name in the chunk.
512 */
513 if (dirLen)
514 memcpy(str, dirName, dirLen);
515
516 strcpy(str + dirLen, dp->d_name);
517
518 /*
519 * Save the allocated file name into the file table.
520 */
521 fileTable[fileCount++] = str;
522 }
523
524 /*
525 * Close the directory and check for any matches.
526 */
527 closedir(dirp);
528
529 if (fileCount == 0)
530 {
531 fprintf(stderr, "No matches\n");
532
533 return -1;
534 }
535
536 /*
537 * Sort the list of file names.
538 */
539 qsort((void *) fileTable, fileCount, sizeof(char *), nameSort);
540
541 /*
542 * Return the file list and count.
543 */
544 *retFileTable = (const char **) fileTable;
545
546 return fileCount;
547}
548
549
550/*
551 * Sort routine for list of fileNames.
552 */
553int
554nameSort(const void * p1, const void * p2)
555{
556 const char ** s1;
557 const char ** s2;
558
559 s1 = (const char **) p1;
560 s2 = (const char **) p2;
561
562 return strcmp(*s1, *s2);
563}
564
565
566
567/*
568 * Routine to see if a text string is matched by a wildcard pattern.
569 * Returns TRUE if the text is matched, or FALSE if it is not matched
570 * or if the pattern is invalid.
571 * * matches zero or more characters
572 * ? matches a single character
573 * [abc] matches 'a', 'b' or 'c'
574 * \c quotes character c
575 * Adapted from code written by Ingo Wilken.
576 */
577BOOL
578match(const char * text, const char * pattern)
579{
580 const char * retryPat;
581 const char * retryText;
582 int ch;
583 BOOL found;
584
585 retryPat = NULL;
586 retryText = NULL;
587
588 while (*text || *pattern)
589 {
590 ch = *pattern++;
591
592 switch (ch)
593 {
594 case '*':
595 retryPat = pattern;
596 retryText = text;
597 break;
598
599 case '[':
600 found = FALSE;
601
602 while ((ch = *pattern++) != ']')
603 {
604 if (ch == '\\')
605 ch = *pattern++;
606
607 if (ch == '\0')
608 return FALSE;
609
610 if (*text == ch)
611 found = TRUE;
612 }
613
614 if (!found)
615 {
616 pattern = retryPat;
617 text = ++retryText;
618 }
619
620 /* fall into next case */
621
622 case '?':
623 if (*text++ == '\0')
624 return FALSE;
625
626 break;
627
628 case '\\':
629 ch = *pattern++;
630
631 if (ch == '\0')
632 return FALSE;
633
634 /* fall into next case */
635
636 default:
637 if (*text == ch)
638 {
639 if (*text)
640 text++;
641 break;
642 }
643
644 if (*text)
645 {
646 pattern = retryPat;
647 text = ++retryText;
648 break;
649 }
650
651 return FALSE;
652 }
653
654 if (pattern == NULL)
655 return FALSE;
656 }
657
658 return TRUE;
659}
660
661
662/*
663 * Take a command string and break it up into an argc, argv list while
664 * handling quoting and wildcards. The returned argument list and
665 * strings are in static memory, and so are overwritten on each call.
666 * The argument list is ended with a NULL pointer for convenience.
667 * Returns TRUE if successful, or FALSE on an error with a message
668 * already output.
669 */
670BOOL
671makeArgs(const char * cmd, int * retArgc, const char *** retArgv)
672{
673 const char * argument;
674 char * cp;
675 char * cpOut;
676 char * newStrings;
677 const char ** fileTable;
678 const char ** newArgTable;
679 int newArgTableSize;
680 int fileCount;
681 int len;
682 int ch;
683 int quote;
684 BOOL quotedWildCards;
685 BOOL unquotedWildCards;
686
687 static int stringsLength;
688 static char * strings;
689 static int argCount;
690 static int argTableSize;
691 static const char ** argTable;
692
693 /*
694 * Clear the returned values until we know them.
695 */
696 argCount = 0;
697 *retArgc = 0;
698 *retArgv = NULL;
699
700 /*
701 * Copy the command string into a buffer that we can modify,
702 * reallocating it if necessary.
703 */
704 len = strlen(cmd) + 1;
705
706 if (len > stringsLength)
707 {
708 newStrings = realloc(strings, len);
709
710 if (newStrings == NULL)
711 {
712 fprintf(stderr, "Cannot allocate string\n");
713
714 return FALSE;
715 }
716
717 strings = newStrings;
718 stringsLength = len;
719 }
720
721 memcpy(strings, cmd, len);
722 cp = strings;
723
724 /*
725 * Keep parsing the command string as long as there are any
726 * arguments left.
727 */
728 while (*cp)
729 {
730 /*
731 * Save the beginning of this argument.
732 */
733 argument = cp;
734 cpOut = cp;
735
736 /*
737 * Reset quoting and wildcarding for this argument.
738 */
739 quote = '\0';
740 quotedWildCards = FALSE;
741 unquotedWildCards = FALSE;
742
743 /*
744 * Loop over the string collecting the next argument while
745 * looking for quoted strings or quoted characters, and
746 * remembering whether there are any wildcard characters
747 * in the argument.
748 */
749 while (*cp)
750 {
751 ch = *cp++;
752
753 /*
754 * If we are not in a quote and we see a blank then
755 * this argument is done.
756 */
757 if (isBlank(ch) && (quote == '\0'))
758 break;
759
760 /*
761 * If we see a backslash then accept the next
762 * character no matter what it is.
763 */
764 if (ch == '\\')
765 {
766 ch = *cp++;
767
768 /*
769 * Make sure there is a next character.
770 */
771 if (ch == '\0')
772 {
773 fprintf(stderr,
774 "Bad quoted character\n");
775
776 return FALSE;
777 }
778
779 /*
780 * Remember whether the quoted character
781 * is a wildcard.
782 */
783 if (isWildCard(ch))
784 quotedWildCards = TRUE;
785
786 *cpOut++ = ch;
787
788 continue;
789 }
790
791 /*
792 * If we see one of the wildcard characters then
793 * remember whether it was seen inside or outside
794 * of quotes.
795 */
796 if (isWildCard(ch))
797 {
798 if (quote)
799 quotedWildCards = TRUE;
800 else
801 unquotedWildCards = TRUE;
802 }
803
804 /*
805 * If we were in a quote and we saw the same quote
806 * character again then the quote is done.
807 */
808 if (ch == quote)
809 {
810 quote = '\0';
811
812 continue;
813 }
814
815 /*
816 * If we weren't in a quote and we see either type
817 * of quote character, then remember that we are
818 * now inside of a quote.
819 */
820 if ((quote == '\0') && ((ch == '\'') || (ch == '"')))
821 {
822 quote = ch;
823
824 continue;
825 }
826
827 /*
828 * Store the character.
829 */
830 *cpOut++ = ch;
831 }
832
833 /*
834 * Make sure that quoting is terminated properly.
835 */
836 if (quote)
837 {
838 fprintf(stderr, "Unmatched quote character\n");
839
840 return FALSE;
841 }
842
843 /*
844 * Null terminate the argument if it had shrunk, and then
845 * skip over all blanks to the next argument, nulling them
846 * out too.
847 */
848 if (cp != cpOut)
849 *cpOut = '\0';
850
851 while (isBlank(*cp))
852 *cp++ = '\0';
853
854 /*
855 * If both quoted and unquoted wildcards were used then
856 * complain since we don't handle them properly.
857 */
858 if (quotedWildCards && unquotedWildCards)
859 {
860 fprintf(stderr,
861 "Cannot use quoted and unquoted wildcards\n");
862
863 return FALSE;
864 }
865
866 /*
867 * Expand the argument into the matching filenames or accept
868 * it as is depending on whether there were any unquoted
869 * wildcard characters in it.
870 */
871 if (unquotedWildCards)
872 {
873 /*
874 * Expand the argument into the matching filenames.
875 */
876 fileCount = expandWildCards(argument, &fileTable);
877
878 /*
879 * Return an error if the wildcards failed to match.
880 */
881 if (fileCount < 0)
882 return FALSE;
883
884 if (fileCount == 0)
885 {
886 fprintf(stderr, "Wildcard expansion error\n");
887
888 return FALSE;
889 }
890 }
891 else
892 {
893 /*
894 * Set up to only store the argument itself.
895 */
896 fileTable = &argument;
897 fileCount = 1;
898 }
899
900 /*
901 * Now reallocate the argument table to hold the file name.
902 */
903 if (argCount + fileCount >= argTableSize)
904 {
905 newArgTableSize = argCount + fileCount + 1;
906
907 newArgTable = (const char **) realloc(argTable,
908 (sizeof(const char *) * newArgTableSize));
909
910 if (newArgTable == NULL)
911 {
912 fprintf(stderr, "No memory for arg list\n");
913
914 return FALSE;
915 }
916
917 argTable = newArgTable;
918 argTableSize = newArgTableSize;
919 }
920
921 /*
922 * Copy the new arguments to the end of the old ones.
923 */
924 memcpy((void *) &argTable[argCount], (const void *) fileTable,
925 (sizeof(const char **) * fileCount));
926
927 /*
928 * Add to the argument count.
929 */
930 argCount += fileCount;
931 }
932
933 /*
934 * Null terminate the argument list and return it.
935 */
936 argTable[argCount] = NULL;
937
938 *retArgc = argCount;
939 *retArgv = argTable;
940
941 return TRUE;
942}
943
944
945/*
946 * Make a NULL-terminated string out of an argc, argv pair. 300 * Make a NULL-terminated string out of an argc, argv pair.
947 * Returns TRUE if successful, or FALSE if the string is too long, 301 * Returns TRUE if successful, or FALSE if the string is too long,
948 * with an error message given. This does not handle spaces within 302 * with an error message given. This does not handle spaces within
@@ -1114,15 +468,14 @@ fullRead(int fd, char * buf, int len)
1114 468
1115 469
1116/* 470/*
1117 * Read all of the supplied buffer from a file. 471 * Walk down all the directories under the specified
1118 * This does multiple reads as necessary. 472 * location, and do something (something specified
1119 * Returns the amount read, or -1 on an error. 473 * by the fileAction and dirAction function pointers).
1120 * A short read is returned on an end of file.
1121 */ 474 */
1122int 475int
1123recursive( const char *fileName, BOOL followLinks, const char* pattern, 476recursiveAction( const char *fileName, BOOL followLinks,
1124 int (*fileAction)(const char* fileName, const struct stat* statbuf), 477 int (*fileAction)(const char* fileName),
1125 int (*dirAction)(const char* fileName, const struct stat* statbuf)) 478 int (*dirAction)(const char* fileName))
1126{ 479{
1127 int status; 480 int status;
1128 struct stat statbuf; 481 struct stat statbuf;
@@ -1135,45 +488,44 @@ recursive( const char *fileName, BOOL followLinks, const char* pattern,
1135 488
1136 if (status < 0) { 489 if (status < 0) {
1137 perror(fileName); 490 perror(fileName);
1138 return( -1); 491 return( FALSE);
1139 } 492 }
1140 493
1141 if (S_ISREG(statbuf.st_mode)) { 494 if (S_ISDIR(statbuf.st_mode)) {
1142 if (match(fileName, pattern)) { 495 DIR *dir;
1143 if (fileAction==NULL) 496 dir = opendir(fileName);
1144 fprintf( stdout, "%s\n", fileName); 497 if (!dir) {
1145 else 498 perror(fileName);
1146 return(fileAction(fileName, &statbuf)); 499 return(FALSE);
1147 } 500 }
1148 } 501 while ((next = readdir (dir)) != NULL) {
1149 else if (S_ISDIR(statbuf.st_mode)) { 502 char nextFile[NAME_MAX];
1150 if (dirAction==NULL) { 503 if ( (strcmp(next->d_name, "..") == 0) || (strcmp(next->d_name, ".") == 0) ) {
1151 DIR *dir; 504 continue;
1152 if (! match(fileName, pattern)) 505 }
1153 return 1; 506 sprintf(nextFile, "%s/%s", fileName, next->d_name);
1154 dir = opendir(fileName); 507 status = recursiveAction(nextFile, followLinks, fileAction, dirAction);
1155 if (!dir) { 508 if (status < 0) {
1156 perror(fileName); 509 closedir(dir);
1157 return( -1); 510 return(FALSE);
1158 } 511 }
1159 while ((next = readdir (dir)) != NULL) { 512 }
1160 status = recursive(fileName, followLinks, pattern, fileAction, dirAction); 513 status = closedir (dir);
1161 if (status < 0) { 514 if (status < 0) {
1162 closedir(dir); 515 perror(fileName);
1163 return(status); 516 return( FALSE);
1164 }
1165 }
1166 status = closedir (dir);
1167 if (status < 0) {
1168 perror(fileName);
1169 return( -1);
1170 }
1171 } 517 }
518 if (dirAction==NULL)
519 return(TRUE);
1172 else 520 else
1173 return(dirAction(fileName, &statbuf)); 521 return(dirAction(fileName));
522 }
523 else {
524 if (fileAction==NULL)
525 return(TRUE);
526 else
527 return(fileAction(fileName));
1174 } 528 }
1175 return( 1);
1176
1177} 529}
1178 530
1179 531
diff --git a/utility.h b/utility.h
deleted file mode 100644
index 7b275baa4..000000000
--- a/utility.h
+++ /dev/null
@@ -1,73 +0,0 @@
1/*
2 * Utility routines.
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 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
21 * Permission has been granted to redistribute this code under the GPL.
22 *
23 */
24
25#include "internal.h"
26#include <stdio.h>
27#include <string.h>
28#include <errno.h>
29#include <fcntl.h>
30//#include <sys/types.h>
31//#include <sys/stat.h>
32#include <dirent.h>
33#include <time.h>
34#include <utime.h>
35
36/*
37 * A chunk of data.
38 * Chunks contain data which is allocated as needed, but which is
39 * not freed until all of the data needs freeing, such as at
40 * the beginning of the next command.
41 */
42typedef struct chunk CHUNK;
43#define CHUNK_INIT_SIZE 4
44
45struct chunk {
46 CHUNK *next;
47 char data[CHUNK_INIT_SIZE]; /* actually of varying length */
48};
49
50const char *modeString(int mode);
51const char *timeString(time_t timeVal);
52BOOL isDirectory(const char *name);
53BOOL isDevice(const char *name);
54BOOL copyFile(const char *srcName, const char *destName, BOOL setModes);
55const char *buildName(const char *dirName, const char *fileName);
56BOOL match(const char *text, const char *pattern);
57BOOL makeArgs(const char *cmd, int *retArgc, const char ***retArgv);
58BOOL makeString(int argc, const char **argv, char *buf, int bufLen);
59char *getChunk(int size);
60char *chunkstrdup(const char *str);
61void freeChunks(void);
62int fullWrite(int fd, const char *buf, int len);
63int fullRead(int fd, char *buf, int len);
64int
65recursive(const char *fileName, BOOL followLinks, const char *pattern,
66 int (*fileAction) (const char *fileName,
67 const struct stat * statbuf),
68 int (*dirAction) (const char *fileName,
69 const struct stat * statbuf));
70
71int nameSort(const void *p1, const void *p2);
72int expandWildCards(const char *fileNamePattern,
73 const char ***retFileTable);