blob: 76299beb71df75b850e601913b00eed681d5a350 [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
Eric Andersen7b91f022001-03-01 07:50:04 +000066/* Busybox mount uses either /proc/mounts or /dev/mtab to
67 * get the list of currently mounted filesystems */
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
Eric Andersen4142d4d2001-02-27 18:22:03 +0000880
881#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000882/* returns a uid given a username */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000883long my_getpwnam(const char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000884{
Eric Andersenab050f52001-01-27 06:01:43 +0000885 struct passwd *myuser;
886
887 myuser = getpwnam(name);
888 if (myuser==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000889 error_msg_and_die("unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000890
891 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000892}
893
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000894/* returns a gid given a group name */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000895long my_getgrnam(const char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000896{
Eric Andersenab050f52001-01-27 06:01:43 +0000897 struct group *mygroup;
898
899 mygroup = getgrnam(name);
900 if (mygroup==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000901 error_msg_and_die("unknown group name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000902
903 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000904}
Eric Andersen4142d4d2001-02-27 18:22:03 +0000905#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +0000906
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000907/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000908void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000909{
Eric Andersenab050f52001-01-27 06:01:43 +0000910 struct passwd *myuser;
911
912 myuser = getpwuid(uid);
913 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000914 sprintf(name, "%-8ld ", (long)uid);
915 else
916 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000917}
918
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000919/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000920void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000921{
Eric Andersenab050f52001-01-27 06:01:43 +0000922 struct group *mygroup;
923
924 mygroup = getgrgid(gid);
925 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000926 sprintf(group, "%-8ld ", (long)gid);
927 else
928 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000929}
930
Matt Kraaie6e81832000-12-30 07:46:23 +0000931#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000932/* gets a gid given a user name */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000933long my_getpwnamegid(const char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000934{
Eric Andersenab050f52001-01-27 06:01:43 +0000935 struct group *mygroup;
936 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000937
Eric Andersenab050f52001-01-27 06:01:43 +0000938 myuser=getpwnam(name);
939 if (myuser==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000940 error_msg_and_die("unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000941
942 mygroup = getgrgid(myuser->pw_gid);
943 if (mygroup==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000944 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000945
946 return mygroup->gr_gid;
947}
948#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000949#endif
950 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
951 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000952
953
Eric Andersenf86aee72000-07-06 17:20:46 +0000954#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000955
Eric Andersenbd22ed82000-07-08 18:55:24 +0000956/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000957static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
958static const int KB_84 = 0x01;
959static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000960
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000962{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000963 char arg;
964
965 arg = 0;
966 return (ioctl(fd, KDGKBTYPE, &arg) == 0
967 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000968}
969
Erik Andersene49d5ec2000-02-08 19:58:47 +0000970static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000971{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000972 int fd;
973
974 /* try read-only */
975 fd = open(fnam, O_RDWR);
976
977 /* if failed, try read-only */
978 if (fd < 0 && errno == EACCES)
979 fd = open(fnam, O_RDONLY);
980
981 /* if failed, try write-only */
982 if (fd < 0 && errno == EACCES)
983 fd = open(fnam, O_WRONLY);
984
985 /* if failed, fail */
986 if (fd < 0)
987 return -1;
988
989 /* if not a console, fail */
990 if (!is_a_console(fd)) {
991 close(fd);
992 return -1;
993 }
994
995 /* success */
996 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000997}
998
999/*
1000 * Get an fd for use with kbd/console ioctls.
1001 * We try several things because opening /dev/console will fail
1002 * if someone else used X (which does a chown on /dev/console).
1003 *
1004 * if tty_name is non-NULL, try this one instead.
1005 */
1006
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001008{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001010
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 if (tty_name) {
1012 if (-1 == (fd = open_a_console(tty_name)))
1013 return -1;
1014 else
1015 return fd;
1016 }
1017
1018 fd = open_a_console("/dev/tty");
1019 if (fd >= 0)
1020 return fd;
1021
1022 fd = open_a_console("/dev/tty0");
1023 if (fd >= 0)
1024 return fd;
1025
1026 fd = open_a_console("/dev/console");
1027 if (fd >= 0)
1028 return fd;
1029
1030 for (fd = 0; fd < 3; fd++)
1031 if (is_a_console(fd))
1032 return fd;
1033
Matt Kraaidd19c692001-01-31 19:00:21 +00001034 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001036}
1037
1038
Eric Andersenf86aee72000-07-06 17:20:46 +00001039#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001040
1041
Eric Andersen9f16d612000-06-12 23:11:16 +00001042#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001043/*
1044 * Routine to see if a text string is matched by a wildcard pattern.
1045 * Returns TRUE if the text is matched, or FALSE if it is not matched
1046 * or if the pattern is invalid.
1047 * * matches zero or more characters
1048 * ? matches a single character
1049 * [abc] matches 'a', 'b' or 'c'
1050 * \c quotes character c
1051 * Adapted from code written by Ingo Wilken, and
1052 * then taken from sash, Copyright (c) 1999 by David I. Bell
1053 * Permission is granted to use, distribute, or modify this source,
1054 * provided that this copyright notice remains intact.
1055 * Permission to distribute this code under the GPL has been granted.
1056 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001058{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 const char *retryPat;
1060 const char *retryText;
1061 int ch;
1062 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001063 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001064
Erik Andersene49d5ec2000-02-08 19:58:47 +00001065 retryPat = NULL;
1066 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001067
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001069 ch = *pattern++;
1070
Erik Andersene49d5ec2000-02-08 19:58:47 +00001071 switch (ch) {
1072 case '*':
1073 retryPat = pattern;
1074 retryText = text;
1075 break;
1076
1077 case '[':
1078 found = FALSE;
1079
1080 while ((ch = *pattern++) != ']') {
1081 if (ch == '\\')
1082 ch = *pattern++;
1083
1084 if (ch == '\0')
1085 return FALSE;
1086
1087 if (*text == ch)
1088 found = TRUE;
1089 }
Erik Andersen632bb572000-04-18 22:09:06 +00001090 len=strlen(text);
1091 if (found == FALSE && len!=0) {
1092 return FALSE;
1093 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001095 if (strlen(pattern)==0 && len==1) {
1096 return TRUE;
1097 }
1098 if (len!=0) {
1099 text++;
1100 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001101 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 }
1103
1104 /* fall into next case */
1105
1106 case '?':
1107 if (*text++ == '\0')
1108 return FALSE;
1109
1110 break;
1111
1112 case '\\':
1113 ch = *pattern++;
1114
1115 if (ch == '\0')
1116 return FALSE;
1117
1118 /* fall into next case */
1119
1120 default:
1121 if (*text == ch) {
1122 if (*text)
1123 text++;
1124 break;
1125 }
1126
1127 if (*text) {
1128 pattern = retryPat;
1129 text = ++retryText;
1130 break;
1131 }
1132
Eric Andersenb186d981999-12-03 09:19:54 +00001133 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001134 }
1135
Erik Andersene49d5ec2000-02-08 19:58:47 +00001136 if (pattern == NULL)
1137 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001138 }
1139
Erik Andersene49d5ec2000-02-08 19:58:47 +00001140 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001141}
Eric Andersen9f16d612000-06-12 23:11:16 +00001142#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001143
1144
Eric Andersen29d2e361999-11-06 06:07:27 +00001145
1146
Erik Andersen23dea9b2000-05-13 00:28:25 +00001147#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001148#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001149/*
1150 * Given a block device, find the mount table entry if that block device
1151 * is mounted.
1152 *
1153 * Given any other file (or directory), find the mount table entry for its
1154 * filesystem.
1155 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001156extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001157{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 struct stat s;
1159 dev_t mountDevice;
1160 FILE *mountTable;
1161 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 if (stat(name, &s) != 0)
1164 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001165
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 if ((s.st_mode & S_IFMT) == S_IFBLK)
1167 mountDevice = s.st_rdev;
1168 else
1169 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001170
1171
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 if ((mountTable = setmntent(table, "r")) == 0)
1173 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001174
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175 while ((mountEntry = getmntent(mountTable)) != 0) {
1176 if (strcmp(name, mountEntry->mnt_dir) == 0
1177 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1178 break;
1179 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1180 break;
1181 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1182 break;
1183 }
1184 endmntent(mountTable);
1185 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001186}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001187#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001189#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001190/* try to open up the specified device */
1191extern int device_open(char *device, int mode)
1192{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001194
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001196
Erik Andersene49d5ec2000-02-08 19:58:47 +00001197 /* Retry up to 5 times */
1198 for (f = 0; f < 5; f++)
1199 if ((fd = open(device, m, 0600)) >= 0)
1200 break;
1201 if (fd < 0)
1202 return fd;
1203 /* Reset original flags. */
1204 if (m != mode)
1205 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001206 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001207}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001208#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001209
1210
Erik Andersen23dea9b2000-05-13 00:28:25 +00001211#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001212#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001213#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001214#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001215
Erik Andersen23dea9b2000-05-13 00:28:25 +00001216#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001217/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001218 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001219 * This finds the pid of the specified process,
1220 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001221 *
Erik Andersen825aead2000-04-07 06:00:07 +00001222 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001223 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001224extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001225{
Erik Andersen825aead2000-04-07 06:00:07 +00001226 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001227 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001228 pid_t num_pids;
1229 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001230 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001231
1232 /* open device */
1233 fd = open(device, O_RDONLY);
1234 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001235 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001236
1237 /* Find out how many processes there are */
1238 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001239 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001240
1241 /* Allocate some memory -- grab a few extras just in case
1242 * some new processes start up while we wait. The kernel will
1243 * just ignore any extras if we give it too many, and will trunc.
1244 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001245 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001246 pid_array[0] = num_pids+10;
1247
1248 /* Now grab the pid list */
1249 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001250 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001251
1252 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001253 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001254 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001255 struct pid_info info;
1256
1257 info.pid = pid_array[i];
1258 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001259 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001260
Erik Andersen0a64de92000-04-18 20:00:03 +00001261 /* Make sure we only match on the process name */
1262 p=info.command_line+1;
1263 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1264 (p)++;
1265 }
1266 if (isspace(*(p)))
1267 *p='\0';
1268
Erik Andersenb9167cb2000-04-18 22:41:30 +00001269 if ((strstr(info.command_line, pidName) != NULL)
1270 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001271 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001272 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001273 }
1274 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001275 if (pidList)
1276 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001277
1278 /* Free memory */
1279 free( pid_array);
1280
1281 /* close device */
1282 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001283 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001284
Erik Andersen825aead2000-04-07 06:00:07 +00001285 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001286}
Erik Andersen269bba22000-03-08 14:50:47 +00001287#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001288
Mark Whitleyf57c9442000-12-07 19:56:48 +00001289/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001290 *
1291 * This finds the pid of the specified process.
1292 * Currently, it's implemented by rummaging through
1293 * the proc filesystem.
1294 *
Erik Andersen825aead2000-04-07 06:00:07 +00001295 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001296 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001297extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001298{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001299 DIR *dir;
1300 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001301 pid_t* pidList=NULL;
1302 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001303
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001304 dir = opendir("/proc");
1305 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001306 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001307
1308 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001309 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001310 char filename[256];
1311 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001312
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001313 /* If it isn't a number, we don't want it */
1314 if (!isdigit(*next->d_name))
1315 continue;
1316
Eric Andersen31e68292000-09-14 21:12:20 +00001317 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001318 status = fopen(filename, "r");
1319 if (!status) {
1320 continue;
1321 }
1322 fgets(buffer, 256, status);
1323 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001324
Eric Andersen31e68292000-09-14 21:12:20 +00001325 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001326 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001327 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001328 }
John Beppuf95ca971999-12-09 22:10:18 +00001329 }
Eric Andersen31e68292000-09-14 21:12:20 +00001330
Erik Andersen0a64de92000-04-18 20:00:03 +00001331 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001332 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001333 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001334}
Erik Andersen269bba22000-03-08 14:50:47 +00001335#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001336#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001337
Eric Andersenae6eae02000-07-19 17:35:54 +00001338#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001339/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001340extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001341{
Eric Andersenae6eae02000-07-19 17:35:54 +00001342 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001343
Eric Andersenae6eae02000-07-19 17:35:54 +00001344 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001345 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001346 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001347}
1348
Eric Andersen501c88b2000-07-28 15:14:45 +00001349extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001350{
1351 void *ptr = realloc(old, size);
1352 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001353 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001354 return ptr;
1355}
Eric Andersen501c88b2000-07-28 15:14:45 +00001356
1357extern void *xcalloc(size_t nmemb, size_t size)
1358{
1359 void *ptr = calloc(nmemb, size);
1360 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001361 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001362 return ptr;
1363}
Eric Andersenae6eae02000-07-19 17:35:54 +00001364#endif
1365
Eric Andersenca0f4582001-02-16 18:43:54 +00001366#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || \
1367 defined BB_WGET || defined BB_DPKG_DEB || defined BB_TAR || \
1368 defined BB_LN
Eric Andersenae6eae02000-07-19 17:35:54 +00001369# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001370extern char * xstrdup (const char *s) {
1371 char *t;
1372
1373 if (s == NULL)
1374 return NULL;
1375
1376 t = strdup (s);
1377
1378 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001379 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001380
1381 return t;
1382}
Eric Andersenae6eae02000-07-19 17:35:54 +00001383# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001384#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001385
Eric Andersenf5d5e772001-01-24 23:34:48 +00001386#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001387extern char * xstrndup (const char *s, int n) {
1388 char *t;
1389
1390 if (s == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001391 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001392
Eric Andersen12dceb12001-02-18 20:15:36 +00001393 t = xmalloc(++n);
1394
1395 return safe_strncpy(t,s,n);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001396}
1397#endif
1398
Eric Andersen12dceb12001-02-18 20:15:36 +00001399#if defined BB_IFCONFIG || defined BB_ROUTE || defined BB_NFSMOUNT || \
1400 defined BB_FEATURE_MOUNT_LOOP
Eric Andersenec455952001-02-14 08:11:27 +00001401/* Like strncpy but make sure the resulting string is always 0 terminated. */
1402extern char * safe_strncpy(char *dst, const char *src, size_t size)
1403{
1404 dst[size-1] = '\0';
1405 return strncpy(dst, src, size-1);
1406}
1407#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001408
Erik Andersen23dea9b2000-05-13 00:28:25 +00001409#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001410extern int vdprintf(int d, const char *format, va_list ap)
1411{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001412 char buf[BUF_SIZE];
1413 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001414
Erik Andersene49d5ec2000-02-08 19:58:47 +00001415 len = vsprintf(buf, format, ap);
1416 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001417}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001418#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001419
Eric Andersen624cc772000-09-21 02:04:51 +00001420
Erik Andersen23dea9b2000-05-13 00:28:25 +00001421#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001422#include <fcntl.h>
1423#include "loop.h" /* Pull in loop device support */
1424
Erik Andersen5cbdd712000-01-26 20:06:48 +00001425extern int del_loop(const char *device)
1426{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001427 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001428
Erik Andersene49d5ec2000-02-08 19:58:47 +00001429 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001430 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001431 return (FALSE);
1432 }
1433 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001434 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001435 return (FALSE);
1436 }
1437 close(fd);
1438 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001439}
Erik Andersende7965c2000-01-26 23:49:21 +00001440
Erik Andersene49d5ec2000-02-08 19:58:47 +00001441extern int set_loop(const char *device, const char *file, int offset,
1442 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001443{
1444 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001445 int fd, ffd, mode;
1446
Erik Andersende7965c2000-01-26 23:49:21 +00001447 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448 if ((ffd = open(file, mode)) < 0 && !*loopro
1449 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001450 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001451 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001452 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001453 if ((fd = open(device, mode)) < 0) {
1454 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001455 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001456 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001457 }
1458 *loopro = (mode == O_RDONLY);
1459
1460 memset(&loopinfo, 0, sizeof(loopinfo));
Eric Andersen12dceb12001-02-18 20:15:36 +00001461 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersende7965c2000-01-26 23:49:21 +00001462
1463 loopinfo.lo_offset = offset;
1464
1465 loopinfo.lo_encrypt_key_size = 0;
1466 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001467 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001468 close(fd);
1469 close(ffd);
1470 return 1;
1471 }
1472 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1473 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001474 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001475 close(fd);
1476 close(ffd);
1477 return 1;
1478 }
1479 close(fd);
1480 close(ffd);
1481 return 0;
1482}
1483
Erik Andersene49d5ec2000-02-08 19:58:47 +00001484extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001485{
1486 char dev[20];
1487 int i, fd;
1488 struct stat statbuf;
1489 struct loop_info loopinfo;
1490
Erik Andersene49d5ec2000-02-08 19:58:47 +00001491 for (i = 0; i <= 7; i++) {
1492 sprintf(dev, "/dev/loop%d", i);
1493 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1494 if ((fd = open(dev, O_RDONLY)) >= 0) {
1495 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1496 if (errno == ENXIO) { /* probably free */
1497 close(fd);
1498 return strdup(dev);
1499 }
1500 }
1501 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001502 }
Erik Andersende7965c2000-01-26 23:49:21 +00001503 }
Erik Andersende7965c2000-01-26 23:49:21 +00001504 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001505 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001506}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001507#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001508
Erik Andersen23dea9b2000-05-13 00:28:25 +00001509#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001510extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001511{
Erik Andersen016ffe92000-03-22 00:58:54 +00001512 DIR *dir;
1513 struct dirent *entry;
1514 struct stat statBuf, rootStat;
1515 char fileName[BUFSIZ];
1516
Erik Andersenec5bd902000-03-22 07:12:05 +00001517 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001518 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001519 return( FALSE);
1520 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001521
Erik Andersenec5bd902000-03-22 07:12:05 +00001522 dir = opendir("/dev");
1523 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001524 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001525 return( FALSE);
1526 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001527
1528 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001529
Erik Andersen016ffe92000-03-22 00:58:54 +00001530 /* Must skip ".." since that is "/", and so we
1531 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001532 if (strcmp(entry->d_name, "..") == 0)
1533 continue;
1534
Eric Andersenf9ca6532000-08-11 17:45:02 +00001535 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001536
1537 if (stat(fileName, &statBuf) != 0)
1538 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001539 /* Some char devices have the same dev_t as block
1540 * devices, so make sure this is a block device */
1541 if (! S_ISBLK(statBuf.st_mode))
1542 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001543 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001544 strcpy(name, fileName);
1545 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001546 }
1547 }
1548
Erik Andersenec5bd902000-03-22 07:12:05 +00001549 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001550}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001551#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001552
Mark Whitley1ca41772000-06-28 22:15:26 +00001553
1554/* get_line_from_file() - This function reads an entire line from a text file
1555 * up to a newline. It returns a malloc'ed char * which must be stored and
1556 * free'ed by the caller. */
1557extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001558{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001559 static const int GROWBY = 80; /* how large we will grow strings by */
1560
Mark Whitley1ca41772000-06-28 22:15:26 +00001561 int ch;
1562 int idx = 0;
1563 char *linebuf = NULL;
1564 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001565
Mark Whitley1ca41772000-06-28 22:15:26 +00001566 while (1) {
1567 ch = fgetc(file);
1568 if (ch == EOF)
1569 break;
1570 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001571 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001572 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001573 linebuf[idx++] = (char)ch;
1574 if ((char)ch == '\n')
1575 break;
1576 }
John Beppu5a728cf2000-04-17 04:22:09 +00001577
Mark Whitley1ca41772000-06-28 22:15:26 +00001578 if (idx == 0)
1579 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001580
Mark Whitley1ca41772000-06-28 22:15:26 +00001581 linebuf[idx] = 0;
1582 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001583}
Erik Andersen016ffe92000-03-22 00:58:54 +00001584
Eric Andersen944be902000-09-25 16:58:39 +00001585#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001586extern void print_file(FILE *file)
1587{
1588 int c;
1589
1590 while ((c = getc(file)) != EOF)
1591 putc(c, stdout);
1592 fclose(file);
1593 fflush(stdout);
1594}
1595
1596extern int print_file_by_name(char *filename)
1597{
1598 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001599 if ((file = wfopen(filename, "r")) == NULL)
1600 return FALSE;
1601 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001602 return TRUE;
1603}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001604#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001605
Matt Kraaicabafae2000-11-30 04:55:40 +00001606#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001607char process_escape_sequence(char **ptr)
1608{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001609 static const char charmap[] = {
1610 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1611 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001612
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001613 const char *p;
1614 char *q;
1615 int num_digits;
1616 unsigned int n;
1617
1618 n = 0;
1619 q = *ptr;
1620
1621 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1622 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1623 break;
1624 }
1625 n = n * 8 + (*q++ - '0');
1626 }
1627
1628 if (num_digits == 0) { /* mnemonic escape sequence? */
1629 for (p=charmap ; *p ; p++) {
1630 if (*p == *q) {
1631 q++;
1632 break;
1633 }
1634 }
1635 n = *(p+(sizeof(charmap)/2));
1636 }
1637
1638 /* doesn't hurt to fall through to here from mnemonic case */
1639 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1640 n /= 8; /* adjust value and */
1641 --q; /* back up one char */
1642 }
1643
1644 *ptr = q;
1645 return (char) n;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001646}
1647#endif
1648
Eric Andersen70c60862001-01-25 00:00:04 +00001649#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001650 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001651char *get_last_path_component(char *path)
1652{
1653 char *s=path+strlen(path)-1;
1654
1655 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001656 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001657 *s-- = '\0';
1658 }
1659
1660 /* find last component */
1661 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001662 if (s == NULL || s[1] == '\0')
1663 return path;
1664 else
1665 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001666}
1667#endif
1668
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001669#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001670#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001671void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001672{
1673 int ret;
1674 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1675 int errmsgsz = regerror(ret, preg, NULL, 0);
1676 char *errmsg = xmalloc(errmsgsz);
1677 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001678 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001679 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001680}
1681#endif
1682
Eric Andersen7a86e612000-10-09 18:21:44 +00001683#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001684FILE *wfopen(const char *path, const char *mode)
1685{
1686 FILE *fp;
1687 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001688 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001689 errno = 0;
1690 }
1691 return fp;
1692}
1693#endif
1694
Eric Andersene790ddb2000-11-17 06:50:42 +00001695#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001696 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001697 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
Eric Andersen12dceb12001-02-18 20:15:36 +00001698 || defined BB_MOUNT || defined BB_ROUTE
Matt Kraaie0bcce02000-09-27 02:29:39 +00001699FILE *xfopen(const char *path, const char *mode)
1700{
1701 FILE *fp;
1702 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001703 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001704 return fp;
1705}
1706#endif
1707
Matt Kraaif2cc2762001-02-01 19:21:20 +00001708static int applet_name_compare(const void *x, const void *y)
Eric Andersene5aef922000-11-17 18:25:26 +00001709{
Matt Kraaif2cc2762001-02-01 19:21:20 +00001710 const char *name = x;
1711 const struct BB_applet *applet = y;
Eric Andersene5aef922000-11-17 18:25:26 +00001712
Matt Kraaif2cc2762001-02-01 19:21:20 +00001713 return strcmp(name, applet->name);
1714}
1715
1716extern size_t NUM_APPLETS;
1717
1718struct BB_applet *find_applet_by_name(const char *name)
1719{
1720 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1721 applet_name_compare);
Eric Andersene5aef922000-11-17 18:25:26 +00001722}
1723
Eric Andersen67991cf2001-02-14 21:23:06 +00001724void run_applet_by_name(const char *name, int argc, char **argv)
1725{
1726 /* Do a binary search to find the applet entry given the name. */
1727 if ((applet_using = find_applet_by_name(name)) != NULL) {
1728 applet_name = applet_using->name;
1729 if (argv[1] && strcmp(argv[1], "--help") == 0)
1730 show_usage();
1731 exit((*(applet_using->main)) (argc, argv));
1732 }
1733}
1734
Eric Andersenca0f4582001-02-16 18:43:54 +00001735#if defined BB_DD || defined BB_TAIL || defined BB_STTY
Matt Kraaia164c642001-02-05 17:50:03 +00001736unsigned long parse_number(const char *numstr,
1737 const struct suffix_mult *suffixes)
Matt Kraai24ac0172000-12-18 21:38:57 +00001738{
Matt Kraaia164c642001-02-05 17:50:03 +00001739 const struct suffix_mult *sm;
Matt Kraai24ac0172000-12-18 21:38:57 +00001740 unsigned long int ret;
1741 int len;
1742 char *end;
1743
1744 ret = strtoul(numstr, &end, 10);
1745 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001746 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001747 while (end[0] != '\0') {
Eric Andersen12dceb12001-02-18 20:15:36 +00001748 sm = suffixes;
1749 while ( sm != 0 ) {
1750 if(sm->suffix) {
1751 len = strlen(sm->suffix);
1752 if (strncmp(sm->suffix, end, len) == 0) {
1753 ret *= sm->mult;
1754 end += len;
1755 break;
1756 }
1757 sm++;
1758
1759 } else
1760 sm = 0;
Matt Kraai24ac0172000-12-18 21:38:57 +00001761 }
Eric Andersen12dceb12001-02-18 20:15:36 +00001762 if (sm == 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001763 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001764 }
1765 return ret;
1766}
1767#endif
1768
Eric Andersen23a93f52001-01-24 23:47:21 +00001769#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001770ssize_t safe_read(int fd, void *buf, size_t count)
1771{
1772 ssize_t n;
1773
1774 do {
1775 n = read(fd, buf, count);
1776 } while (n < 0 && errno == EINTR);
1777
1778 return n;
1779}
1780#endif
1781
Richard June6d0921c2001-01-22 22:35:38 +00001782#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001783const char *format(unsigned long val, unsigned long hr)
Richard June6d0921c2001-01-22 22:35:38 +00001784{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001785 static const char strings[] = { '0', 0, 'k', 0, 'M', 0, 'G', 0 };
1786 static const char fmt[] = "%lu";
1787 static const char fmt_u[] = "%lu.%lu%s";
Richard June6d0921c2001-01-22 22:35:38 +00001788
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001789 static char str[10];
1790
1791 unsigned long frac __attribute__ ((unused)); /* 'may be uninitialized' warning is ok */
1792 const char *u;
1793 const char *f;
1794
1795#if 1
1796 if(val == 0) { /* This may be omitted to reduce size */
1797 return strings; /* at the cost of speed. */
1798 }
1799#endif
1800
1801 u = strings;
1802 f = fmt;
1803 if (hr) {
1804 val /= hr;
1805 } else {
1806 while ((val >= KILOBYTE) && (*u != 'G')) {
1807 f = fmt_u;
1808 u += 2;
1809 frac = (((val % KILOBYTE) * 10) + (KILOBYTE/2)) / KILOBYTE;
1810 val /= KILOBYTE;
1811 if (frac >= 10) { /* We need to round up here. */
1812 ++val;
1813 frac = 0;
1814 }
1815 }
1816 }
1817
1818 /* If f==fmt then 'frac' and 'u' are ignored and need not be set. */
1819 snprintf(str, sizeof(str), f, val, frac, u);
1820
1821 return str;
Richard June6d0921c2001-01-22 22:35:38 +00001822}
1823#endif
1824
Matt Kraai05e782d2001-02-01 16:49:30 +00001825#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS)
1826void chomp(char *s)
1827{
1828 size_t len = strlen(s);
1829
1830 if (len == 0)
1831 return;
1832
1833 if (s[len-1] == '\n')
1834 s[len-1] = '\0';
1835}
1836#endif
1837
Eric Andersencc8ed391999-10-05 16:24:54 +00001838/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001839/*
1840Local Variables:
1841c-file-style: "linux"
1842c-basic-offset: 4
1843tab-width: 4
1844End:
1845*/