blob: 3dedc2c71493d18c2ba78cb14f66a40e0202d137 [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>
Erik Andersen5cbdd712000-01-26 20:06:48 +000056#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
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
136#include <sys/syscall.h>
Eric Andersena44790f2000-06-28 20:11:28 +0000137#include <linux/unistd.h>
Eric Andersen10dc9d42000-06-26 10:45:52 +0000138_syscall1(int, sysinfo, struct sysinfo *, info);
139#endif /* BB_INIT */
140
Eric Andersena57ba4d2000-07-08 19:20:49 +0000141#if defined BB_MOUNT || defined BB_UMOUNT
142#include <sys/syscall.h>
143#include <linux/unistd.h>
144
145#ifndef __NR_umount2
146#define __NR_umount2 52
147#endif
148
149/* Include our own version of <sys/mount.h>, since libc5 doesn't
150 * know about umount2 */
151extern _syscall1(int, umount, const char *, special_file);
152extern _syscall2(int, umount2, const char *, special_file, int, flags);
153extern _syscall5(int, mount, const char *, special_file, const char *, dir,
154 const char *, fstype, unsigned long int, rwflag, const void *, data);
155#endif
156
Eric Andersen10dc9d42000-06-26 10:45:52 +0000157
158
Erik Andersen23dea9b2000-05-13 00:28:25 +0000159#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000160
Erik Andersen029011b2000-03-04 21:19:32 +0000161#define HASH_SIZE 311 /* Should be prime */
162#define hash_inode(i) ((i) % HASH_SIZE)
163
164static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
165
166/*
167 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
168 * `ino_dev_hashtable', else return 0
169 *
170 * If NAME is a non-NULL pointer to a character pointer, and there is
171 * a match, then set *NAME to the value of the name slot in that
172 * bucket.
173 */
174int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
175{
176 ino_dev_hashtable_bucket_t *bucket;
177
178 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
179 while (bucket != NULL) {
180 if ((bucket->ino == statbuf->st_ino) &&
181 (bucket->dev == statbuf->st_dev))
182 {
183 if (name) *name = bucket->name;
184 return 1;
185 }
186 bucket = bucket->next;
187 }
188 return 0;
189}
190
191/* Add statbuf to statbuf hash table */
192void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
193{
194 int i;
195 size_t s;
196 ino_dev_hashtable_bucket_t *bucket;
197
198 i = hash_inode(statbuf->st_ino);
199 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000200 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000201 bucket->ino = statbuf->st_ino;
202 bucket->dev = statbuf->st_dev;
203 if (name)
204 strcpy(bucket->name, name);
205 else
206 bucket->name[0] = '\0';
207 bucket->next = ino_dev_hashtable[i];
208 ino_dev_hashtable[i] = bucket;
209}
210
211/* Clear statbuf hash table */
212void reset_ino_dev_hashtable(void)
213{
214 int i;
215 ino_dev_hashtable_bucket_t *bucket;
216
217 for (i = 0; i < HASH_SIZE; i++) {
218 while (ino_dev_hashtable[i] != NULL) {
219 bucket = ino_dev_hashtable[i]->next;
220 free(ino_dev_hashtable[i]);
221 ino_dev_hashtable[i] = bucket;
222 }
223 }
224}
225
Erik Andersen23dea9b2000-05-13 00:28:25 +0000226#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000227
Erik Andersen23dea9b2000-05-13 00:28:25 +0000228#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000229/*
230 * Return TRUE if a fileName is a directory.
231 * Nonexistant files return FALSE.
232 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000233int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000234{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000236 int didMalloc = 0;
237
238 if (statBuf == NULL) {
239 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
240 ++didMalloc;
241 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000242
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000244 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000246 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000247
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000248 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
249 status = FALSE;
250 }
251 else status = TRUE;
252
253 if (didMalloc) {
254 free(statBuf);
255 statBuf = NULL;
256 }
257 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000258}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000259#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000260
Erik Andersen23dea9b2000-05-13 00:28:25 +0000261#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000262/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000263 * Copy one file to another, while possibly preserving its modes, times, and
264 * modes. Returns TRUE if successful, or FALSE on a failure with an error
265 * message output. (Failure is not indicated if attributes cannot be set.)
266 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000267 */
268int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000270 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000271{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 int rfd;
273 int wfd;
274 int rcc;
275 int status;
276 char buf[BUF_SIZE];
277 struct stat srcStatBuf;
278 struct stat dstStatBuf;
279 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000280
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 if (followLinks == TRUE)
282 status = stat(srcName, &srcStatBuf);
283 else
284 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000285
Erik Andersenfac10d72000-02-07 05:29:42 +0000286 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 perror(srcName);
288 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000289 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290
291 if (followLinks == TRUE)
292 status = stat(destName, &dstStatBuf);
293 else
294 status = lstat(destName, &dstStatBuf);
295
Erik Andersen59b9e872000-05-10 05:05:45 +0000296 if (status < 0 || forceFlag==TRUE) {
297 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 dstStatBuf.st_ino = -1;
299 dstStatBuf.st_dev = -1;
300 }
301
302 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
303 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
304 fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
305 return FALSE;
306 }
307
308 if (S_ISDIR(srcStatBuf.st_mode)) {
309 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
310 /* Make sure the directory is writable */
311 status = mkdir(destName, 0777777 ^ umask(0));
312 if (status < 0 && errno != EEXIST) {
313 perror(destName);
314 return FALSE;
315 }
316 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000317 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 int link_size;
319
320 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000321 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
322 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 if (link_size < 0) {
324 perror(srcName);
325 return FALSE;
326 }
327 link_val[link_size] = '\0';
328 status = symlink(link_val, destName);
329 if (status < 0) {
330 perror(destName);
331 return FALSE;
332 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000333#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000335 /* Try to set owner, but fail silently like GNU cp */
336 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000338#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 return TRUE;
340 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
341 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
342 if (mkfifo(destName, 0644) < 0) {
343 perror(destName);
344 return FALSE;
345 }
346 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
347 || S_ISSOCK(srcStatBuf.st_mode)) {
348 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
349 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
350 perror(destName);
351 return FALSE;
352 }
353 } else if (S_ISREG(srcStatBuf.st_mode)) {
354 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
355 rfd = open(srcName, O_RDONLY);
356 if (rfd < 0) {
357 perror(srcName);
358 return FALSE;
359 }
360
361 wfd =
362 open(destName, O_WRONLY | O_CREAT | O_TRUNC,
363 srcStatBuf.st_mode);
364 if (wfd < 0) {
365 perror(destName);
366 close(rfd);
367 return FALSE;
368 }
369
370 while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
371 if (fullWrite(wfd, buf, rcc) < 0)
372 goto error_exit;
373 }
374 if (rcc < 0) {
375 goto error_exit;
376 }
377
378 close(rfd);
379 if (close(wfd) < 0) {
380 return FALSE;
381 }
382 }
383
384 if (setModes == TRUE) {
385 /* This is fine, since symlinks never get here */
386 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
387 perror(destName);
388 exit FALSE;
389 }
390 if (chmod(destName, srcStatBuf.st_mode) < 0) {
391 perror(destName);
392 exit FALSE;
393 }
394 times.actime = srcStatBuf.st_atime;
395 times.modtime = srcStatBuf.st_mtime;
396 if (utime(destName, &times) < 0) {
397 perror(destName);
398 exit FALSE;
399 }
400 }
401
Erik Andersenfac10d72000-02-07 05:29:42 +0000402 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000403
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 error_exit:
405 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000406 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000408
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000410}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000411#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000412
413
414
Erik Andersen23dea9b2000-05-13 00:28:25 +0000415#if defined BB_TAR || defined BB_LS
416
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000417#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
418#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
419
420/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
421static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 0, 0, S_ISUID,
423 0, 0, S_ISGID,
424 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000425};
426
427/* The 9 mode bits to test */
428static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 S_IRUSR, S_IWUSR, S_IXUSR,
430 S_IRGRP, S_IWGRP, S_IXGRP,
431 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000432};
433
434#define MODE1 "rwxrwxrwx"
435#define MODE0 "---------"
436#define SMODE1 "..s..s..t"
437#define SMODE0 "..S..S..T"
438
Eric Andersencc8ed391999-10-05 16:24:54 +0000439/*
440 * Return the standard ls-like mode string from a file mode.
441 * This is static and so is overwritten on each call.
442 */
Eric Andersenf811e071999-10-09 00:25:00 +0000443const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000444{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000446
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 int i;
448
449 buf[0] = TYPECHAR(mode);
450 for (i = 0; i < 9; i++) {
451 if (mode & SBIT[i])
452 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
453 else
454 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
455 }
456 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000457}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000458#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000459
460
Erik Andersen23dea9b2000-05-13 00:28:25 +0000461#if defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000462/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000463 * Return the standard ls-like time string from a time_t
464 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000465 */
Eric Andersenf811e071999-10-09 00:25:00 +0000466const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000467{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 time_t now;
469 char *str;
470 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000471
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000473
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000475
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 strcpy(buf, &str[4]);
477 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000478
Erik Andersene49d5ec2000-02-08 19:58:47 +0000479 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
480 strcpy(&buf[7], &str[20]);
481 buf[11] = '\0';
482 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000483
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000485}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000486#endif /* BB_TAR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000487
Erik Andersen23dea9b2000-05-13 00:28:25 +0000488#if defined BB_TAR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000489/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000490 * Write all of the supplied buffer out to a file.
491 * This does multiple writes as necessary.
492 * Returns the amount written, or -1 on an error.
493 */
Eric Andersenf811e071999-10-09 00:25:00 +0000494int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000495{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 int cc;
497 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000498
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000500
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501 while (len > 0) {
502 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000503
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 if (cc < 0)
505 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000506
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 buf += cc;
508 total += cc;
509 len -= cc;
510 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000513}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000514#endif /* BB_TAR || BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000515
516
Erik Andersen23dea9b2000-05-13 00:28:25 +0000517#if defined BB_TAR || defined BB_TAIL
Eric Andersencc8ed391999-10-05 16:24:54 +0000518/*
519 * Read all of the supplied buffer from a file.
520 * This does multiple reads as necessary.
521 * Returns the amount read, or -1 on an error.
522 * A short read is returned on an end of file.
523 */
Eric Andersenf811e071999-10-09 00:25:00 +0000524int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000525{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 int cc;
527 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000528
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000530
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 while (len > 0) {
532 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000533
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534 if (cc < 0)
535 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000536
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 if (cc == 0)
538 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000539
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 buf += cc;
541 total += cc;
542 len -= cc;
543 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000544
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000546}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000547#endif /* BB_TAR || BB_TAIL */
Eric Andersencc8ed391999-10-05 16:24:54 +0000548
549
Erik Andersen23dea9b2000-05-13 00:28:25 +0000550#if defined (BB_CHMOD_CHOWN_CHGRP) \
551 || defined (BB_CP_MV) \
552 || defined (BB_FIND) \
553 || defined (BB_INSMOD) \
554 || defined (BB_RM) \
555 || defined (BB_TAR)
556
Eric Andersencc8ed391999-10-05 16:24:54 +0000557/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000558 * Walk down all the directories under the specified
559 * location, and do something (something specified
560 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000561 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000562 * Unfortunatly, while nftw(3) could replace this and reduce
563 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000564 * and so isn't sufficiently portable to take over since glibc2.1
565 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000566 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000567int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 int recurse, int followLinks, int depthFirst,
569 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000570 struct stat * statbuf,
571 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000573 struct stat * statbuf,
574 void* userData),
575 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000576{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 int status;
578 struct stat statbuf;
579 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000580
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 if (followLinks == TRUE)
582 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000583 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000585
Eric Andersen2b69c401999-10-05 22:58:32 +0000586 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000587#ifdef BB_DEBUG_PRINT_SCAFFOLD
588 fprintf(stderr,
589 "status=%d followLinks=%d TRUE=%d\n",
590 status, followLinks, TRUE);
591#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000592 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000593 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000594 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595
596 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
597 if (fileAction == NULL)
598 return TRUE;
599 else
Erik Andersen3364d782000-03-28 00:58:14 +0000600 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 }
602
603 if (recurse == FALSE) {
604 if (S_ISDIR(statbuf.st_mode)) {
605 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000606 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 else
608 return TRUE;
609 }
610 }
611
612 if (S_ISDIR(statbuf.st_mode)) {
613 DIR *dir;
614
615 dir = opendir(fileName);
616 if (!dir) {
617 perror(fileName);
618 return FALSE;
619 }
620 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000621 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622 if (status == FALSE) {
623 perror(fileName);
624 return FALSE;
625 }
626 }
627 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000628 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629
630 if ((strcmp(next->d_name, "..") == 0)
631 || (strcmp(next->d_name, ".") == 0)) {
632 continue;
633 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000634 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 fprintf(stderr, name_too_long, "ftw");
636 return FALSE;
637 }
Erik Andersenc3660502000-04-18 21:18:57 +0000638 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 sprintf(nextFile, "%s/%s", fileName, next->d_name);
640 status =
641 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000642 fileAction, dirAction, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000643 if (status < 0) {
644 closedir(dir);
645 return FALSE;
646 }
647 }
648 status = closedir(dir);
649 if (status < 0) {
650 perror(fileName);
651 return FALSE;
652 }
653 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000654 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 if (status == FALSE) {
656 perror(fileName);
657 return FALSE;
658 }
659 }
660 } else {
661 if (fileAction == NULL)
662 return TRUE;
663 else
Erik Andersen3364d782000-03-28 00:58:14 +0000664 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 }
666 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000667}
668
Erik Andersen23dea9b2000-05-13 00:28:25 +0000669#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000670
Eric Andersenf6be9441999-10-13 21:12:06 +0000671
672
Erik Andersen23dea9b2000-05-13 00:28:25 +0000673#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000674/*
675 * Attempt to create the directories along the specified path, except for
676 * the final component. The mode is given for the final directory only,
677 * while all previous ones get default protections. Errors are not reported
678 * here, as failures to restore files can be reported later.
679 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000681{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 char *cp;
683 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000684 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000686
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 strcpy(buf, name);
688 for (cp = buf; *cp == '/'; cp++);
689 cp = strchr(cp, '/');
690 while (cp) {
691 cpOld = cp;
692 cp = strchr(cp + 1, '/');
693 *cpOld = '\0';
694 retVal = mkdir(buf, cp ? 0777 : mode);
695 if (retVal != 0 && errno != EEXIST) {
696 perror(buf);
697 return FALSE;
698 }
699 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000700 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000701 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000702}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000703#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000704
705
706
Erik Andersen23dea9b2000-05-13 00:28:25 +0000707#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000708/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000709
710
711
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000713{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 mode_t andMode =
715
716 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000717 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 mode_t mode = 0;
719 mode_t groups = 0;
720 char type;
721 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000722
723 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000724 for (;;) {
725 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000726 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000727 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000728 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000730 continue;
731 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000733 continue;
734 case 'o':
735 groups |= S_IRWXO;
736 continue;
737 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000739 continue;
740 case '+':
741 case '=':
742 case '-':
743 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 if (groups == 0) /* The default is "all" */
745 groups |=
746 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000747 break;
748 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 if (isdigit(c) && c >= '0' && c <= '7' &&
750 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000751 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000752 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000753 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000754 return (FALSE);
755 }
756 break;
757 }
758
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 while ((c = *s++) != '\0') {
760 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000761 case ',':
762 break;
763 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000764 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000765 continue;
766 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000768 continue;
769 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000771 continue;
772 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000774 continue;
775 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000776 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000777 continue;
778 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000779 *theMode &= andMode;
780 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000781 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000782 }
783 break;
784 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000785 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000786 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000787 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000788 /* fall through */
789 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000790 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000791 break;
792 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000793 andMode &= ~(mode & groups);
794 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000795 break;
796 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000797 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000798 *theMode &= andMode;
799 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000800 return (TRUE);
801}
Eric Andersence8f3b91999-10-20 07:03:36 +0000802
803
Erik Andersen23dea9b2000-05-13 00:28:25 +0000804#endif /* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000805
Eric Andersene674eb71999-10-19 20:52:57 +0000806
807
Erik Andersen23dea9b2000-05-13 00:28:25 +0000808
809
810#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID
811
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000812/* This parses entries in /etc/passwd and /etc/group. This is desirable
813 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
814 * increases target size and is often not needed or wanted for embedded
815 * systems.
816 *
817 * /etc/passwd entries look like this:
818 * root:x:0:0:root:/root:/bin/bash
819 * and /etc/group entries look like this:
820 * root:x:0:
821 *
822 * This uses buf as storage to hold things.
823 *
824 */
Erik Andersen5b46d792000-05-17 05:24:24 +0000825unsigned long my_getid(const char *filename, char *name, unsigned long id, unsigned long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000826{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000827 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000828 char *rname, *start, *end, buf[128];
Erik Andersen330fd2b2000-05-19 05:35:19 +0000829 unsigned long rid;
Erik Andersen5b46d792000-05-17 05:24:24 +0000830 unsigned long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000831
Erik Andersene49d5ec2000-02-08 19:58:47 +0000832 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000833 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000834 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000835 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000837 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000838
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000840 if (buf[0] == '#')
841 continue;
842
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000843 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000844 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000846 if (end == NULL)
847 continue;
848 *end = '\0';
849 rname = start;
850
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000851 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000852 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000853 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000854 if (end == NULL)
855 continue;
856
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000857 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000858 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000859 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000860 if (end == start)
861 continue;
862
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000863 /* gid in passwd */
864 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000865 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000866
Eric Andersend23f9ba1999-10-20 19:18:15 +0000867 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000869 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000870 fclose(file);
871 return (rid);
872 }
873 }
874 if (id != -1 && id == rid) {
875 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000876 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 fclose(file);
878 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000879 }
880 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000881 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000882 return (-1);
883}
884
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000885/* returns a uid given a username */
Erik Andersen5b46d792000-05-17 05:24:24 +0000886unsigned long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000887{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000888 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000889}
890
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000891/* returns a gid given a group name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000892unsigned long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000893{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000894 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000895}
896
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000897/* gets a username given a uid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000898void my_getpwuid(char *name, unsigned long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000899{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000900 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000901}
902
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000903/* gets a groupname given a gid */
Erik Andersen5b46d792000-05-17 05:24:24 +0000904void my_getgrgid(char *group, unsigned long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000905{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000906 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000907}
908
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000909/* gets a gid given a user name */
Erik Andersen5b46d792000-05-17 05:24:24 +0000910unsigned long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000911{
Erik Andersen5b46d792000-05-17 05:24:24 +0000912 unsigned long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000913 my_getid("/etc/passwd", name, -1, &gid);
914 return gid;
915}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000916
Erik Andersen23dea9b2000-05-13 00:28:25 +0000917#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */
918
919
Eric Andersenf86aee72000-07-06 17:20:46 +0000920#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000921
Eric Andersenbd22ed82000-07-08 18:55:24 +0000922/* From <linux/kd.h> */
923#define KDGKBTYPE 0x4B33 /* get keyboard type */
924#define KB_84 0x01
925#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000926
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000928{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 char arg;
930
931 arg = 0;
932 return (ioctl(fd, KDGKBTYPE, &arg) == 0
933 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000934}
935
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000937{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 int fd;
939
940 /* try read-only */
941 fd = open(fnam, O_RDWR);
942
943 /* if failed, try read-only */
944 if (fd < 0 && errno == EACCES)
945 fd = open(fnam, O_RDONLY);
946
947 /* if failed, try write-only */
948 if (fd < 0 && errno == EACCES)
949 fd = open(fnam, O_WRONLY);
950
951 /* if failed, fail */
952 if (fd < 0)
953 return -1;
954
955 /* if not a console, fail */
956 if (!is_a_console(fd)) {
957 close(fd);
958 return -1;
959 }
960
961 /* success */
962 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000963}
964
965/*
966 * Get an fd for use with kbd/console ioctls.
967 * We try several things because opening /dev/console will fail
968 * if someone else used X (which does a chown on /dev/console).
969 *
970 * if tty_name is non-NULL, try this one instead.
971 */
972
Erik Andersene49d5ec2000-02-08 19:58:47 +0000973int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000974{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000976
Erik Andersene49d5ec2000-02-08 19:58:47 +0000977 if (tty_name) {
978 if (-1 == (fd = open_a_console(tty_name)))
979 return -1;
980 else
981 return fd;
982 }
983
984 fd = open_a_console("/dev/tty");
985 if (fd >= 0)
986 return fd;
987
988 fd = open_a_console("/dev/tty0");
989 if (fd >= 0)
990 return fd;
991
992 fd = open_a_console("/dev/console");
993 if (fd >= 0)
994 return fd;
995
996 for (fd = 0; fd < 3; fd++)
997 if (is_a_console(fd))
998 return fd;
999
1000 fprintf(stderr,
1001 "Couldnt get a file descriptor referring to the console\n");
1002 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001003}
1004
1005
Eric Andersenf86aee72000-07-06 17:20:46 +00001006#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001007
1008
Erik Andersen23dea9b2000-05-13 00:28:25 +00001009#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
1010
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001011/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001013{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001014 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001015
Erik Andersene49d5ec2000-02-08 19:58:47 +00001016 while (*haystack) {
1017 if (!strncasecmp(haystack, needle, len))
1018 break;
1019 haystack++;
1020 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001021
Erik Andersene49d5ec2000-02-08 19:58:47 +00001022 if (!(*haystack))
1023 haystack = NULL;
1024
1025 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001026}
1027
Eric Andersenc1525e81999-10-29 00:07:31 +00001028/* This tries to find a needle in a haystack, but does so by
1029 * only trying to match literal strings (look 'ma, no regexps!)
1030 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001031 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +00001032 * -Erik Andersen
1033 */
1034extern int find_match(char *haystack, char *needle, int ignoreCase)
1035{
1036
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 if (ignoreCase == FALSE)
1038 haystack = strstr(haystack, needle);
1039 else
1040 haystack = stristr(haystack, needle);
1041 if (haystack == NULL)
1042 return FALSE;
1043 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001044}
1045
1046
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001047/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001048extern int replace_match(char *haystack, char *needle, char *newNeedle,
1049 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +00001050{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051 int foundOne = 0;
1052 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +00001053
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054 if (ignoreCase == FALSE)
1055 where = strstr(haystack, needle);
1056 else
1057 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001058
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 if (strcmp(needle, newNeedle) == 0)
1060 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001061
Erik Andersen0d068a22000-03-21 22:32:57 +00001062 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001063 while (where != NULL) {
1064 foundOne++;
1065 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001066 for (slider = haystack, slider1 = oldhayStack; slider != where;
1067 slider++, slider1++);
1068 *slider = 0;
1069 haystack = strcat(haystack, newNeedle);
1070 slider1 += strlen(needle);
1071 haystack = strcat(haystack, slider1);
1072 where = strstr(slider, needle);
1073 }
1074 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001075
Erik Andersene49d5ec2000-02-08 19:58:47 +00001076 if (foundOne > 0)
1077 return TRUE;
1078 else
1079 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001080}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001081
1082#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001083
1084
Eric Andersen9f16d612000-06-12 23:11:16 +00001085#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001086/*
1087 * Routine to see if a text string is matched by a wildcard pattern.
1088 * Returns TRUE if the text is matched, or FALSE if it is not matched
1089 * or if the pattern is invalid.
1090 * * matches zero or more characters
1091 * ? matches a single character
1092 * [abc] matches 'a', 'b' or 'c'
1093 * \c quotes character c
1094 * Adapted from code written by Ingo Wilken, and
1095 * then taken from sash, Copyright (c) 1999 by David I. Bell
1096 * Permission is granted to use, distribute, or modify this source,
1097 * provided that this copyright notice remains intact.
1098 * Permission to distribute this code under the GPL has been granted.
1099 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001100extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001101{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 const char *retryPat;
1103 const char *retryText;
1104 int ch;
1105 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001106 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001107
Erik Andersene49d5ec2000-02-08 19:58:47 +00001108 retryPat = NULL;
1109 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001110
Erik Andersene49d5ec2000-02-08 19:58:47 +00001111 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001112 ch = *pattern++;
1113
Erik Andersene49d5ec2000-02-08 19:58:47 +00001114 switch (ch) {
1115 case '*':
1116 retryPat = pattern;
1117 retryText = text;
1118 break;
1119
1120 case '[':
1121 found = FALSE;
1122
1123 while ((ch = *pattern++) != ']') {
1124 if (ch == '\\')
1125 ch = *pattern++;
1126
1127 if (ch == '\0')
1128 return FALSE;
1129
1130 if (*text == ch)
1131 found = TRUE;
1132 }
Erik Andersen632bb572000-04-18 22:09:06 +00001133 len=strlen(text);
1134 if (found == FALSE && len!=0) {
1135 return FALSE;
1136 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001137 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001138 if (strlen(pattern)==0 && len==1) {
1139 return TRUE;
1140 }
1141 if (len!=0) {
1142 text++;
1143 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001144 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 }
1146
1147 /* fall into next case */
1148
1149 case '?':
1150 if (*text++ == '\0')
1151 return FALSE;
1152
1153 break;
1154
1155 case '\\':
1156 ch = *pattern++;
1157
1158 if (ch == '\0')
1159 return FALSE;
1160
1161 /* fall into next case */
1162
1163 default:
1164 if (*text == ch) {
1165 if (*text)
1166 text++;
1167 break;
1168 }
1169
1170 if (*text) {
1171 pattern = retryPat;
1172 text = ++retryText;
1173 break;
1174 }
1175
Eric Andersenb186d981999-12-03 09:19:54 +00001176 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001177 }
1178
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 if (pattern == NULL)
1180 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001181 }
1182
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001184}
Eric Andersen9f16d612000-06-12 23:11:16 +00001185#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001186
1187
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
1189
Erik Andersen23dea9b2000-05-13 00:28:25 +00001190#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001191/*
1192 * Given a block device, find the mount table entry if that block device
1193 * is mounted.
1194 *
1195 * Given any other file (or directory), find the mount table entry for its
1196 * filesystem.
1197 */
1198extern struct mntent *findMountPoint(const char *name, const char *table)
1199{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001200 struct stat s;
1201 dev_t mountDevice;
1202 FILE *mountTable;
1203 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001204
Erik Andersene49d5ec2000-02-08 19:58:47 +00001205 if (stat(name, &s) != 0)
1206 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001207
Erik Andersene49d5ec2000-02-08 19:58:47 +00001208 if ((s.st_mode & S_IFMT) == S_IFBLK)
1209 mountDevice = s.st_rdev;
1210 else
1211 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001212
1213
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 if ((mountTable = setmntent(table, "r")) == 0)
1215 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001216
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 while ((mountEntry = getmntent(mountTable)) != 0) {
1218 if (strcmp(name, mountEntry->mnt_dir) == 0
1219 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1220 break;
1221 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1222 break;
1223 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1224 break;
1225 }
1226 endmntent(mountTable);
1227 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001228}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001229#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001230
1231
1232
Erik Andersen23dea9b2000-05-13 00:28:25 +00001233#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001234/*
1235 * Read a number with a possible multiplier.
1236 * Returns -1 if the number format is illegal.
1237 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001239{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001240 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001241
Erik Andersene49d5ec2000-02-08 19:58:47 +00001242 if (!isDecimal(*cp))
1243 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001244
Erik Andersene49d5ec2000-02-08 19:58:47 +00001245 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001246
Erik Andersene49d5ec2000-02-08 19:58:47 +00001247 while (isDecimal(*cp))
1248 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001249
Erik Andersene49d5ec2000-02-08 19:58:47 +00001250 switch (*cp++) {
1251 case 'M':
1252 case 'm': /* `tail' uses it traditionally */
1253 value *= 1048576;
1254 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001255
Erik Andersene49d5ec2000-02-08 19:58:47 +00001256 case 'k':
1257 value *= 1024;
1258 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001259
Erik Andersene49d5ec2000-02-08 19:58:47 +00001260 case 'b':
1261 value *= 512;
1262 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001263
Erik Andersene49d5ec2000-02-08 19:58:47 +00001264 case 'w':
1265 value *= 2;
1266 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001267
Erik Andersene49d5ec2000-02-08 19:58:47 +00001268 case '\0':
1269 return value;
1270
1271 default:
1272 return -1;
1273 }
1274
1275 if (*cp)
1276 return -1;
1277
Eric Andersen1792f8c1999-12-09 06:11:36 +00001278 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001279}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001280#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001281
Eric Andersen6805d5d1999-12-09 22:39:55 +00001282
Erik Andersen23dea9b2000-05-13 00:28:25 +00001283#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001284/* try to open up the specified device */
1285extern int device_open(char *device, int mode)
1286{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001287 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001288
Erik Andersene49d5ec2000-02-08 19:58:47 +00001289 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001290
Erik Andersene49d5ec2000-02-08 19:58:47 +00001291 /* Retry up to 5 times */
1292 for (f = 0; f < 5; f++)
1293 if ((fd = open(device, m, 0600)) >= 0)
1294 break;
1295 if (fd < 0)
1296 return fd;
1297 /* Reset original flags. */
1298 if (m != mode)
1299 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001300 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001301}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001302#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001303
1304
Erik Andersen23dea9b2000-05-13 00:28:25 +00001305#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001306#ifdef BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001307#include <linux/devps.h>
Erik Andersen23dea9b2000-05-13 00:28:25 +00001308#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309
Erik Andersen23dea9b2000-05-13 00:28:25 +00001310#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001311/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001312 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001313 * This finds the pid of the specified process,
1314 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001315 *
Erik Andersen825aead2000-04-07 06:00:07 +00001316 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001317 */
Erik Andersen825aead2000-04-07 06:00:07 +00001318extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001319{
Erik Andersen825aead2000-04-07 06:00:07 +00001320 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001321 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001322 pid_t num_pids;
1323 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001324 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001325
1326 /* open device */
1327 fd = open(device, O_RDONLY);
1328 if (fd < 0)
1329 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1330
1331 /* Find out how many processes there are */
1332 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1333 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1334
1335 /* Allocate some memory -- grab a few extras just in case
1336 * some new processes start up while we wait. The kernel will
1337 * just ignore any extras if we give it too many, and will trunc.
1338 * the list if we give it too few. */
1339 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1340 pid_array[0] = num_pids+10;
1341
1342 /* Now grab the pid list */
1343 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1344 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1345
1346 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001347 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001348 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001349 struct pid_info info;
1350
1351 info.pid = pid_array[i];
1352 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1353 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1354
Erik Andersen0a64de92000-04-18 20:00:03 +00001355 /* Make sure we only match on the process name */
1356 p=info.command_line+1;
1357 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1358 (p)++;
1359 }
1360 if (isspace(*(p)))
1361 *p='\0';
1362
Erik Andersenb9167cb2000-04-18 22:41:30 +00001363 if ((strstr(info.command_line, pidName) != NULL)
1364 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001365 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1366 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001367 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001368 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001369 }
1370 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001371 if (pidList)
1372 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001373
1374 /* Free memory */
1375 free( pid_array);
1376
1377 /* close device */
1378 if (close (fd) != 0)
1379 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1380
Erik Andersen825aead2000-04-07 06:00:07 +00001381 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001382}
Erik Andersen269bba22000-03-08 14:50:47 +00001383#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001384#if ! defined BB_FEATURE_USE_PROCFS
1385#error Sorry, I depend on the /proc filesystem right now.
1386#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001387
Erik Andersen246cc6d2000-03-07 07:41:42 +00001388/* findPidByName()
1389 *
1390 * This finds the pid of the specified process.
1391 * Currently, it's implemented by rummaging through
1392 * the proc filesystem.
1393 *
Erik Andersen825aead2000-04-07 06:00:07 +00001394 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001395 */
Erik Andersen825aead2000-04-07 06:00:07 +00001396extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001397{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001398 DIR *dir;
1399 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001400 pid_t* pidList=NULL;
1401 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001402
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001403 dir = opendir("/proc");
1404 if (!dir)
1405 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1406
1407 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001408 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001409 char filename[256];
1410 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001411 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001412
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001413 /* If it isn't a number, we don't want it */
1414 if (!isdigit(*next->d_name))
1415 continue;
1416
Erik Andersen825aead2000-04-07 06:00:07 +00001417 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001418 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001419 status = fopen(filename, "r");
1420 if (!status) {
1421 continue;
1422 }
1423 fgets(buffer, 256, status);
1424 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001425
Erik Andersen0a64de92000-04-18 20:00:03 +00001426 /* Make sure we only match on the process name */
1427 p=buffer+5; /* Skip the name */
1428 while ((p)++) {
1429 if (*p==0 || *p=='\n') {
1430 *p='\0';
1431 break;
1432 }
1433 }
1434 p=buffer+6; /* Skip the "Name:\t" */
1435
Erik Andersenb9167cb2000-04-18 22:41:30 +00001436 if ((strstr(p, pidName) != NULL)
1437 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001438 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1439 if (pidList==NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001440 fatalError(memory_exhausted, "");
Erik Andersen825aead2000-04-07 06:00:07 +00001441 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001442 }
John Beppuf95ca971999-12-09 22:10:18 +00001443 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001444 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001445 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001446 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001447}
Erik Andersen269bba22000-03-08 14:50:47 +00001448#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001449#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001450
Erik Andersen0d068a22000-03-21 22:32:57 +00001451/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001452extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001453{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 void *cp = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001455
Erik Andersen0d068a22000-03-21 22:32:57 +00001456 if (cp == NULL)
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00001457 fatalError(memory_exhausted, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001458 return cp;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001459}
1460
Erik Andersen23dea9b2000-05-13 00:28:25 +00001461#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001462extern int vdprintf(int d, const char *format, va_list ap)
1463{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001464 char buf[BUF_SIZE];
1465 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001466
Erik Andersene49d5ec2000-02-08 19:58:47 +00001467 len = vsprintf(buf, format, ap);
1468 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001469}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001470#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001471
Erik Andersen23dea9b2000-05-13 00:28:25 +00001472#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen5cbdd712000-01-26 20:06:48 +00001473extern int del_loop(const char *device)
1474{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001475 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001476
Erik Andersene49d5ec2000-02-08 19:58:47 +00001477 if ((fd = open(device, O_RDONLY)) < 0) {
1478 perror(device);
1479 return (FALSE);
1480 }
1481 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1482 perror("ioctl: LOOP_CLR_FD");
1483 return (FALSE);
1484 }
1485 close(fd);
1486 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001487}
Erik Andersende7965c2000-01-26 23:49:21 +00001488
Erik Andersene49d5ec2000-02-08 19:58:47 +00001489extern int set_loop(const char *device, const char *file, int offset,
1490 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001491{
1492 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001493 int fd, ffd, mode;
1494
Erik Andersende7965c2000-01-26 23:49:21 +00001495 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001496 if ((ffd = open(file, mode)) < 0 && !*loopro
1497 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1498 perror(file);
1499 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001500 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001501 if ((fd = open(device, mode)) < 0) {
1502 close(ffd);
1503 perror(device);
1504 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001505 }
1506 *loopro = (mode == O_RDONLY);
1507
1508 memset(&loopinfo, 0, sizeof(loopinfo));
1509 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001510 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001511
1512 loopinfo.lo_offset = offset;
1513
1514 loopinfo.lo_encrypt_key_size = 0;
1515 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1516 perror("ioctl: LOOP_SET_FD");
1517 close(fd);
1518 close(ffd);
1519 return 1;
1520 }
1521 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1522 (void) ioctl(fd, LOOP_CLR_FD, 0);
1523 perror("ioctl: LOOP_SET_STATUS");
1524 close(fd);
1525 close(ffd);
1526 return 1;
1527 }
1528 close(fd);
1529 close(ffd);
1530 return 0;
1531}
1532
Erik Andersene49d5ec2000-02-08 19:58:47 +00001533extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001534{
1535 char dev[20];
1536 int i, fd;
1537 struct stat statbuf;
1538 struct loop_info loopinfo;
1539
Erik Andersene49d5ec2000-02-08 19:58:47 +00001540 for (i = 0; i <= 7; i++) {
1541 sprintf(dev, "/dev/loop%d", i);
1542 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1543 if ((fd = open(dev, O_RDONLY)) >= 0) {
1544 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1545 if (errno == ENXIO) { /* probably free */
1546 close(fd);
1547 return strdup(dev);
1548 }
1549 }
1550 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001551 }
Erik Andersende7965c2000-01-26 23:49:21 +00001552 }
Erik Andersende7965c2000-01-26 23:49:21 +00001553 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001554 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001555}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001556#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001557
Erik Andersen23dea9b2000-05-13 00:28:25 +00001558#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001559extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001560{
Erik Andersen016ffe92000-03-22 00:58:54 +00001561 DIR *dir;
1562 struct dirent *entry;
1563 struct stat statBuf, rootStat;
1564 char fileName[BUFSIZ];
1565
Erik Andersenec5bd902000-03-22 07:12:05 +00001566 if (stat("/", &rootStat) != 0) {
1567 errorMsg("could not stat '/'\n");
1568 return( FALSE);
1569 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001570
Erik Andersenec5bd902000-03-22 07:12:05 +00001571 dir = opendir("/dev");
1572 if (!dir) {
1573 errorMsg("could not open '/dev'\n");
1574 return( FALSE);
1575 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001576
1577 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001578
Erik Andersen016ffe92000-03-22 00:58:54 +00001579 /* Must skip ".." since that is "/", and so we
1580 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001581 if (strcmp(entry->d_name, "..") == 0)
1582 continue;
1583
1584 sprintf( fileName, "/dev/%s", entry->d_name);
1585
1586 if (stat(fileName, &statBuf) != 0)
1587 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001588 /* Some char devices have the same dev_t as block
1589 * devices, so make sure this is a block device */
1590 if (! S_ISBLK(statBuf.st_mode))
1591 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001592 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001593 strcpy(name, fileName);
1594 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001595 }
1596 }
1597
Erik Andersenec5bd902000-03-22 07:12:05 +00001598 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001599}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001600#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001601
Mark Whitley1ca41772000-06-28 22:15:26 +00001602static const int GROWBY = 80; /* how large we will grow strings by */
1603
1604/* get_line_from_file() - This function reads an entire line from a text file
1605 * up to a newline. It returns a malloc'ed char * which must be stored and
1606 * free'ed by the caller. */
1607extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001608{
Mark Whitley1ca41772000-06-28 22:15:26 +00001609 int ch;
1610 int idx = 0;
1611 char *linebuf = NULL;
1612 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001613
Mark Whitley1ca41772000-06-28 22:15:26 +00001614 while (1) {
1615 ch = fgetc(file);
1616 if (ch == EOF)
1617 break;
1618 /* grow the line buffer as necessary */
1619 if (idx > linebufsz-1)
1620 linebuf = realloc(linebuf, linebufsz += GROWBY);
1621 linebuf[idx++] = (char)ch;
1622 if ((char)ch == '\n')
1623 break;
1624 }
John Beppu5a728cf2000-04-17 04:22:09 +00001625
Mark Whitley1ca41772000-06-28 22:15:26 +00001626 if (idx == 0)
1627 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001628
Mark Whitley1ca41772000-06-28 22:15:26 +00001629 linebuf[idx] = 0;
1630 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001631}
Erik Andersen016ffe92000-03-22 00:58:54 +00001632
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001633#if defined BB_ECHO || defined BB_TR
1634char process_escape_sequence(char **ptr)
1635{
1636 char c;
1637
1638 switch (c = *(*ptr)++) {
1639 case 'a':
1640 c = '\a';
1641 break;
1642 case 'b':
1643 c = '\b';
1644 break;
1645 case 'f':
1646 c = '\f';
1647 break;
1648 case 'n':
1649 c = '\n';
1650 break;
1651 case 't':
1652 c = '\t';
1653 break;
1654 case 'v':
1655 c = '\v';
1656 break;
1657 case '\\':
1658 c = '\\';
1659 break;
1660 case '0': case '1': case '2': case '3':
1661 case '4': case '5': case '6': case '7':
1662 c -= '0';
1663 if ('0' <= **ptr && **ptr <= '7') {
1664 c = c * 8 + (*(*ptr)++ - '0');
1665 if ('0' <= **ptr && **ptr <= '7')
1666 c = c * 8 + (*(*ptr)++ - '0');
1667 }
1668 break;
1669 default:
1670 (*ptr)--;
1671 c = '\\';
1672 break;
1673 }
1674 return c;
1675}
1676#endif
1677
Eric Andersencc8ed391999-10-05 16:24:54 +00001678/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001679/*
1680Local Variables:
1681c-file-style: "linux"
1682c-basic-offset: 4
1683tab-width: 4
1684End:
1685*/