diff options
Diffstat (limited to 'libbb')
39 files changed, 3401 insertions, 0 deletions
diff --git a/libbb/ask_confirmation.c b/libbb/ask_confirmation.c new file mode 100644 index 000000000..f2922379c --- /dev/null +++ b/libbb/ask_confirmation.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | int ask_confirmation() | ||
33 | { | ||
34 | int c = '\0'; | ||
35 | int ret = 0; | ||
36 | |||
37 | while (c != '\n') { | ||
38 | c = getchar(); | ||
39 | if ( c != '\n' ) { | ||
40 | ret = ((c=='y')||(c=='Y')) ? 1 : 0; | ||
41 | } | ||
42 | } | ||
43 | return ret; | ||
44 | } | ||
45 | |||
46 | /* END CODE */ | ||
47 | /* | ||
48 | Local Variables: | ||
49 | c-file-style: "linux" | ||
50 | c-basic-offset: 4 | ||
51 | tab-width: 4 | ||
52 | End: | ||
53 | */ | ||
diff --git a/libbb/check_wildcard_match.c b/libbb/check_wildcard_match.c new file mode 100644 index 000000000..7bd3c7998 --- /dev/null +++ b/libbb/check_wildcard_match.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | /* | ||
33 | * Routine to see if a text string is matched by a wildcard pattern. | ||
34 | * Returns TRUE if the text is matched, or FALSE if it is not matched | ||
35 | * or if the pattern is invalid. | ||
36 | * * matches zero or more characters | ||
37 | * ? matches a single character | ||
38 | * [abc] matches 'a', 'b' or 'c' | ||
39 | * \c quotes character c | ||
40 | * Adapted from code written by Ingo Wilken, and | ||
41 | * then taken from sash, Copyright (c) 1999 by David I. Bell | ||
42 | * Permission is granted to use, distribute, or modify this source, | ||
43 | * provided that this copyright notice remains intact. | ||
44 | * Permission to distribute this code under the GPL has been granted. | ||
45 | */ | ||
46 | extern int check_wildcard_match(const char *text, const char *pattern) | ||
47 | { | ||
48 | const char *retryPat; | ||
49 | const char *retryText; | ||
50 | int ch; | ||
51 | int found; | ||
52 | int len; | ||
53 | |||
54 | retryPat = NULL; | ||
55 | retryText = NULL; | ||
56 | |||
57 | while (*text || *pattern) { | ||
58 | ch = *pattern++; | ||
59 | |||
60 | switch (ch) { | ||
61 | case '*': | ||
62 | retryPat = pattern; | ||
63 | retryText = text; | ||
64 | break; | ||
65 | |||
66 | case '[': | ||
67 | found = FALSE; | ||
68 | |||
69 | while ((ch = *pattern++) != ']') { | ||
70 | if (ch == '\\') | ||
71 | ch = *pattern++; | ||
72 | |||
73 | if (ch == '\0') | ||
74 | return FALSE; | ||
75 | |||
76 | if (*text == ch) | ||
77 | found = TRUE; | ||
78 | } | ||
79 | len=strlen(text); | ||
80 | if (found == FALSE && len!=0) { | ||
81 | return FALSE; | ||
82 | } | ||
83 | if (found == TRUE) { | ||
84 | if (strlen(pattern)==0 && len==1) { | ||
85 | return TRUE; | ||
86 | } | ||
87 | if (len!=0) { | ||
88 | text++; | ||
89 | continue; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | /* fall into next case */ | ||
94 | |||
95 | case '?': | ||
96 | if (*text++ == '\0') | ||
97 | return FALSE; | ||
98 | |||
99 | break; | ||
100 | |||
101 | case '\\': | ||
102 | ch = *pattern++; | ||
103 | |||
104 | if (ch == '\0') | ||
105 | return FALSE; | ||
106 | |||
107 | /* fall into next case */ | ||
108 | |||
109 | default: | ||
110 | if (*text == ch) { | ||
111 | if (*text) | ||
112 | text++; | ||
113 | break; | ||
114 | } | ||
115 | |||
116 | if (*text) { | ||
117 | pattern = retryPat; | ||
118 | text = ++retryText; | ||
119 | break; | ||
120 | } | ||
121 | |||
122 | return FALSE; | ||
123 | } | ||
124 | |||
125 | if (pattern == NULL) | ||
126 | return FALSE; | ||
127 | } | ||
128 | |||
129 | return TRUE; | ||
130 | } | ||
131 | |||
132 | |||
133 | /* END CODE */ | ||
134 | /* | ||
135 | Local Variables: | ||
136 | c-file-style: "linux" | ||
137 | c-basic-offset: 4 | ||
138 | tab-width: 4 | ||
139 | End: | ||
140 | */ | ||
diff --git a/libbb/chomp.c b/libbb/chomp.c new file mode 100644 index 000000000..cbd3d04bc --- /dev/null +++ b/libbb/chomp.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | void chomp(char *s) | ||
33 | { | ||
34 | size_t len = strlen(s); | ||
35 | |||
36 | if (len == 0) | ||
37 | return; | ||
38 | |||
39 | if (s[len-1] == '\n') | ||
40 | s[len-1] = '\0'; | ||
41 | } | ||
42 | |||
43 | |||
44 | /* END CODE */ | ||
45 | /* | ||
46 | Local Variables: | ||
47 | c-file-style: "linux" | ||
48 | c-basic-offset: 4 | ||
49 | tab-width: 4 | ||
50 | End: | ||
51 | */ | ||
diff --git a/libbb/copy_file.c b/libbb/copy_file.c new file mode 100644 index 000000000..64b65b9fa --- /dev/null +++ b/libbb/copy_file.c | |||
@@ -0,0 +1,180 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <utime.h> | ||
31 | #include <unistd.h> | ||
32 | #include <fcntl.h> | ||
33 | #include <sys/stat.h> | ||
34 | #include "libbb.h" | ||
35 | |||
36 | |||
37 | /* | ||
38 | * Copy one file to another, while possibly preserving its modes, times, and | ||
39 | * modes. Returns TRUE if successful, or FALSE on a failure with an error | ||
40 | * message output. (Failure is not indicated if attributes cannot be set.) | ||
41 | * -Erik Andersen | ||
42 | */ | ||
43 | int | ||
44 | copy_file(const char *srcName, const char *destName, | ||
45 | int setModes, int followLinks, int forceFlag) | ||
46 | { | ||
47 | int rfd; | ||
48 | int wfd; | ||
49 | int status; | ||
50 | struct stat srcStatBuf; | ||
51 | struct stat dstStatBuf; | ||
52 | struct utimbuf times; | ||
53 | |||
54 | if (followLinks == TRUE) | ||
55 | status = stat(srcName, &srcStatBuf); | ||
56 | else | ||
57 | status = lstat(srcName, &srcStatBuf); | ||
58 | |||
59 | if (status < 0) { | ||
60 | perror_msg("%s", srcName); | ||
61 | return FALSE; | ||
62 | } | ||
63 | |||
64 | if (followLinks == TRUE) | ||
65 | status = stat(destName, &dstStatBuf); | ||
66 | else | ||
67 | status = lstat(destName, &dstStatBuf); | ||
68 | |||
69 | if (status < 0 || forceFlag==TRUE) { | ||
70 | unlink(destName); | ||
71 | dstStatBuf.st_ino = -1; | ||
72 | dstStatBuf.st_dev = -1; | ||
73 | } | ||
74 | |||
75 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && | ||
76 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { | ||
77 | error_msg("Copying file \"%s\" to itself", srcName); | ||
78 | return FALSE; | ||
79 | } | ||
80 | |||
81 | if (S_ISDIR(srcStatBuf.st_mode)) { | ||
82 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); | ||
83 | /* Make sure the directory is writable */ | ||
84 | status = mkdir(destName, 0777777 ^ umask(0)); | ||
85 | if (status < 0 && errno != EEXIST) { | ||
86 | perror_msg("%s", destName); | ||
87 | return FALSE; | ||
88 | } | ||
89 | } else if (S_ISLNK(srcStatBuf.st_mode)) { | ||
90 | char link_val[BUFSIZ + 1]; | ||
91 | int link_size; | ||
92 | |||
93 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); | ||
94 | /* Warning: This could possibly truncate silently, to BUFSIZ chars */ | ||
95 | link_size = readlink(srcName, &link_val[0], BUFSIZ); | ||
96 | if (link_size < 0) { | ||
97 | perror_msg("%s", srcName); | ||
98 | return FALSE; | ||
99 | } | ||
100 | link_val[link_size] = '\0'; | ||
101 | status = symlink(link_val, destName); | ||
102 | if (status < 0) { | ||
103 | perror_msg("%s", destName); | ||
104 | return FALSE; | ||
105 | } | ||
106 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) | ||
107 | if (setModes == TRUE) { | ||
108 | /* Try to set owner, but fail silently like GNU cp */ | ||
109 | lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); | ||
110 | } | ||
111 | #endif | ||
112 | return TRUE; | ||
113 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { | ||
114 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); | ||
115 | if (mkfifo(destName, 0644) < 0) { | ||
116 | perror_msg("%s", destName); | ||
117 | return FALSE; | ||
118 | } | ||
119 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) | ||
120 | || S_ISSOCK(srcStatBuf.st_mode)) { | ||
121 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); | ||
122 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) { | ||
123 | perror_msg("%s", destName); | ||
124 | return FALSE; | ||
125 | } | ||
126 | } else if (S_ISREG(srcStatBuf.st_mode)) { | ||
127 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); | ||
128 | rfd = open(srcName, O_RDONLY); | ||
129 | if (rfd < 0) { | ||
130 | perror_msg("%s", srcName); | ||
131 | return FALSE; | ||
132 | } | ||
133 | |||
134 | wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC, | ||
135 | srcStatBuf.st_mode); | ||
136 | if (wfd < 0) { | ||
137 | perror_msg("%s", destName); | ||
138 | close(rfd); | ||
139 | return FALSE; | ||
140 | } | ||
141 | |||
142 | if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE) | ||
143 | goto error_exit; | ||
144 | |||
145 | close(rfd); | ||
146 | if (close(wfd) < 0) { | ||
147 | return FALSE; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | if (setModes == TRUE) { | ||
152 | /* This is fine, since symlinks never get here */ | ||
153 | if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) | ||
154 | perror_msg_and_die("%s", destName); | ||
155 | if (chmod(destName, srcStatBuf.st_mode) < 0) | ||
156 | perror_msg_and_die("%s", destName); | ||
157 | times.actime = srcStatBuf.st_atime; | ||
158 | times.modtime = srcStatBuf.st_mtime; | ||
159 | if (utime(destName, ×) < 0) | ||
160 | perror_msg_and_die("%s", destName); | ||
161 | } | ||
162 | |||
163 | return TRUE; | ||
164 | |||
165 | error_exit: | ||
166 | perror_msg("%s", destName); | ||
167 | close(rfd); | ||
168 | close(wfd); | ||
169 | |||
170 | return FALSE; | ||
171 | } | ||
172 | |||
173 | /* END CODE */ | ||
174 | /* | ||
175 | Local Variables: | ||
176 | c-file-style: "linux" | ||
177 | c-basic-offset: 4 | ||
178 | tab-width: 4 | ||
179 | End: | ||
180 | */ | ||
diff --git a/libbb/copy_file_chunk.c b/libbb/copy_file_chunk.c new file mode 100644 index 000000000..3c657dd06 --- /dev/null +++ b/libbb/copy_file_chunk.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <sys/stat.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | /* | ||
33 | * Copy chunksize bytes between two file descriptors | ||
34 | */ | ||
35 | int copy_file_chunk(int srcfd, int dstfd, size_t chunksize) | ||
36 | { | ||
37 | size_t size; | ||
38 | char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */ | ||
39 | |||
40 | while (chunksize > 0) { | ||
41 | if (chunksize > BUFSIZ) | ||
42 | size = BUFSIZ; | ||
43 | else | ||
44 | size = chunksize; | ||
45 | if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size) | ||
46 | return(FALSE); | ||
47 | chunksize -= size; | ||
48 | } | ||
49 | return (TRUE); | ||
50 | } | ||
51 | |||
52 | |||
53 | /* END CODE */ | ||
54 | /* | ||
55 | Local Variables: | ||
56 | c-file-style: "linux" | ||
57 | c-basic-offset: 4 | ||
58 | tab-width: 4 | ||
59 | End: | ||
60 | */ | ||
diff --git a/libbb/create_path.c b/libbb/create_path.c new file mode 100644 index 000000000..328afc351 --- /dev/null +++ b/libbb/create_path.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <string.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | /* | ||
34 | * Attempt to create the directories along the specified path, except for | ||
35 | * the final component. The mode is given for the final directory only, | ||
36 | * while all previous ones get default protections. Errors are not reported | ||
37 | * here, as failures to restore files can be reported later. | ||
38 | */ | ||
39 | extern int create_path(const char *name, int mode) | ||
40 | { | ||
41 | char *cp; | ||
42 | char *cpOld; | ||
43 | char buf[BUFSIZ + 1]; | ||
44 | int retVal = 0; | ||
45 | |||
46 | strcpy(buf, name); | ||
47 | for (cp = buf; *cp == '/'; cp++); | ||
48 | cp = strchr(cp, '/'); | ||
49 | while (cp) { | ||
50 | cpOld = cp; | ||
51 | cp = strchr(cp + 1, '/'); | ||
52 | *cpOld = '\0'; | ||
53 | retVal = mkdir(buf, cp ? 0777 : mode); | ||
54 | if (retVal != 0 && errno != EEXIST) { | ||
55 | perror_msg("%s", buf); | ||
56 | return FALSE; | ||
57 | } | ||
58 | *cpOld = '/'; | ||
59 | } | ||
60 | return TRUE; | ||
61 | } | ||
62 | |||
63 | |||
64 | /* END CODE */ | ||
65 | /* | ||
66 | Local Variables: | ||
67 | c-file-style: "linux" | ||
68 | c-basic-offset: 4 | ||
69 | tab-width: 4 | ||
70 | End: | ||
71 | */ | ||
diff --git a/libbb/device_open.c b/libbb/device_open.c new file mode 100644 index 000000000..8e97ce6c5 --- /dev/null +++ b/libbb/device_open.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <fcntl.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | |||
33 | /* try to open up the specified device */ | ||
34 | extern int device_open(char *device, int mode) | ||
35 | { | ||
36 | int m, f, fd = -1; | ||
37 | |||
38 | m = mode | O_NONBLOCK; | ||
39 | |||
40 | /* Retry up to 5 times */ | ||
41 | for (f = 0; f < 5; f++) | ||
42 | if ((fd = open(device, m, 0600)) >= 0) | ||
43 | break; | ||
44 | if (fd < 0) | ||
45 | return fd; | ||
46 | /* Reset original flags. */ | ||
47 | if (m != mode) | ||
48 | fcntl(fd, F_SETFL, mode); | ||
49 | return fd; | ||
50 | } | ||
51 | |||
52 | /* END CODE */ | ||
53 | /* | ||
54 | Local Variables: | ||
55 | c-file-style: "linux" | ||
56 | c-basic-offset: 4 | ||
57 | tab-width: 4 | ||
58 | End: | ||
59 | */ | ||
diff --git a/libbb/error_msg.c b/libbb/error_msg.c new file mode 100644 index 000000000..7773d32a2 --- /dev/null +++ b/libbb/error_msg.c | |||
@@ -0,0 +1,100 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <string.h> | ||
31 | #include <stdlib.h> | ||
32 | #include "libbb.h" | ||
33 | |||
34 | extern const char *applet_name; | ||
35 | |||
36 | static void verror_msg(const char *s, va_list p) | ||
37 | { | ||
38 | fflush(stdout); | ||
39 | fprintf(stderr, "%s: ", applet_name); | ||
40 | vfprintf(stderr, s, p); | ||
41 | } | ||
42 | |||
43 | extern void error_msg(const char *s, ...) | ||
44 | { | ||
45 | va_list p; | ||
46 | |||
47 | va_start(p, s); | ||
48 | verror_msg(s, p); | ||
49 | va_end(p); | ||
50 | putc('\n', stderr); | ||
51 | } | ||
52 | |||
53 | extern void error_msg_and_die(const char *s, ...) | ||
54 | { | ||
55 | va_list p; | ||
56 | |||
57 | va_start(p, s); | ||
58 | verror_msg(s, p); | ||
59 | va_end(p); | ||
60 | putc('\n', stderr); | ||
61 | exit(EXIT_FAILURE); | ||
62 | } | ||
63 | |||
64 | static void vperror_msg(const char *s, va_list p) | ||
65 | { | ||
66 | int err=errno; | ||
67 | if(s == 0) s = ""; | ||
68 | verror_msg(s, p); | ||
69 | if (*s) s = ": "; | ||
70 | fprintf(stderr, "%s%s\n", s, strerror(err)); | ||
71 | } | ||
72 | |||
73 | extern void perror_msg(const char *s, ...) | ||
74 | { | ||
75 | va_list p; | ||
76 | |||
77 | va_start(p, s); | ||
78 | vperror_msg(s, p); | ||
79 | va_end(p); | ||
80 | } | ||
81 | |||
82 | extern void perror_msg_and_die(const char *s, ...) | ||
83 | { | ||
84 | va_list p; | ||
85 | |||
86 | va_start(p, s); | ||
87 | vperror_msg(s, p); | ||
88 | va_end(p); | ||
89 | exit(EXIT_FAILURE); | ||
90 | } | ||
91 | |||
92 | |||
93 | /* END CODE */ | ||
94 | /* | ||
95 | Local Variables: | ||
96 | c-file-style: "linux" | ||
97 | c-basic-offset: 4 | ||
98 | tab-width: 4 | ||
99 | End: | ||
100 | */ | ||
diff --git a/libbb/find_mount_point.c b/libbb/find_mount_point.c new file mode 100644 index 000000000..27322f7f0 --- /dev/null +++ b/libbb/find_mount_point.c | |||
@@ -0,0 +1,80 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | #include <mntent.h> | ||
33 | /* | ||
34 | * Given a block device, find the mount table entry if that block device | ||
35 | * is mounted. | ||
36 | * | ||
37 | * Given any other file (or directory), find the mount table entry for its | ||
38 | * filesystem. | ||
39 | */ | ||
40 | extern struct mntent *find_mount_point(const char *name, const char *table) | ||
41 | { | ||
42 | struct stat s; | ||
43 | dev_t mountDevice; | ||
44 | FILE *mountTable; | ||
45 | struct mntent *mountEntry; | ||
46 | |||
47 | if (stat(name, &s) != 0) | ||
48 | return 0; | ||
49 | |||
50 | if ((s.st_mode & S_IFMT) == S_IFBLK) | ||
51 | mountDevice = s.st_rdev; | ||
52 | else | ||
53 | mountDevice = s.st_dev; | ||
54 | |||
55 | |||
56 | if ((mountTable = setmntent(table, "r")) == 0) | ||
57 | return 0; | ||
58 | |||
59 | while ((mountEntry = getmntent(mountTable)) != 0) { | ||
60 | if (strcmp(name, mountEntry->mnt_dir) == 0 | ||
61 | || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */ | ||
62 | break; | ||
63 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */ | ||
64 | break; | ||
65 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */ | ||
66 | break; | ||
67 | } | ||
68 | endmntent(mountTable); | ||
69 | return mountEntry; | ||
70 | } | ||
71 | |||
72 | |||
73 | /* END CODE */ | ||
74 | /* | ||
75 | Local Variables: | ||
76 | c-file-style: "linux" | ||
77 | c-basic-offset: 4 | ||
78 | tab-width: 4 | ||
79 | End: | ||
80 | */ | ||
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c new file mode 100644 index 000000000..a22ee1ff8 --- /dev/null +++ b/libbb/find_pid_by_name.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <ctype.h> | ||
30 | #include <string.h> | ||
31 | #include <dirent.h> | ||
32 | #include <stdlib.h> | ||
33 | #include "libbb.h" | ||
34 | |||
35 | |||
36 | /* For Erik's nifty devps device driver */ | ||
37 | #ifdef BB_FEATURE_USE_DEVPS_PATCH | ||
38 | #include <linux/devps.h> | ||
39 | |||
40 | /* find_pid_by_name() | ||
41 | * | ||
42 | * This finds the pid of the specified process, | ||
43 | * by using the /dev/ps device driver. | ||
44 | * | ||
45 | * Returns a list of all matching PIDs | ||
46 | */ | ||
47 | extern pid_t* find_pid_by_name( char* pidName) | ||
48 | { | ||
49 | int fd, i, j; | ||
50 | char device[] = "/dev/ps"; | ||
51 | pid_t num_pids; | ||
52 | pid_t* pid_array = NULL; | ||
53 | pid_t* pidList=NULL; | ||
54 | |||
55 | /* open device */ | ||
56 | fd = open(device, O_RDONLY); | ||
57 | if (fd < 0) | ||
58 | perror_msg_and_die("open failed for `%s'", device); | ||
59 | |||
60 | /* Find out how many processes there are */ | ||
61 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) | ||
62 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); | ||
63 | |||
64 | /* Allocate some memory -- grab a few extras just in case | ||
65 | * some new processes start up while we wait. The kernel will | ||
66 | * just ignore any extras if we give it too many, and will trunc. | ||
67 | * the list if we give it too few. */ | ||
68 | pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); | ||
69 | pid_array[0] = num_pids+10; | ||
70 | |||
71 | /* Now grab the pid list */ | ||
72 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) | ||
73 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); | ||
74 | |||
75 | /* Now search for a match */ | ||
76 | for (i=1, j=0; i<pid_array[0] ; i++) { | ||
77 | char* p; | ||
78 | struct pid_info info; | ||
79 | |||
80 | info.pid = pid_array[i]; | ||
81 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) | ||
82 | perror_msg_and_die("\nDEVPS_GET_PID_INFO"); | ||
83 | |||
84 | /* Make sure we only match on the process name */ | ||
85 | p=info.command_line+1; | ||
86 | while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) { | ||
87 | (p)++; | ||
88 | } | ||
89 | if (isspace(*(p))) | ||
90 | *p='\0'; | ||
91 | |||
92 | if ((strstr(info.command_line, pidName) != NULL) | ||
93 | && (strlen(pidName) == strlen(info.command_line))) { | ||
94 | pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); | ||
95 | pidList[j++]=info.pid; | ||
96 | } | ||
97 | } | ||
98 | if (pidList) | ||
99 | pidList[j]=0; | ||
100 | |||
101 | /* Free memory */ | ||
102 | free( pid_array); | ||
103 | |||
104 | /* close device */ | ||
105 | if (close (fd) != 0) | ||
106 | perror_msg_and_die("close failed for `%s'", device); | ||
107 | |||
108 | return pidList; | ||
109 | } | ||
110 | |||
111 | #else /* BB_FEATURE_USE_DEVPS_PATCH */ | ||
112 | |||
113 | /* find_pid_by_name() | ||
114 | * | ||
115 | * This finds the pid of the specified process. | ||
116 | * Currently, it's implemented by rummaging through | ||
117 | * the proc filesystem. | ||
118 | * | ||
119 | * Returns a list of all matching PIDs | ||
120 | */ | ||
121 | extern pid_t* find_pid_by_name( char* pidName) | ||
122 | { | ||
123 | DIR *dir; | ||
124 | struct dirent *next; | ||
125 | pid_t* pidList=NULL; | ||
126 | int i=0; | ||
127 | |||
128 | dir = opendir("/proc"); | ||
129 | if (!dir) | ||
130 | perror_msg_and_die("Cannot open /proc"); | ||
131 | |||
132 | while ((next = readdir(dir)) != NULL) { | ||
133 | FILE *status; | ||
134 | char filename[256]; | ||
135 | char buffer[256]; | ||
136 | |||
137 | /* If it isn't a number, we don't want it */ | ||
138 | if (!isdigit(*next->d_name)) | ||
139 | continue; | ||
140 | |||
141 | sprintf(filename, "/proc/%s/cmdline", next->d_name); | ||
142 | status = fopen(filename, "r"); | ||
143 | if (!status) { | ||
144 | continue; | ||
145 | } | ||
146 | fgets(buffer, 256, status); | ||
147 | fclose(status); | ||
148 | |||
149 | if (strstr(get_last_path_component(buffer), pidName) != NULL) { | ||
150 | pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); | ||
151 | pidList[i++]=strtol(next->d_name, NULL, 0); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | if (pidList) | ||
156 | pidList[i]=0; | ||
157 | return pidList; | ||
158 | } | ||
159 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ | ||
160 | |||
161 | /* END CODE */ | ||
162 | /* | ||
163 | Local Variables: | ||
164 | c-file-style: "linux" | ||
165 | c-basic-offset: 4 | ||
166 | tab-width: 4 | ||
167 | End: | ||
168 | */ | ||
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c new file mode 100644 index 000000000..de765ce44 --- /dev/null +++ b/libbb/find_root_device.c | |||
@@ -0,0 +1,85 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <dirent.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | |||
34 | |||
35 | extern int find_real_root_device_name(char* name) | ||
36 | { | ||
37 | DIR *dir; | ||
38 | struct dirent *entry; | ||
39 | struct stat statBuf, rootStat; | ||
40 | char fileName[BUFSIZ]; | ||
41 | |||
42 | if (stat("/", &rootStat) != 0) { | ||
43 | error_msg("could not stat '/'"); | ||
44 | return( FALSE); | ||
45 | } | ||
46 | |||
47 | dir = opendir("/dev"); | ||
48 | if (!dir) { | ||
49 | error_msg("could not open '/dev'"); | ||
50 | return( FALSE); | ||
51 | } | ||
52 | |||
53 | while((entry = readdir(dir)) != NULL) { | ||
54 | |||
55 | /* Must skip ".." since that is "/", and so we | ||
56 | * would get a false positive on ".." */ | ||
57 | if (strcmp(entry->d_name, "..") == 0) | ||
58 | continue; | ||
59 | |||
60 | snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name); | ||
61 | |||
62 | if (stat(fileName, &statBuf) != 0) | ||
63 | continue; | ||
64 | /* Some char devices have the same dev_t as block | ||
65 | * devices, so make sure this is a block device */ | ||
66 | if (! S_ISBLK(statBuf.st_mode)) | ||
67 | continue; | ||
68 | if (statBuf.st_rdev == rootStat.st_rdev) { | ||
69 | strcpy(name, fileName); | ||
70 | return ( TRUE); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | return( FALSE); | ||
75 | } | ||
76 | |||
77 | |||
78 | /* END CODE */ | ||
79 | /* | ||
80 | Local Variables: | ||
81 | c-file-style: "linux" | ||
82 | c-basic-offset: 4 | ||
83 | tab-width: 4 | ||
84 | End: | ||
85 | */ | ||
diff --git a/libbb/full_read.c b/libbb/full_read.c new file mode 100644 index 000000000..e9c4bbfc6 --- /dev/null +++ b/libbb/full_read.c | |||
@@ -0,0 +1,70 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <unistd.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | |||
33 | /* | ||
34 | * Read all of the supplied buffer from a file. | ||
35 | * This does multiple reads as necessary. | ||
36 | * Returns the amount read, or -1 on an error. | ||
37 | * A short read is returned on an end of file. | ||
38 | */ | ||
39 | int full_read(int fd, char *buf, int len) | ||
40 | { | ||
41 | int cc; | ||
42 | int total; | ||
43 | |||
44 | total = 0; | ||
45 | |||
46 | while (len > 0) { | ||
47 | cc = read(fd, buf, len); | ||
48 | |||
49 | if (cc < 0) | ||
50 | return -1; | ||
51 | |||
52 | if (cc == 0) | ||
53 | break; | ||
54 | |||
55 | buf += cc; | ||
56 | total += cc; | ||
57 | len -= cc; | ||
58 | } | ||
59 | |||
60 | return total; | ||
61 | } | ||
62 | |||
63 | /* END CODE */ | ||
64 | /* | ||
65 | Local Variables: | ||
66 | c-file-style: "linux" | ||
67 | c-basic-offset: 4 | ||
68 | tab-width: 4 | ||
69 | End: | ||
70 | */ | ||
diff --git a/libbb/full_write.c b/libbb/full_write.c new file mode 100644 index 000000000..dc9937fa3 --- /dev/null +++ b/libbb/full_write.c | |||
@@ -0,0 +1,66 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <unistd.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | /* | ||
33 | * Write all of the supplied buffer out to a file. | ||
34 | * This does multiple writes as necessary. | ||
35 | * Returns the amount written, or -1 on an error. | ||
36 | */ | ||
37 | int full_write(int fd, const char *buf, int len) | ||
38 | { | ||
39 | int cc; | ||
40 | int total; | ||
41 | |||
42 | total = 0; | ||
43 | |||
44 | while (len > 0) { | ||
45 | cc = write(fd, buf, len); | ||
46 | |||
47 | if (cc < 0) | ||
48 | return -1; | ||
49 | |||
50 | buf += cc; | ||
51 | total += cc; | ||
52 | len -= cc; | ||
53 | } | ||
54 | |||
55 | return total; | ||
56 | } | ||
57 | |||
58 | |||
59 | /* END CODE */ | ||
60 | /* | ||
61 | Local Variables: | ||
62 | c-file-style: "linux" | ||
63 | c-basic-offset: 4 | ||
64 | tab-width: 4 | ||
65 | End: | ||
66 | */ | ||
diff --git a/libbb/get_console.c b/libbb/get_console.c new file mode 100644 index 000000000..4be9adc84 --- /dev/null +++ b/libbb/get_console.c | |||
@@ -0,0 +1,129 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <fcntl.h> | ||
31 | #include <unistd.h> | ||
32 | #include <sys/ioctl.h> | ||
33 | #include "libbb.h" | ||
34 | |||
35 | |||
36 | |||
37 | |||
38 | |||
39 | /* From <linux/kd.h> */ | ||
40 | static const int KDGKBTYPE = 0x4B33; /* get keyboard type */ | ||
41 | static const int KB_84 = 0x01; | ||
42 | static const int KB_101 = 0x02; /* this is what we always answer */ | ||
43 | |||
44 | int is_a_console(int fd) | ||
45 | { | ||
46 | char arg; | ||
47 | |||
48 | arg = 0; | ||
49 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 | ||
50 | && ((arg == KB_101) || (arg == KB_84))); | ||
51 | } | ||
52 | |||
53 | static int open_a_console(char *fnam) | ||
54 | { | ||
55 | int fd; | ||
56 | |||
57 | /* try read-only */ | ||
58 | fd = open(fnam, O_RDWR); | ||
59 | |||
60 | /* if failed, try read-only */ | ||
61 | if (fd < 0 && errno == EACCES) | ||
62 | fd = open(fnam, O_RDONLY); | ||
63 | |||
64 | /* if failed, try write-only */ | ||
65 | if (fd < 0 && errno == EACCES) | ||
66 | fd = open(fnam, O_WRONLY); | ||
67 | |||
68 | /* if failed, fail */ | ||
69 | if (fd < 0) | ||
70 | return -1; | ||
71 | |||
72 | /* if not a console, fail */ | ||
73 | if (!is_a_console(fd)) { | ||
74 | close(fd); | ||
75 | return -1; | ||
76 | } | ||
77 | |||
78 | /* success */ | ||
79 | return fd; | ||
80 | } | ||
81 | |||
82 | /* | ||
83 | * Get an fd for use with kbd/console ioctls. | ||
84 | * We try several things because opening /dev/console will fail | ||
85 | * if someone else used X (which does a chown on /dev/console). | ||
86 | * | ||
87 | * if tty_name is non-NULL, try this one instead. | ||
88 | */ | ||
89 | |||
90 | int get_console_fd(char *tty_name) | ||
91 | { | ||
92 | int fd; | ||
93 | |||
94 | if (tty_name) { | ||
95 | if (-1 == (fd = open_a_console(tty_name))) | ||
96 | return -1; | ||
97 | else | ||
98 | return fd; | ||
99 | } | ||
100 | |||
101 | fd = open_a_console("/dev/tty"); | ||
102 | if (fd >= 0) | ||
103 | return fd; | ||
104 | |||
105 | fd = open_a_console("/dev/tty0"); | ||
106 | if (fd >= 0) | ||
107 | return fd; | ||
108 | |||
109 | fd = open_a_console("/dev/console"); | ||
110 | if (fd >= 0) | ||
111 | return fd; | ||
112 | |||
113 | for (fd = 0; fd < 3; fd++) | ||
114 | if (is_a_console(fd)) | ||
115 | return fd; | ||
116 | |||
117 | error_msg("Couldnt get a file descriptor referring to the console"); | ||
118 | return -1; /* total failure */ | ||
119 | } | ||
120 | |||
121 | |||
122 | /* END CODE */ | ||
123 | /* | ||
124 | Local Variables: | ||
125 | c-file-style: "linux" | ||
126 | c-basic-offset: 4 | ||
127 | tab-width: 4 | ||
128 | End: | ||
129 | */ | ||
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c new file mode 100644 index 000000000..a322288a6 --- /dev/null +++ b/libbb/get_last_path_component.c | |||
@@ -0,0 +1,58 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | |||
33 | char *get_last_path_component(char *path) | ||
34 | { | ||
35 | char *s=path+strlen(path)-1; | ||
36 | |||
37 | /* strip trailing slashes */ | ||
38 | while (s != path && *s == '/') { | ||
39 | *s-- = '\0'; | ||
40 | } | ||
41 | |||
42 | /* find last component */ | ||
43 | s = strrchr(path, '/'); | ||
44 | if (s == NULL || s[1] == '\0') | ||
45 | return path; | ||
46 | else | ||
47 | return s+1; | ||
48 | } | ||
49 | |||
50 | |||
51 | /* END CODE */ | ||
52 | /* | ||
53 | Local Variables: | ||
54 | c-file-style: "linux" | ||
55 | c-basic-offset: 4 | ||
56 | tab-width: 4 | ||
57 | End: | ||
58 | */ | ||
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c new file mode 100644 index 000000000..759481731 --- /dev/null +++ b/libbb/get_line_from_file.c | |||
@@ -0,0 +1,72 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | |||
33 | /* get_line_from_file() - This function reads an entire line from a text file | ||
34 | * up to a newline. It returns a malloc'ed char * which must be stored and | ||
35 | * free'ed by the caller. */ | ||
36 | extern char *get_line_from_file(FILE *file) | ||
37 | { | ||
38 | static const int GROWBY = 80; /* how large we will grow strings by */ | ||
39 | |||
40 | int ch; | ||
41 | int idx = 0; | ||
42 | char *linebuf = NULL; | ||
43 | int linebufsz = 0; | ||
44 | |||
45 | while (1) { | ||
46 | ch = fgetc(file); | ||
47 | if (ch == EOF) | ||
48 | break; | ||
49 | /* grow the line buffer as necessary */ | ||
50 | while (idx > linebufsz-2) | ||
51 | linebuf = xrealloc(linebuf, linebufsz += GROWBY); | ||
52 | linebuf[idx++] = (char)ch; | ||
53 | if ((char)ch == '\n') | ||
54 | break; | ||
55 | } | ||
56 | |||
57 | if (idx == 0) | ||
58 | return NULL; | ||
59 | |||
60 | linebuf[idx] = 0; | ||
61 | return linebuf; | ||
62 | } | ||
63 | |||
64 | |||
65 | /* END CODE */ | ||
66 | /* | ||
67 | Local Variables: | ||
68 | c-file-style: "linux" | ||
69 | c-basic-offset: 4 | ||
70 | tab-width: 4 | ||
71 | End: | ||
72 | */ | ||
diff --git a/libbb/human_readable.c b/libbb/human_readable.c new file mode 100644 index 000000000..ff2175175 --- /dev/null +++ b/libbb/human_readable.c | |||
@@ -0,0 +1,62 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | |||
33 | const char *make_human_readable_str(unsigned long val, unsigned long hr) | ||
34 | { | ||
35 | int i=0; | ||
36 | static char str[10] = "\0"; | ||
37 | static const char strings[] = { 'k', 'M', 'G', 'T', 0 }; | ||
38 | unsigned long divisor = 1; | ||
39 | |||
40 | if(val == 0) | ||
41 | return("0"); | ||
42 | if(hr) | ||
43 | snprintf(str, 9, "%ld", val/hr); | ||
44 | else { | ||
45 | while(val >= divisor && i <= 4) { | ||
46 | divisor=divisor<<10, i++; | ||
47 | } | ||
48 | divisor=divisor>>10, i--; | ||
49 | snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]); | ||
50 | } | ||
51 | return(str); | ||
52 | } | ||
53 | |||
54 | |||
55 | /* END CODE */ | ||
56 | /* | ||
57 | Local Variables: | ||
58 | c-file-style: "linux" | ||
59 | c-basic-offset: 4 | ||
60 | tab-width: 4 | ||
61 | End: | ||
62 | */ | ||
diff --git a/libbb/inode_hash.c b/libbb/inode_hash.c new file mode 100644 index 000000000..db68025dd --- /dev/null +++ b/libbb/inode_hash.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | #define HASH_SIZE 311 /* Should be prime */ | ||
33 | #define hash_inode(i) ((i) % HASH_SIZE) | ||
34 | |||
35 | static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE]; | ||
36 | |||
37 | /* | ||
38 | * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in | ||
39 | * `ino_dev_hashtable', else return 0 | ||
40 | * | ||
41 | * If NAME is a non-NULL pointer to a character pointer, and there is | ||
42 | * a match, then set *NAME to the value of the name slot in that | ||
43 | * bucket. | ||
44 | */ | ||
45 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name) | ||
46 | { | ||
47 | ino_dev_hashtable_bucket_t *bucket; | ||
48 | |||
49 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; | ||
50 | while (bucket != NULL) { | ||
51 | if ((bucket->ino == statbuf->st_ino) && | ||
52 | (bucket->dev == statbuf->st_dev)) | ||
53 | { | ||
54 | if (name) *name = bucket->name; | ||
55 | return 1; | ||
56 | } | ||
57 | bucket = bucket->next; | ||
58 | } | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | /* Add statbuf to statbuf hash table */ | ||
63 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) | ||
64 | { | ||
65 | int i; | ||
66 | size_t s; | ||
67 | ino_dev_hashtable_bucket_t *bucket; | ||
68 | |||
69 | i = hash_inode(statbuf->st_ino); | ||
70 | s = name ? strlen(name) : 0; | ||
71 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s); | ||
72 | bucket->ino = statbuf->st_ino; | ||
73 | bucket->dev = statbuf->st_dev; | ||
74 | if (name) | ||
75 | strcpy(bucket->name, name); | ||
76 | else | ||
77 | bucket->name[0] = '\0'; | ||
78 | bucket->next = ino_dev_hashtable[i]; | ||
79 | ino_dev_hashtable[i] = bucket; | ||
80 | } | ||
81 | |||
82 | /* Clear statbuf hash table */ | ||
83 | void reset_ino_dev_hashtable(void) | ||
84 | { | ||
85 | int i; | ||
86 | ino_dev_hashtable_bucket_t *bucket; | ||
87 | |||
88 | for (i = 0; i < HASH_SIZE; i++) { | ||
89 | while (ino_dev_hashtable[i] != NULL) { | ||
90 | bucket = ino_dev_hashtable[i]->next; | ||
91 | free(ino_dev_hashtable[i]); | ||
92 | ino_dev_hashtable[i] = bucket; | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | |||
97 | |||
98 | /* END CODE */ | ||
99 | /* | ||
100 | Local Variables: | ||
101 | c-file-style: "linux" | ||
102 | c-basic-offset: 4 | ||
103 | tab-width: 4 | ||
104 | End: | ||
105 | */ | ||
diff --git a/libbb/isdirectory.c b/libbb/isdirectory.c new file mode 100644 index 000000000..65f4fee00 --- /dev/null +++ b/libbb/isdirectory.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include <sys/stat.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | /* | ||
34 | * Return TRUE if a fileName is a directory. | ||
35 | * Nonexistant files return FALSE. | ||
36 | */ | ||
37 | int is_directory(const char *fileName, const int followLinks, struct stat *statBuf) | ||
38 | { | ||
39 | int status; | ||
40 | int didMalloc = 0; | ||
41 | |||
42 | if (statBuf == NULL) { | ||
43 | statBuf = (struct stat *)xmalloc(sizeof(struct stat)); | ||
44 | ++didMalloc; | ||
45 | } | ||
46 | |||
47 | if (followLinks == TRUE) | ||
48 | status = stat(fileName, statBuf); | ||
49 | else | ||
50 | status = lstat(fileName, statBuf); | ||
51 | |||
52 | if (status < 0 || !(S_ISDIR(statBuf->st_mode))) { | ||
53 | status = FALSE; | ||
54 | } | ||
55 | else status = TRUE; | ||
56 | |||
57 | if (didMalloc) { | ||
58 | free(statBuf); | ||
59 | statBuf = NULL; | ||
60 | } | ||
61 | return status; | ||
62 | } | ||
63 | |||
64 | /* END CODE */ | ||
65 | /* | ||
66 | Local Variables: | ||
67 | c-file-style: "linux" | ||
68 | c-basic-offset: 4 | ||
69 | tab-width: 4 | ||
70 | End: | ||
71 | */ | ||
diff --git a/libbb/kernel_version.c b/libbb/kernel_version.c new file mode 100644 index 000000000..09cd582c4 --- /dev/null +++ b/libbb/kernel_version.c | |||
@@ -0,0 +1,66 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <stdlib.h> | ||
31 | #include <sys/utsname.h> /* for uname(2) */ | ||
32 | |||
33 | #include "libbb.h" | ||
34 | |||
35 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, | ||
36 | * so, for example, to check if the kernel is greater than 2.2.11: | ||
37 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } | ||
38 | */ | ||
39 | extern int get_kernel_revision(void) | ||
40 | { | ||
41 | struct utsname name; | ||
42 | char *s; | ||
43 | int i, r; | ||
44 | |||
45 | if (uname(&name) == -1) { | ||
46 | perror_msg("cannot get system information"); | ||
47 | return (0); | ||
48 | } | ||
49 | |||
50 | s = name.release; | ||
51 | r = 0; | ||
52 | for (i=0 ; i<3 ; i++) { | ||
53 | r = r * 256 + atoi(strtok(s, ".")); | ||
54 | s = NULL; | ||
55 | } | ||
56 | return r; | ||
57 | } | ||
58 | |||
59 | /* END CODE */ | ||
60 | /* | ||
61 | Local Variables: | ||
62 | c-file-style: "linux" | ||
63 | c-basic-offset: 4 | ||
64 | tab-width: 4 | ||
65 | End: | ||
66 | */ | ||
diff --git a/libbb/libbb.h b/libbb/libbb.h new file mode 100644 index 000000000..a85987d9e --- /dev/null +++ b/libbb/libbb.h | |||
@@ -0,0 +1,209 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Busybox main internal header file | ||
4 | * | ||
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 | #ifndef __LIBBB_H__ | ||
25 | #define __LIBBB_H__ 1 | ||
26 | |||
27 | #include <stdio.h> | ||
28 | #include <stdarg.h> | ||
29 | #include <sys/stat.h> | ||
30 | #include <sys/types.h> | ||
31 | |||
32 | #ifdef DMALLOC | ||
33 | #include "dmalloc.h" | ||
34 | #endif | ||
35 | |||
36 | #include <features.h> | ||
37 | /* Stupid libc doesn't have a reliable way for use to know | ||
38 | * that libc5 is being used. Assume this is good enough */ | ||
39 | #if ! defined __GLIBC__ && ! defined __UCLIBC__ | ||
40 | /* libc5 doesn't define socklen_t */ | ||
41 | typedef unsigned int socklen_t; | ||
42 | #endif | ||
43 | |||
44 | /* Some useful definitions */ | ||
45 | #define FALSE ((int) 0) | ||
46 | #define TRUE ((int) 1) | ||
47 | #define SKIP ((int) 2) | ||
48 | |||
49 | /* for mtab.c */ | ||
50 | #define MTAB_GETMOUNTPT '1' | ||
51 | #define MTAB_GETDEVICE '2' | ||
52 | |||
53 | #define BUF_SIZE 8192 | ||
54 | #define EXPAND_ALLOC 1024 | ||
55 | |||
56 | static inline int is_decimal(ch) { return ((ch >= '0') && (ch <= '9')); } | ||
57 | static inline int is_octal(ch) { return ((ch >= '0') && (ch <= '7')); } | ||
58 | |||
59 | /* Macros for min/max. */ | ||
60 | #ifndef MIN | ||
61 | #define MIN(a,b) (((a)<(b))?(a):(b)) | ||
62 | #endif | ||
63 | |||
64 | #ifndef MAX | ||
65 | #define MAX(a,b) (((a)>(b))?(a):(b)) | ||
66 | #endif | ||
67 | |||
68 | |||
69 | |||
70 | extern void show_usage(void) __attribute__ ((noreturn)); | ||
71 | extern void error_msg(const char *s, ...); | ||
72 | extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); | ||
73 | extern void perror_msg(const char *s, ...); | ||
74 | extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn)); | ||
75 | |||
76 | const char *mode_string(int mode); | ||
77 | const char *time_string(time_t timeVal); | ||
78 | int is_directory(const char *name, const int followLinks, struct stat *statBuf); | ||
79 | int isDevice(const char *name); | ||
80 | |||
81 | typedef struct ino_dev_hash_bucket_struct { | ||
82 | struct ino_dev_hash_bucket_struct *next; | ||
83 | ino_t ino; | ||
84 | dev_t dev; | ||
85 | char name[1]; | ||
86 | } ino_dev_hashtable_bucket_t; | ||
87 | int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name); | ||
88 | void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); | ||
89 | void reset_ino_dev_hashtable(void); | ||
90 | |||
91 | int copy_file(const char *srcName, const char *destName, | ||
92 | int setModes, int followLinks, int forceFlag); | ||
93 | int copy_file_chunk(int srcFd, int dstFd, size_t remaining); | ||
94 | char *buildName(const char *dirName, const char *fileName); | ||
95 | int makeString(int argc, const char **argv, char *buf, int bufLen); | ||
96 | char *getChunk(int size); | ||
97 | char *chunkstrdup(const char *str); | ||
98 | void freeChunks(void); | ||
99 | ssize_t safe_read(int fd, void *buf, size_t count); | ||
100 | int full_write(int fd, const char *buf, int len); | ||
101 | int full_read(int fd, char *buf, int len); | ||
102 | int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst, | ||
103 | int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData), | ||
104 | int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData), | ||
105 | void* userData); | ||
106 | |||
107 | extern int create_path (const char *name, int mode); | ||
108 | extern int parse_mode( const char* s, mode_t* theMode); | ||
109 | |||
110 | extern int get_kernel_revision(void); | ||
111 | |||
112 | extern int get_console_fd(char* tty_name); | ||
113 | extern struct mntent *find_mount_point(const char *name, const char *table); | ||
114 | extern void write_mtab(char* blockDevice, char* directory, | ||
115 | char* filesystemType, long flags, char* string_flags); | ||
116 | extern void erase_mtab(const char * name); | ||
117 | extern void mtab_read(void); | ||
118 | extern char *mtab_first(void **iter); | ||
119 | extern char *mtab_next(void **iter); | ||
120 | extern char *mtab_getinfo(const char *match, const char which); | ||
121 | extern int check_wildcard_match(const char* text, const char* pattern); | ||
122 | extern long atoi_w_units (const char *cp); | ||
123 | extern pid_t* find_pid_by_name( char* pidName); | ||
124 | extern int find_real_root_device_name(char* name); | ||
125 | extern char *get_line_from_file(FILE *file); | ||
126 | extern void print_file(FILE *file); | ||
127 | extern int print_file_by_name(char *filename); | ||
128 | extern char process_escape_sequence(char **ptr); | ||
129 | extern char *get_last_path_component(char *path); | ||
130 | extern FILE *wfopen(const char *path, const char *mode); | ||
131 | extern FILE *xfopen(const char *path, const char *mode); | ||
132 | extern void chomp(char *s); | ||
133 | extern void trim(char *s); | ||
134 | extern struct BB_applet *find_applet_by_name(const char *name); | ||
135 | void run_applet_by_name(const char *name, int argc, char **argv); | ||
136 | |||
137 | #ifndef DMALLOC | ||
138 | extern void *xmalloc (size_t size); | ||
139 | extern void *xrealloc(void *old, size_t size); | ||
140 | extern void *xcalloc(size_t nmemb, size_t size); | ||
141 | extern char *xstrdup (const char *s); | ||
142 | #endif | ||
143 | extern char *xstrndup (const char *s, int n); | ||
144 | extern char * safe_strncpy(char *dst, const char *src, size_t size); | ||
145 | |||
146 | struct suffix_mult { | ||
147 | char *suffix; | ||
148 | int mult; | ||
149 | }; | ||
150 | |||
151 | extern unsigned long parse_number(const char *numstr, | ||
152 | const struct suffix_mult *suffixes); | ||
153 | |||
154 | |||
155 | /* These parse entries in /etc/passwd and /etc/group. This is desirable | ||
156 | * for BusyBox since we want to avoid using the glibc NSS stuff, which | ||
157 | * increases target size and is often not needed embedded systems. */ | ||
158 | extern long my_getpwnam(const char *name); | ||
159 | extern long my_getgrnam(const char *name); | ||
160 | extern void my_getpwuid(char *name, long uid); | ||
161 | extern void my_getgrgid(char *group, long gid); | ||
162 | extern long my_getpwnamegid(const char *name); | ||
163 | |||
164 | extern int device_open(char *device, int mode); | ||
165 | |||
166 | extern int del_loop(const char *device); | ||
167 | extern int set_loop(const char *device, const char *file, int offset, int *loopro); | ||
168 | extern char *find_unused_loop_device (void); | ||
169 | |||
170 | |||
171 | #if (__GLIBC__ < 2) | ||
172 | extern int vdprintf(int d, const char *format, va_list ap); | ||
173 | #endif | ||
174 | |||
175 | int nfsmount(const char *spec, const char *node, int *flags, | ||
176 | char **extra_opts, char **mount_opts, int running_bg); | ||
177 | |||
178 | void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg); | ||
179 | void syslog_msg(int facility, int pri, const char *msg); | ||
180 | |||
181 | /* Include our own copy of struct sysinfo to avoid binary compatability | ||
182 | * problems with Linux 2.4, which changed things. Grumble, grumble. */ | ||
183 | struct sysinfo { | ||
184 | long uptime; /* Seconds since boot */ | ||
185 | unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ | ||
186 | unsigned long totalram; /* Total usable main memory size */ | ||
187 | unsigned long freeram; /* Available memory size */ | ||
188 | unsigned long sharedram; /* Amount of shared memory */ | ||
189 | unsigned long bufferram; /* Memory used by buffers */ | ||
190 | unsigned long totalswap; /* Total swap space size */ | ||
191 | unsigned long freeswap; /* swap space still available */ | ||
192 | unsigned short procs; /* Number of current processes */ | ||
193 | unsigned long totalhigh; /* Total high memory size */ | ||
194 | unsigned long freehigh; /* Available high memory size */ | ||
195 | unsigned int mem_unit; /* Memory unit size in bytes */ | ||
196 | char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ | ||
197 | }; | ||
198 | extern int sysinfo (struct sysinfo* info); | ||
199 | |||
200 | const char *make_human_readable_str(unsigned long val, unsigned long hr); | ||
201 | enum { | ||
202 | KILOBYTE = 1024, | ||
203 | MEGABYTE = (KILOBYTE*1024), | ||
204 | GIGABYTE = (MEGABYTE*1024) | ||
205 | }; | ||
206 | |||
207 | int ask_confirmation(void); | ||
208 | |||
209 | #endif /* __LIBBB_H__ */ | ||
diff --git a/libbb/loop.c b/libbb/loop.c new file mode 100644 index 000000000..4754b8da1 --- /dev/null +++ b/libbb/loop.c | |||
@@ -0,0 +1,128 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <fcntl.h> | ||
31 | #include <string.h> | ||
32 | #include <unistd.h> | ||
33 | #include <sys/ioctl.h> | ||
34 | #include "libbb.h" | ||
35 | #include "loop.h" /* Pull in loop device support */ | ||
36 | |||
37 | extern int del_loop(const char *device) | ||
38 | { | ||
39 | int fd; | ||
40 | |||
41 | if ((fd = open(device, O_RDONLY)) < 0) { | ||
42 | perror_msg("%s", device); | ||
43 | return (FALSE); | ||
44 | } | ||
45 | if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { | ||
46 | perror_msg("ioctl: LOOP_CLR_FD"); | ||
47 | return (FALSE); | ||
48 | } | ||
49 | close(fd); | ||
50 | return (TRUE); | ||
51 | } | ||
52 | |||
53 | extern int set_loop(const char *device, const char *file, int offset, | ||
54 | int *loopro) | ||
55 | { | ||
56 | struct loop_info loopinfo; | ||
57 | int fd, ffd, mode; | ||
58 | |||
59 | mode = *loopro ? O_RDONLY : O_RDWR; | ||
60 | if ((ffd = open(file, mode)) < 0 && !*loopro | ||
61 | && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { | ||
62 | perror_msg("%s", file); | ||
63 | return 1; | ||
64 | } | ||
65 | if ((fd = open(device, mode)) < 0) { | ||
66 | close(ffd); | ||
67 | perror_msg("%s", device); | ||
68 | return 1; | ||
69 | } | ||
70 | *loopro = (mode == O_RDONLY); | ||
71 | |||
72 | memset(&loopinfo, 0, sizeof(loopinfo)); | ||
73 | safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE); | ||
74 | |||
75 | loopinfo.lo_offset = offset; | ||
76 | |||
77 | loopinfo.lo_encrypt_key_size = 0; | ||
78 | if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { | ||
79 | perror_msg("ioctl: LOOP_SET_FD"); | ||
80 | close(fd); | ||
81 | close(ffd); | ||
82 | return 1; | ||
83 | } | ||
84 | if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { | ||
85 | (void) ioctl(fd, LOOP_CLR_FD, 0); | ||
86 | perror_msg("ioctl: LOOP_SET_STATUS"); | ||
87 | close(fd); | ||
88 | close(ffd); | ||
89 | return 1; | ||
90 | } | ||
91 | close(fd); | ||
92 | close(ffd); | ||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | extern char *find_unused_loop_device(void) | ||
97 | { | ||
98 | char dev[20]; | ||
99 | int i, fd; | ||
100 | struct stat statbuf; | ||
101 | struct loop_info loopinfo; | ||
102 | |||
103 | for (i = 0; i <= 7; i++) { | ||
104 | sprintf(dev, "/dev/loop%d", i); | ||
105 | if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) { | ||
106 | if ((fd = open(dev, O_RDONLY)) >= 0) { | ||
107 | if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) { | ||
108 | if (errno == ENXIO) { /* probably free */ | ||
109 | close(fd); | ||
110 | return strdup(dev); | ||
111 | } | ||
112 | } | ||
113 | close(fd); | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | return NULL; | ||
118 | } | ||
119 | |||
120 | |||
121 | /* END CODE */ | ||
122 | /* | ||
123 | Local Variables: | ||
124 | c-file-style: "linux" | ||
125 | c-basic-offset: 4 | ||
126 | tab-width: 4 | ||
127 | End: | ||
128 | */ | ||
diff --git a/libbb/mode_string.c b/libbb/mode_string.c new file mode 100644 index 000000000..0a3d6e6f0 --- /dev/null +++ b/libbb/mode_string.c | |||
@@ -0,0 +1,82 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | |||
33 | #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) | ||
34 | #define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) | ||
35 | |||
36 | /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ | ||
37 | static const mode_t SBIT[] = { | ||
38 | 0, 0, S_ISUID, | ||
39 | 0, 0, S_ISGID, | ||
40 | 0, 0, S_ISVTX | ||
41 | }; | ||
42 | |||
43 | /* The 9 mode bits to test */ | ||
44 | static const mode_t MBIT[] = { | ||
45 | S_IRUSR, S_IWUSR, S_IXUSR, | ||
46 | S_IRGRP, S_IWGRP, S_IXGRP, | ||
47 | S_IROTH, S_IWOTH, S_IXOTH | ||
48 | }; | ||
49 | |||
50 | static const char MODE1[] = "rwxrwxrwx"; | ||
51 | static const char MODE0[] = "---------"; | ||
52 | static const char SMODE1[] = "..s..s..t"; | ||
53 | static const char SMODE0[] = "..S..S..T"; | ||
54 | |||
55 | /* | ||
56 | * Return the standard ls-like mode string from a file mode. | ||
57 | * This is static and so is overwritten on each call. | ||
58 | */ | ||
59 | const char *mode_string(int mode) | ||
60 | { | ||
61 | static char buf[12]; | ||
62 | |||
63 | int i; | ||
64 | |||
65 | buf[0] = TYPECHAR(mode); | ||
66 | for (i = 0; i < 9; i++) { | ||
67 | if (mode & SBIT[i]) | ||
68 | buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i]; | ||
69 | else | ||
70 | buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i]; | ||
71 | } | ||
72 | return buf; | ||
73 | } | ||
74 | |||
75 | /* END CODE */ | ||
76 | /* | ||
77 | Local Variables: | ||
78 | c-file-style: "linux" | ||
79 | c-basic-offset: 4 | ||
80 | tab-width: 4 | ||
81 | End: | ||
82 | */ | ||
diff --git a/libbb/parse_mode.c b/libbb/parse_mode.c new file mode 100644 index 000000000..33a878122 --- /dev/null +++ b/libbb/parse_mode.c | |||
@@ -0,0 +1,138 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | |||
33 | /* This function parses the sort of string you might pass | ||
34 | * to chmod (i.e. [ugoa]{+|-|=}[rwxst] ) and returns the | ||
35 | * correct mode described by the string. */ | ||
36 | extern int parse_mode(const char *s, mode_t * theMode) | ||
37 | { | ||
38 | static const mode_t group_set[] = { | ||
39 | S_ISUID | S_IRWXU, /* u */ | ||
40 | S_ISGID | S_IRWXG, /* g */ | ||
41 | S_IRWXO, /* o */ | ||
42 | S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */ | ||
43 | }; | ||
44 | |||
45 | static const mode_t mode_set[] = { | ||
46 | S_IRUSR | S_IRGRP | S_IROTH, /* r */ | ||
47 | S_IWUSR | S_IWGRP | S_IWOTH, /* w */ | ||
48 | S_IXUSR | S_IXGRP | S_IXOTH, /* x */ | ||
49 | S_ISUID | S_ISGID, /* s */ | ||
50 | S_ISVTX /* t */ | ||
51 | }; | ||
52 | |||
53 | static const char group_string[] = "ugoa"; | ||
54 | static const char mode_string[] = "rwxst"; | ||
55 | |||
56 | const char *p; | ||
57 | |||
58 | mode_t andMode = | ||
59 | S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; | ||
60 | mode_t orMode = 0; | ||
61 | mode_t mode; | ||
62 | mode_t groups; | ||
63 | char type; | ||
64 | char c; | ||
65 | |||
66 | if (s==NULL) { | ||
67 | return (FALSE); | ||
68 | } | ||
69 | |||
70 | do { | ||
71 | mode = 0; | ||
72 | groups = 0; | ||
73 | NEXT_GROUP: | ||
74 | if ((c = *s++) == '\0') { | ||
75 | return -1; | ||
76 | } | ||
77 | for (p=group_string ; *p ; p++) { | ||
78 | if (*p == c) { | ||
79 | groups |= group_set[(int)(p-group_string)]; | ||
80 | goto NEXT_GROUP; | ||
81 | } | ||
82 | } | ||
83 | switch (c) { | ||
84 | case '=': | ||
85 | case '+': | ||
86 | case '-': | ||
87 | type = c; | ||
88 | if (groups == 0) { /* The default is "all" */ | ||
89 | groups |= S_ISUID | S_ISGID | S_ISVTX | ||
90 | | S_IRWXU | S_IRWXG | S_IRWXO; | ||
91 | } | ||
92 | break; | ||
93 | default: | ||
94 | if ((c < '0') || (c > '7') || (mode | groups)) { | ||
95 | return (FALSE); | ||
96 | } else { | ||
97 | *theMode = strtol(--s, NULL, 8); | ||
98 | return (TRUE); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | NEXT_MODE: | ||
103 | if (((c = *s++) != '\0') && (c != ',')) { | ||
104 | for (p=mode_string ; *p ; p++) { | ||
105 | if (*p == c) { | ||
106 | mode |= mode_set[(int)(p-mode_string)]; | ||
107 | goto NEXT_MODE; | ||
108 | } | ||
109 | } | ||
110 | break; /* We're done so break out of loop.*/ | ||
111 | } | ||
112 | switch (type) { | ||
113 | case '=': | ||
114 | andMode &= ~(groups); /* Now fall through. */ | ||
115 | case '+': | ||
116 | orMode |= mode & groups; | ||
117 | break; | ||
118 | case '-': | ||
119 | andMode &= ~(mode & groups); | ||
120 | orMode &= ~(mode & groups); | ||
121 | break; | ||
122 | } | ||
123 | } while (c == ','); | ||
124 | |||
125 | *theMode &= andMode; | ||
126 | *theMode |= orMode; | ||
127 | |||
128 | return TRUE; | ||
129 | } | ||
130 | |||
131 | /* END CODE */ | ||
132 | /* | ||
133 | Local Variables: | ||
134 | c-file-style: "linux" | ||
135 | c-basic-offset: 4 | ||
136 | tab-width: 4 | ||
137 | End: | ||
138 | */ | ||
diff --git a/libbb/parse_number.c b/libbb/parse_number.c new file mode 100644 index 000000000..c90511dca --- /dev/null +++ b/libbb/parse_number.c | |||
@@ -0,0 +1,74 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <stdlib.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | |||
34 | unsigned long parse_number(const char *numstr, | ||
35 | const struct suffix_mult *suffixes) | ||
36 | { | ||
37 | const struct suffix_mult *sm; | ||
38 | unsigned long int ret; | ||
39 | int len; | ||
40 | char *end; | ||
41 | |||
42 | ret = strtoul(numstr, &end, 10); | ||
43 | if (numstr == end) | ||
44 | error_msg_and_die("invalid number `%s'", numstr); | ||
45 | while (end[0] != '\0') { | ||
46 | sm = suffixes; | ||
47 | while ( sm != 0 ) { | ||
48 | if(sm->suffix) { | ||
49 | len = strlen(sm->suffix); | ||
50 | if (strncmp(sm->suffix, end, len) == 0) { | ||
51 | ret *= sm->mult; | ||
52 | end += len; | ||
53 | break; | ||
54 | } | ||
55 | sm++; | ||
56 | |||
57 | } else | ||
58 | sm = 0; | ||
59 | } | ||
60 | if (sm == 0) | ||
61 | error_msg_and_die("invalid number `%s'", numstr); | ||
62 | } | ||
63 | return ret; | ||
64 | } | ||
65 | |||
66 | |||
67 | /* END CODE */ | ||
68 | /* | ||
69 | Local Variables: | ||
70 | c-file-style: "linux" | ||
71 | c-basic-offset: 4 | ||
72 | tab-width: 4 | ||
73 | End: | ||
74 | */ | ||
diff --git a/libbb/print_file.c b/libbb/print_file.c new file mode 100644 index 000000000..52a39774f --- /dev/null +++ b/libbb/print_file.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | extern void print_file(FILE *file) | ||
33 | { | ||
34 | int c; | ||
35 | |||
36 | while ((c = getc(file)) != EOF) | ||
37 | putc(c, stdout); | ||
38 | fclose(file); | ||
39 | fflush(stdout); | ||
40 | } | ||
41 | |||
42 | extern int print_file_by_name(char *filename) | ||
43 | { | ||
44 | FILE *file; | ||
45 | if ((file = wfopen(filename, "r")) == NULL) | ||
46 | return FALSE; | ||
47 | print_file(file); | ||
48 | return TRUE; | ||
49 | } | ||
50 | |||
51 | |||
52 | /* END CODE */ | ||
53 | /* | ||
54 | Local Variables: | ||
55 | c-file-style: "linux" | ||
56 | c-basic-offset: 4 | ||
57 | tab-width: 4 | ||
58 | End: | ||
59 | */ | ||
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c new file mode 100644 index 000000000..ad2be94ee --- /dev/null +++ b/libbb/process_escape_sequence.c | |||
@@ -0,0 +1,83 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <limits.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | |||
33 | |||
34 | char process_escape_sequence(char **ptr) | ||
35 | { | ||
36 | static const char charmap[] = { | ||
37 | 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, | ||
38 | '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; | ||
39 | |||
40 | const char *p; | ||
41 | char *q; | ||
42 | int num_digits; | ||
43 | unsigned int n; | ||
44 | |||
45 | n = 0; | ||
46 | q = *ptr; | ||
47 | |||
48 | for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) { | ||
49 | if ((*q < '0') || (*q > '7')) { /* not a digit? */ | ||
50 | break; | ||
51 | } | ||
52 | n = n * 8 + (*q++ - '0'); | ||
53 | } | ||
54 | |||
55 | if (num_digits == 0) { /* mnemonic escape sequence? */ | ||
56 | for (p=charmap ; *p ; p++) { | ||
57 | if (*p == *q) { | ||
58 | q++; | ||
59 | break; | ||
60 | } | ||
61 | } | ||
62 | n = *(p+(sizeof(charmap)/2)); | ||
63 | } | ||
64 | |||
65 | /* doesn't hurt to fall through to here from mnemonic case */ | ||
66 | if (n > UCHAR_MAX) { /* is octal code too big for a char? */ | ||
67 | n /= 8; /* adjust value and */ | ||
68 | --q; /* back up one char */ | ||
69 | } | ||
70 | |||
71 | *ptr = q; | ||
72 | return (char) n; | ||
73 | } | ||
74 | |||
75 | |||
76 | /* END CODE */ | ||
77 | /* | ||
78 | Local Variables: | ||
79 | c-file-style: "linux" | ||
80 | c-basic-offset: 4 | ||
81 | tab-width: 4 | ||
82 | End: | ||
83 | */ | ||
diff --git a/libbb/pwd_grp_wrappers.c b/libbb/pwd_grp_wrappers.c new file mode 100644 index 000000000..e7b7e3820 --- /dev/null +++ b/libbb/pwd_grp_wrappers.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include "../pwd_grp/pwd.h" | ||
31 | #include "../pwd_grp/grp.h" | ||
32 | #include "libbb.h" | ||
33 | |||
34 | |||
35 | |||
36 | /* returns a uid given a username */ | ||
37 | long my_getpwnam(const char *name) | ||
38 | { | ||
39 | struct passwd *myuser; | ||
40 | |||
41 | myuser = getpwnam(name); | ||
42 | if (myuser==NULL) | ||
43 | error_msg_and_die("unknown user name: %s", name); | ||
44 | |||
45 | return myuser->pw_uid; | ||
46 | } | ||
47 | |||
48 | /* returns a gid given a group name */ | ||
49 | long my_getgrnam(const char *name) | ||
50 | { | ||
51 | struct group *mygroup; | ||
52 | |||
53 | mygroup = getgrnam(name); | ||
54 | if (mygroup==NULL) | ||
55 | error_msg_and_die("unknown group name: %s", name); | ||
56 | |||
57 | return (mygroup->gr_gid); | ||
58 | } | ||
59 | |||
60 | /* gets a username given a uid */ | ||
61 | void my_getpwuid(char *name, long uid) | ||
62 | { | ||
63 | struct passwd *myuser; | ||
64 | |||
65 | myuser = getpwuid(uid); | ||
66 | if (myuser==NULL) | ||
67 | sprintf(name, "%-8ld ", (long)uid); | ||
68 | else | ||
69 | strcpy(name, myuser->pw_name); | ||
70 | } | ||
71 | |||
72 | /* gets a groupname given a gid */ | ||
73 | void my_getgrgid(char *group, long gid) | ||
74 | { | ||
75 | struct group *mygroup; | ||
76 | |||
77 | mygroup = getgrgid(gid); | ||
78 | if (mygroup==NULL) | ||
79 | sprintf(group, "%-8ld ", (long)gid); | ||
80 | else | ||
81 | strcpy(group, mygroup->gr_name); | ||
82 | } | ||
83 | |||
84 | /* gets a gid given a user name */ | ||
85 | long my_getpwnamegid(const char *name) | ||
86 | { | ||
87 | struct group *mygroup; | ||
88 | struct passwd *myuser; | ||
89 | |||
90 | myuser=getpwnam(name); | ||
91 | if (myuser==NULL) | ||
92 | error_msg_and_die("unknown user name: %s", name); | ||
93 | |||
94 | mygroup = getgrgid(myuser->pw_gid); | ||
95 | if (mygroup==NULL) | ||
96 | error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid); | ||
97 | |||
98 | return mygroup->gr_gid; | ||
99 | } | ||
100 | |||
101 | |||
102 | /* END CODE */ | ||
103 | /* | ||
104 | Local Variables: | ||
105 | c-file-style: "linux" | ||
106 | c-basic-offset: 4 | ||
107 | tab-width: 4 | ||
108 | End: | ||
109 | */ | ||
diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c new file mode 100644 index 000000000..6b93340be --- /dev/null +++ b/libbb/recursive_action.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <dirent.h> | ||
30 | #include <sys/stat.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | /* same conditions as recursive_action */ | ||
34 | #define bb_need_name_too_long | ||
35 | #define BB_DECLARE_EXTERN | ||
36 | #include "../messages.c" | ||
37 | |||
38 | #undef DEBUG_RECURS_ACTION | ||
39 | |||
40 | |||
41 | /* | ||
42 | * Walk down all the directories under the specified | ||
43 | * location, and do something (something specified | ||
44 | * by the fileAction and dirAction function pointers). | ||
45 | * | ||
46 | * Unfortunately, while nftw(3) could replace this and reduce | ||
47 | * code size a bit, nftw() wasn't supported before GNU libc 2.1, | ||
48 | * and so isn't sufficiently portable to take over since glibc2.1 | ||
49 | * is so stinking huge. | ||
50 | */ | ||
51 | int recursive_action(const char *fileName, | ||
52 | int recurse, int followLinks, int depthFirst, | ||
53 | int (*fileAction) (const char *fileName, | ||
54 | struct stat * statbuf, | ||
55 | void* userData), | ||
56 | int (*dirAction) (const char *fileName, | ||
57 | struct stat * statbuf, | ||
58 | void* userData), | ||
59 | void* userData) | ||
60 | { | ||
61 | int status; | ||
62 | struct stat statbuf; | ||
63 | struct dirent *next; | ||
64 | |||
65 | if (followLinks == TRUE) | ||
66 | status = stat(fileName, &statbuf); | ||
67 | else | ||
68 | status = lstat(fileName, &statbuf); | ||
69 | |||
70 | if (status < 0) { | ||
71 | #ifdef DEBUG_RECURS_ACTION | ||
72 | fprintf(stderr, | ||
73 | "status=%d followLinks=%d TRUE=%d\n", | ||
74 | status, followLinks, TRUE); | ||
75 | #endif | ||
76 | perror_msg("%s", fileName); | ||
77 | return FALSE; | ||
78 | } | ||
79 | |||
80 | if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) { | ||
81 | if (fileAction == NULL) | ||
82 | return TRUE; | ||
83 | else | ||
84 | return fileAction(fileName, &statbuf, userData); | ||
85 | } | ||
86 | |||
87 | if (recurse == FALSE) { | ||
88 | if (S_ISDIR(statbuf.st_mode)) { | ||
89 | if (dirAction != NULL) | ||
90 | return (dirAction(fileName, &statbuf, userData)); | ||
91 | else | ||
92 | return TRUE; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | if (S_ISDIR(statbuf.st_mode)) { | ||
97 | DIR *dir; | ||
98 | |||
99 | if (dirAction != NULL && depthFirst == FALSE) { | ||
100 | status = dirAction(fileName, &statbuf, userData); | ||
101 | if (status == FALSE) { | ||
102 | perror_msg("%s", fileName); | ||
103 | return FALSE; | ||
104 | } else if (status == SKIP) | ||
105 | return TRUE; | ||
106 | } | ||
107 | dir = opendir(fileName); | ||
108 | if (!dir) { | ||
109 | perror_msg("%s", fileName); | ||
110 | return FALSE; | ||
111 | } | ||
112 | status = TRUE; | ||
113 | while ((next = readdir(dir)) != NULL) { | ||
114 | char nextFile[PATH_MAX]; | ||
115 | |||
116 | if ((strcmp(next->d_name, "..") == 0) | ||
117 | || (strcmp(next->d_name, ".") == 0)) { | ||
118 | continue; | ||
119 | } | ||
120 | if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) { | ||
121 | error_msg(name_too_long); | ||
122 | return FALSE; | ||
123 | } | ||
124 | memset(nextFile, 0, sizeof(nextFile)); | ||
125 | if (fileName[strlen(fileName)-1] == '/') | ||
126 | sprintf(nextFile, "%s%s", fileName, next->d_name); | ||
127 | else | ||
128 | sprintf(nextFile, "%s/%s", fileName, next->d_name); | ||
129 | if (recursive_action(nextFile, TRUE, followLinks, depthFirst, | ||
130 | fileAction, dirAction, userData) == FALSE) { | ||
131 | status = FALSE; | ||
132 | } | ||
133 | } | ||
134 | closedir(dir); | ||
135 | if (dirAction != NULL && depthFirst == TRUE) { | ||
136 | if (dirAction(fileName, &statbuf, userData) == FALSE) { | ||
137 | perror_msg("%s", fileName); | ||
138 | return FALSE; | ||
139 | } | ||
140 | } | ||
141 | if (status == FALSE) | ||
142 | return FALSE; | ||
143 | } else { | ||
144 | if (fileAction == NULL) | ||
145 | return TRUE; | ||
146 | else | ||
147 | return fileAction(fileName, &statbuf, userData); | ||
148 | } | ||
149 | return TRUE; | ||
150 | } | ||
151 | |||
152 | |||
153 | /* END CODE */ | ||
154 | /* | ||
155 | Local Variables: | ||
156 | c-file-style: "linux" | ||
157 | c-basic-offset: 4 | ||
158 | tab-width: 4 | ||
159 | End: | ||
160 | */ | ||
diff --git a/libbb/safe_read.c b/libbb/safe_read.c new file mode 100644 index 000000000..dbf4aa7e4 --- /dev/null +++ b/libbb/safe_read.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <unistd.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | |||
34 | |||
35 | ssize_t safe_read(int fd, void *buf, size_t count) | ||
36 | { | ||
37 | ssize_t n; | ||
38 | |||
39 | do { | ||
40 | n = read(fd, buf, count); | ||
41 | } while (n < 0 && errno == EINTR); | ||
42 | |||
43 | return n; | ||
44 | } | ||
45 | |||
46 | |||
47 | /* END CODE */ | ||
48 | /* | ||
49 | Local Variables: | ||
50 | c-file-style: "linux" | ||
51 | c-basic-offset: 4 | ||
52 | tab-width: 4 | ||
53 | End: | ||
54 | */ | ||
diff --git a/libbb/safe_strncpy.c b/libbb/safe_strncpy.c new file mode 100644 index 000000000..55ec79802 --- /dev/null +++ b/libbb/safe_strncpy.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <string.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | |||
33 | /* Like strncpy but make sure the resulting string is always 0 terminated. */ | ||
34 | extern char * safe_strncpy(char *dst, const char *src, size_t size) | ||
35 | { | ||
36 | dst[size-1] = '\0'; | ||
37 | return strncpy(dst, src, size-1); | ||
38 | } | ||
39 | |||
40 | |||
41 | /* END CODE */ | ||
42 | /* | ||
43 | Local Variables: | ||
44 | c-file-style: "linux" | ||
45 | c-basic-offset: 4 | ||
46 | tab-width: 4 | ||
47 | End: | ||
48 | */ | ||
diff --git a/libbb/syscalls.c b/libbb/syscalls.c new file mode 100644 index 000000000..021154602 --- /dev/null +++ b/libbb/syscalls.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include <sys/syscall.h> | ||
31 | #include <linux/unistd.h> | ||
32 | |||
33 | #include "libbb.h" | ||
34 | |||
35 | |||
36 | _syscall1(int, sysinfo, struct sysinfo *, info); | ||
37 | |||
38 | /* Include our own version of <sys/mount.h>, since libc5 doesn't | ||
39 | * know about umount2 */ | ||
40 | extern _syscall1(int, umount, const char *, special_file); | ||
41 | extern _syscall5(int, mount, const char *, special_file, const char *, dir, | ||
42 | const char *, fstype, unsigned long int, rwflag, const void *, data); | ||
43 | |||
44 | #ifndef __NR_umount2 | ||
45 | # warning This kernel does not support the umount2 syscall | ||
46 | # warning The umount2 system call is being stubbed out... | ||
47 | int umount2(const char * special_file, int flags) | ||
48 | { | ||
49 | /* BusyBox was compiled against a kernel that did not support | ||
50 | * the umount2 system call. To make this application work, | ||
51 | * you will need to recompile with a kernel supporting the | ||
52 | * umount2 system call. | ||
53 | */ | ||
54 | fprintf(stderr, "\n\nTo make this application work, you will need to recompile\n"); | ||
55 | fprintf(stderr, "with a kernel supporting the umount2 system call. -Erik\n\n"); | ||
56 | errno=ENOSYS; | ||
57 | return -1; | ||
58 | } | ||
59 | # else | ||
60 | extern _syscall2(int, umount2, const char *, special_file, int, flags); | ||
61 | #endif | ||
62 | |||
63 | #ifndef __NR_query_module | ||
64 | static const int __NR_query_module = 167; | ||
65 | #endif | ||
66 | _syscall5(int, query_module, const char *, name, int, which, | ||
67 | void *, buf, size_t, bufsize, size_t*, ret); | ||
68 | |||
69 | /* END CODE */ | ||
70 | /* | ||
71 | Local Variables: | ||
72 | c-file-style: "linux" | ||
73 | c-basic-offset: 4 | ||
74 | tab-width: 4 | ||
75 | End: | ||
76 | */ | ||
diff --git a/libbb/syslog_msg_with_name.c b/libbb/syslog_msg_with_name.c new file mode 100644 index 000000000..d72755a37 --- /dev/null +++ b/libbb/syslog_msg_with_name.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <sys/syslog.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | extern const char *applet_name; | ||
33 | |||
34 | void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg) | ||
35 | { | ||
36 | openlog(name, 0, facility); | ||
37 | syslog(pri, "%s", msg); | ||
38 | closelog(); | ||
39 | } | ||
40 | |||
41 | void syslog_msg(int facility, int pri, const char *msg) | ||
42 | { | ||
43 | syslog_msg_with_name(applet_name, facility, pri, msg); | ||
44 | } | ||
45 | |||
46 | /* END CODE */ | ||
47 | /* | ||
48 | Local Variables: | ||
49 | c-file-style: "linux" | ||
50 | c-basic-offset: 4 | ||
51 | tab-width: 4 | ||
52 | End: | ||
53 | */ | ||
diff --git a/libbb/time_string.c b/libbb/time_string.c new file mode 100644 index 000000000..076529006 --- /dev/null +++ b/libbb/time_string.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <time.h> | ||
31 | #include <utime.h> | ||
32 | #include "libbb.h" | ||
33 | |||
34 | |||
35 | /* | ||
36 | * Return the standard ls-like time string from a time_t | ||
37 | * This is static and so is overwritten on each call. | ||
38 | */ | ||
39 | const char *time_string(time_t timeVal) | ||
40 | { | ||
41 | time_t now; | ||
42 | char *str; | ||
43 | static char buf[26]; | ||
44 | |||
45 | time(&now); | ||
46 | |||
47 | str = ctime(&timeVal); | ||
48 | |||
49 | strcpy(buf, &str[4]); | ||
50 | buf[12] = '\0'; | ||
51 | |||
52 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { | ||
53 | strcpy(&buf[7], &str[20]); | ||
54 | buf[11] = '\0'; | ||
55 | } | ||
56 | |||
57 | return buf; | ||
58 | } | ||
59 | |||
60 | |||
61 | /* END CODE */ | ||
62 | /* | ||
63 | Local Variables: | ||
64 | c-file-style: "linux" | ||
65 | c-basic-offset: 4 | ||
66 | tab-width: 4 | ||
67 | End: | ||
68 | */ | ||
diff --git a/libbb/trim.c b/libbb/trim.c new file mode 100644 index 000000000..8a9b07a14 --- /dev/null +++ b/libbb/trim.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <ctype.h> | ||
31 | #include "libbb.h" | ||
32 | |||
33 | |||
34 | void trim(char *s) | ||
35 | { | ||
36 | /* trim trailing whitespace */ | ||
37 | while (isspace(s[strlen(s)-1])) | ||
38 | s[strlen(s)-1]='\0'; | ||
39 | |||
40 | /* trim leading whitespace */ | ||
41 | memmove(s, &s[strspn(s, " \n\r\t\v")], strlen(s)); | ||
42 | |||
43 | } | ||
44 | |||
45 | /* END CODE */ | ||
46 | /* | ||
47 | Local Variables: | ||
48 | c-file-style: "linux" | ||
49 | c-basic-offset: 4 | ||
50 | tab-width: 4 | ||
51 | End: | ||
52 | */ | ||
diff --git a/libbb/vdprintf.c b/libbb/vdprintf.c new file mode 100644 index 000000000..a18291b3b --- /dev/null +++ b/libbb/vdprintf.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | |||
31 | |||
32 | |||
33 | #if (__GLIBC__ < 2) | ||
34 | extern int vdprintf(int d, const char *format, va_list ap) | ||
35 | { | ||
36 | char buf[BUF_SIZE]; | ||
37 | int len; | ||
38 | |||
39 | len = vsprintf(buf, format, ap); | ||
40 | return write(d, buf, len); | ||
41 | } | ||
42 | #endif | ||
43 | |||
44 | |||
45 | /* END CODE */ | ||
46 | /* | ||
47 | Local Variables: | ||
48 | c-file-style: "linux" | ||
49 | c-basic-offset: 4 | ||
50 | tab-width: 4 | ||
51 | End: | ||
52 | */ | ||
diff --git a/libbb/wfopen.c b/libbb/wfopen.c new file mode 100644 index 000000000..8b074d2f7 --- /dev/null +++ b/libbb/wfopen.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <errno.h> | ||
30 | #include "libbb.h" | ||
31 | |||
32 | FILE *wfopen(const char *path, const char *mode) | ||
33 | { | ||
34 | FILE *fp; | ||
35 | if ((fp = fopen(path, mode)) == NULL) { | ||
36 | perror_msg("%s", path); | ||
37 | errno = 0; | ||
38 | } | ||
39 | return fp; | ||
40 | } | ||
41 | |||
42 | |||
43 | /* END CODE */ | ||
44 | /* | ||
45 | Local Variables: | ||
46 | c-file-style: "linux" | ||
47 | c-basic-offset: 4 | ||
48 | tab-width: 4 | ||
49 | End: | ||
50 | */ | ||
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c new file mode 100644 index 000000000..e7f41aae2 --- /dev/null +++ b/libbb/xfuncs.c | |||
@@ -0,0 +1,107 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <stdlib.h> | ||
31 | #include <unistd.h> | ||
32 | #include "libbb.h" | ||
33 | |||
34 | /* same conditions as recursive_action */ | ||
35 | #define bb_need_memory_exhausted | ||
36 | #define BB_DECLARE_EXTERN | ||
37 | #include "../messages.c" | ||
38 | |||
39 | |||
40 | #ifndef DMALLOC | ||
41 | extern void *xmalloc(size_t size) | ||
42 | { | ||
43 | void *ptr = malloc(size); | ||
44 | |||
45 | if (!ptr) | ||
46 | error_msg_and_die(memory_exhausted); | ||
47 | return ptr; | ||
48 | } | ||
49 | |||
50 | extern void *xrealloc(void *old, size_t size) | ||
51 | { | ||
52 | void *ptr = realloc(old, size); | ||
53 | if (!ptr) | ||
54 | error_msg_and_die(memory_exhausted); | ||
55 | return ptr; | ||
56 | } | ||
57 | |||
58 | extern void *xcalloc(size_t nmemb, size_t size) | ||
59 | { | ||
60 | void *ptr = calloc(nmemb, size); | ||
61 | if (!ptr) | ||
62 | error_msg_and_die(memory_exhausted); | ||
63 | return ptr; | ||
64 | } | ||
65 | |||
66 | extern char * xstrdup (const char *s) { | ||
67 | char *t; | ||
68 | |||
69 | if (s == NULL) | ||
70 | return NULL; | ||
71 | |||
72 | t = strdup (s); | ||
73 | |||
74 | if (t == NULL) | ||
75 | error_msg_and_die(memory_exhausted); | ||
76 | |||
77 | return t; | ||
78 | } | ||
79 | #endif | ||
80 | |||
81 | extern char * xstrndup (const char *s, int n) { | ||
82 | char *t; | ||
83 | |||
84 | if (s == NULL) | ||
85 | error_msg_and_die("xstrndup bug"); | ||
86 | |||
87 | t = xmalloc(++n); | ||
88 | |||
89 | return safe_strncpy(t,s,n); | ||
90 | } | ||
91 | |||
92 | FILE *xfopen(const char *path, const char *mode) | ||
93 | { | ||
94 | FILE *fp; | ||
95 | if ((fp = fopen(path, mode)) == NULL) | ||
96 | perror_msg_and_die("%s", path); | ||
97 | return fp; | ||
98 | } | ||
99 | |||
100 | /* END CODE */ | ||
101 | /* | ||
102 | Local Variables: | ||
103 | c-file-style: "linux" | ||
104 | c-basic-offset: 4 | ||
105 | tab-width: 4 | ||
106 | End: | ||
107 | */ | ||
diff --git a/libbb/xregcomp.c b/libbb/xregcomp.c new file mode 100644 index 000000000..6f5e2f0cb --- /dev/null +++ b/libbb/xregcomp.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) tons of folks. Tracking down who wrote what | ||
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation; either version 2 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include "libbb.h" | ||
30 | #include <regex.h> | ||
31 | |||
32 | |||
33 | |||
34 | void xregcomp(regex_t *preg, const char *regex, int cflags) | ||
35 | { | ||
36 | int ret; | ||
37 | if ((ret = regcomp(preg, regex, cflags)) != 0) { | ||
38 | int errmsgsz = regerror(ret, preg, NULL, 0); | ||
39 | char *errmsg = xmalloc(errmsgsz); | ||
40 | regerror(ret, preg, errmsg, errmsgsz); | ||
41 | error_msg_and_die("xregcomp: %s", errmsg); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | |||
46 | /* END CODE */ | ||
47 | /* | ||
48 | Local Variables: | ||
49 | c-file-style: "linux" | ||
50 | c-basic-offset: 4 | ||
51 | tab-width: 4 | ||
52 | End: | ||
53 | */ | ||