blob: c274bfa1536e4f72318172b50c8666dadb21c7bd [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
3 * Utility routines.
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * 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.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 *
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
Eric Andersen2b69c401999-10-05 22:58:32 +000028#include "internal.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000029#if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
32 || defined (BB_LS) \
33 || defined (BB_INSMOD)
34/* same conditions as recursiveAction */
35#define bb_need_name_too_long
36#endif
37#define BB_DECLARE_EXTERN
38#include "messages.c"
39
Eric Andersen2b69c401999-10-05 22:58:32 +000040#include <stdio.h>
41#include <string.h>
42#include <errno.h>
43#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <dirent.h>
45#include <time.h>
46#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000047#include <sys/stat.h>
48#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000049#include <ctype.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +000050#include <sys/param.h> /* for PATH_MAX */
Erik Andersend07ee462000-02-21 21:26:32 +000051#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Erik Andersen5cbdd712000-01-26 20:06:48 +000053#if defined BB_FEATURE_MOUNT_LOOP
54#include <fcntl.h>
55#include <sys/ioctl.h>
56#include <linux/loop.h>
57#endif
58
Erik Andersen246cc6d2000-03-07 07:41:42 +000059/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
60 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000061#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000062//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
63#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
64#endif
65
Erik Andersen5cbdd712000-01-26 20:06:48 +000066
Eric Andersend0246fb1999-11-04 21:18:07 +000067#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersen0ecb54a1999-12-05 23:24:55 +000068# if defined BB_FEATURE_USE_PROCFS
Eric Andersend0246fb1999-11-04 21:18:07 +000069const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070# else
71# if defined BB_MTAB
72const char mtab_file[] = "/etc/mtab";
73# else
Erik Andersen269bba22000-03-08 14:50:47 +000074# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000075const char mtab_file[] = "/dev/mtab";
76# else
Erik Andersen269bba22000-03-08 14:50:47 +000077# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or ( BB_FEATURE_USE_PROCFS | BB_FEATURE_USE_DEVPS_PATCH)
Eric Andersen0ecb54a1999-12-05 23:24:55 +000078# endif
79# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000080# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000081#endif
82
83
Eric Andersend73dc5b1999-11-10 23:13:02 +000084extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000085{
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
87 BB_VER, BB_BT);
88 fprintf(stderr, "Usage: %s\n", usage);
89 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000090}
91
Erik Andersen9ffdaa62000-02-11 21:55:04 +000092extern void errorMsg(char *s, ...)
93{
94 va_list p;
95
96 va_start(p, s);
97 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000098 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000099 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000100 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000101}
102
103extern void fatalError(char *s, ...)
104{
105 va_list p;
106
107 va_start(p, s);
108 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000110 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000111 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000112 exit( FALSE);
113}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000114
Eric Andersend23f9ba1999-10-20 19:18:15 +0000115#if defined (BB_INIT) || defined (BB_PS)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000116/* Returns kernel version encoded as major*65536 + minor*256 + patch,
117 * so, for example, to check if the kernel is greater than 2.2.11:
118 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
119 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120int get_kernel_revision()
Eric Andersend23f9ba1999-10-20 19:18:15 +0000121{
Erik Andersend07ee462000-02-21 21:26:32 +0000122 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000124
Erik Andersend07ee462000-02-21 21:26:32 +0000125 if (uname(&name) == -1) {
126 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 return (0);
128 }
Erik Andersend07ee462000-02-21 21:26:32 +0000129 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000131}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132#endif /* BB_INIT || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000133
Erik Andersen029011b2000-03-04 21:19:32 +0000134#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000135
Erik Andersen029011b2000-03-04 21:19:32 +0000136#define HASH_SIZE 311 /* Should be prime */
137#define hash_inode(i) ((i) % HASH_SIZE)
138
139static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
140
141/*
142 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
143 * `ino_dev_hashtable', else return 0
144 *
145 * If NAME is a non-NULL pointer to a character pointer, and there is
146 * a match, then set *NAME to the value of the name slot in that
147 * bucket.
148 */
149int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
150{
151 ino_dev_hashtable_bucket_t *bucket;
152
153 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
154 while (bucket != NULL) {
155 if ((bucket->ino == statbuf->st_ino) &&
156 (bucket->dev == statbuf->st_dev))
157 {
158 if (name) *name = bucket->name;
159 return 1;
160 }
161 bucket = bucket->next;
162 }
163 return 0;
164}
165
166/* Add statbuf to statbuf hash table */
167void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
168{
169 int i;
170 size_t s;
171 ino_dev_hashtable_bucket_t *bucket;
172
173 i = hash_inode(statbuf->st_ino);
174 s = name ? strlen(name) : 0;
175 bucket = malloc(sizeof(ino_dev_hashtable_bucket_t) + s);
176 if (bucket == NULL)
177 fatalError("Not enough memory.");
178 bucket->ino = statbuf->st_ino;
179 bucket->dev = statbuf->st_dev;
180 if (name)
181 strcpy(bucket->name, name);
182 else
183 bucket->name[0] = '\0';
184 bucket->next = ino_dev_hashtable[i];
185 ino_dev_hashtable[i] = bucket;
186}
187
188/* Clear statbuf hash table */
189void reset_ino_dev_hashtable(void)
190{
191 int i;
192 ino_dev_hashtable_bucket_t *bucket;
193
194 for (i = 0; i < HASH_SIZE; i++) {
195 while (ino_dev_hashtable[i] != NULL) {
196 bucket = ino_dev_hashtable[i]->next;
197 free(ino_dev_hashtable[i]);
198 ino_dev_hashtable[i] = bucket;
199 }
200 }
201}
202
203#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000204
Erik Andersenfac10d72000-02-07 05:29:42 +0000205#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000206/*
207 * Return TRUE if a fileName is a directory.
208 * Nonexistant files return FALSE.
209 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000210int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000211{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000213 int didMalloc = 0;
214
215 if (statBuf == NULL) {
216 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
217 ++didMalloc;
218 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000219
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000221 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000223 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000224
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000225 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
226 status = FALSE;
227 }
228 else status = TRUE;
229
230 if (didMalloc) {
231 free(statBuf);
232 statBuf = NULL;
233 }
234 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000235}
Erik Andersenfac10d72000-02-07 05:29:42 +0000236#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000237
Erik Andersenfac10d72000-02-07 05:29:42 +0000238#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000239/*
240 * Copy one file to another, while possibly preserving its modes, times,
241 * and modes. Returns TRUE if successful, or FALSE on a failure with an
Erik Andersen029011b2000-03-04 21:19:32 +0000242 * error message output. (Failure is not indicated if the attributes cannot
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000243 * be set.)
Eric Andersenc4996011999-10-20 22:08:37 +0000244 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000245 */
246int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247copyFile(const char *srcName, const char *destName,
248 int setModes, int followLinks)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000249{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 int rfd;
251 int wfd;
252 int rcc;
253 int status;
254 char buf[BUF_SIZE];
255 struct stat srcStatBuf;
256 struct stat dstStatBuf;
257 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000258
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 if (followLinks == TRUE)
260 status = stat(srcName, &srcStatBuf);
261 else
262 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000263
Erik Andersenfac10d72000-02-07 05:29:42 +0000264 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 perror(srcName);
266 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000267 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268
269 if (followLinks == TRUE)
270 status = stat(destName, &dstStatBuf);
271 else
272 status = lstat(destName, &dstStatBuf);
273
274 if (status < 0) {
275 dstStatBuf.st_ino = -1;
276 dstStatBuf.st_dev = -1;
277 }
278
279 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
280 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
281 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
282 return FALSE;
283 }
284
285 if (S_ISDIR(srcStatBuf.st_mode)) {
286 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
287 /* Make sure the directory is writable */
288 status = mkdir(destName, 0777777 ^ umask(0));
289 if (status < 0 && errno != EEXIST) {
290 perror(destName);
291 return FALSE;
292 }
293 } else if (S_ISLNK(srcStatBuf.st_mode)) {
294 char link_val[PATH_MAX + 1];
295 int link_size;
296
297 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
298 /* Warning: This could possibly truncate silently, to PATH_MAX chars */
299 link_size = readlink(srcName, &link_val[0], PATH_MAX);
300 if (link_size < 0) {
301 perror(srcName);
302 return FALSE;
303 }
304 link_val[link_size] = '\0';
305 status = symlink(link_val, destName);
306 if (status < 0) {
307 perror(destName);
308 return FALSE;
309 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000310#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 if (setModes == TRUE) {
312 if (lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
313 perror(destName);
314 return FALSE;
315 }
316 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000317#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 return TRUE;
319 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
320 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
321 if (mkfifo(destName, 0644) < 0) {
322 perror(destName);
323 return FALSE;
324 }
325 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
326 || S_ISSOCK(srcStatBuf.st_mode)) {
327 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
328 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
329 perror(destName);
330 return FALSE;
331 }
332 } else if (S_ISREG(srcStatBuf.st_mode)) {
333 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
334 rfd = open(srcName, O_RDONLY);
335 if (rfd < 0) {
336 perror(srcName);
337 return FALSE;
338 }
339
340 wfd =
341 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
342 srcStatBuf.st_mode);
343 if (wfd < 0) {
344 perror(destName);
345 close(rfd);
346 return FALSE;
347 }
348
349 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
350 if (fullWrite(wfd, buf, rcc) < 0)
351 goto error_exit;
352 }
353 if (rcc < 0) {
354 goto error_exit;
355 }
356
357 close(rfd);
358 if (close(wfd) < 0) {
359 return FALSE;
360 }
361 }
362
363 if (setModes == TRUE) {
364 /* This is fine, since symlinks never get here */
365 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
366 perror(destName);
367 exit FALSE;
368 }
369 if (chmod(destName, srcStatBuf.st_mode) < 0) {
370 perror(destName);
371 exit FALSE;
372 }
373 times.actime = srcStatBuf.st_atime;
374 times.modtime = srcStatBuf.st_mtime;
375 if (utime(destName, &times) < 0) {
376 perror(destName);
377 exit FALSE;
378 }
379 }
380
Erik Andersenfac10d72000-02-07 05:29:42 +0000381 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000382
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 error_exit:
384 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000385 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000387
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000389}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000391
392
393
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000394#if defined BB_TAR || defined BB_LS
395
396#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
397#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
398
399/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
400static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 0, 0, S_ISUID,
402 0, 0, S_ISGID,
403 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000404};
405
406/* The 9 mode bits to test */
407static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 S_IRUSR, S_IWUSR, S_IXUSR,
409 S_IRGRP, S_IWGRP, S_IXGRP,
410 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000411};
412
413#define MODE1 "rwxrwxrwx"
414#define MODE0 "---------"
415#define SMODE1 "..s..s..t"
416#define SMODE0 "..S..S..T"
417
Eric Andersencc8ed391999-10-05 16:24:54 +0000418/*
419 * Return the standard ls-like mode string from a file mode.
420 * This is static and so is overwritten on each call.
421 */
Eric Andersenf811e071999-10-09 00:25:00 +0000422const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000423{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000425
Erik Andersene49d5ec2000-02-08 19:58:47 +0000426 int i;
427
428 buf[0] = TYPECHAR(mode);
429 for (i = 0; i < 9; i++) {
430 if (mode & SBIT[i])
431 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
432 else
433 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
434 }
435 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000436}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000438
439
Eric Andersen1792f8c1999-12-09 06:11:36 +0000440#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000441/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000442 * Return the standard ls-like time string from a time_t
443 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000444 */
Eric Andersenf811e071999-10-09 00:25:00 +0000445const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000446{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 time_t now;
448 char *str;
449 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000450
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000454
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 strcpy(buf, &str[4]);
456 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000457
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
459 strcpy(&buf[7], &str[20]);
460 buf[11] = '\0';
461 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000462
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000464}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000466
Erik Andersenfac10d72000-02-07 05:29:42 +0000467#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000468/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000469 * Write all of the supplied buffer out to a file.
470 * This does multiple writes as necessary.
471 * Returns the amount written, or -1 on an error.
472 */
Eric Andersenf811e071999-10-09 00:25:00 +0000473int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000474{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 int cc;
476 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000477
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000479
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 while (len > 0) {
481 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000482
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483 if (cc < 0)
484 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000485
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 buf += cc;
487 total += cc;
488 len -= cc;
489 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000490
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000492}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000494
495
Eric Andersen1792f8c1999-12-09 06:11:36 +0000496#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000497/*
498 * Read all of the supplied buffer from a file.
499 * This does multiple reads as necessary.
500 * Returns the amount read, or -1 on an error.
501 * A short read is returned on an end of file.
502 */
Eric Andersenf811e071999-10-09 00:25:00 +0000503int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000504{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 int cc;
506 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000509
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 while (len > 0) {
511 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000512
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 if (cc < 0)
514 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000515
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 if (cc == 0)
517 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 buf += cc;
520 total += cc;
521 len -= cc;
522 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000523
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000525}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000527
528
Erik Andersenfac10d72000-02-07 05:29:42 +0000529#if defined (BB_CHMOD_CHOWN_CHGRP) \
530 || defined (BB_CP_MV) \
531 || defined (BB_FIND) \
532 || defined (BB_LS) \
533 || defined (BB_INSMOD)
Eric Andersencc8ed391999-10-05 16:24:54 +0000534/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000535 * Walk down all the directories under the specified
536 * location, and do something (something specified
537 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000538 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000539 * Unfortunatly, while nftw(3) could replace this and reduce
540 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000541 * and so isn't sufficiently portable to take over since glibc2.1
542 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000543 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000544int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 int recurse, int followLinks, int depthFirst,
546 int (*fileAction) (const char *fileName,
547 struct stat * statbuf),
548 int (*dirAction) (const char *fileName,
549 struct stat * statbuf))
Eric Andersencc8ed391999-10-05 16:24:54 +0000550{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 int status;
552 struct stat statbuf;
553 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000554
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 if (followLinks == TRUE)
556 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000557 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000559
Eric Andersen2b69c401999-10-05 22:58:32 +0000560 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561#ifdef BB_DEBUG_PRINT_SCAFFOLD
562 fprintf(stderr,
563 "status=%d followLinks=%d TRUE=%d\n",
564 status, followLinks, TRUE);
565#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000566 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000567 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000568 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569
570 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
571 if (fileAction == NULL)
572 return TRUE;
573 else
574 return fileAction(fileName, &statbuf);
575 }
576
577 if (recurse == FALSE) {
578 if (S_ISDIR(statbuf.st_mode)) {
579 if (dirAction != NULL)
580 return (dirAction(fileName, &statbuf));
581 else
582 return TRUE;
583 }
584 }
585
586 if (S_ISDIR(statbuf.st_mode)) {
587 DIR *dir;
588
589 dir = opendir(fileName);
590 if (!dir) {
591 perror(fileName);
592 return FALSE;
593 }
594 if (dirAction != NULL && depthFirst == FALSE) {
595 status = dirAction(fileName, &statbuf);
596 if (status == FALSE) {
597 perror(fileName);
598 return FALSE;
599 }
600 }
601 while ((next = readdir(dir)) != NULL) {
602 char nextFile[PATH_MAX + 1];
603
604 if ((strcmp(next->d_name, "..") == 0)
605 || (strcmp(next->d_name, ".") == 0)) {
606 continue;
607 }
608 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
609 fprintf(stderr, name_too_long, "ftw");
610 return FALSE;
611 }
612 sprintf(nextFile, "%s/%s", fileName, next->d_name);
613 status =
614 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
615 fileAction, dirAction);
616 if (status < 0) {
617 closedir(dir);
618 return FALSE;
619 }
620 }
621 status = closedir(dir);
622 if (status < 0) {
623 perror(fileName);
624 return FALSE;
625 }
626 if (dirAction != NULL && depthFirst == TRUE) {
627 status = dirAction(fileName, &statbuf);
628 if (status == FALSE) {
629 perror(fileName);
630 return FALSE;
631 }
632 }
633 } else {
634 if (fileAction == NULL)
635 return TRUE;
636 else
637 return fileAction(fileName, &statbuf);
638 }
639 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000640}
641
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000643
Eric Andersenf6be9441999-10-13 21:12:06 +0000644
645
Erik Andersena8991081999-12-29 03:34:00 +0000646#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000647/*
648 * Attempt to create the directories along the specified path, except for
649 * the final component. The mode is given for the final directory only,
650 * while all previous ones get default protections. Errors are not reported
651 * here, as failures to restore files can be reported later.
652 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000654{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 char *cp;
656 char *cpOld;
657 char buf[PATH_MAX + 1];
658 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000659
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660 strcpy(buf, name);
661 for (cp = buf; *cp == '/'; cp++);
662 cp = strchr(cp, '/');
663 while (cp) {
664 cpOld = cp;
665 cp = strchr(cp + 1, '/');
666 *cpOld = '\0';
667 retVal = mkdir(buf, cp ? 0777 : mode);
668 if (retVal != 0 && errno != EEXIST) {
669 perror(buf);
670 return FALSE;
671 }
672 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000673 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000675}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000677
678
679
680#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000681/* [ugoa]{+|-|=}[rwxst] */
682
683
684
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000686{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 mode_t andMode =
688
689 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000690 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 mode_t mode = 0;
692 mode_t groups = 0;
693 char type;
694 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000695
696 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 for (;;) {
698 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000699 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000700 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000701 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000703 continue;
704 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000706 continue;
707 case 'o':
708 groups |= S_IRWXO;
709 continue;
710 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000712 continue;
713 case '+':
714 case '=':
715 case '-':
716 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 if (groups == 0) /* The default is "all" */
718 groups |=
719 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000720 break;
721 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 if (isdigit(c) && c >= '0' && c <= '7' &&
723 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000724 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000725 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000727 return (FALSE);
728 }
729 break;
730 }
731
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 while ((c = *s++) != '\0') {
733 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000734 case ',':
735 break;
736 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000738 continue;
739 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000741 continue;
742 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000744 continue;
745 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000747 continue;
748 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000749 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000750 continue;
751 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000752 *theMode &= andMode;
753 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000755 }
756 break;
757 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000759 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000760 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000761 /* fall through */
762 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000763 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000764 break;
765 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000766 andMode &= ~(mode & groups);
767 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000768 break;
769 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000771 *theMode &= andMode;
772 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 return (TRUE);
774}
Eric Andersence8f3b91999-10-20 07:03:36 +0000775
776
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000778
Eric Andersene674eb71999-10-19 20:52:57 +0000779
780
Eric Andersend23f9ba1999-10-20 19:18:15 +0000781
782
783
784
785#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
786
787/* Use this to avoid needing the glibc NSS stuff
788 * This uses storage buf to hold things.
789 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000790uid_t my_getid(const char *filename, char *name, uid_t id)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000791{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000792 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000793 char *rname, *start, *end, buf[128];
794 uid_t rid;
795
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000797 if (file == NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 perror(filename);
799 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000800 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000801
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000803 if (buf[0] == '#')
804 continue;
805
806 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000808 if (end == NULL)
809 continue;
810 *end = '\0';
811 rname = start;
812
813 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000815 if (end == NULL)
816 continue;
817
818 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000819 rid = (uid_t) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000820 if (end == start)
821 continue;
822
823 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 if (0 == strcmp(rname, name)) {
825 fclose(file);
826 return (rid);
827 }
828 }
829 if (id != -1 && id == rid) {
830 strncpy(name, rname, 8);
831 fclose(file);
832 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000833 }
834 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000835 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000836 return (-1);
837}
838
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839uid_t my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000840{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 return my_getid("/etc/passwd", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000842}
843
Erik Andersene49d5ec2000-02-08 19:58:47 +0000844gid_t my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000845{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000846 return my_getid("/etc/group", name, -1);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000847}
848
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849void my_getpwuid(char *name, uid_t uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000850{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 my_getid("/etc/passwd", name, uid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000852}
853
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854void my_getgrgid(char *group, gid_t gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000855{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000856 my_getid("/etc/group", group, gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000857}
858
859
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000861
862
Eric Andersenaa0765e1999-10-22 04:30:20 +0000863
Eric Andersen0460ff21999-10-25 23:32:44 +0000864
865#if (defined BB_CHVT) || (defined BB_DEALLOCVT)
866
867
868#include <linux/kd.h>
869#include <sys/ioctl.h>
870
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000872{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000873 char arg;
874
875 arg = 0;
876 return (ioctl(fd, KDGKBTYPE, &arg) == 0
877 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000878}
879
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000881{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000882 int fd;
883
884 /* try read-only */
885 fd = open(fnam, O_RDWR);
886
887 /* if failed, try read-only */
888 if (fd < 0 && errno == EACCES)
889 fd = open(fnam, O_RDONLY);
890
891 /* if failed, try write-only */
892 if (fd < 0 && errno == EACCES)
893 fd = open(fnam, O_WRONLY);
894
895 /* if failed, fail */
896 if (fd < 0)
897 return -1;
898
899 /* if not a console, fail */
900 if (!is_a_console(fd)) {
901 close(fd);
902 return -1;
903 }
904
905 /* success */
906 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000907}
908
909/*
910 * Get an fd for use with kbd/console ioctls.
911 * We try several things because opening /dev/console will fail
912 * if someone else used X (which does a chown on /dev/console).
913 *
914 * if tty_name is non-NULL, try this one instead.
915 */
916
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000918{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000920
Erik Andersene49d5ec2000-02-08 19:58:47 +0000921 if (tty_name) {
922 if (-1 == (fd = open_a_console(tty_name)))
923 return -1;
924 else
925 return fd;
926 }
927
928 fd = open_a_console("/dev/tty");
929 if (fd >= 0)
930 return fd;
931
932 fd = open_a_console("/dev/tty0");
933 if (fd >= 0)
934 return fd;
935
936 fd = open_a_console("/dev/console");
937 if (fd >= 0)
938 return fd;
939
940 for (fd = 0; fd < 3; fd++)
941 if (is_a_console(fd))
942 return fd;
943
944 fprintf(stderr,
945 "Couldnt get a file descriptor referring to the console\n");
946 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000947}
948
949
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950#endif /* BB_CHVT || BB_DEALLOCVT */
Eric Andersen0460ff21999-10-25 23:32:44 +0000951
952
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000954
955/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000957{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000959
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 while (*haystack) {
961 if (!strncasecmp(haystack, needle, len))
962 break;
963 haystack++;
964 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000965
Erik Andersene49d5ec2000-02-08 19:58:47 +0000966 if (!(*haystack))
967 haystack = NULL;
968
969 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000970}
971
Eric Andersenc1525e81999-10-29 00:07:31 +0000972/* This tries to find a needle in a haystack, but does so by
973 * only trying to match literal strings (look 'ma, no regexps!)
974 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000975 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +0000976 * -Erik Andersen
977 */
978extern int find_match(char *haystack, char *needle, int ignoreCase)
979{
980
Erik Andersene49d5ec2000-02-08 19:58:47 +0000981 if (ignoreCase == FALSE)
982 haystack = strstr(haystack, needle);
983 else
984 haystack = stristr(haystack, needle);
985 if (haystack == NULL)
986 return FALSE;
987 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +0000988}
989
990
Eric Andersen24d8e7d1999-10-29 06:50:17 +0000991/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000992extern int replace_match(char *haystack, char *needle, char *newNeedle,
993 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +0000994{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000995 int foundOne = 0;
996 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +0000997
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 if (ignoreCase == FALSE)
999 where = strstr(haystack, needle);
1000 else
1001 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001002
Erik Andersene49d5ec2000-02-08 19:58:47 +00001003 if (strcmp(needle, newNeedle) == 0)
1004 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001005
Erik Andersene49d5ec2000-02-08 19:58:47 +00001006 oldhayStack = (char *) malloc((unsigned) (strlen(haystack)));
1007 while (where != NULL) {
1008 foundOne++;
1009 strcpy(oldhayStack, haystack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001010#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 if (strlen(newNeedle) > strlen(needle)) {
1012 haystack =
1013 (char *) realloc(haystack,
1014 (unsigned) (strlen(haystack) -
1015 strlen(needle) +
1016 strlen(newNeedle)));
1017 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001018#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001019 for (slider = haystack, slider1 = oldhayStack; slider != where;
1020 slider++, slider1++);
1021 *slider = 0;
1022 haystack = strcat(haystack, newNeedle);
1023 slider1 += strlen(needle);
1024 haystack = strcat(haystack, slider1);
1025 where = strstr(slider, needle);
1026 }
1027 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001028
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 if (foundOne > 0)
1030 return TRUE;
1031 else
1032 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001033}
1034
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001036
1037
Eric Andersenb186d981999-12-03 09:19:54 +00001038#if defined BB_FIND
1039/*
1040 * Routine to see if a text string is matched by a wildcard pattern.
1041 * Returns TRUE if the text is matched, or FALSE if it is not matched
1042 * or if the pattern is invalid.
1043 * * matches zero or more characters
1044 * ? matches a single character
1045 * [abc] matches 'a', 'b' or 'c'
1046 * \c quotes character c
1047 * Adapted from code written by Ingo Wilken, and
1048 * then taken from sash, Copyright (c) 1999 by David I. Bell
1049 * Permission is granted to use, distribute, or modify this source,
1050 * provided that this copyright notice remains intact.
1051 * Permission to distribute this code under the GPL has been granted.
1052 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001053extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001054{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 const char *retryPat;
1056 const char *retryText;
1057 int ch;
1058 int found;
Eric Andersenb186d981999-12-03 09:19:54 +00001059
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 retryPat = NULL;
1061 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001062
Erik Andersene49d5ec2000-02-08 19:58:47 +00001063 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001064 ch = *pattern++;
1065
Erik Andersene49d5ec2000-02-08 19:58:47 +00001066 switch (ch) {
1067 case '*':
1068 retryPat = pattern;
1069 retryText = text;
1070 break;
1071
1072 case '[':
1073 found = FALSE;
1074
1075 while ((ch = *pattern++) != ']') {
1076 if (ch == '\\')
1077 ch = *pattern++;
1078
1079 if (ch == '\0')
1080 return FALSE;
1081
1082 if (*text == ch)
1083 found = TRUE;
1084 }
1085
1086 //if (!found)
1087 if (found == TRUE) {
1088 pattern = retryPat;
1089 text = ++retryText;
1090 }
1091
1092 /* fall into next case */
1093
1094 case '?':
1095 if (*text++ == '\0')
1096 return FALSE;
1097
1098 break;
1099
1100 case '\\':
1101 ch = *pattern++;
1102
1103 if (ch == '\0')
1104 return FALSE;
1105
1106 /* fall into next case */
1107
1108 default:
1109 if (*text == ch) {
1110 if (*text)
1111 text++;
1112 break;
1113 }
1114
1115 if (*text) {
1116 pattern = retryPat;
1117 text = ++retryText;
1118 break;
1119 }
1120
Eric Andersenb186d981999-12-03 09:19:54 +00001121 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001122 }
1123
Erik Andersene49d5ec2000-02-08 19:58:47 +00001124 if (pattern == NULL)
1125 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001126 }
1127
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001129}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001130#endif /* BB_FIND */
Eric Andersenb186d981999-12-03 09:19:54 +00001131
1132
Eric Andersen29d2e361999-11-06 06:07:27 +00001133
1134
Erik Andersen7dc16072000-01-04 01:10:25 +00001135#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001136/*
1137 * Given a block device, find the mount table entry if that block device
1138 * is mounted.
1139 *
1140 * Given any other file (or directory), find the mount table entry for its
1141 * filesystem.
1142 */
1143extern struct mntent *findMountPoint(const char *name, const char *table)
1144{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 struct stat s;
1146 dev_t mountDevice;
1147 FILE *mountTable;
1148 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001149
Erik Andersene49d5ec2000-02-08 19:58:47 +00001150 if (stat(name, &s) != 0)
1151 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001152
Erik Andersene49d5ec2000-02-08 19:58:47 +00001153 if ((s.st_mode & S_IFMT) == S_IFBLK)
1154 mountDevice = s.st_rdev;
1155 else
1156 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001157
1158
Erik Andersene49d5ec2000-02-08 19:58:47 +00001159 if ((mountTable = setmntent(table, "r")) == 0)
1160 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001161
Erik Andersene49d5ec2000-02-08 19:58:47 +00001162 while ((mountEntry = getmntent(mountTable)) != 0) {
1163 if (strcmp(name, mountEntry->mnt_dir) == 0
1164 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1165 break;
1166 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1167 break;
1168 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1169 break;
1170 }
1171 endmntent(mountTable);
1172 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001173}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001174#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001175
1176
1177
Eric Andersen1792f8c1999-12-09 06:11:36 +00001178#if defined BB_DD || defined BB_TAIL
1179/*
1180 * Read a number with a possible multiplier.
1181 * Returns -1 if the number format is illegal.
1182 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001184{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001186
Erik Andersene49d5ec2000-02-08 19:58:47 +00001187 if (!isDecimal(*cp))
1188 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001189
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 while (isDecimal(*cp))
1193 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001194
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 switch (*cp++) {
1196 case 'M':
1197 case 'm': /* `tail' uses it traditionally */
1198 value *= 1048576;
1199 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001200
Erik Andersene49d5ec2000-02-08 19:58:47 +00001201 case 'k':
1202 value *= 1024;
1203 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001204
Erik Andersene49d5ec2000-02-08 19:58:47 +00001205 case 'b':
1206 value *= 512;
1207 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001208
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 case 'w':
1210 value *= 2;
1211 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001212
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213 case '\0':
1214 return value;
1215
1216 default:
1217 return -1;
1218 }
1219
1220 if (*cp)
1221 return -1;
1222
Eric Andersen1792f8c1999-12-09 06:11:36 +00001223 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001224}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001225#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001226
Eric Andersen6805d5d1999-12-09 22:39:55 +00001227
Erik Andersen31cf8e02000-02-07 20:31:19 +00001228#if defined BB_INIT || defined BB_SYSLOGD
1229/* try to open up the specified device */
1230extern int device_open(char *device, int mode)
1231{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001233
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001235
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 /* Retry up to 5 times */
1237 for (f = 0; f < 5; f++)
1238 if ((fd = open(device, m, 0600)) >= 0)
1239 break;
1240 if (fd < 0)
1241 return fd;
1242 /* Reset original flags. */
1243 if (m != mode)
1244 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001245 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001246}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001247#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001248
1249
Erik Andersen6273f652000-03-17 01:12:41 +00001250#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001251#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001252#include <linux/devps.h>
Eric Andersen6805d5d1999-12-09 22:39:55 +00001253#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001254
Erik Andersen269bba22000-03-08 14:50:47 +00001255#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001256/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001257 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001258 * This finds the pid of the specified process,
1259 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001260 *
1261 * [return]
1262 * 0 failure
Erik Andersen246cc6d2000-03-07 07:41:42 +00001263 * pid when the pid is found.
John Beppuf95ca971999-12-09 22:10:18 +00001264 */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001265extern pid_t findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001266{
Erik Andersen246cc6d2000-03-07 07:41:42 +00001267 int fd, i;
1268 char device[] = "/dev/ps";
1269 pid_t thePid = 0;
1270 pid_t num_pids;
1271 pid_t* pid_array = NULL;
1272
1273 /* open device */
1274 fd = open(device, O_RDONLY);
1275 if (fd < 0)
1276 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1277
1278 /* Find out how many processes there are */
1279 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1280 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1281
1282 /* Allocate some memory -- grab a few extras just in case
1283 * some new processes start up while we wait. The kernel will
1284 * just ignore any extras if we give it too many, and will trunc.
1285 * the list if we give it too few. */
1286 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1287 pid_array[0] = num_pids+10;
1288
1289 /* Now grab the pid list */
1290 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1291 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1292
1293 /* Now search for a match */
1294 for (i=1; i<pid_array[0] ; i++) {
1295 struct pid_info info;
1296
1297 info.pid = pid_array[i];
1298 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1299 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1300
1301 if ((strstr(info.command_line, pidName) != NULL)) {
1302 thePid = info.pid;
1303 break;
1304 }
1305 }
1306
1307 /* Free memory */
1308 free( pid_array);
1309
1310 /* close device */
1311 if (close (fd) != 0)
1312 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1313
1314 return thePid;
1315}
Erik Andersen269bba22000-03-08 14:50:47 +00001316#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001317#if ! defined BB_FEATURE_USE_PROCFS
1318#error Sorry, I depend on the /proc filesystem right now.
1319#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001320
Erik Andersen246cc6d2000-03-07 07:41:42 +00001321/* findPidByName()
1322 *
1323 * This finds the pid of the specified process.
1324 * Currently, it's implemented by rummaging through
1325 * the proc filesystem.
1326 *
1327 * [return]
1328 * 0 failure
1329 * pid when the pid is found.
1330 */
1331extern pid_t findPidByName( char* pidName)
1332{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001333 DIR *dir;
1334 struct dirent *next;
John Beppuf95ca971999-12-09 22:10:18 +00001335
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001336 dir = opendir("/proc");
1337 if (!dir)
1338 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1339
1340 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001341 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001342 char filename[256];
1343 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001344
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001345 /* If it isn't a number, we don't want it */
1346 if (!isdigit(*next->d_name))
1347 continue;
1348
1349 /* Now open the command line file */
1350 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001351 status = fopen(filename, "r");
1352 if (!status) {
1353 continue;
1354 }
1355 fgets(buffer, 256, status);
1356 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001357
Erik Andersen246cc6d2000-03-07 07:41:42 +00001358 if ((strstr(buffer, pidName) != NULL)) {
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001359 return strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001360 }
John Beppuf95ca971999-12-09 22:10:18 +00001361 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001362 return 0;
John Beppuf95ca971999-12-09 22:10:18 +00001363}
Erik Andersen269bba22000-03-08 14:50:47 +00001364#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen6273f652000-03-17 01:12:41 +00001365#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001366
Erik Andersenfac10d72000-02-07 05:29:42 +00001367#if defined BB_GUNZIP \
1368 || defined BB_GZIP \
1369 || defined BB_PRINTF \
1370 || defined BB_TAIL
Erik Andersene49d5ec2000-02-08 19:58:47 +00001371extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001372{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001373 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001374
Erik Andersene49d5ec2000-02-08 19:58:47 +00001375 if (cp == NULL) {
Erik Andersen9ffdaa62000-02-11 21:55:04 +00001376 errorMsg("out of memory");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001377 }
1378 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001379}
1380
Erik Andersene49d5ec2000-02-08 19:58:47 +00001381#endif /* BB_GUNZIP || BB_GZIP || BB_PRINTF || BB_TAIL */
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001382
Erik Andersen7dc16072000-01-04 01:10:25 +00001383#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
1384extern int vdprintf(int d, const char *format, va_list ap)
1385{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001386 char buf[BUF_SIZE];
1387 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001388
Erik Andersene49d5ec2000-02-08 19:58:47 +00001389 len = vsprintf(buf, format, ap);
1390 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001391}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001392#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001393
Erik Andersen5cbdd712000-01-26 20:06:48 +00001394#if defined BB_FEATURE_MOUNT_LOOP
1395extern int del_loop(const char *device)
1396{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001397 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001398
Erik Andersene49d5ec2000-02-08 19:58:47 +00001399 if ((fd = open(device, O_RDONLY)) < 0) {
1400 perror(device);
1401 return (FALSE);
1402 }
1403 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1404 perror("ioctl: LOOP_CLR_FD");
1405 return (FALSE);
1406 }
1407 close(fd);
1408 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001409}
Erik Andersende7965c2000-01-26 23:49:21 +00001410
Erik Andersene49d5ec2000-02-08 19:58:47 +00001411extern int set_loop(const char *device, const char *file, int offset,
1412 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001413{
1414 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001415 int fd, ffd, mode;
1416
Erik Andersende7965c2000-01-26 23:49:21 +00001417 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001418 if ((ffd = open(file, mode)) < 0 && !*loopro
1419 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1420 perror(file);
1421 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001422 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001423 if ((fd = open(device, mode)) < 0) {
1424 close(ffd);
1425 perror(device);
1426 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001427 }
1428 *loopro = (mode == O_RDONLY);
1429
1430 memset(&loopinfo, 0, sizeof(loopinfo));
1431 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001433
1434 loopinfo.lo_offset = offset;
1435
1436 loopinfo.lo_encrypt_key_size = 0;
1437 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1438 perror("ioctl: LOOP_SET_FD");
1439 close(fd);
1440 close(ffd);
1441 return 1;
1442 }
1443 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1444 (void) ioctl(fd, LOOP_CLR_FD, 0);
1445 perror("ioctl: LOOP_SET_STATUS");
1446 close(fd);
1447 close(ffd);
1448 return 1;
1449 }
1450 close(fd);
1451 close(ffd);
1452 return 0;
1453}
1454
Erik Andersene49d5ec2000-02-08 19:58:47 +00001455extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001456{
1457 char dev[20];
1458 int i, fd;
1459 struct stat statbuf;
1460 struct loop_info loopinfo;
1461
Erik Andersene49d5ec2000-02-08 19:58:47 +00001462 for (i = 0; i <= 7; i++) {
1463 sprintf(dev, "/dev/loop%d", i);
1464 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1465 if ((fd = open(dev, O_RDONLY)) >= 0) {
1466 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1467 if (errno == ENXIO) { /* probably free */
1468 close(fd);
1469 return strdup(dev);
1470 }
1471 }
1472 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001473 }
Erik Andersende7965c2000-01-26 23:49:21 +00001474 }
Erik Andersende7965c2000-01-26 23:49:21 +00001475 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001476 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001477}
Erik Andersene49d5ec2000-02-08 19:58:47 +00001478#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001479
Erik Andersene132f4b2000-02-09 04:16:43 +00001480#if defined BB_MTAB
1481#define whine_if_fstab_is_missing() {}
1482#else
1483extern void whine_if_fstab_is_missing()
1484{
1485 struct stat statBuf;
1486
1487 if (stat("/etc/fstab", &statBuf) < 0)
1488 fprintf(stderr,
1489 "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1490}
1491#endif
1492
Eric Andersencc8ed391999-10-05 16:24:54 +00001493/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001494/*
1495Local Variables:
1496c-file-style: "linux"
1497c-basic-offset: 4
1498tab-width: 4
1499End:
1500*/