blob: 69637c4751976c9555fe853c58ef62c95914d5a2 [file] [log] [blame]
Eric Andersencc8ed391999-10-05 16:24:54 +00001/*
2 * Utility routines.
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 * Copyright (C) tons of folks. Tracking down who wrote what
5 * isn't something I'm going to worry about... If you wrote something
6 * here, please feel free to acknowledge your work.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
23 * Permission has been granted to redistribute this code under the GPL.
24 *
25 */
26
Eric Andersen2b69c401999-10-05 22:58:32 +000027#include "internal.h"
28#include <stdio.h>
29#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000032#include <dirent.h>
33#include <time.h>
34#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000035#include <sys/stat.h>
36#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000037#include <ctype.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000038
Erik Andersen5cbdd712000-01-26 20:06:48 +000039#if defined BB_FEATURE_MOUNT_LOOP
40#include <fcntl.h>
41#include <sys/ioctl.h>
42#include <linux/loop.h>
43#endif
44
45
Eric Andersend0246fb1999-11-04 21:18:07 +000046#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000047# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000048const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000049# else
50# if defined BB_MTAB
51const char mtab_file[] = "/etc/mtab";
52# else
53# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
54# endif
55# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000056#endif
57
58
Eric Andersend73dc5b1999-11-10 23:13:02 +000059extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000060{
Eric Andersena07f0b01999-10-22 19:49:09 +000061 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
Eric Andersenb0e9a701999-10-18 22:28:26 +000062 fprintf(stderr, "Usage: %s\n", usage);
63 exit(FALSE);
64}
65
66
Eric Andersend23f9ba1999-10-20 19:18:15 +000067#if defined (BB_INIT) || defined (BB_PS)
68
Eric Andersen0ecb54a1999-12-05 23:24:55 +000069#if ! defined BB_FEATURE_USE_PROCFS
70#error Sorry, I depend on the /proc filesystem right now.
71#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +000072/* Returns kernel version encoded as major*65536 + minor*256 + patch,
73 * so, for example, to check if the kernel is greater than 2.2.11:
74 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
75 */
76int
77get_kernel_revision()
78{
Eric Andersenaa0765e1999-10-22 04:30:20 +000079 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +000080 int major=0, minor=0, patch=0;
Eric Andersenaa0765e1999-10-22 04:30:20 +000081 char* filename="/proc/sys/kernel/osrelease";
Eric Andersend23f9ba1999-10-20 19:18:15 +000082
Eric Andersenaa0765e1999-10-22 04:30:20 +000083 file = fopen(filename,"r");
84 if (file == NULL) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +000085 /* bummer, /proc must not be mounted... */
Eric Andersenaa0765e1999-10-22 04:30:20 +000086 return( 0);
87 }
88 fscanf(file,"%d.%d.%d",&major,&minor,&patch);
89 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +000090 return major*65536 + minor*256 + patch;
91}
92
93#endif
94
95
Eric Andersenc6cb79d1999-10-13 18:01:10 +000096
97#if defined (BB_CP) || defined (BB_MV)
98/*
99 * Return TRUE if a fileName is a directory.
100 * Nonexistant files return FALSE.
101 */
102int isDirectory(const char *name)
Eric Andersencc8ed391999-10-05 16:24:54 +0000103{
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000104 struct stat statBuf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000105
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000106 if (stat(name, &statBuf) < 0)
107 return FALSE;
Eric Andersen9b587181999-10-17 05:43:39 +0000108 if (S_ISDIR(statBuf.st_mode))
109 return TRUE;
110 return(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000111}
112
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000113
114/*
115 * Copy one file to another, while possibly preserving its modes, times,
116 * and modes. Returns TRUE if successful, or FALSE on a failure with an
117 * error message output. (Failure is not indicted if the attributes cannot
118 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000119 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000120 */
121int
Eric Andersen3c163821999-10-14 22:16:57 +0000122copyFile( const char *srcName, const char *destName,
123 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000124{
125 int rfd;
126 int wfd;
127 int rcc;
128 int result;
129 char buf[BUF_SIZE];
130 struct stat srcStatBuf;
131 struct stat dstStatBuf;
132 struct utimbuf times;
133
Erik Andersence5b4662000-01-27 19:50:47 +0000134 /* Grab the source file's stats */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000135 if (followLinks == FALSE)
136 result = stat(srcName, &srcStatBuf);
137 else
138 result = lstat(srcName, &srcStatBuf);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000139 if (result < 0) {
140 perror(srcName);
141 return FALSE;
142 }
143
Erik Andersence5b4662000-01-27 19:50:47 +0000144 /* Grab the dest file's stats */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000145 if (followLinks == FALSE)
146 result = stat(destName, &dstStatBuf);
147 else
148 result = lstat(destName, &dstStatBuf);
149 if (result < 0) {
150 dstStatBuf.st_ino = -1;
151 dstStatBuf.st_dev = -1;
152 }
153
154 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
155 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
156 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
157 return FALSE;
158 }
159
160 if (S_ISDIR(srcStatBuf.st_mode)) {
161 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
162 /* Make sure the directory is writable */
Erik Andersen2fe08c71999-12-29 02:10:35 +0000163 result = mkdir(destName, 0777777 ^ umask(0));
164 if (result < 0 && errno != EEXIST) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000165 perror(destName);
166 return (FALSE);
167 }
168 } else if (S_ISLNK(srcStatBuf.st_mode)) {
169 char *link_val;
170 int link_size;
171
172 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
173 link_val = (char *) alloca(PATH_MAX + 2);
174 link_size = readlink(srcName, link_val, PATH_MAX + 1);
175 if (link_size < 0) {
176 perror(srcName);
177 return (FALSE);
178 }
179 link_val[link_size] = '\0';
Eric Andersen3c163821999-10-14 22:16:57 +0000180 link_size = symlink(link_val, destName);
181 if (link_size != 0) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000182 perror(destName);
183 return (FALSE);
184 }
185 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
186 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000187 if (mkfifo(destName, 0644)) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000188 perror(destName);
189 return (FALSE);
190 }
191 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
192 || S_ISSOCK (srcStatBuf.st_mode)) {
193 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
194 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
195 perror(destName);
196 return (FALSE);
197 }
198 } else if (S_ISREG(srcStatBuf.st_mode)) {
199 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
200 rfd = open(srcName, O_RDONLY);
201 if (rfd < 0) {
202 perror(srcName);
203 return FALSE;
204 }
205
206 wfd = creat(destName, srcStatBuf.st_mode);
207 if (wfd < 0) {
208 perror(destName);
209 close(rfd);
210 return FALSE;
211 }
212
213 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
214 if (fullWrite(wfd, buf, rcc) < 0)
215 goto error_exit;
216 }
217 if (rcc < 0) {
218 goto error_exit;
219 }
220
221 close(rfd);
222 if (close(wfd) < 0) {
223 return FALSE;
224 }
225 }
226
227 if (setModes == TRUE) {
Erik Andersence5b4662000-01-27 19:50:47 +0000228 if (! S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4d1d0111999-12-17 18:44:15 +0000229 chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersence5b4662000-01-27 19:50:47 +0000230 /* Never chmod a symlink; it follows the link */
231 chmod(destName, srcStatBuf.st_mode);
232 }
233#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
234 else {
235 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
236 }
237#endif
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000238 times.actime = srcStatBuf.st_atime;
239 times.modtime = srcStatBuf.st_mtime;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000240 utime(destName, &times);
241 }
242
243 return TRUE;
244
245
246 error_exit:
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000247 perror(destName);
248 close(rfd);
249 close(wfd);
250
251 return FALSE;
252}
Eric Andersencc8ed391999-10-05 16:24:54 +0000253#endif
254
255
256
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000257#if defined BB_TAR || defined BB_LS
258
259#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
260#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
261
262/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
263static const mode_t SBIT[] = {
264 0, 0, S_ISUID,
265 0, 0, S_ISGID,
266 0, 0, S_ISVTX
267};
268
269/* The 9 mode bits to test */
270static const mode_t MBIT[] = {
271 S_IRUSR, S_IWUSR, S_IXUSR,
272 S_IRGRP, S_IWGRP, S_IXGRP,
273 S_IROTH, S_IWOTH, S_IXOTH
274};
275
276#define MODE1 "rwxrwxrwx"
277#define MODE0 "---------"
278#define SMODE1 "..s..s..t"
279#define SMODE0 "..S..S..T"
280
Eric Andersencc8ed391999-10-05 16:24:54 +0000281/*
282 * Return the standard ls-like mode string from a file mode.
283 * This is static and so is overwritten on each call.
284 */
Eric Andersenf811e071999-10-09 00:25:00 +0000285const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000286{
Eric Andersenf811e071999-10-09 00:25:00 +0000287 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000288
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000289 int i;
290 buf[0] = TYPECHAR(mode);
291 for (i=0; i<9; i++) {
292 if (mode & SBIT[i])
293 buf[i+1] = (mode & MBIT[i])?
294 SMODE1[i] : SMODE0[i];
295 else
296 buf[i+1] = (mode & MBIT[i])?
297 MODE1[i] : MODE0[i];
298 }
Eric Andersenf811e071999-10-09 00:25:00 +0000299 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000300}
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000301#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000302
303
Eric Andersen1792f8c1999-12-09 06:11:36 +0000304#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000305/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000306 * Return the standard ls-like time string from a time_t
307 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000308 */
Eric Andersenf811e071999-10-09 00:25:00 +0000309const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000310{
Eric Andersenf811e071999-10-09 00:25:00 +0000311 time_t now;
312 char *str;
313 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000314
Eric Andersenf811e071999-10-09 00:25:00 +0000315 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000316
Eric Andersenf811e071999-10-09 00:25:00 +0000317 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000318
Eric Andersenf811e071999-10-09 00:25:00 +0000319 strcpy(buf, &str[4]);
320 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000321
Eric Andersenf811e071999-10-09 00:25:00 +0000322 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
323 strcpy(&buf[7], &str[20]);
324 buf[11] = '\0';
325 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000326
Eric Andersenf811e071999-10-09 00:25:00 +0000327 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000328}
Erik Andersen31638212000-01-15 22:28:50 +0000329#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000330
Erik Andersen31638212000-01-15 22:28:50 +0000331#if defined BB_TAR || defined BB_CP || defined BB_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000332/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000333 * Write all of the supplied buffer out to a file.
334 * This does multiple writes as necessary.
335 * Returns the amount written, or -1 on an error.
336 */
Eric Andersenf811e071999-10-09 00:25:00 +0000337int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000338{
Eric Andersenf811e071999-10-09 00:25:00 +0000339 int cc;
340 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000341
Eric Andersenf811e071999-10-09 00:25:00 +0000342 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000343
Eric Andersenf811e071999-10-09 00:25:00 +0000344 while (len > 0) {
345 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000346
Eric Andersenf811e071999-10-09 00:25:00 +0000347 if (cc < 0)
348 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000349
Eric Andersenf811e071999-10-09 00:25:00 +0000350 buf += cc;
351 total += cc;
352 len -= cc;
353 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000354
Eric Andersenf811e071999-10-09 00:25:00 +0000355 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000356}
Eric Andersen1792f8c1999-12-09 06:11:36 +0000357#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000358
359
Eric Andersen1792f8c1999-12-09 06:11:36 +0000360#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000361/*
362 * Read all of the supplied buffer from a file.
363 * This does multiple reads as necessary.
364 * Returns the amount read, or -1 on an error.
365 * A short read is returned on an end of file.
366 */
Eric Andersenf811e071999-10-09 00:25:00 +0000367int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000368{
Eric Andersenf811e071999-10-09 00:25:00 +0000369 int cc;
370 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000371
Eric Andersenf811e071999-10-09 00:25:00 +0000372 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000373
Eric Andersenf811e071999-10-09 00:25:00 +0000374 while (len > 0) {
375 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000376
Eric Andersenf811e071999-10-09 00:25:00 +0000377 if (cc < 0)
378 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000379
Eric Andersenf811e071999-10-09 00:25:00 +0000380 if (cc == 0)
381 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000382
Eric Andersenf811e071999-10-09 00:25:00 +0000383 buf += cc;
384 total += cc;
385 len -= cc;
386 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
Eric Andersenf811e071999-10-09 00:25:00 +0000388 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000389}
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000390#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000391
392
Erik Andersen7dc16072000-01-04 01:10:25 +0000393#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS) || defined (BB_INSMOD)
Eric Andersencc8ed391999-10-05 16:24:54 +0000394/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000395 * Walk down all the directories under the specified
396 * location, and do something (something specified
397 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000398 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000399 * Unfortunatly, while nftw(3) could replace this and reduce
400 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000401 * and so isn't sufficiently portable to take over since glibc2.1
402 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000403 */
404int
Eric Andersen63a0e531999-10-22 05:12:14 +0000405recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
Eric Andersen9b587181999-10-17 05:43:39 +0000406 int (*fileAction) (const char *fileName, struct stat* statbuf),
407 int (*dirAction) (const char *fileName, struct stat* statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000408{
Eric Andersenf811e071999-10-09 00:25:00 +0000409 int status;
Erik Andersen05df2392000-01-13 04:43:48 +0000410 struct stat statbuf, statbuf1;
Eric Andersenf811e071999-10-09 00:25:00 +0000411 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000412
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000413 if (followLinks == TRUE)
Eric Andersenf811e071999-10-09 00:25:00 +0000414 status = stat(fileName, &statbuf);
Eric Andersen3c163821999-10-14 22:16:57 +0000415 else
416 status = lstat(fileName, &statbuf);
417
Eric Andersencc8ed391999-10-05 16:24:54 +0000418 if (status < 0) {
Erik Andersence5b4662000-01-27 19:50:47 +0000419 fprintf(stderr, "status=%d followLinks=%d TRUE=%d\n", status, followLinks, TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000420 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000421 return (FALSE);
422 }
423
Eric Andersen50d63601999-11-09 01:47:36 +0000424 if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
425 if (fileAction == NULL)
426 return (TRUE);
427 else
428 return (fileAction(fileName, &statbuf));
429 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000430
Eric Andersenf811e071999-10-09 00:25:00 +0000431 if (recurse == FALSE) {
432 if (S_ISDIR(statbuf.st_mode)) {
Eric Andersen3c163821999-10-14 22:16:57 +0000433 if (dirAction != NULL)
Eric Andersen9b587181999-10-17 05:43:39 +0000434 return (dirAction(fileName, &statbuf));
Eric Andersenf811e071999-10-09 00:25:00 +0000435 else
Eric Andersen3c163821999-10-14 22:16:57 +0000436 return (TRUE);
437 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000438 }
Erik Andersen05df2392000-01-13 04:43:48 +0000439
440 status = lstat(fileName, &statbuf1);
441 if (status < 0) {
442 perror(fileName);
443 return (FALSE);
444 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000445
Erik Andersen05df2392000-01-13 04:43:48 +0000446 if (S_ISDIR(statbuf.st_mode) && S_ISDIR(statbuf1.st_mode)) {
Eric Andersen2b69c401999-10-05 22:58:32 +0000447 DIR *dir;
448 dir = opendir(fileName);
449 if (!dir) {
450 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000451 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000452 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000453 if (dirAction != NULL && depthFirst == FALSE) {
Eric Andersen9b587181999-10-17 05:43:39 +0000454 status = dirAction(fileName, &statbuf);
Eric Andersenf811e071999-10-09 00:25:00 +0000455 if (status == FALSE) {
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000456 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000457 return (FALSE);
458 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000459 }
Eric Andersenf811e071999-10-09 00:25:00 +0000460 while ((next = readdir(dir)) != NULL) {
461 char nextFile[NAME_MAX];
462 if ((strcmp(next->d_name, "..") == 0)
463 || (strcmp(next->d_name, ".") == 0)) {
464 continue;
465 }
466 sprintf(nextFile, "%s/%s", fileName, next->d_name);
467 status =
Eric Andersen63a0e531999-10-22 05:12:14 +0000468 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Eric Andersenbed30e91999-10-18 19:02:32 +0000469 fileAction, dirAction);
Eric Andersenf811e071999-10-09 00:25:00 +0000470 if (status < 0) {
471 closedir(dir);
472 return (FALSE);
473 }
474 }
475 status = closedir(dir);
Eric Andersen2b69c401999-10-05 22:58:32 +0000476 if (status < 0) {
477 perror(fileName);
Eric Andersenf811e071999-10-09 00:25:00 +0000478 return (FALSE);
Eric Andersen2b69c401999-10-05 22:58:32 +0000479 }
Eric Andersen63a0e531999-10-22 05:12:14 +0000480 if (dirAction != NULL && depthFirst == TRUE) {
Eric Andersenbed30e91999-10-18 19:02:32 +0000481 status = dirAction(fileName, &statbuf);
482 if (status == FALSE) {
483 perror(fileName);
484 return (FALSE);
485 }
486 }
Eric Andersenf811e071999-10-09 00:25:00 +0000487 } else {
Eric Andersenf811e071999-10-09 00:25:00 +0000488 if (fileAction == NULL)
489 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000490 else
Eric Andersen9b587181999-10-17 05:43:39 +0000491 return (fileAction(fileName, &statbuf));
Eric Andersencc8ed391999-10-05 16:24:54 +0000492 }
Eric Andersenf811e071999-10-09 00:25:00 +0000493 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000494}
495
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000496#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000497
Eric Andersenf6be9441999-10-13 21:12:06 +0000498
499
Erik Andersena8991081999-12-29 03:34:00 +0000500#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000501/*
502 * Attempt to create the directories along the specified path, except for
503 * the final component. The mode is given for the final directory only,
504 * while all previous ones get default protections. Errors are not reported
505 * here, as failures to restore files can be reported later.
506 */
Erik Andersen06936df2000-01-23 02:14:20 +0000507extern int createPath (const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000508{
509 char *cp;
510 char *cpOld;
511 char buf[NAME_MAX];
Erik Andersen06936df2000-01-23 02:14:20 +0000512 int retVal=0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000513
Eric Andersencb41c2e1999-11-22 07:41:00 +0000514 strcpy( buf, name);
Eric Andersenf6be9441999-10-13 21:12:06 +0000515 cp = strchr (buf, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000516 while (cp) {
517 cpOld = cp;
518 cp = strchr (cp + 1, '/');
Eric Andersenf6be9441999-10-13 21:12:06 +0000519 *cpOld = '\0';
Erik Andersen06936df2000-01-23 02:14:20 +0000520 retVal = mkdir (buf, cp ? 0777 : mode);
Erik Andersence5b4662000-01-27 19:50:47 +0000521 if (retVal != 0 && errno != EEXIST) {
522 perror( buf);
Erik Andersen06936df2000-01-23 02:14:20 +0000523 return( FALSE);
524 }
Erik Andersence5b4662000-01-27 19:50:47 +0000525 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000526 }
527 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000528}
529#endif
530
531
532
533#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000534/* [ugoa]{+|-|=}[rwxst] */
535
536
537
538extern int
539parse_mode( const char* s, mode_t* theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000540{
Eric Andersence8f3b91999-10-20 07:03:36 +0000541 mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
542 mode_t orMode = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000543 mode_t mode = 0;
Eric Andersence8f3b91999-10-20 07:03:36 +0000544 mode_t groups = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000545 char type;
546 char c;
547
548 do {
549 for ( ; ; ) {
550 switch ( c = *s++ ) {
551 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000552 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000553 case 'u':
554 groups |= S_ISUID|S_IRWXU;
555 continue;
556 case 'g':
557 groups |= S_ISGID|S_IRWXG;
558 continue;
559 case 'o':
560 groups |= S_IRWXO;
561 continue;
562 case 'a':
563 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
564 continue;
565 case '+':
566 case '=':
567 case '-':
568 type = c;
Eric Andersence8f3b91999-10-20 07:03:36 +0000569 if ( groups == 0 ) /* The default is "all" */
Eric Andersenf6be9441999-10-13 21:12:06 +0000570 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
571 break;
572 default:
Eric Andersenfa0540f1999-10-22 18:18:31 +0000573 if ( isdigit(c) && c >= '0' && c <= '7' &&
574 mode == 0 && groups == 0 ) {
575 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000576 return (TRUE);
577 }
578 else
579 return (FALSE);
580 }
581 break;
582 }
583
584 while ( (c = *s++) != '\0' ) {
585 switch ( c ) {
586 case ',':
587 break;
588 case 'r':
589 mode |= S_IRUSR|S_IRGRP|S_IROTH;
590 continue;
591 case 'w':
592 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
593 continue;
594 case 'x':
595 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
596 continue;
597 case 's':
598 mode |= S_IXGRP|S_ISUID|S_ISGID;
599 continue;
600 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000601 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000602 continue;
603 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000604 *theMode &= andMode;
605 *theMode |= orMode;
606 return( TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000607 }
608 break;
609 }
610 switch ( type ) {
611 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000612 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000613 /* fall through */
614 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000615 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000616 break;
617 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000618 andMode &= ~(mode & groups);
619 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000620 break;
621 }
622 } while ( c == ',' );
Eric Andersence8f3b91999-10-20 07:03:36 +0000623 *theMode &= andMode;
624 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000625 return (TRUE);
626}
Eric Andersence8f3b91999-10-20 07:03:36 +0000627
628
Eric Andersenf6be9441999-10-13 21:12:06 +0000629#endif
630
Eric Andersene674eb71999-10-19 20:52:57 +0000631
632
Eric Andersend23f9ba1999-10-20 19:18:15 +0000633
634
635
636
637#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
638
639/* Use this to avoid needing the glibc NSS stuff
640 * This uses storage buf to hold things.
641 * */
642uid_t
643my_getid(const char *filename, char *name, uid_t id)
644{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000645 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000646 char *rname, *start, *end, buf[128];
647 uid_t rid;
648
Eric Andersenaa0765e1999-10-22 04:30:20 +0000649 file=fopen(filename,"r");
650 if (file == NULL) {
651 perror(filename);
652 return (-1);
653 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000654
Eric Andersenaa0765e1999-10-22 04:30:20 +0000655 while (fgets (buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000656 if (buf[0] == '#')
657 continue;
658
659 start = buf;
660 end = strchr (start, ':');
661 if (end == NULL)
662 continue;
663 *end = '\0';
664 rname = start;
665
666 start = end + 1;
667 end = strchr (start, ':');
668 if (end == NULL)
669 continue;
670
671 start = end + 1;
672 rid = (uid_t) strtol (start, &end, 10);
673 if (end == start)
674 continue;
675
676 if (name) {
Eric Andersen48091fb1999-12-09 01:15:52 +0000677 if (0 == strcmp(rname, name)) {
678 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000679 return( rid);
Eric Andersen48091fb1999-12-09 01:15:52 +0000680 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000681 }
682 if ( id != -1 && id == rid ) {
683 strncpy(name, rname, 8);
Eric Andersen48091fb1999-12-09 01:15:52 +0000684 fclose( file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000685 return( TRUE);
686 }
687 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000688 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000689 return (-1);
690}
691
692uid_t
693my_getpwnam(char *name)
694{
695 return my_getid("/etc/passwd", name, -1);
696}
697
698gid_t
699my_getgrnam(char *name)
700{
701 return my_getid("/etc/group", name, -1);
702}
703
704void
705my_getpwuid(char* name, uid_t uid)
706{
707 my_getid("/etc/passwd", name, uid);
708}
709
710void
711my_getgrgid(char* group, gid_t gid)
712{
713 my_getid("/etc/group", group, gid);
714}
715
716
717#endif
718
719
Eric Andersenaa0765e1999-10-22 04:30:20 +0000720
Eric Andersen0460ff21999-10-25 23:32:44 +0000721
722#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
723
724
725#include <linux/kd.h>
726#include <sys/ioctl.h>
727
728int is_a_console(int fd)
729{
730 char arg;
731
732 arg = 0;
733 return (ioctl(fd, KDGKBTYPE, &arg) == 0
734 && ((arg == KB_101) || (arg == KB_84)));
735}
736
737static int open_a_console(char *fnam)
738{
739 int fd;
740
741 /* try read-only */
742 fd = open(fnam, O_RDWR);
743
744 /* if failed, try read-only */
745 if (fd < 0 && errno == EACCES)
746 fd = open(fnam, O_RDONLY);
747
748 /* if failed, try write-only */
749 if (fd < 0 && errno == EACCES)
750 fd = open(fnam, O_WRONLY);
751
752 /* if failed, fail */
753 if (fd < 0)
754 return -1;
755
756 /* if not a console, fail */
757 if (! is_a_console(fd))
758 {
759 close(fd);
760 return -1;
761 }
762
763 /* success */
764 return fd;
765}
766
767/*
768 * Get an fd for use with kbd/console ioctls.
769 * We try several things because opening /dev/console will fail
770 * if someone else used X (which does a chown on /dev/console).
771 *
772 * if tty_name is non-NULL, try this one instead.
773 */
774
775int get_console_fd(char* tty_name)
776{
777 int fd;
778
779 if (tty_name)
780 {
781 if (-1 == (fd = open_a_console(tty_name)))
782 return -1;
783 else
784 return fd;
785 }
786
787 fd = open_a_console("/dev/tty");
788 if (fd >= 0)
789 return fd;
790
791 fd = open_a_console("/dev/tty0");
792 if (fd >= 0)
793 return fd;
794
795 fd = open_a_console("/dev/console");
796 if (fd >= 0)
797 return fd;
798
799 for (fd = 0; fd < 3; fd++)
800 if (is_a_console(fd))
801 return fd;
802
803 fprintf(stderr,
804 "Couldnt get a file descriptor referring to the console\n");
805 return -1; /* total failure */
806}
807
808
809#endif
810
811
Eric Andersenb186d981999-12-03 09:19:54 +0000812#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000813
814/* Do a case insensitive strstr() */
815char* stristr(char *haystack, const char *needle)
816{
817 int len = strlen( needle );
818 while( *haystack ) {
819 if( !strncasecmp( haystack, needle, len ) )
820 break;
821 haystack++;
822 }
823
824 if( !(*haystack) )
825 haystack = NULL;
826
827 return haystack;
828}
829
Eric Andersenc1525e81999-10-29 00:07:31 +0000830/* This tries to find a needle in a haystack, but does so by
831 * only trying to match literal strings (look 'ma, no regexps!)
832 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000833 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000834 * -Erik Andersen
835 */
836extern int find_match(char *haystack, char *needle, int ignoreCase)
837{
838
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000839 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000840 haystack = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000841 else
842 haystack = stristr (haystack, needle);
843 if (haystack == NULL)
844 return FALSE;
845 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000846}
847
848
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000849/* This performs substitutions after a string match has been found. */
Eric Andersenc1525e81999-10-29 00:07:31 +0000850extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
851{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000852 int foundOne=0;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000853 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000854
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000855 if (ignoreCase == FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000856 where = strstr (haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000857 else
858 where = stristr (haystack, needle);
859
860 if (strcmp(needle, newNeedle)==0)
861 return FALSE;
862
863 oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
864 while(where!=NULL) {
865 foundOne++;
866 strcpy(oldhayStack, haystack);
867#if 0
868 if ( strlen(newNeedle) > strlen(needle)) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000869 haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) -
870 strlen(needle) + strlen(newNeedle)));
Eric Andersenc1525e81999-10-29 00:07:31 +0000871 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000872#endif
873 for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
874 *slider=0;
875 haystack=strcat(haystack, newNeedle);
876 slider1+=strlen(needle);
877 haystack = strcat(haystack, slider1);
878 where = strstr (slider, needle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000879 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000880 free( oldhayStack);
881
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000882 if (foundOne > 0)
Eric Andersenc1525e81999-10-29 00:07:31 +0000883 return TRUE;
884 else
885 return FALSE;
886}
887
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000888
Eric Andersenc1525e81999-10-29 00:07:31 +0000889#endif
Eric Andersen29d2e361999-11-06 06:07:27 +0000890
891
Eric Andersenb186d981999-12-03 09:19:54 +0000892#if defined BB_FIND
893/*
894 * Routine to see if a text string is matched by a wildcard pattern.
895 * Returns TRUE if the text is matched, or FALSE if it is not matched
896 * or if the pattern is invalid.
897 * * matches zero or more characters
898 * ? matches a single character
899 * [abc] matches 'a', 'b' or 'c'
900 * \c quotes character c
901 * Adapted from code written by Ingo Wilken, and
902 * then taken from sash, Copyright (c) 1999 by David I. Bell
903 * Permission is granted to use, distribute, or modify this source,
904 * provided that this copyright notice remains intact.
905 * Permission to distribute this code under the GPL has been granted.
906 */
907extern int
908check_wildcard_match(const char* text, const char* pattern)
909{
910 const char* retryPat;
911 const char* retryText;
912 int ch;
913 int found;
914
915 retryPat = NULL;
916 retryText = NULL;
917
918 while (*text || *pattern)
919 {
920 ch = *pattern++;
921
922 switch (ch)
923 {
924 case '*':
925 retryPat = pattern;
926 retryText = text;
927 break;
928
929 case '[':
930 found = FALSE;
931
932 while ((ch = *pattern++) != ']')
933 {
934 if (ch == '\\')
935 ch = *pattern++;
936
937 if (ch == '\0')
938 return FALSE;
939
940 if (*text == ch)
941 found = TRUE;
942 }
943
944 //if (!found)
945 if (found==TRUE)
946 {
947 pattern = retryPat;
948 text = ++retryText;
949 }
950
951 /* fall into next case */
952
953 case '?':
954 if (*text++ == '\0')
955 return FALSE;
956
957 break;
958
959 case '\\':
960 ch = *pattern++;
961
962 if (ch == '\0')
963 return FALSE;
964
965 /* fall into next case */
966
967 default:
968 if (*text == ch)
969 {
970 if (*text)
971 text++;
972 break;
973 }
974
975 if (*text)
976 {
977 pattern = retryPat;
978 text = ++retryText;
979 break;
980 }
981
982 return FALSE;
983 }
984
985 if (pattern == NULL)
986 return FALSE;
987 }
988
989 return TRUE;
990}
991#endif
992
993
Eric Andersen29d2e361999-11-06 06:07:27 +0000994
995
Erik Andersen7dc16072000-01-04 01:10:25 +0000996#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +0000997/*
998 * Given a block device, find the mount table entry if that block device
999 * is mounted.
1000 *
1001 * Given any other file (or directory), find the mount table entry for its
1002 * filesystem.
1003 */
1004extern struct mntent *findMountPoint(const char *name, const char *table)
1005{
1006 struct stat s;
1007 dev_t mountDevice;
1008 FILE *mountTable;
1009 struct mntent *mountEntry;
1010
1011 if (stat(name, &s) != 0)
1012 return 0;
1013
1014 if ((s.st_mode & S_IFMT) == S_IFBLK)
1015 mountDevice = s.st_rdev;
1016 else
1017 mountDevice = s.st_dev;
1018
1019
1020 if ((mountTable = setmntent(table, "r")) == 0)
1021 return 0;
1022
1023 while ((mountEntry = getmntent(mountTable)) != 0) {
1024 if (strcmp(name, mountEntry->mnt_dir) == 0
1025 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1026 break;
1027 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1028 break;
1029 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1030 break;
1031 }
1032 endmntent(mountTable);
1033 return mountEntry;
1034}
Eric Andersen29d2e361999-11-06 06:07:27 +00001035#endif
1036
1037
1038
Eric Andersen1792f8c1999-12-09 06:11:36 +00001039#if defined BB_DD || defined BB_TAIL
1040/*
1041 * Read a number with a possible multiplier.
1042 * Returns -1 if the number format is illegal.
1043 */
1044extern long getNum (const char *cp)
1045{
1046 long value;
1047
1048 if (!isDecimal (*cp))
1049 return -1;
1050
1051 value = 0;
1052
1053 while (isDecimal (*cp))
1054 value = value * 10 + *cp++ - '0';
1055
1056 switch (*cp++) {
1057 case 'm':
1058 value *= 1048576;
1059 break;
1060
1061 case 'k':
1062 value *= 1024;
1063 break;
1064
1065 case 'b':
1066 value *= 512;
1067 break;
1068
1069 case 'w':
1070 value *= 2;
1071 break;
1072
1073 case '\0':
1074 return value;
1075
1076 default:
1077 return -1;
1078 }
1079
1080 if (*cp)
1081 return -1;
1082
1083 return value;
1084}
1085#endif
1086
Eric Andersen6805d5d1999-12-09 22:39:55 +00001087
1088#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
1089
1090#if ! defined BB_FEATURE_USE_PROCFS
1091#error Sorry, I depend on the /proc filesystem right now.
1092#endif
John Beppuf95ca971999-12-09 22:10:18 +00001093/* findInitPid()
1094 *
1095 * This finds the pid of init (which is not always 1).
1096 * Currently, it's implemented by rummaging through the proc filesystem.
1097 *
1098 * [return]
1099 * 0 failure
1100 * pid when init's pid is found.
1101 */
1102extern pid_t
1103findInitPid()
1104{
1105 pid_t init_pid;
1106 char filename[256];
1107 char buffer[256];
1108
1109 /* no need to opendir ;) */
1110 for (init_pid = 1; init_pid < 65536; init_pid++) {
1111 FILE *status;
1112
1113 sprintf(filename, "/proc/%d/status", init_pid);
1114 status = fopen(filename, "r");
1115 if (!status) { continue; }
1116 fgets(buffer, 256, status);
1117 fclose(status);
1118
Eric Andersen6805d5d1999-12-09 22:39:55 +00001119 if ( (strstr(buffer, "init\n") != NULL )) {
John Beppuf95ca971999-12-09 22:10:18 +00001120 return init_pid;
1121 }
1122 }
1123 return 0;
1124}
1125#endif
Eric Andersen1792f8c1999-12-09 06:11:36 +00001126
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001127#if defined BB_GUNZIP || defined BB_GZIP || defined BB_PRINTF || defined BB_TAIL
1128extern void *xmalloc (size_t size)
1129{
1130 void *cp = malloc (size);
1131
1132 if (cp == NULL) {
1133 error("out of memory");
1134 }
1135 return cp;
1136}
1137
1138extern void error(char *msg)
1139{
1140 fprintf(stderr, "\n%s\n", msg);
1141 exit(1);
1142}
1143#endif
1144
Erik Andersen7dc16072000-01-04 01:10:25 +00001145#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1146extern int vdprintf(int d, const char *format, va_list ap)
1147{
1148 char buf[BUF_SIZE];
1149 int len;
1150
1151 len = vsprintf(buf, format, ap);
1152 return write(d, buf, len);
1153}
1154#endif
1155
Erik Andersen5cbdd712000-01-26 20:06:48 +00001156#if defined BB_FEATURE_MOUNT_LOOP
1157extern int del_loop(const char *device)
1158{
1159 int fd;
1160
1161 if ((fd = open(device, O_RDONLY)) < 0) {
1162 perror(device);
1163 return( FALSE);
1164 }
1165 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1166 perror("ioctl: LOOP_CLR_FD");
1167 return( FALSE);
1168 }
1169 close(fd);
1170 return( TRUE);
1171}
Erik Andersende7965c2000-01-26 23:49:21 +00001172
1173extern int set_loop(const char *device, const char *file, int offset, int *loopro)
1174{
1175 struct loop_info loopinfo;
1176 int fd, ffd, mode;
1177
1178 mode = *loopro ? O_RDONLY : O_RDWR;
1179 if ((ffd = open (file, mode)) < 0 && !*loopro
1180 && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
1181 perror (file);
1182 return 1;
1183 }
1184 if ((fd = open (device, mode)) < 0) {
1185 close(ffd);
1186 perror (device);
1187 return 1;
1188 }
1189 *loopro = (mode == O_RDONLY);
1190
1191 memset(&loopinfo, 0, sizeof(loopinfo));
1192 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
1193 loopinfo.lo_name[LO_NAME_SIZE-1] = 0;
1194
1195 loopinfo.lo_offset = offset;
1196
1197 loopinfo.lo_encrypt_key_size = 0;
1198 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1199 perror("ioctl: LOOP_SET_FD");
1200 close(fd);
1201 close(ffd);
1202 return 1;
1203 }
1204 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1205 (void) ioctl(fd, LOOP_CLR_FD, 0);
1206 perror("ioctl: LOOP_SET_STATUS");
1207 close(fd);
1208 close(ffd);
1209 return 1;
1210 }
1211 close(fd);
1212 close(ffd);
1213 return 0;
1214}
1215
1216extern char *find_unused_loop_device (void)
1217{
1218 char dev[20];
1219 int i, fd;
1220 struct stat statbuf;
1221 struct loop_info loopinfo;
1222
1223 for(i = 0; i <= 7; i++) {
1224 sprintf(dev, "/dev/loop%d", i);
1225 if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1226 if ((fd = open (dev, O_RDONLY)) >= 0) {
1227 if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1228 if (errno == ENXIO) { /* probably free */
1229 close (fd);
1230 return strdup(dev);
1231 }
1232 }
1233 close (fd);
1234 }
1235 }
1236 }
1237 return NULL;
1238}
1239#endif /* BB_FEATURE_MOUNT_LOOP */
1240
Erik Andersen5cbdd712000-01-26 20:06:48 +00001241
Eric Andersencc8ed391999-10-05 16:24:54 +00001242/* END CODE */