blob: 0f78f287f32700fcdd73faee9a63b2431b2e61f5 [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
Eric Andersen9f16d612000-06-12 23:11:16 +000037#define bb_need_memory_exhausted
Erik Andersenfac10d72000-02-07 05:29:42 +000038#define BB_DECLARE_EXTERN
39#include "messages.c"
40
Eric Andersen2b69c401999-10-05 22:58:32 +000041#include <stdio.h>
42#include <string.h>
43#include <errno.h>
44#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000045#include <dirent.h>
46#include <time.h>
47#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000048#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000049#include <ctype.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000050#include <sys/stat.h>
51#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000052#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000053
Erik Andersen5cbdd712000-01-26 20:06:48 +000054#if defined BB_FEATURE_MOUNT_LOOP
55#include <fcntl.h>
Eric Andersenc674d702000-07-10 22:57:14 +000056#include <linux/loop.h> /* Pull in loop device support */
Erik Andersen5cbdd712000-01-26 20:06:48 +000057#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
Eric Andersend73dc5b1999-11-10 23:13:02 +000083extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000084{
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
86 BB_VER, BB_BT);
87 fprintf(stderr, "Usage: %s\n", usage);
88 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000089}
90
Erik Andersen1ad302a2000-03-24 00:54:46 +000091extern void errorMsg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000092{
93 va_list p;
94
95 va_start(p, s);
96 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000097 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000098 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +000099 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000100}
101
Erik Andersen1ad302a2000-03-24 00:54:46 +0000102extern void fatalError(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000103{
104 va_list p;
105
106 va_start(p, s);
107 fflush(stdout);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000108 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000110 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000111 exit( FALSE);
112}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000113
Erik Andersen23dea9b2000-05-13 00:28:25 +0000114#if defined BB_INIT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000115/* Returns kernel version encoded as major*65536 + minor*256 + patch,
116 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000117 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000118 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000119extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000120{
Erik Andersend07ee462000-02-21 21:26:32 +0000121 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000123
Erik Andersend07ee462000-02-21 21:26:32 +0000124 if (uname(&name) == -1) {
125 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 return (0);
127 }
Erik Andersend07ee462000-02-21 21:26:32 +0000128 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000130}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000131#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000132
Eric Andersen10dc9d42000-06-26 10:45:52 +0000133
134
135#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000136_syscall1(int, sysinfo, struct sysinfo *, info);
137#endif /* BB_INIT */
138
Eric Andersena57ba4d2000-07-08 19:20:49 +0000139#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000140
141#ifndef __NR_umount2
142#define __NR_umount2 52
143#endif
144
145/* Include our own version of <sys/mount.h>, since libc5 doesn't
146 * know about umount2 */
147extern _syscall1(int, umount, const char *, special_file);
148extern _syscall2(int, umount2, const char *, special_file, int, flags);
149extern _syscall5(int, mount, const char *, special_file, const char *, dir,
150 const char *, fstype, unsigned long int, rwflag, const void *, data);
151#endif
152
Eric Andersen10dc9d42000-06-26 10:45:52 +0000153
154
Erik Andersen23dea9b2000-05-13 00:28:25 +0000155#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000156
Erik Andersen029011b2000-03-04 21:19:32 +0000157#define HASH_SIZE 311 /* Should be prime */
158#define hash_inode(i) ((i) % HASH_SIZE)
159
160static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
161
162/*
163 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
164 * `ino_dev_hashtable', else return 0
165 *
166 * If NAME is a non-NULL pointer to a character pointer, and there is
167 * a match, then set *NAME to the value of the name slot in that
168 * bucket.
169 */
170int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
171{
172 ino_dev_hashtable_bucket_t *bucket;
173
174 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
175 while (bucket != NULL) {
176 if ((bucket->ino == statbuf->st_ino) &&
177 (bucket->dev == statbuf->st_dev))
178 {
179 if (name) *name = bucket->name;
180 return 1;
181 }
182 bucket = bucket->next;
183 }
184 return 0;
185}
186
187/* Add statbuf to statbuf hash table */
188void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
189{
190 int i;
191 size_t s;
192 ino_dev_hashtable_bucket_t *bucket;
193
194 i = hash_inode(statbuf->st_ino);
195 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000196 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000197 bucket->ino = statbuf->st_ino;
198 bucket->dev = statbuf->st_dev;
199 if (name)
200 strcpy(bucket->name, name);
201 else
202 bucket->name[0] = '\0';
203 bucket->next = ino_dev_hashtable[i];
204 ino_dev_hashtable[i] = bucket;
205}
206
207/* Clear statbuf hash table */
208void reset_ino_dev_hashtable(void)
209{
210 int i;
211 ino_dev_hashtable_bucket_t *bucket;
212
213 for (i = 0; i < HASH_SIZE; i++) {
214 while (ino_dev_hashtable[i] != NULL) {
215 bucket = ino_dev_hashtable[i]->next;
216 free(ino_dev_hashtable[i]);
217 ino_dev_hashtable[i] = bucket;
218 }
219 }
220}
221
Erik Andersen23dea9b2000-05-13 00:28:25 +0000222#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000223
Erik Andersen23dea9b2000-05-13 00:28:25 +0000224#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000225/*
226 * Return TRUE if a fileName is a directory.
227 * Nonexistant files return FALSE.
228 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000229int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000230{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000232 int didMalloc = 0;
233
234 if (statBuf == NULL) {
235 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
236 ++didMalloc;
237 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000240 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000242 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000243
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000244 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
245 status = FALSE;
246 }
247 else status = TRUE;
248
249 if (didMalloc) {
250 free(statBuf);
251 statBuf = NULL;
252 }
253 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000254}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000255#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000256
Erik Andersen23dea9b2000-05-13 00:28:25 +0000257#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000258/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000259 * Copy one file to another, while possibly preserving its modes, times, and
260 * modes. Returns TRUE if successful, or FALSE on a failure with an error
261 * message output. (Failure is not indicated if attributes cannot be set.)
262 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000263 */
264int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000266 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000267{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 int rfd;
269 int wfd;
270 int rcc;
271 int status;
272 char buf[BUF_SIZE];
273 struct stat srcStatBuf;
274 struct stat dstStatBuf;
275 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 if (followLinks == TRUE)
278 status = stat(srcName, &srcStatBuf);
279 else
280 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000281
Erik Andersenfac10d72000-02-07 05:29:42 +0000282 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 perror(srcName);
284 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000285 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286
287 if (followLinks == TRUE)
288 status = stat(destName, &dstStatBuf);
289 else
290 status = lstat(destName, &dstStatBuf);
291
Erik Andersen59b9e872000-05-10 05:05:45 +0000292 if (status < 0 || forceFlag==TRUE) {
293 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 dstStatBuf.st_ino = -1;
295 dstStatBuf.st_dev = -1;
296 }
297
298 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
299 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
300 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
301 return FALSE;
302 }
303
304 if (S_ISDIR(srcStatBuf.st_mode)) {
305 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
306 /* Make sure the directory is writable */
307 status = mkdir(destName, 0777777 ^ umask(0));
308 if (status < 0 && errno != EEXIST) {
309 perror(destName);
310 return FALSE;
311 }
312 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000313 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 int link_size;
315
316 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000317 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
318 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 if (link_size < 0) {
320 perror(srcName);
321 return FALSE;
322 }
323 link_val[link_size] = '\0';
324 status = symlink(link_val, destName);
325 if (status < 0) {
326 perror(destName);
327 return FALSE;
328 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000329#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000331 /* Try to set owner, but fail silently like GNU cp */
332 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000334#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 return TRUE;
336 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
337 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
338 if (mkfifo(destName, 0644) < 0) {
339 perror(destName);
340 return FALSE;
341 }
342 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
343 || S_ISSOCK(srcStatBuf.st_mode)) {
344 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
345 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
346 perror(destName);
347 return FALSE;
348 }
349 } else if (S_ISREG(srcStatBuf.st_mode)) {
350 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
351 rfd = open(srcName, O_RDONLY);
352 if (rfd < 0) {
353 perror(srcName);
354 return FALSE;
355 }
356
357 wfd =
358 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
359 srcStatBuf.st_mode);
360 if (wfd < 0) {
361 perror(destName);
362 close(rfd);
363 return FALSE;
364 }
365
366 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
367 if (fullWrite(wfd, buf, rcc) < 0)
368 goto error_exit;
369 }
370 if (rcc < 0) {
371 goto error_exit;
372 }
373
374 close(rfd);
375 if (close(wfd) < 0) {
376 return FALSE;
377 }
378 }
379
380 if (setModes == TRUE) {
381 /* This is fine, since symlinks never get here */
382 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
383 perror(destName);
384 exit FALSE;
385 }
386 if (chmod(destName, srcStatBuf.st_mode) < 0) {
387 perror(destName);
388 exit FALSE;
389 }
390 times.actime = srcStatBuf.st_atime;
391 times.modtime = srcStatBuf.st_mtime;
392 if (utime(destName, &times) < 0) {
393 perror(destName);
394 exit FALSE;
395 }
396 }
397
Erik Andersenfac10d72000-02-07 05:29:42 +0000398 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000399
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 error_exit:
401 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000402 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000404
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000406}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000407#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000408
409
410
Erik Andersen23dea9b2000-05-13 00:28:25 +0000411#if defined BB_TAR || defined BB_LS
412
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000413#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
414#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
415
416/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
417static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000418 0, 0, S_ISUID,
419 0, 0, S_ISGID,
420 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000421};
422
423/* The 9 mode bits to test */
424static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 S_IRUSR, S_IWUSR, S_IXUSR,
426 S_IRGRP, S_IWGRP, S_IXGRP,
427 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000428};
429
430#define MODE1 "rwxrwxrwx"
431#define MODE0 "---------"
432#define SMODE1 "..s..s..t"
433#define SMODE0 "..S..S..T"
434
Eric Andersencc8ed391999-10-05 16:24:54 +0000435/*
436 * Return the standard ls-like mode string from a file mode.
437 * This is static and so is overwritten on each call.
438 */
Eric Andersenf811e071999-10-09 00:25:00 +0000439const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000440{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000442
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 int i;
444
445 buf[0] = TYPECHAR(mode);
446 for (i = 0; i < 9; i++) {
447 if (mode & SBIT[i])
448 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
449 else
450 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
451 }
452 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000453}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000454#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000455
456
Erik Andersen23dea9b2000-05-13 00:28:25 +0000457#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000458/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000459 * Return the standard ls-like time string from a time_t
460 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000461 */
Eric Andersenf811e071999-10-09 00:25:00 +0000462const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000463{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 time_t now;
465 char *str;
466 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000467
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000469
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000471
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 strcpy(buf, &str[4]);
473 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000474
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
476 strcpy(&buf[7], &str[20]);
477 buf[11] = '\0';
478 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000479
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000481}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000482#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000483
Erik Andersen23dea9b2000-05-13 00:28:25 +0000484#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000485/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000486 * Write all of the supplied buffer out to a file.
487 * This does multiple writes as necessary.
488 * Returns the amount written, or -1 on an error.
489 */
Eric Andersenf811e071999-10-09 00:25:00 +0000490int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000491{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 int cc;
493 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000494
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000496
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 while (len > 0) {
498 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000499
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 if (cc < 0)
501 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000502
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 buf += cc;
504 total += cc;
505 len -= cc;
506 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000509}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000510#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
512
Erik Andersen23dea9b2000-05-13 00:28:25 +0000513#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000514/*
515 * Read all of the supplied buffer from a file.
516 * This does multiple reads as necessary.
517 * Returns the amount read, or -1 on an error.
518 * A short read is returned on an end of file.
519 */
Eric Andersenf811e071999-10-09 00:25:00 +0000520int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000521{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 int cc;
523 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000524
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000526
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527 while (len > 0) {
528 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000529
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 if (cc < 0)
531 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000532
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 if (cc == 0)
534 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000535
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 buf += cc;
537 total += cc;
538 len -= cc;
539 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000540
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000542}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000543#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000544
545
Erik Andersen23dea9b2000-05-13 00:28:25 +0000546#if defined (BB_CHMOD_CHOWN_CHGRP) \
547 || defined (BB_CP_MV) \
548 || defined (BB_FIND) \
549 || defined (BB_INSMOD) \
550 || defined (BB_RM) \
551 || defined (BB_TAR)
552
Eric Andersencc8ed391999-10-05 16:24:54 +0000553/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000554 * Walk down all the directories under the specified
555 * location, and do something (something specified
556 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000557 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000558 * Unfortunatly, while nftw(3) could replace this and reduce
559 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000560 * and so isn't sufficiently portable to take over since glibc2.1
561 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000562 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000563int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564 int recurse, int followLinks, int depthFirst,
565 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000566 struct stat * statbuf,
567 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000569 struct stat * statbuf,
570 void* userData),
571 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000572{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 int status;
574 struct stat statbuf;
575 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000576
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 if (followLinks == TRUE)
578 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000579 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000581
Eric Andersen2b69c401999-10-05 22:58:32 +0000582 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583#ifdef BB_DEBUG_PRINT_SCAFFOLD
584 fprintf(stderr,
585 "status=%d followLinks=%d TRUE=%d\n",
586 status, followLinks, TRUE);
587#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000588 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000589 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000590 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591
592 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
593 if (fileAction == NULL)
594 return TRUE;
595 else
Erik Andersen3364d782000-03-28 00:58:14 +0000596 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 }
598
599 if (recurse == FALSE) {
600 if (S_ISDIR(statbuf.st_mode)) {
601 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000602 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 else
604 return TRUE;
605 }
606 }
607
608 if (S_ISDIR(statbuf.st_mode)) {
609 DIR *dir;
610
611 dir = opendir(fileName);
612 if (!dir) {
613 perror(fileName);
614 return FALSE;
615 }
616 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000617 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 if (status == FALSE) {
619 perror(fileName);
620 return FALSE;
621 }
622 }
623 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000624 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625
626 if ((strcmp(next->d_name, "..") == 0)
627 || (strcmp(next->d_name, ".") == 0)) {
628 continue;
629 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000630 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 fprintf(stderr, name_too_long, "ftw");
632 return FALSE;
633 }
Erik Andersenc3660502000-04-18 21:18:57 +0000634 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 sprintf(nextFile, "%s/%s", fileName, next->d_name);
636 status =
637 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000638 fileAction, dirAction, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 if (status < 0) {
640 closedir(dir);
641 return FALSE;
642 }
643 }
644 status = closedir(dir);
645 if (status < 0) {
646 perror(fileName);
647 return FALSE;
648 }
649 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000650 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651 if (status == FALSE) {
652 perror(fileName);
653 return FALSE;
654 }
655 }
656 } else {
657 if (fileAction == NULL)
658 return TRUE;
659 else
Erik Andersen3364d782000-03-28 00:58:14 +0000660 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000661 }
662 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000663}
664
Erik Andersen23dea9b2000-05-13 00:28:25 +0000665#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000666
Eric Andersenf6be9441999-10-13 21:12:06 +0000667
668
Erik Andersen23dea9b2000-05-13 00:28:25 +0000669#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000670/*
671 * Attempt to create the directories along the specified path, except for
672 * the final component. The mode is given for the final directory only,
673 * while all previous ones get default protections. Errors are not reported
674 * here, as failures to restore files can be reported later.
675 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000677{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 char *cp;
679 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000680 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000681 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000682
Erik Andersene49d5ec2000-02-08 19:58:47 +0000683 strcpy(buf, name);
684 for (cp = buf; *cp == '/'; cp++);
685 cp = strchr(cp, '/');
686 while (cp) {
687 cpOld = cp;
688 cp = strchr(cp + 1, '/');
689 *cpOld = '\0';
690 retVal = mkdir(buf, cp ? 0777 : mode);
691 if (retVal != 0 && errno != EEXIST) {
692 perror(buf);
693 return FALSE;
694 }
695 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000696 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000698}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000699#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000700
701
702
Erik Andersen23dea9b2000-05-13 00:28:25 +0000703#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000704/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000705
706
707
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000709{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000710 mode_t andMode =
711
712 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000713 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 mode_t mode = 0;
715 mode_t groups = 0;
716 char type;
717 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000718
719 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 for (;;) {
721 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000722 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000723 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000724 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000726 continue;
727 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000729 continue;
730 case 'o':
731 groups |= S_IRWXO;
732 continue;
733 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000734 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000735 continue;
736 case '+':
737 case '=':
738 case '-':
739 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740 if (groups == 0) /* The default is "all" */
741 groups |=
742 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000743 break;
744 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 if (isdigit(c) && c >= '0' && c <= '7' &&
746 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000747 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000748 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000750 return (FALSE);
751 }
752 break;
753 }
754
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755 while ((c = *s++) != '\0') {
756 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000757 case ',':
758 break;
759 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000761 continue;
762 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000763 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000764 continue;
765 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000767 continue;
768 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000770 continue;
771 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000772 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 continue;
774 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000775 *theMode &= andMode;
776 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000778 }
779 break;
780 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000781 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000782 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000783 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000784 /* fall through */
785 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000786 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000787 break;
788 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000789 andMode &= ~(mode & groups);
790 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000791 break;
792 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000794 *theMode &= andMode;
795 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000796 return (TRUE);
797}
Eric Andersence8f3b91999-10-20 07:03:36 +0000798
799
Erik Andersen23dea9b2000-05-13 00:28:25 +0000800#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000801
Eric Andersene674eb71999-10-19 20:52:57 +0000802
803
Erik Andersen23dea9b2000-05-13 00:28:25 +0000804
805
806#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID
807
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000808/* This parses entries in /etc/passwd and /etc/group. This is desirable
809 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
810 * increases target size and is often not needed or wanted for embedded
811 * systems.
812 *
813 * /etc/passwd entries look like this:
814 * root:x:0:0:root:/root:/bin/bash
815 * and /etc/group entries look like this:
816 * root:x:0:
817 *
818 * This uses buf as storage to hold things.
819 *
820 */
Erik Andersen5b46d792000-05-17 05:24:24 +0000821unsigned long my_getid(const char *filename, char *name, unsigned long id, unsigned long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000822{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000823 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000824 char *rname, *start, *end, buf[128];
Erik Andersen330fd2b2000-05-19 05:35:19 +0000825 unsigned long rid;
Erik Andersen5b46d792000-05-17 05:24:24 +0000826 unsigned long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000827
Erik Andersene49d5ec2000-02-08 19:58:47 +0000828 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000829 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000830 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000831 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000832 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000833 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000834
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000836 if (buf[0] == '#')
837 continue;
838
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000839 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000840 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000842 if (end == NULL)
843 continue;
844 *end = '\0';
845 rname = start;
846
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000847 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000848 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000850 if (end == NULL)
851 continue;
852
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000853 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000854 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000855 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000856 if (end == start)
857 continue;
858
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000859 /* gid in passwd */
860 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000861 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000862
Eric Andersend23f9ba1999-10-20 19:18:15 +0000863 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000865 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000866 fclose(file);
867 return (rid);
868 }
869 }
870 if (id != -1 && id == rid) {
871 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000872 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000873 fclose(file);
874 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000875 }
876 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000877 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000878 return (-1);
879}
880
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000881/* returns a uid given a username */
Erik Andersen5b46d792000-05-17 05:24:24 +0000882unsigned long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000883{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000884 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000885}
886
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000887/* returns a gid given a group name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000888unsigned long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000889{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000890 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000891}
892
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000893/* gets a username given a uid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000894void my_getpwuid(char *name, unsigned long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000895{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000896 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000897}
898
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000899/* gets a groupname given a gid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000900void my_getgrgid(char *group, unsigned long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000901{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000902 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000903}
904
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000905/* gets a gid given a user name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000906unsigned long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000907{
Erik Andersen5b46d792000-05-17 05:24:24 +0000908 unsigned long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000909 my_getid("/etc/passwd", name, -1, &gid);
910 return gid;
911}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000912
Erik Andersen23dea9b2000-05-13 00:28:25 +0000913#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */
914
915
Eric Andersenf86aee72000-07-06 17:20:46 +0000916#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000917
Eric Andersenbd22ed82000-07-08 18:55:24 +0000918/* From <linux/kd.h> */
919#define KDGKBTYPE 0x4B33 /* get keyboard type */
920#define KB_84 0x01
921#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000922
Erik Andersene49d5ec2000-02-08 19:58:47 +0000923int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000924{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 char arg;
926
927 arg = 0;
928 return (ioctl(fd, KDGKBTYPE, &arg) == 0
929 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000930}
931
Erik Andersene49d5ec2000-02-08 19:58:47 +0000932static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000933{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 int fd;
935
936 /* try read-only */
937 fd = open(fnam, O_RDWR);
938
939 /* if failed, try read-only */
940 if (fd < 0 && errno == EACCES)
941 fd = open(fnam, O_RDONLY);
942
943 /* if failed, try write-only */
944 if (fd < 0 && errno == EACCES)
945 fd = open(fnam, O_WRONLY);
946
947 /* if failed, fail */
948 if (fd < 0)
949 return -1;
950
951 /* if not a console, fail */
952 if (!is_a_console(fd)) {
953 close(fd);
954 return -1;
955 }
956
957 /* success */
958 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000959}
960
961/*
962 * Get an fd for use with kbd/console ioctls.
963 * We try several things because opening /dev/console will fail
964 * if someone else used X (which does a chown on /dev/console).
965 *
966 * if tty_name is non-NULL, try this one instead.
967 */
968
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000970{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000972
Erik Andersene49d5ec2000-02-08 19:58:47 +0000973 if (tty_name) {
974 if (-1 == (fd = open_a_console(tty_name)))
975 return -1;
976 else
977 return fd;
978 }
979
980 fd = open_a_console("/dev/tty");
981 if (fd >= 0)
982 return fd;
983
984 fd = open_a_console("/dev/tty0");
985 if (fd >= 0)
986 return fd;
987
988 fd = open_a_console("/dev/console");
989 if (fd >= 0)
990 return fd;
991
992 for (fd = 0; fd < 3; fd++)
993 if (is_a_console(fd))
994 return fd;
995
996 fprintf(stderr,
997 "Couldnt get a file descriptor referring to the console\n");
998 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +0000999}
1000
1001
Eric Andersenf86aee72000-07-06 17:20:46 +00001002#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001003
1004
Erik Andersen23dea9b2000-05-13 00:28:25 +00001005#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
1006
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001007/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001008char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001009{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001010 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001011
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012 while (*haystack) {
1013 if (!strncasecmp(haystack, needle, len))
1014 break;
1015 haystack++;
1016 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001017
Erik Andersene49d5ec2000-02-08 19:58:47 +00001018 if (!(*haystack))
1019 haystack = NULL;
1020
1021 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001022}
1023
Eric Andersenc1525e81999-10-29 00:07:31 +00001024/* This tries to find a needle in a haystack, but does so by
1025 * only trying to match literal strings (look 'ma, no regexps!)
1026 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001027 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +00001028 * -Erik Andersen
1029 */
1030extern int find_match(char *haystack, char *needle, int ignoreCase)
1031{
1032
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033 if (ignoreCase == FALSE)
1034 haystack = strstr(haystack, needle);
1035 else
1036 haystack = stristr(haystack, needle);
1037 if (haystack == NULL)
1038 return FALSE;
1039 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001040}
1041
1042
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001043/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001044extern int replace_match(char *haystack, char *needle, char *newNeedle,
1045 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +00001046{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047 int foundOne = 0;
1048 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +00001049
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 if (ignoreCase == FALSE)
1051 where = strstr(haystack, needle);
1052 else
1053 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001054
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 if (strcmp(needle, newNeedle) == 0)
1056 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001057
Erik Andersen0d068a22000-03-21 22:32:57 +00001058 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 while (where != NULL) {
1060 foundOne++;
1061 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001062 for (slider = haystack, slider1 = oldhayStack; slider != where;
1063 slider++, slider1++);
1064 *slider = 0;
1065 haystack = strcat(haystack, newNeedle);
1066 slider1 += strlen(needle);
1067 haystack = strcat(haystack, slider1);
1068 where = strstr(slider, needle);
1069 }
1070 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001071
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 if (foundOne > 0)
1073 return TRUE;
1074 else
1075 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001076}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001077
1078#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001079
1080
Eric Andersen9f16d612000-06-12 23:11:16 +00001081#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001082/*
1083 * Routine to see if a text string is matched by a wildcard pattern.
1084 * Returns TRUE if the text is matched, or FALSE if it is not matched
1085 * or if the pattern is invalid.
1086 * * matches zero or more characters
1087 * ? matches a single character
1088 * [abc] matches 'a', 'b' or 'c'
1089 * \c quotes character c
1090 * Adapted from code written by Ingo Wilken, and
1091 * then taken from sash, Copyright (c) 1999 by David I. Bell
1092 * Permission is granted to use, distribute, or modify this source,
1093 * provided that this copyright notice remains intact.
1094 * Permission to distribute this code under the GPL has been granted.
1095 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001096extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001097{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 const char *retryPat;
1099 const char *retryText;
1100 int ch;
1101 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001102 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001103
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 retryPat = NULL;
1105 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001106
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001108 ch = *pattern++;
1109
Erik Andersene49d5ec2000-02-08 19:58:47 +00001110 switch (ch) {
1111 case '*':
1112 retryPat = pattern;
1113 retryText = text;
1114 break;
1115
1116 case '[':
1117 found = FALSE;
1118
1119 while ((ch = *pattern++) != ']') {
1120 if (ch == '\\')
1121 ch = *pattern++;
1122
1123 if (ch == '\0')
1124 return FALSE;
1125
1126 if (*text == ch)
1127 found = TRUE;
1128 }
Erik Andersen632bb572000-04-18 22:09:06 +00001129 len=strlen(text);
1130 if (found == FALSE && len!=0) {
1131 return FALSE;
1132 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001133 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001134 if (strlen(pattern)==0 && len==1) {
1135 return TRUE;
1136 }
1137 if (len!=0) {
1138 text++;
1139 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001140 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001141 }
1142
1143 /* fall into next case */
1144
1145 case '?':
1146 if (*text++ == '\0')
1147 return FALSE;
1148
1149 break;
1150
1151 case '\\':
1152 ch = *pattern++;
1153
1154 if (ch == '\0')
1155 return FALSE;
1156
1157 /* fall into next case */
1158
1159 default:
1160 if (*text == ch) {
1161 if (*text)
1162 text++;
1163 break;
1164 }
1165
1166 if (*text) {
1167 pattern = retryPat;
1168 text = ++retryText;
1169 break;
1170 }
1171
Eric Andersenb186d981999-12-03 09:19:54 +00001172 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001173 }
1174
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175 if (pattern == NULL)
1176 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001177 }
1178
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001180}
Eric Andersen9f16d612000-06-12 23:11:16 +00001181#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001182
1183
Eric Andersen29d2e361999-11-06 06:07:27 +00001184
1185
Erik Andersen23dea9b2000-05-13 00:28:25 +00001186#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001187/*
1188 * Given a block device, find the mount table entry if that block device
1189 * is mounted.
1190 *
1191 * Given any other file (or directory), find the mount table entry for its
1192 * filesystem.
1193 */
1194extern struct mntent *findMountPoint(const char *name, const char *table)
1195{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001196 struct stat s;
1197 dev_t mountDevice;
1198 FILE *mountTable;
1199 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001200
Erik Andersene49d5ec2000-02-08 19:58:47 +00001201 if (stat(name, &s) != 0)
1202 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001203
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 if ((s.st_mode & S_IFMT) == S_IFBLK)
1205 mountDevice = s.st_rdev;
1206 else
1207 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001208
1209
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210 if ((mountTable = setmntent(table, "r")) == 0)
1211 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001212
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213 while ((mountEntry = getmntent(mountTable)) != 0) {
1214 if (strcmp(name, mountEntry->mnt_dir) == 0
1215 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1216 break;
1217 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1218 break;
1219 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1220 break;
1221 }
1222 endmntent(mountTable);
1223 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001224}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001225#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001226
1227
1228
Erik Andersen23dea9b2000-05-13 00:28:25 +00001229#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001230/*
1231 * Read a number with a possible multiplier.
1232 * Returns -1 if the number format is illegal.
1233 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001235{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001237
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238 if (!isDecimal(*cp))
1239 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001240
Erik Andersene49d5ec2000-02-08 19:58:47 +00001241 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001242
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 while (isDecimal(*cp))
1244 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001245
Erik Andersene49d5ec2000-02-08 19:58:47 +00001246 switch (*cp++) {
1247 case 'M':
1248 case 'm': /* `tail' uses it traditionally */
1249 value *= 1048576;
1250 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001251
Erik Andersene49d5ec2000-02-08 19:58:47 +00001252 case 'k':
1253 value *= 1024;
1254 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001255
Erik Andersene49d5ec2000-02-08 19:58:47 +00001256 case 'b':
1257 value *= 512;
1258 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001259
Erik Andersene49d5ec2000-02-08 19:58:47 +00001260 case 'w':
1261 value *= 2;
1262 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001263
Erik Andersene49d5ec2000-02-08 19:58:47 +00001264 case '\0':
1265 return value;
1266
1267 default:
1268 return -1;
1269 }
1270
1271 if (*cp)
1272 return -1;
1273
Eric Andersen1792f8c1999-12-09 06:11:36 +00001274 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001275}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001276#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001277
Eric Andersen6805d5d1999-12-09 22:39:55 +00001278
Erik Andersen23dea9b2000-05-13 00:28:25 +00001279#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001280/* try to open up the specified device */
1281extern int device_open(char *device, int mode)
1282{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001283 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001284
Erik Andersene49d5ec2000-02-08 19:58:47 +00001285 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001286
Erik Andersene49d5ec2000-02-08 19:58:47 +00001287 /* Retry up to 5 times */
1288 for (f = 0; f < 5; f++)
1289 if ((fd = open(device, m, 0600)) >= 0)
1290 break;
1291 if (fd < 0)
1292 return fd;
1293 /* Reset original flags. */
1294 if (m != mode)
1295 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001296 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001297}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001298#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001299
1300
Erik Andersen23dea9b2000-05-13 00:28:25 +00001301#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001302#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001303#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001304#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001305
Erik Andersen23dea9b2000-05-13 00:28:25 +00001306#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001307/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001308 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309 * This finds the pid of the specified process,
1310 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001311 *
Erik Andersen825aead2000-04-07 06:00:07 +00001312 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001313 */
Erik Andersen825aead2000-04-07 06:00:07 +00001314extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001315{
Erik Andersen825aead2000-04-07 06:00:07 +00001316 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001317 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001318 pid_t num_pids;
1319 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001320 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001321
1322 /* open device */
1323 fd = open(device, O_RDONLY);
1324 if (fd < 0)
1325 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1326
1327 /* Find out how many processes there are */
1328 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1329 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1330
1331 /* Allocate some memory -- grab a few extras just in case
1332 * some new processes start up while we wait. The kernel will
1333 * just ignore any extras if we give it too many, and will trunc.
1334 * the list if we give it too few. */
1335 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1336 pid_array[0] = num_pids+10;
1337
1338 /* Now grab the pid list */
1339 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1340 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1341
1342 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001343 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001344 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001345 struct pid_info info;
1346
1347 info.pid = pid_array[i];
1348 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1349 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1350
Erik Andersen0a64de92000-04-18 20:00:03 +00001351 /* Make sure we only match on the process name */
1352 p=info.command_line+1;
1353 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1354 (p)++;
1355 }
1356 if (isspace(*(p)))
1357 *p='\0';
1358
Erik Andersenb9167cb2000-04-18 22:41:30 +00001359 if ((strstr(info.command_line, pidName) != NULL)
1360 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001361 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1362 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001363 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001364 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001365 }
1366 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001367 if (pidList)
1368 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001369
1370 /* Free memory */
1371 free( pid_array);
1372
1373 /* close device */
1374 if (close (fd) != 0)
1375 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1376
Erik Andersen825aead2000-04-07 06:00:07 +00001377 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001378}
Erik Andersen269bba22000-03-08 14:50:47 +00001379#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001380#if ! defined BB_FEATURE_USE_PROCFS
1381#error Sorry, I depend on the /proc filesystem right now.
1382#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001383
Erik Andersen246cc6d2000-03-07 07:41:42 +00001384/* findPidByName()
1385 *
1386 * This finds the pid of the specified process.
1387 * Currently, it's implemented by rummaging through
1388 * the proc filesystem.
1389 *
Erik Andersen825aead2000-04-07 06:00:07 +00001390 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001391 */
Erik Andersen825aead2000-04-07 06:00:07 +00001392extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001393{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001394 DIR *dir;
1395 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001396 pid_t* pidList=NULL;
1397 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001398
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001399 dir = opendir("/proc");
1400 if (!dir)
1401 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1402
1403 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001404 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001405 char filename[256];
1406 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001407 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001408
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001409 /* If it isn't a number, we don't want it */
1410 if (!isdigit(*next->d_name))
1411 continue;
1412
Erik Andersen825aead2000-04-07 06:00:07 +00001413 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001414 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001415 status = fopen(filename, "r");
1416 if (!status) {
1417 continue;
1418 }
1419 fgets(buffer, 256, status);
1420 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001421
Erik Andersen0a64de92000-04-18 20:00:03 +00001422 /* Make sure we only match on the process name */
1423 p=buffer+5; /* Skip the name */
1424 while ((p)++) {
1425 if (*p==0 || *p=='\n') {
1426 *p='\0';
1427 break;
1428 }
1429 }
1430 p=buffer+6; /* Skip the "Name:\t" */
1431
Erik Andersenb9167cb2000-04-18 22:41:30 +00001432 if ((strstr(p, pidName) != NULL)
1433 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001434 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1435 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001436 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001437 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001438 }
John Beppuf95ca971999-12-09 22:10:18 +00001439 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001440 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001441 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001442 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001443}
Erik Andersen269bba22000-03-08 14:50:47 +00001444#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001445#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001446
Erik Andersen0d068a22000-03-21 22:32:57 +00001447/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001449{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001450 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001451
Erik Andersen0d068a22000-03-21 22:32:57 +00001452 if (cp == NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001453 fatalError(memory_exhausted, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001455}
1456
Eric Andersenda1d1e72000-07-10 23:39:44 +00001457#if defined BB_FEATURE_NFSMOUNT
1458extern char * xstrdup (const char *s) {
1459 char *t;
1460
1461 if (s == NULL)
1462 return NULL;
1463
1464 t = strdup (s);
1465
1466 if (t == NULL)
1467 fatalError(memory_exhausted, "");
1468
1469 return t;
1470}
1471
1472extern char * xstrndup (const char *s, int n) {
1473 char *t;
1474
1475 if (s == NULL)
1476 fatalError("xstrndup bug");
1477
1478 t = xmalloc(n+1);
1479 strncpy(t,s,n);
1480 t[n] = 0;
1481
1482 return t;
1483}
1484#endif
1485
1486
Erik Andersen23dea9b2000-05-13 00:28:25 +00001487#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001488extern int vdprintf(int d, const char *format, va_list ap)
1489{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001490 char buf[BUF_SIZE];
1491 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001492
Erik Andersene49d5ec2000-02-08 19:58:47 +00001493 len = vsprintf(buf, format, ap);
1494 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001495}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001496#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001497
Erik Andersen23dea9b2000-05-13 00:28:25 +00001498#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen5cbdd712000-01-26 20:06:48 +00001499extern int del_loop(const char *device)
1500{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001501 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001502
Erik Andersene49d5ec2000-02-08 19:58:47 +00001503 if ((fd = open(device, O_RDONLY)) < 0) {
1504 perror(device);
1505 return (FALSE);
1506 }
1507 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1508 perror("ioctl: LOOP_CLR_FD");
1509 return (FALSE);
1510 }
1511 close(fd);
1512 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001513}
Erik Andersende7965c2000-01-26 23:49:21 +00001514
Erik Andersene49d5ec2000-02-08 19:58:47 +00001515extern int set_loop(const char *device, const char *file, int offset,
1516 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001517{
1518 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001519 int fd, ffd, mode;
1520
Erik Andersende7965c2000-01-26 23:49:21 +00001521 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001522 if ((ffd = open(file, mode)) < 0 && !*loopro
1523 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1524 perror(file);
1525 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001526 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001527 if ((fd = open(device, mode)) < 0) {
1528 close(ffd);
1529 perror(device);
1530 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001531 }
1532 *loopro = (mode == O_RDONLY);
1533
1534 memset(&loopinfo, 0, sizeof(loopinfo));
1535 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001536 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001537
1538 loopinfo.lo_offset = offset;
1539
1540 loopinfo.lo_encrypt_key_size = 0;
1541 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1542 perror("ioctl: LOOP_SET_FD");
1543 close(fd);
1544 close(ffd);
1545 return 1;
1546 }
1547 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1548 (void) ioctl(fd, LOOP_CLR_FD, 0);
1549 perror("ioctl: LOOP_SET_STATUS");
1550 close(fd);
1551 close(ffd);
1552 return 1;
1553 }
1554 close(fd);
1555 close(ffd);
1556 return 0;
1557}
1558
Erik Andersene49d5ec2000-02-08 19:58:47 +00001559extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001560{
1561 char dev[20];
1562 int i, fd;
1563 struct stat statbuf;
1564 struct loop_info loopinfo;
1565
Erik Andersene49d5ec2000-02-08 19:58:47 +00001566 for (i = 0; i <= 7; i++) {
1567 sprintf(dev, "/dev/loop%d", i);
1568 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1569 if ((fd = open(dev, O_RDONLY)) >= 0) {
1570 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1571 if (errno == ENXIO) { /* probably free */
1572 close(fd);
1573 return strdup(dev);
1574 }
1575 }
1576 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001577 }
Erik Andersende7965c2000-01-26 23:49:21 +00001578 }
Erik Andersende7965c2000-01-26 23:49:21 +00001579 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001580 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001581}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001582#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001583
Erik Andersen23dea9b2000-05-13 00:28:25 +00001584#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001585extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001586{
Erik Andersen016ffe92000-03-22 00:58:54 +00001587 DIR *dir;
1588 struct dirent *entry;
1589 struct stat statBuf, rootStat;
1590 char fileName[BUFSIZ];
1591
Erik Andersenec5bd902000-03-22 07:12:05 +00001592 if (stat("/", &rootStat) != 0) {
1593 errorMsg("could not stat '/'\n");
1594 return( FALSE);
1595 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001596
Erik Andersenec5bd902000-03-22 07:12:05 +00001597 dir = opendir("/dev");
1598 if (!dir) {
1599 errorMsg("could not open '/dev'\n");
1600 return( FALSE);
1601 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001602
1603 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001604
Erik Andersen016ffe92000-03-22 00:58:54 +00001605 /* Must skip ".." since that is "/", and so we
1606 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001607 if (strcmp(entry->d_name, "..") == 0)
1608 continue;
1609
1610 sprintf( fileName, "/dev/%s", entry->d_name);
1611
1612 if (stat(fileName, &statBuf) != 0)
1613 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001614 /* Some char devices have the same dev_t as block
1615 * devices, so make sure this is a block device */
1616 if (! S_ISBLK(statBuf.st_mode))
1617 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001618 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001619 strcpy(name, fileName);
1620 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001621 }
1622 }
1623
Erik Andersenec5bd902000-03-22 07:12:05 +00001624 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001625}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001626#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001627
Mark Whitley1ca41772000-06-28 22:15:26 +00001628
1629/* get_line_from_file() - This function reads an entire line from a text file
1630 * up to a newline. It returns a malloc'ed char * which must be stored and
1631 * free'ed by the caller. */
1632extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001633{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001634 static const int GROWBY = 80; /* how large we will grow strings by */
1635
Mark Whitley1ca41772000-06-28 22:15:26 +00001636 int ch;
1637 int idx = 0;
1638 char *linebuf = NULL;
1639 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001640
Mark Whitley1ca41772000-06-28 22:15:26 +00001641 while (1) {
1642 ch = fgetc(file);
1643 if (ch == EOF)
1644 break;
1645 /* grow the line buffer as necessary */
Eric Andersen161cf932000-07-09 02:38:01 +00001646 if (idx > linebufsz-2)
Mark Whitley1ca41772000-06-28 22:15:26 +00001647 linebuf = realloc(linebuf, linebufsz += GROWBY);
1648 linebuf[idx++] = (char)ch;
1649 if ((char)ch == '\n')
1650 break;
1651 }
John Beppu5a728cf2000-04-17 04:22:09 +00001652
Mark Whitley1ca41772000-06-28 22:15:26 +00001653 if (idx == 0)
1654 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001655
Mark Whitley1ca41772000-06-28 22:15:26 +00001656 linebuf[idx] = 0;
1657 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001658}
Erik Andersen016ffe92000-03-22 00:58:54 +00001659
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001660#if defined BB_ECHO || defined BB_TR
1661char process_escape_sequence(char **ptr)
1662{
1663 char c;
1664
1665 switch (c = *(*ptr)++) {
1666 case 'a':
1667 c = '\a';
1668 break;
1669 case 'b':
1670 c = '\b';
1671 break;
1672 case 'f':
1673 c = '\f';
1674 break;
1675 case 'n':
1676 c = '\n';
1677 break;
1678 case 't':
1679 c = '\t';
1680 break;
1681 case 'v':
1682 c = '\v';
1683 break;
1684 case '\\':
1685 c = '\\';
1686 break;
1687 case '0': case '1': case '2': case '3':
1688 case '4': case '5': case '6': case '7':
1689 c -= '0';
1690 if ('0' <= **ptr && **ptr <= '7') {
1691 c = c * 8 + (*(*ptr)++ - '0');
1692 if ('0' <= **ptr && **ptr <= '7')
1693 c = c * 8 + (*(*ptr)++ - '0');
1694 }
1695 break;
1696 default:
1697 (*ptr)--;
1698 c = '\\';
1699 break;
1700 }
1701 return c;
1702}
1703#endif
1704
Eric Andersen0b4551f2000-07-10 16:44:03 +00001705#if defined BB_BASENAME || defined BB_LN
1706char *get_last_path_component(char *path)
1707{
1708 char *s=path+strlen(path)-1;
1709
1710 /* strip trailing slashes */
1711 while (s && *s == '/') {
1712 *s-- = '\0';
1713 }
1714
1715 /* find last component */
1716 s = strrchr(path, '/');
1717 if (s==NULL) return path;
1718 else return s+1;
1719}
1720#endif
1721
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001722#if defined BB_GREP || defined BB_SED
1723int bb_regcomp(regex_t *preg, const char *regex, int cflags)
1724{
1725 int ret;
1726 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1727 int errmsgsz = regerror(ret, preg, NULL, 0);
1728 char *errmsg = xmalloc(errmsgsz);
1729 regerror(ret, preg, errmsg, errmsgsz);
1730 errorMsg("bb_regcomp: %s\n", errmsg);
1731 free(errmsg);
1732 regfree(preg);
1733 }
1734 return ret;
1735}
1736#endif
1737
Eric Andersencc8ed391999-10-05 16:24:54 +00001738/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001739/*
1740Local Variables:
1741c-file-style: "linux"
1742c-basic-offset: 4
1743tab-width: 4
1744End:
1745*/