blob: e3535966f367cbcdec3c3fa525506ccf5b0b2d38 [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 Andersencbe31da2001-02-20 06:14:08 +000028#include <stdio.h>
29#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <dirent.h>
33#include <time.h>
34#include <utime.h>
35#include <unistd.h>
36#include <ctype.h>
37#include <stdlib.h>
38#include <limits.h>
David Kimdon68c1d842001-02-23 03:55:24 +000039#include <stdarg.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000040#include <sys/ioctl.h>
41#include <sys/utsname.h> /* for uname(2) */
42
Eric Andersen3570a342000-09-25 21:45:58 +000043#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000044#if defined (BB_CHMOD_CHOWN_CHGRP) \
45 || defined (BB_CP_MV) \
46 || defined (BB_FIND) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000047 || defined (BB_INSMOD) \
Erik Andersenfac10d72000-02-07 05:29:42 +000048 || defined (BB_LS) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000049 || defined (BB_RM) \
50 || defined (BB_TAR)
Mark Whitleyf57c9442000-12-07 19:56:48 +000051/* same conditions as recursive_action */
Erik Andersenfac10d72000-02-07 05:29:42 +000052#define bb_need_name_too_long
53#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000054#define bb_need_memory_exhausted
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000055#define bb_need_full_version
Erik Andersenfac10d72000-02-07 05:29:42 +000056#define BB_DECLARE_EXTERN
57#include "messages.c"
58
Eric Andersenab050f52001-01-27 06:01:43 +000059#include "pwd_grp/pwd.h"
60#include "pwd_grp/grp.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Eric Andersened3ef502001-01-27 08:24:39 +000062/* for the _syscall() macros */
63#include <sys/syscall.h>
64#include <linux/unistd.h>
65
Erik Andersen246cc6d2000-03-07 07:41:42 +000066/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
67 * list of available filesystems used for the -t auto option */
Eric Andersend0246fb1999-11-04 21:18:07 +000068#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000069# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000071# else
Erik Andersen269bba22000-03-08 14:50:47 +000072# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000073const char mtab_file[] = "/dev/mtab";
74# else
Eric Andersenaf4ac772001-02-01 22:43:49 +000075const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000076# endif
77# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000078#endif
79
Eric Andersen67991cf2001-02-14 21:23:06 +000080static struct BB_applet *applet_using;
81
82extern void show_usage(void)
Eric Andersenb0e9a701999-10-18 22:28:26 +000083{
Eric Andersen7e9276b2001-02-22 01:15:47 +000084 const char *format_string;
85 const char *usage_string = usage_messages;
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000086 int i;
Eric Andersen67991cf2001-02-14 21:23:06 +000087
Eric Andersen7e9276b2001-02-22 01:15:47 +000088 for (i = applet_using - applets; i > 0; ) {
89 if (!*usage_string++) {
90 --i;
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000091 }
Eric Andersen67991cf2001-02-14 21:23:06 +000092 }
Eric Andersen7e9276b2001-02-22 01:15:47 +000093 format_string = "%s\n\nUsage: %s %s\n\n";
94 if(*usage_string == 0)
95 format_string = "%s\n\nNo help available.\n\n";
96 fprintf(stderr, format_string,
97 full_version, applet_using->name, usage_string);
Matt Kraaie0bcce02000-09-27 02:29:39 +000098 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000099}
100
Eric Andersen67991cf2001-02-14 21:23:06 +0000101
Mark Whitleyf57c9442000-12-07 19:56:48 +0000102static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000103{
104 fflush(stdout);
105 fprintf(stderr, "%s: ", applet_name);
106 vfprintf(stderr, s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000107}
108
Mark Whitleyf57c9442000-12-07 19:56:48 +0000109extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000110{
111 va_list p;
112
113 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000114 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000115 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000116 putc('\n', stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000117}
118
Mark Whitleyf57c9442000-12-07 19:56:48 +0000119extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000120{
121 va_list p;
122
123 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000124 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000125 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000126 putc('\n', stderr);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000127 exit(EXIT_FAILURE);
128}
129
Mark Whitleyf57c9442000-12-07 19:56:48 +0000130static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000131{
Eric Andersenc73a7f92001-01-20 21:41:11 +0000132 int err=errno;
133 if(s == 0) s = "";
134 verror_msg(s, p);
135 if (*s) s = ": ";
136 fprintf(stderr, "%s%s\n", s, strerror(err));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000137}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000138
Mark Whitleyf57c9442000-12-07 19:56:48 +0000139extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000140{
141 va_list p;
142
143 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000144 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000145 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000146}
147
Mark Whitleyf57c9442000-12-07 19:56:48 +0000148extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000149{
150 va_list p;
151
152 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000153 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000154 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000155 exit(EXIT_FAILURE);
156}
157
Eric Andersenf5d5e772001-01-24 23:34:48 +0000158#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000159/* Returns kernel version encoded as major*65536 + minor*256 + patch,
160 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000161 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000162 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000163extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000164{
Erik Andersend07ee462000-02-21 21:26:32 +0000165 struct utsname name;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000166 char *s;
167 int i, r;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000168
Erik Andersend07ee462000-02-21 21:26:32 +0000169 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000170 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 return (0);
172 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000173
174 s = name.release;
175 r = 0;
176 for (i=0 ; i<3 ; i++) {
177 r = r * 256 + atoi(strtok(s, "."));
178 s = NULL;
179 }
180 return r;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000181}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000182#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000183
Eric Andersen10dc9d42000-06-26 10:45:52 +0000184
185
186#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000187_syscall1(int, sysinfo, struct sysinfo *, info);
188#endif /* BB_INIT */
189
Eric Andersena57ba4d2000-07-08 19:20:49 +0000190#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000191
192#ifndef __NR_umount2
Mark Whitley59ab0252001-01-23 22:30:04 +0000193static const int __NR_umount2 = 52;
Eric Andersena57ba4d2000-07-08 19:20:49 +0000194#endif
195
196/* Include our own version of <sys/mount.h>, since libc5 doesn't
197 * know about umount2 */
198extern _syscall1(int, umount, const char *, special_file);
199extern _syscall2(int, umount2, const char *, special_file, int, flags);
200extern _syscall5(int, mount, const char *, special_file, const char *, dir,
201 const char *, fstype, unsigned long int, rwflag, const void *, data);
202#endif
203
Eric Andersen8079b152001-02-23 02:04:33 +0000204#if defined BB_FEATURE_NEW_MODULE_INTERFACE && ( defined BB_INSMOD || defined BB_LSMOD )
Eric Andersenc86e5e62000-08-21 19:38:49 +0000205#ifndef __NR_query_module
Mark Whitley59ab0252001-01-23 22:30:04 +0000206static const int __NR_query_module = 167;
Eric Andersenc86e5e62000-08-21 19:38:49 +0000207#endif
208_syscall5(int, query_module, const char *, name, int, which,
209 void *, buf, size_t, bufsize, size_t*, ret);
210#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000211
212
Erik Andersen23dea9b2000-05-13 00:28:25 +0000213#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000214
Erik Andersen029011b2000-03-04 21:19:32 +0000215#define HASH_SIZE 311 /* Should be prime */
216#define hash_inode(i) ((i) % HASH_SIZE)
217
218static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
219
220/*
221 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
222 * `ino_dev_hashtable', else return 0
223 *
224 * If NAME is a non-NULL pointer to a character pointer, and there is
225 * a match, then set *NAME to the value of the name slot in that
226 * bucket.
227 */
228int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
229{
230 ino_dev_hashtable_bucket_t *bucket;
231
232 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
233 while (bucket != NULL) {
234 if ((bucket->ino == statbuf->st_ino) &&
235 (bucket->dev == statbuf->st_dev))
236 {
237 if (name) *name = bucket->name;
238 return 1;
239 }
240 bucket = bucket->next;
241 }
242 return 0;
243}
244
245/* Add statbuf to statbuf hash table */
246void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
247{
248 int i;
249 size_t s;
250 ino_dev_hashtable_bucket_t *bucket;
251
252 i = hash_inode(statbuf->st_ino);
253 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000254 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000255 bucket->ino = statbuf->st_ino;
256 bucket->dev = statbuf->st_dev;
257 if (name)
258 strcpy(bucket->name, name);
259 else
260 bucket->name[0] = '\0';
261 bucket->next = ino_dev_hashtable[i];
262 ino_dev_hashtable[i] = bucket;
263}
264
265/* Clear statbuf hash table */
266void reset_ino_dev_hashtable(void)
267{
268 int i;
269 ino_dev_hashtable_bucket_t *bucket;
270
271 for (i = 0; i < HASH_SIZE; i++) {
272 while (ino_dev_hashtable[i] != NULL) {
273 bucket = ino_dev_hashtable[i]->next;
274 free(ino_dev_hashtable[i]);
275 ino_dev_hashtable[i] = bucket;
276 }
277 }
278}
279
Erik Andersen23dea9b2000-05-13 00:28:25 +0000280#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000281
Glenn L McGrath5e23e3d2001-01-03 01:38:29 +0000282#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000283/*
284 * Return TRUE if a fileName is a directory.
285 * Nonexistant files return FALSE.
286 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000287int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000288{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000290 int didMalloc = 0;
291
292 if (statBuf == NULL) {
293 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
294 ++didMalloc;
295 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000296
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000298 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000300 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000301
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000302 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
303 status = FALSE;
304 }
305 else status = TRUE;
306
307 if (didMalloc) {
308 free(statBuf);
309 statBuf = NULL;
310 }
311 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000312}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000313#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000314
Eric Andersen70c60862001-01-25 00:00:04 +0000315#if defined BB_AR || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000316/*
Mark Whitley47583682000-12-05 20:03:17 +0000317 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000318 */
Mark Whitley47583682000-12-05 20:03:17 +0000319int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000320{
321 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000322 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000323
Mark Whitley47583682000-12-05 20:03:17 +0000324 while (chunksize > 0) {
325 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000326 size = BUFSIZ;
327 else
Mark Whitley47583682000-12-05 20:03:17 +0000328 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000329 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000330 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000331 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000332 }
333 return (TRUE);
334}
335#endif
336
337
Glenn L McGrathde556612001-02-10 14:53:44 +0000338#if defined (BB_CP_MV) || defined BB_DPKG
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000339/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000340 * Copy one file to another, while possibly preserving its modes, times, and
341 * modes. Returns TRUE if successful, or FALSE on a failure with an error
342 * message output. (Failure is not indicated if attributes cannot be set.)
343 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000344 */
345int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000346copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000347 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000348{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 int rfd;
350 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 struct stat srcStatBuf;
353 struct stat dstStatBuf;
354 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000355
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 if (followLinks == TRUE)
357 status = stat(srcName, &srcStatBuf);
358 else
359 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000360
Erik Andersenfac10d72000-02-07 05:29:42 +0000361 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000362 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000364 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365
366 if (followLinks == TRUE)
367 status = stat(destName, &dstStatBuf);
368 else
369 status = lstat(destName, &dstStatBuf);
370
Erik Andersen59b9e872000-05-10 05:05:45 +0000371 if (status < 0 || forceFlag==TRUE) {
372 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 dstStatBuf.st_ino = -1;
374 dstStatBuf.st_dev = -1;
375 }
376
377 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
378 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000379 error_msg("Copying file \"%s\" to itself", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 return FALSE;
381 }
382
383 if (S_ISDIR(srcStatBuf.st_mode)) {
384 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
385 /* Make sure the directory is writable */
386 status = mkdir(destName, 0777777 ^ umask(0));
387 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000388 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 return FALSE;
390 }
391 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000392 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000393 int link_size;
394
395 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000396 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
397 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 if (link_size < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000399 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 return FALSE;
401 }
402 link_val[link_size] = '\0';
403 status = symlink(link_val, destName);
404 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000405 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 return FALSE;
407 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000408#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000410 /* Try to set owner, but fail silently like GNU cp */
411 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000412 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000413#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000414 return TRUE;
415 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
416 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
417 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000418 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 return FALSE;
420 }
421 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
422 || S_ISSOCK(srcStatBuf.st_mode)) {
423 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
424 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000425 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000426 return FALSE;
427 }
428 } else if (S_ISREG(srcStatBuf.st_mode)) {
429 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
430 rfd = open(srcName, O_RDONLY);
431 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000432 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 return FALSE;
434 }
435
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000436 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 srcStatBuf.st_mode);
438 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000439 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 close(rfd);
441 return FALSE;
442 }
443
Mark Whitley47583682000-12-05 20:03:17 +0000444 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000445 goto error_exit;
446
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 close(rfd);
448 if (close(wfd) < 0) {
449 return FALSE;
450 }
451 }
452
453 if (setModes == TRUE) {
454 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000455 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
456 perror_msg_and_die("%s", destName);
457 if (chmod(destName, srcStatBuf.st_mode) < 0)
458 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 times.actime = srcStatBuf.st_atime;
460 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000461 if (utime(destName, &times) < 0)
462 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 }
464
Erik Andersenfac10d72000-02-07 05:29:42 +0000465 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000466
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000468 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000469 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000471
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000473}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000474#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000475
476
477
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000478#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000479
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000480#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
481#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
482
483/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
484static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 0, 0, S_ISUID,
486 0, 0, S_ISGID,
487 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000488};
489
490/* The 9 mode bits to test */
491static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 S_IRUSR, S_IWUSR, S_IXUSR,
493 S_IRGRP, S_IWGRP, S_IXGRP,
494 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000495};
496
Mark Whitley26a3adb2000-12-05 20:10:27 +0000497static const char MODE1[] = "rwxrwxrwx";
498static const char MODE0[] = "---------";
499static const char SMODE1[] = "..s..s..t";
500static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000501
Eric Andersencc8ed391999-10-05 16:24:54 +0000502/*
503 * Return the standard ls-like mode string from a file mode.
504 * This is static and so is overwritten on each call.
505 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000506const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000507{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000509
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 int i;
511
512 buf[0] = TYPECHAR(mode);
513 for (i = 0; i < 9; i++) {
514 if (mode & SBIT[i])
515 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
516 else
517 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
518 }
519 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000520}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000521#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000522
523
Pavel Roskin21ddb382000-07-17 19:31:54 +0000524#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000525/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000526 * Return the standard ls-like time string from a time_t
527 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000528 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000529const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000530{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 time_t now;
532 char *str;
533 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000534
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000536
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000538
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 strcpy(buf, &str[4]);
540 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000541
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
543 strcpy(&buf[7], &str[20]);
544 buf[11] = '\0';
545 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000546
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000548}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000549#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000550
Eric Andersen70c60862001-01-25 00:00:04 +0000551#if defined BB_DD || defined BB_NC || defined BB_TAIL || defined BB_TAR || defined BB_AR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000552/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000553 * Write all of the supplied buffer out to a file.
554 * This does multiple writes as necessary.
555 * Returns the amount written, or -1 on an error.
556 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000557int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000558{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559 int cc;
560 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000561
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000563
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564 while (len > 0) {
565 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000566
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 if (cc < 0)
568 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000569
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570 buf += cc;
571 total += cc;
572 len -= cc;
573 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000574
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000576}
Matt Kraai24ac0172000-12-18 21:38:57 +0000577#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000578
Matt Kraai24ac0172000-12-18 21:38:57 +0000579#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000580/*
581 * Read all of the supplied buffer from a file.
582 * This does multiple reads as necessary.
583 * Returns the amount read, or -1 on an error.
584 * A short read is returned on an end of file.
585 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000586int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000587{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588 int cc;
589 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000590
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000592
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593 while (len > 0) {
594 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000595
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 if (cc < 0)
597 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000598
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 if (cc == 0)
600 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000601
Erik Andersene49d5ec2000-02-08 19:58:47 +0000602 buf += cc;
603 total += cc;
604 len -= cc;
605 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000606
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000608}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000609#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000610
611
Erik Andersen23dea9b2000-05-13 00:28:25 +0000612#if defined (BB_CHMOD_CHOWN_CHGRP) \
613 || defined (BB_CP_MV) \
614 || defined (BB_FIND) \
615 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000616 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000617 || defined (BB_RM) \
618 || defined (BB_TAR)
619
Eric Andersencc8ed391999-10-05 16:24:54 +0000620/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000621 * Walk down all the directories under the specified
622 * location, and do something (something specified
623 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000624 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000625 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000626 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000627 * and so isn't sufficiently portable to take over since glibc2.1
628 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000629 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000630int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 int recurse, int followLinks, int depthFirst,
632 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000633 struct stat * statbuf,
634 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000636 struct stat * statbuf,
637 void* userData),
638 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000639{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640 int status;
641 struct stat statbuf;
642 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000643
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 if (followLinks == TRUE)
645 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000646 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000648
Eric Andersen2b69c401999-10-05 22:58:32 +0000649 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650#ifdef BB_DEBUG_PRINT_SCAFFOLD
651 fprintf(stderr,
652 "status=%d followLinks=%d TRUE=%d\n",
653 status, followLinks, TRUE);
654#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000655 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000656 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000657 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658
659 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
660 if (fileAction == NULL)
661 return TRUE;
662 else
Erik Andersen3364d782000-03-28 00:58:14 +0000663 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 }
665
666 if (recurse == FALSE) {
667 if (S_ISDIR(statbuf.st_mode)) {
668 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000669 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 else
671 return TRUE;
672 }
673 }
674
675 if (S_ISDIR(statbuf.st_mode)) {
676 DIR *dir;
677
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000679 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000681 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000683 } else if (status == SKIP)
684 return TRUE;
685 }
686 dir = opendir(fileName);
687 if (!dir) {
688 perror_msg("%s", fileName);
689 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 }
691 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000692 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693
694 if ((strcmp(next->d_name, "..") == 0)
695 || (strcmp(next->d_name, ".") == 0)) {
696 continue;
697 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000698 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000699 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 return FALSE;
701 }
Erik Andersenc3660502000-04-18 21:18:57 +0000702 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 sprintf(nextFile, "%s/%s", fileName, next->d_name);
704 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000705 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000706 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000707 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 closedir(dir);
709 return FALSE;
710 }
711 }
712 status = closedir(dir);
713 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000714 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 return FALSE;
716 }
717 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000718 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000720 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000721 return FALSE;
722 }
723 }
724 } else {
725 if (fileAction == NULL)
726 return TRUE;
727 else
Erik Andersen3364d782000-03-28 00:58:14 +0000728 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 }
730 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000731}
732
Erik Andersen23dea9b2000-05-13 00:28:25 +0000733#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000734
Eric Andersenf6be9441999-10-13 21:12:06 +0000735
736
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000737#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000738/*
739 * Attempt to create the directories along the specified path, except for
740 * the final component. The mode is given for the final directory only,
741 * while all previous ones get default protections. Errors are not reported
742 * here, as failures to restore files can be reported later.
743 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000744extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000745{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 char *cp;
747 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000748 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000750
Erik Andersene49d5ec2000-02-08 19:58:47 +0000751 strcpy(buf, name);
752 for (cp = buf; *cp == '/'; cp++);
753 cp = strchr(cp, '/');
754 while (cp) {
755 cpOld = cp;
756 cp = strchr(cp + 1, '/');
757 *cpOld = '\0';
758 retVal = mkdir(buf, cp ? 0777 : mode);
759 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000760 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000761 return FALSE;
762 }
763 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000764 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000765 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000766}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000767#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000768
769
770
Pavel Roskin21ddb382000-07-17 19:31:54 +0000771#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000772 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000773/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000774
Erik Andersene49d5ec2000-02-08 19:58:47 +0000775extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000776{
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000777 static const mode_t group_set[] = {
778 S_ISUID | S_IRWXU, /* u */
779 S_ISGID | S_IRWXG, /* g */
780 S_IRWXO, /* o */
781 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
782 };
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000784 static const mode_t mode_set[] = {
785 S_IRUSR | S_IRGRP | S_IROTH, /* r */
786 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
787 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
788 S_ISUID | S_ISGID, /* s */
789 S_ISVTX /* t */
790 };
791
792 static const char group_string[] = "ugoa";
793 static const char mode_string[] = "rwxst";
794
795 const char *p;
796
797 mode_t andMode =
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000799 mode_t orMode = 0;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000800 mode_t mode;
801 mode_t groups;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802 char type;
803 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000804
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000805 if (s==NULL) {
Eric Andersen4e61db22000-09-01 00:06:17 +0000806 return (FALSE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000807 }
Eric Andersen4e61db22000-09-01 00:06:17 +0000808
Eric Andersenf6be9441999-10-13 21:12:06 +0000809 do {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000810 mode = 0;
811 groups = 0;
812 NEXT_GROUP:
813 if ((c = *s++) == '\0') {
814 return -1;
815 }
816 for (p=group_string ; *p ; p++) {
817 if (*p == c) {
818 groups |= group_set[(int)(p-group_string)];
819 goto NEXT_GROUP;
820 }
821 }
822 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000823 case '=':
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000824 case '+':
Eric Andersenf6be9441999-10-13 21:12:06 +0000825 case '-':
826 type = c;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000827 if (groups == 0) { /* The default is "all" */
828 groups |= S_ISUID | S_ISGID | S_ISVTX
829 | S_IRWXU | S_IRWXG | S_IRWXO;
830 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000831 break;
832 default:
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000833 if ((c < '0') || (c > '7') || (mode | groups)) {
834 return (FALSE);
835 } else {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000836 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000837 return (TRUE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000838 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000839 }
840
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000841 NEXT_MODE:
842 if (((c = *s++) != '\0') && (c != ',')) {
843 for (p=mode_string ; *p ; p++) {
844 if (*p == c) {
845 mode |= mode_set[(int)(p-mode_string)];
846 goto NEXT_MODE;
847 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000848 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000849 break; /* We're done so break out of loop.*/
Eric Andersenf6be9441999-10-13 21:12:06 +0000850 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 switch (type) {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000852 case '=':
853 andMode &= ~(groups); /* Now fall through. */
854 case '+':
855 orMode |= mode & groups;
856 break;
857 case '-':
858 andMode &= ~(mode & groups);
859 orMode &= ~(mode & groups);
860 break;
Eric Andersenf6be9441999-10-13 21:12:06 +0000861 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 } while (c == ',');
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000863
Eric Andersence8f3b91999-10-20 07:03:36 +0000864 *theMode &= andMode;
865 *theMode |= orMode;
Eric Andersence8f3b91999-10-20 07:03:36 +0000866
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000867 return TRUE;
868}
Eric Andersence8f3b91999-10-20 07:03:36 +0000869
Pavel Roskin21ddb382000-07-17 19:31:54 +0000870#endif
871/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000872
Eric Andersene674eb71999-10-19 20:52:57 +0000873
874
Erik Andersen23dea9b2000-05-13 00:28:25 +0000875
876
Pavel Roskin21ddb382000-07-17 19:31:54 +0000877#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
878 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000879 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000880/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000881long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000882{
Eric Andersenab050f52001-01-27 06:01:43 +0000883 struct passwd *myuser;
884
885 myuser = getpwnam(name);
886 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000887 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000888
889 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000890}
891
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000892/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000893long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000894{
Eric Andersenab050f52001-01-27 06:01:43 +0000895 struct group *mygroup;
896
897 mygroup = getgrnam(name);
898 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000899 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000900
901 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000902}
903
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000904/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000905void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000906{
Eric Andersenab050f52001-01-27 06:01:43 +0000907 struct passwd *myuser;
908
909 myuser = getpwuid(uid);
910 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000911 sprintf(name, "%-8ld ", (long)uid);
912 else
913 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000914}
915
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000916/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000917void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000918{
Eric Andersenab050f52001-01-27 06:01:43 +0000919 struct group *mygroup;
920
921 mygroup = getgrgid(gid);
922 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000923 sprintf(group, "%-8ld ", (long)gid);
924 else
925 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000926}
927
Matt Kraaie6e81832000-12-30 07:46:23 +0000928#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000929/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000930long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000931{
Eric Andersenab050f52001-01-27 06:01:43 +0000932 struct group *mygroup;
933 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000934
Eric Andersenab050f52001-01-27 06:01:43 +0000935 myuser=getpwnam(name);
936 if (myuser==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000937 error_msg_and_die( "unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000938
939 mygroup = getgrgid(myuser->pw_gid);
940 if (mygroup==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000941 error_msg_and_die( "unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000942
943 return mygroup->gr_gid;
944}
945#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000946#endif
947 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
948 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000949
950
Eric Andersenf86aee72000-07-06 17:20:46 +0000951#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000952
Eric Andersenbd22ed82000-07-08 18:55:24 +0000953/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000954static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
955static const int KB_84 = 0x01;
956static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000957
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000959{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 char arg;
961
962 arg = 0;
963 return (ioctl(fd, KDGKBTYPE, &arg) == 0
964 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000965}
966
Erik Andersene49d5ec2000-02-08 19:58:47 +0000967static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000968{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 int fd;
970
971 /* try read-only */
972 fd = open(fnam, O_RDWR);
973
974 /* if failed, try read-only */
975 if (fd < 0 && errno == EACCES)
976 fd = open(fnam, O_RDONLY);
977
978 /* if failed, try write-only */
979 if (fd < 0 && errno == EACCES)
980 fd = open(fnam, O_WRONLY);
981
982 /* if failed, fail */
983 if (fd < 0)
984 return -1;
985
986 /* if not a console, fail */
987 if (!is_a_console(fd)) {
988 close(fd);
989 return -1;
990 }
991
992 /* success */
993 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000994}
995
996/*
997 * Get an fd for use with kbd/console ioctls.
998 * We try several things because opening /dev/console will fail
999 * if someone else used X (which does a chown on /dev/console).
1000 *
1001 * if tty_name is non-NULL, try this one instead.
1002 */
1003
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001005{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001006 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001007
Erik Andersene49d5ec2000-02-08 19:58:47 +00001008 if (tty_name) {
1009 if (-1 == (fd = open_a_console(tty_name)))
1010 return -1;
1011 else
1012 return fd;
1013 }
1014
1015 fd = open_a_console("/dev/tty");
1016 if (fd >= 0)
1017 return fd;
1018
1019 fd = open_a_console("/dev/tty0");
1020 if (fd >= 0)
1021 return fd;
1022
1023 fd = open_a_console("/dev/console");
1024 if (fd >= 0)
1025 return fd;
1026
1027 for (fd = 0; fd < 3; fd++)
1028 if (is_a_console(fd))
1029 return fd;
1030
Matt Kraaidd19c692001-01-31 19:00:21 +00001031 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001033}
1034
1035
Eric Andersenf86aee72000-07-06 17:20:46 +00001036#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001037
1038
Eric Andersen9f16d612000-06-12 23:11:16 +00001039#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001040/*
1041 * Routine to see if a text string is matched by a wildcard pattern.
1042 * Returns TRUE if the text is matched, or FALSE if it is not matched
1043 * or if the pattern is invalid.
1044 * * matches zero or more characters
1045 * ? matches a single character
1046 * [abc] matches 'a', 'b' or 'c'
1047 * \c quotes character c
1048 * Adapted from code written by Ingo Wilken, and
1049 * then taken from sash, Copyright (c) 1999 by David I. Bell
1050 * Permission is granted to use, distribute, or modify this source,
1051 * provided that this copyright notice remains intact.
1052 * Permission to distribute this code under the GPL has been granted.
1053 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001055{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056 const char *retryPat;
1057 const char *retryText;
1058 int ch;
1059 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001060 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001061
Erik Andersene49d5ec2000-02-08 19:58:47 +00001062 retryPat = NULL;
1063 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001064
Erik Andersene49d5ec2000-02-08 19:58:47 +00001065 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001066 ch = *pattern++;
1067
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 switch (ch) {
1069 case '*':
1070 retryPat = pattern;
1071 retryText = text;
1072 break;
1073
1074 case '[':
1075 found = FALSE;
1076
1077 while ((ch = *pattern++) != ']') {
1078 if (ch == '\\')
1079 ch = *pattern++;
1080
1081 if (ch == '\0')
1082 return FALSE;
1083
1084 if (*text == ch)
1085 found = TRUE;
1086 }
Erik Andersen632bb572000-04-18 22:09:06 +00001087 len=strlen(text);
1088 if (found == FALSE && len!=0) {
1089 return FALSE;
1090 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001092 if (strlen(pattern)==0 && len==1) {
1093 return TRUE;
1094 }
1095 if (len!=0) {
1096 text++;
1097 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001098 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001099 }
1100
1101 /* fall into next case */
1102
1103 case '?':
1104 if (*text++ == '\0')
1105 return FALSE;
1106
1107 break;
1108
1109 case '\\':
1110 ch = *pattern++;
1111
1112 if (ch == '\0')
1113 return FALSE;
1114
1115 /* fall into next case */
1116
1117 default:
1118 if (*text == ch) {
1119 if (*text)
1120 text++;
1121 break;
1122 }
1123
1124 if (*text) {
1125 pattern = retryPat;
1126 text = ++retryText;
1127 break;
1128 }
1129
Eric Andersenb186d981999-12-03 09:19:54 +00001130 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001131 }
1132
Erik Andersene49d5ec2000-02-08 19:58:47 +00001133 if (pattern == NULL)
1134 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001135 }
1136
Erik Andersene49d5ec2000-02-08 19:58:47 +00001137 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001138}
Eric Andersen9f16d612000-06-12 23:11:16 +00001139#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001140
1141
Eric Andersen29d2e361999-11-06 06:07:27 +00001142
1143
Erik Andersen23dea9b2000-05-13 00:28:25 +00001144#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001145#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001146/*
1147 * Given a block device, find the mount table entry if that block device
1148 * is mounted.
1149 *
1150 * Given any other file (or directory), find the mount table entry for its
1151 * filesystem.
1152 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001153extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001154{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001155 struct stat s;
1156 dev_t mountDevice;
1157 FILE *mountTable;
1158 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001159
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 if (stat(name, &s) != 0)
1161 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 if ((s.st_mode & S_IFMT) == S_IFBLK)
1164 mountDevice = s.st_rdev;
1165 else
1166 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001167
1168
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 if ((mountTable = setmntent(table, "r")) == 0)
1170 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001171
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 while ((mountEntry = getmntent(mountTable)) != 0) {
1173 if (strcmp(name, mountEntry->mnt_dir) == 0
1174 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1175 break;
1176 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1177 break;
1178 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1179 break;
1180 }
1181 endmntent(mountTable);
1182 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001183}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001184#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001185
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001186#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001187/* try to open up the specified device */
1188extern int device_open(char *device, int mode)
1189{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001193
Erik Andersene49d5ec2000-02-08 19:58:47 +00001194 /* Retry up to 5 times */
1195 for (f = 0; f < 5; f++)
1196 if ((fd = open(device, m, 0600)) >= 0)
1197 break;
1198 if (fd < 0)
1199 return fd;
1200 /* Reset original flags. */
1201 if (m != mode)
1202 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001203 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001204}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001205#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001206
1207
Erik Andersen23dea9b2000-05-13 00:28:25 +00001208#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001209#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001210#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001211#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001212
Erik Andersen23dea9b2000-05-13 00:28:25 +00001213#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001214/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001215 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001216 * This finds the pid of the specified process,
1217 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001218 *
Erik Andersen825aead2000-04-07 06:00:07 +00001219 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001220 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001221extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001222{
Erik Andersen825aead2000-04-07 06:00:07 +00001223 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001224 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001225 pid_t num_pids;
1226 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001227 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001228
1229 /* open device */
1230 fd = open(device, O_RDONLY);
1231 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001232 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001233
1234 /* Find out how many processes there are */
1235 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001236 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001237
1238 /* Allocate some memory -- grab a few extras just in case
1239 * some new processes start up while we wait. The kernel will
1240 * just ignore any extras if we give it too many, and will trunc.
1241 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001242 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001243 pid_array[0] = num_pids+10;
1244
1245 /* Now grab the pid list */
1246 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001247 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001248
1249 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001250 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001251 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001252 struct pid_info info;
1253
1254 info.pid = pid_array[i];
1255 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001256 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001257
Erik Andersen0a64de92000-04-18 20:00:03 +00001258 /* Make sure we only match on the process name */
1259 p=info.command_line+1;
1260 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1261 (p)++;
1262 }
1263 if (isspace(*(p)))
1264 *p='\0';
1265
Erik Andersenb9167cb2000-04-18 22:41:30 +00001266 if ((strstr(info.command_line, pidName) != NULL)
1267 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001268 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001269 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001270 }
1271 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001272 if (pidList)
1273 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001274
1275 /* Free memory */
1276 free( pid_array);
1277
1278 /* close device */
1279 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001280 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001281
Erik Andersen825aead2000-04-07 06:00:07 +00001282 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001283}
Erik Andersen269bba22000-03-08 14:50:47 +00001284#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001285
Mark Whitleyf57c9442000-12-07 19:56:48 +00001286/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001287 *
1288 * This finds the pid of the specified process.
1289 * Currently, it's implemented by rummaging through
1290 * the proc filesystem.
1291 *
Erik Andersen825aead2000-04-07 06:00:07 +00001292 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001293 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001294extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001295{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001296 DIR *dir;
1297 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001298 pid_t* pidList=NULL;
1299 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001300
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001301 dir = opendir("/proc");
1302 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001303 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001304
1305 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001306 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001307 char filename[256];
1308 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001309
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001310 /* If it isn't a number, we don't want it */
1311 if (!isdigit(*next->d_name))
1312 continue;
1313
Eric Andersen31e68292000-09-14 21:12:20 +00001314 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001315 status = fopen(filename, "r");
1316 if (!status) {
1317 continue;
1318 }
1319 fgets(buffer, 256, status);
1320 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001321
Eric Andersen31e68292000-09-14 21:12:20 +00001322 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001323 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001324 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001325 }
John Beppuf95ca971999-12-09 22:10:18 +00001326 }
Eric Andersen31e68292000-09-14 21:12:20 +00001327
Erik Andersen0a64de92000-04-18 20:00:03 +00001328 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001329 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001330 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001331}
Erik Andersen269bba22000-03-08 14:50:47 +00001332#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001333#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001334
Eric Andersenae6eae02000-07-19 17:35:54 +00001335#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001336/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001337extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001338{
Eric Andersenae6eae02000-07-19 17:35:54 +00001339 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001340
Eric Andersenae6eae02000-07-19 17:35:54 +00001341 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001342 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001343 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001344}
1345
Eric Andersen501c88b2000-07-28 15:14:45 +00001346extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001347{
1348 void *ptr = realloc(old, size);
1349 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001350 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001351 return ptr;
1352}
Eric Andersen501c88b2000-07-28 15:14:45 +00001353
1354extern void *xcalloc(size_t nmemb, size_t size)
1355{
1356 void *ptr = calloc(nmemb, size);
1357 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001358 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001359 return ptr;
1360}
Eric Andersenae6eae02000-07-19 17:35:54 +00001361#endif
1362
Eric Andersenca0f4582001-02-16 18:43:54 +00001363#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || \
1364 defined BB_WGET || defined BB_DPKG_DEB || defined BB_TAR || \
1365 defined BB_LN
Eric Andersenae6eae02000-07-19 17:35:54 +00001366# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001367extern char * xstrdup (const char *s) {
1368 char *t;
1369
1370 if (s == NULL)
1371 return NULL;
1372
1373 t = strdup (s);
1374
1375 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001376 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001377
1378 return t;
1379}
Eric Andersenae6eae02000-07-19 17:35:54 +00001380# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001381#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001382
Eric Andersenf5d5e772001-01-24 23:34:48 +00001383#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001384extern char * xstrndup (const char *s, int n) {
1385 char *t;
1386
1387 if (s == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001388 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001389
Eric Andersen12dceb12001-02-18 20:15:36 +00001390 t = xmalloc(++n);
1391
1392 return safe_strncpy(t,s,n);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001393}
1394#endif
1395
Eric Andersen12dceb12001-02-18 20:15:36 +00001396#if defined BB_IFCONFIG || defined BB_ROUTE || defined BB_NFSMOUNT || \
1397 defined BB_FEATURE_MOUNT_LOOP
Eric Andersenec455952001-02-14 08:11:27 +00001398/* Like strncpy but make sure the resulting string is always 0 terminated. */
1399extern char * safe_strncpy(char *dst, const char *src, size_t size)
1400{
1401 dst[size-1] = '\0';
1402 return strncpy(dst, src, size-1);
1403}
1404#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001405
Erik Andersen23dea9b2000-05-13 00:28:25 +00001406#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001407extern int vdprintf(int d, const char *format, va_list ap)
1408{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001409 char buf[BUF_SIZE];
1410 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001411
Erik Andersene49d5ec2000-02-08 19:58:47 +00001412 len = vsprintf(buf, format, ap);
1413 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001414}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001415#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001416
Eric Andersen624cc772000-09-21 02:04:51 +00001417
Erik Andersen23dea9b2000-05-13 00:28:25 +00001418#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001419#include <fcntl.h>
1420#include "loop.h" /* Pull in loop device support */
1421
Erik Andersen5cbdd712000-01-26 20:06:48 +00001422extern int del_loop(const char *device)
1423{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001424 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001425
Erik Andersene49d5ec2000-02-08 19:58:47 +00001426 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001427 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001428 return (FALSE);
1429 }
1430 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001431 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 return (FALSE);
1433 }
1434 close(fd);
1435 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001436}
Erik Andersende7965c2000-01-26 23:49:21 +00001437
Erik Andersene49d5ec2000-02-08 19:58:47 +00001438extern int set_loop(const char *device, const char *file, int offset,
1439 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001440{
1441 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001442 int fd, ffd, mode;
1443
Erik Andersende7965c2000-01-26 23:49:21 +00001444 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001445 if ((ffd = open(file, mode)) < 0 && !*loopro
1446 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001447 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001449 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001450 if ((fd = open(device, mode)) < 0) {
1451 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001452 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001453 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001454 }
1455 *loopro = (mode == O_RDONLY);
1456
1457 memset(&loopinfo, 0, sizeof(loopinfo));
Eric Andersen12dceb12001-02-18 20:15:36 +00001458 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersende7965c2000-01-26 23:49:21 +00001459
1460 loopinfo.lo_offset = offset;
1461
1462 loopinfo.lo_encrypt_key_size = 0;
1463 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001464 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001465 close(fd);
1466 close(ffd);
1467 return 1;
1468 }
1469 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1470 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001471 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001472 close(fd);
1473 close(ffd);
1474 return 1;
1475 }
1476 close(fd);
1477 close(ffd);
1478 return 0;
1479}
1480
Erik Andersene49d5ec2000-02-08 19:58:47 +00001481extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001482{
1483 char dev[20];
1484 int i, fd;
1485 struct stat statbuf;
1486 struct loop_info loopinfo;
1487
Erik Andersene49d5ec2000-02-08 19:58:47 +00001488 for (i = 0; i <= 7; i++) {
1489 sprintf(dev, "/dev/loop%d", i);
1490 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1491 if ((fd = open(dev, O_RDONLY)) >= 0) {
1492 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1493 if (errno == ENXIO) { /* probably free */
1494 close(fd);
1495 return strdup(dev);
1496 }
1497 }
1498 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001499 }
Erik Andersende7965c2000-01-26 23:49:21 +00001500 }
Erik Andersende7965c2000-01-26 23:49:21 +00001501 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001502 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001503}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001504#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001505
Erik Andersen23dea9b2000-05-13 00:28:25 +00001506#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001507extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001508{
Erik Andersen016ffe92000-03-22 00:58:54 +00001509 DIR *dir;
1510 struct dirent *entry;
1511 struct stat statBuf, rootStat;
1512 char fileName[BUFSIZ];
1513
Erik Andersenec5bd902000-03-22 07:12:05 +00001514 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001515 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001516 return( FALSE);
1517 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001518
Erik Andersenec5bd902000-03-22 07:12:05 +00001519 dir = opendir("/dev");
1520 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001521 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001522 return( FALSE);
1523 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001524
1525 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001526
Erik Andersen016ffe92000-03-22 00:58:54 +00001527 /* Must skip ".." since that is "/", and so we
1528 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001529 if (strcmp(entry->d_name, "..") == 0)
1530 continue;
1531
Eric Andersenf9ca6532000-08-11 17:45:02 +00001532 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001533
1534 if (stat(fileName, &statBuf) != 0)
1535 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001536 /* Some char devices have the same dev_t as block
1537 * devices, so make sure this is a block device */
1538 if (! S_ISBLK(statBuf.st_mode))
1539 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001540 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001541 strcpy(name, fileName);
1542 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001543 }
1544 }
1545
Erik Andersenec5bd902000-03-22 07:12:05 +00001546 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001547}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001548#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001549
Mark Whitley1ca41772000-06-28 22:15:26 +00001550
1551/* get_line_from_file() - This function reads an entire line from a text file
1552 * up to a newline. It returns a malloc'ed char * which must be stored and
1553 * free'ed by the caller. */
1554extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001555{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001556 static const int GROWBY = 80; /* how large we will grow strings by */
1557
Mark Whitley1ca41772000-06-28 22:15:26 +00001558 int ch;
1559 int idx = 0;
1560 char *linebuf = NULL;
1561 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001562
Mark Whitley1ca41772000-06-28 22:15:26 +00001563 while (1) {
1564 ch = fgetc(file);
1565 if (ch == EOF)
1566 break;
1567 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001568 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001569 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001570 linebuf[idx++] = (char)ch;
1571 if ((char)ch == '\n')
1572 break;
1573 }
John Beppu5a728cf2000-04-17 04:22:09 +00001574
Mark Whitley1ca41772000-06-28 22:15:26 +00001575 if (idx == 0)
1576 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001577
Mark Whitley1ca41772000-06-28 22:15:26 +00001578 linebuf[idx] = 0;
1579 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001580}
Erik Andersen016ffe92000-03-22 00:58:54 +00001581
Eric Andersen944be902000-09-25 16:58:39 +00001582#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001583extern void print_file(FILE *file)
1584{
1585 int c;
1586
1587 while ((c = getc(file)) != EOF)
1588 putc(c, stdout);
1589 fclose(file);
1590 fflush(stdout);
1591}
1592
1593extern int print_file_by_name(char *filename)
1594{
1595 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001596 if ((file = wfopen(filename, "r")) == NULL)
1597 return FALSE;
1598 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001599 return TRUE;
1600}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001601#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001602
Matt Kraaicabafae2000-11-30 04:55:40 +00001603#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001604char process_escape_sequence(char **ptr)
1605{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001606 static const char charmap[] = {
1607 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1608 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001609
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001610 const char *p;
1611 char *q;
1612 int num_digits;
1613 unsigned int n;
1614
1615 n = 0;
1616 q = *ptr;
1617
1618 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1619 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1620 break;
1621 }
1622 n = n * 8 + (*q++ - '0');
1623 }
1624
1625 if (num_digits == 0) { /* mnemonic escape sequence? */
1626 for (p=charmap ; *p ; p++) {
1627 if (*p == *q) {
1628 q++;
1629 break;
1630 }
1631 }
1632 n = *(p+(sizeof(charmap)/2));
1633 }
1634
1635 /* doesn't hurt to fall through to here from mnemonic case */
1636 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1637 n /= 8; /* adjust value and */
1638 --q; /* back up one char */
1639 }
1640
1641 *ptr = q;
1642 return (char) n;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001643}
1644#endif
1645
Eric Andersen70c60862001-01-25 00:00:04 +00001646#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001647 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001648char *get_last_path_component(char *path)
1649{
1650 char *s=path+strlen(path)-1;
1651
1652 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001653 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001654 *s-- = '\0';
1655 }
1656
1657 /* find last component */
1658 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001659 if (s == NULL || s[1] == '\0')
1660 return path;
1661 else
1662 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001663}
1664#endif
1665
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001666#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001667#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001668void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001669{
1670 int ret;
1671 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1672 int errmsgsz = regerror(ret, preg, NULL, 0);
1673 char *errmsg = xmalloc(errmsgsz);
1674 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001675 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001676 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001677}
1678#endif
1679
Eric Andersen7a86e612000-10-09 18:21:44 +00001680#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001681FILE *wfopen(const char *path, const char *mode)
1682{
1683 FILE *fp;
1684 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001685 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001686 errno = 0;
1687 }
1688 return fp;
1689}
1690#endif
1691
Eric Andersene790ddb2000-11-17 06:50:42 +00001692#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001693 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001694 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
Eric Andersen12dceb12001-02-18 20:15:36 +00001695 || defined BB_MOUNT || defined BB_ROUTE
Matt Kraaie0bcce02000-09-27 02:29:39 +00001696FILE *xfopen(const char *path, const char *mode)
1697{
1698 FILE *fp;
1699 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001700 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001701 return fp;
1702}
1703#endif
1704
Matt Kraaif2cc2762001-02-01 19:21:20 +00001705static int applet_name_compare(const void *x, const void *y)
Eric Andersene5aef922000-11-17 18:25:26 +00001706{
Matt Kraaif2cc2762001-02-01 19:21:20 +00001707 const char *name = x;
1708 const struct BB_applet *applet = y;
Eric Andersene5aef922000-11-17 18:25:26 +00001709
Matt Kraaif2cc2762001-02-01 19:21:20 +00001710 return strcmp(name, applet->name);
1711}
1712
1713extern size_t NUM_APPLETS;
1714
1715struct BB_applet *find_applet_by_name(const char *name)
1716{
1717 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1718 applet_name_compare);
Eric Andersene5aef922000-11-17 18:25:26 +00001719}
1720
Eric Andersen67991cf2001-02-14 21:23:06 +00001721void run_applet_by_name(const char *name, int argc, char **argv)
1722{
1723 /* Do a binary search to find the applet entry given the name. */
1724 if ((applet_using = find_applet_by_name(name)) != NULL) {
1725 applet_name = applet_using->name;
1726 if (argv[1] && strcmp(argv[1], "--help") == 0)
1727 show_usage();
1728 exit((*(applet_using->main)) (argc, argv));
1729 }
1730}
1731
Eric Andersenca0f4582001-02-16 18:43:54 +00001732#if defined BB_DD || defined BB_TAIL || defined BB_STTY
Matt Kraaia164c642001-02-05 17:50:03 +00001733unsigned long parse_number(const char *numstr,
1734 const struct suffix_mult *suffixes)
Matt Kraai24ac0172000-12-18 21:38:57 +00001735{
Matt Kraaia164c642001-02-05 17:50:03 +00001736 const struct suffix_mult *sm;
Matt Kraai24ac0172000-12-18 21:38:57 +00001737 unsigned long int ret;
1738 int len;
1739 char *end;
1740
1741 ret = strtoul(numstr, &end, 10);
1742 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001743 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001744 while (end[0] != '\0') {
Eric Andersen12dceb12001-02-18 20:15:36 +00001745 sm = suffixes;
1746 while ( sm != 0 ) {
1747 if(sm->suffix) {
1748 len = strlen(sm->suffix);
1749 if (strncmp(sm->suffix, end, len) == 0) {
1750 ret *= sm->mult;
1751 end += len;
1752 break;
1753 }
1754 sm++;
1755
1756 } else
1757 sm = 0;
Matt Kraai24ac0172000-12-18 21:38:57 +00001758 }
Eric Andersen12dceb12001-02-18 20:15:36 +00001759 if (sm == 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001760 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001761 }
1762 return ret;
1763}
1764#endif
1765
Eric Andersen23a93f52001-01-24 23:47:21 +00001766#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001767ssize_t safe_read(int fd, void *buf, size_t count)
1768{
1769 ssize_t n;
1770
1771 do {
1772 n = read(fd, buf, count);
1773 } while (n < 0 && errno == EINTR);
1774
1775 return n;
1776}
1777#endif
1778
Richard June6d0921c2001-01-22 22:35:38 +00001779#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001780const char *format(unsigned long val, unsigned long hr)
Richard June6d0921c2001-01-22 22:35:38 +00001781{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001782 static const char strings[] = { '0', 0, 'k', 0, 'M', 0, 'G', 0 };
1783 static const char fmt[] = "%lu";
1784 static const char fmt_u[] = "%lu.%lu%s";
Richard June6d0921c2001-01-22 22:35:38 +00001785
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001786 static char str[10];
1787
1788 unsigned long frac __attribute__ ((unused)); /* 'may be uninitialized' warning is ok */
1789 const char *u;
1790 const char *f;
1791
1792#if 1
1793 if(val == 0) { /* This may be omitted to reduce size */
1794 return strings; /* at the cost of speed. */
1795 }
1796#endif
1797
1798 u = strings;
1799 f = fmt;
1800 if (hr) {
1801 val /= hr;
1802 } else {
1803 while ((val >= KILOBYTE) && (*u != 'G')) {
1804 f = fmt_u;
1805 u += 2;
1806 frac = (((val % KILOBYTE) * 10) + (KILOBYTE/2)) / KILOBYTE;
1807 val /= KILOBYTE;
1808 if (frac >= 10) { /* We need to round up here. */
1809 ++val;
1810 frac = 0;
1811 }
1812 }
1813 }
1814
1815 /* If f==fmt then 'frac' and 'u' are ignored and need not be set. */
1816 snprintf(str, sizeof(str), f, val, frac, u);
1817
1818 return str;
Richard June6d0921c2001-01-22 22:35:38 +00001819}
1820#endif
1821
Matt Kraai05e782d2001-02-01 16:49:30 +00001822#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS)
1823void chomp(char *s)
1824{
1825 size_t len = strlen(s);
1826
1827 if (len == 0)
1828 return;
1829
1830 if (s[len-1] == '\n')
1831 s[len-1] = '\0';
1832}
1833#endif
1834
Eric Andersencc8ed391999-10-05 16:24:54 +00001835/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001836/*
1837Local Variables:
1838c-file-style: "linux"
1839c-basic-offset: 4
1840tab-width: 4
1841End:
1842*/