blob: 89b601d04cf60bb2be414d5c3a8afcbca5bf4abc [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 Andersen3570a342000-09-25 21:45:58 +000028#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000029#if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000032 || defined (BB_INSMOD) \
Erik Andersenfac10d72000-02-07 05:29:42 +000033 || defined (BB_LS) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000034 || defined (BB_RM) \
35 || defined (BB_TAR)
Mark Whitleyf57c9442000-12-07 19:56:48 +000036/* same conditions as recursive_action */
Erik Andersenfac10d72000-02-07 05:29:42 +000037#define bb_need_name_too_long
38#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000039#define bb_need_memory_exhausted
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000040#define bb_need_full_version
Erik Andersenfac10d72000-02-07 05:29:42 +000041#define BB_DECLARE_EXTERN
42#include "messages.c"
Eric Andersen67991cf2001-02-14 21:23:06 +000043#include "usage.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000044
Eric Andersen2b69c401999-10-05 22:58:32 +000045#include <stdio.h>
46#include <string.h>
47#include <errno.h>
48#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000049#include <dirent.h>
50#include <time.h>
51#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000052#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000053#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000054#include <stdlib.h>
Eric Andersenfc83c4c2001-02-14 07:15:30 +000055#include <limits.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000056#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000057#include <sys/utsname.h> /* for uname(2) */
Eric Andersened3ef502001-01-27 08:24:39 +000058
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 Andersen67991cf2001-02-14 21:23:06 +000084 static const char no_help[] = "No help available.\n";
85
86 const char *usage_string = no_help;
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000087 int i;
Eric Andersen67991cf2001-02-14 21:23:06 +000088
89 if (applet_using->usage_index >= 0) {
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000090 usage_string = usage_messages;
91 for (i=applet_using->usage_index ; i>0 ; ) {
92 if (!*usage_string++) {
93 --i;
94 }
95 }
Eric Andersen67991cf2001-02-14 21:23:06 +000096 }
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000097 fprintf(stderr, "%s\n\nUsage: %s %s\n\n", full_version,
Eric Andersen67991cf2001-02-14 21:23:06 +000098 applet_using->name, usage_string);
Matt Kraaie0bcce02000-09-27 02:29:39 +000099 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +0000100}
101
Eric Andersen67991cf2001-02-14 21:23:06 +0000102
Mark Whitleyf57c9442000-12-07 19:56:48 +0000103static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000104{
105 fflush(stdout);
106 fprintf(stderr, "%s: ", applet_name);
107 vfprintf(stderr, s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000108}
109
Mark Whitleyf57c9442000-12-07 19:56:48 +0000110extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000111{
112 va_list p;
113
114 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000115 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000116 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000117 putc('\n', stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000118}
119
Mark Whitleyf57c9442000-12-07 19:56:48 +0000120extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000121{
122 va_list p;
123
124 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000125 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000126 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000127 putc('\n', stderr);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000128 exit(EXIT_FAILURE);
129}
130
Mark Whitleyf57c9442000-12-07 19:56:48 +0000131static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000132{
Eric Andersenc73a7f92001-01-20 21:41:11 +0000133 int err=errno;
134 if(s == 0) s = "";
135 verror_msg(s, p);
136 if (*s) s = ": ";
137 fprintf(stderr, "%s%s\n", s, strerror(err));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000138}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000139
Mark Whitleyf57c9442000-12-07 19:56:48 +0000140extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000141{
142 va_list p;
143
144 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000145 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000146 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000147}
148
Mark Whitleyf57c9442000-12-07 19:56:48 +0000149extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000150{
151 va_list p;
152
153 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000154 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000155 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000156 exit(EXIT_FAILURE);
157}
158
Eric Andersenf5d5e772001-01-24 23:34:48 +0000159#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000160/* Returns kernel version encoded as major*65536 + minor*256 + patch,
161 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000162 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000163 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000164extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000165{
Erik Andersend07ee462000-02-21 21:26:32 +0000166 struct utsname name;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000167 char *s;
168 int i, r;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000169
Erik Andersend07ee462000-02-21 21:26:32 +0000170 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000171 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 return (0);
173 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000174
175 s = name.release;
176 r = 0;
177 for (i=0 ; i<3 ; i++) {
178 r = r * 256 + atoi(strtok(s, "."));
179 s = NULL;
180 }
181 return r;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000182}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000183#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000184
Eric Andersen10dc9d42000-06-26 10:45:52 +0000185
186
187#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000188_syscall1(int, sysinfo, struct sysinfo *, info);
189#endif /* BB_INIT */
190
Eric Andersena57ba4d2000-07-08 19:20:49 +0000191#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000192
193#ifndef __NR_umount2
Mark Whitley59ab0252001-01-23 22:30:04 +0000194static const int __NR_umount2 = 52;
Eric Andersena57ba4d2000-07-08 19:20:49 +0000195#endif
196
197/* Include our own version of <sys/mount.h>, since libc5 doesn't
198 * know about umount2 */
199extern _syscall1(int, umount, const char *, special_file);
200extern _syscall2(int, umount2, const char *, special_file, int, flags);
201extern _syscall5(int, mount, const char *, special_file, const char *, dir,
202 const char *, fstype, unsigned long int, rwflag, const void *, data);
203#endif
204
Eric Andersenc86e5e62000-08-21 19:38:49 +0000205#if defined BB_INSMOD || defined BB_LSMOD
206#ifndef __NR_query_module
Mark Whitley59ab0252001-01-23 22:30:04 +0000207static const int __NR_query_module = 167;
Eric Andersenc86e5e62000-08-21 19:38:49 +0000208#endif
209_syscall5(int, query_module, const char *, name, int, which,
210 void *, buf, size_t, bufsize, size_t*, ret);
211#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000212
213
Erik Andersen23dea9b2000-05-13 00:28:25 +0000214#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000215
Erik Andersen029011b2000-03-04 21:19:32 +0000216#define HASH_SIZE 311 /* Should be prime */
217#define hash_inode(i) ((i) % HASH_SIZE)
218
219static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
220
221/*
222 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
223 * `ino_dev_hashtable', else return 0
224 *
225 * If NAME is a non-NULL pointer to a character pointer, and there is
226 * a match, then set *NAME to the value of the name slot in that
227 * bucket.
228 */
229int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
230{
231 ino_dev_hashtable_bucket_t *bucket;
232
233 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
234 while (bucket != NULL) {
235 if ((bucket->ino == statbuf->st_ino) &&
236 (bucket->dev == statbuf->st_dev))
237 {
238 if (name) *name = bucket->name;
239 return 1;
240 }
241 bucket = bucket->next;
242 }
243 return 0;
244}
245
246/* Add statbuf to statbuf hash table */
247void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
248{
249 int i;
250 size_t s;
251 ino_dev_hashtable_bucket_t *bucket;
252
253 i = hash_inode(statbuf->st_ino);
254 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000255 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000256 bucket->ino = statbuf->st_ino;
257 bucket->dev = statbuf->st_dev;
258 if (name)
259 strcpy(bucket->name, name);
260 else
261 bucket->name[0] = '\0';
262 bucket->next = ino_dev_hashtable[i];
263 ino_dev_hashtable[i] = bucket;
264}
265
266/* Clear statbuf hash table */
267void reset_ino_dev_hashtable(void)
268{
269 int i;
270 ino_dev_hashtable_bucket_t *bucket;
271
272 for (i = 0; i < HASH_SIZE; i++) {
273 while (ino_dev_hashtable[i] != NULL) {
274 bucket = ino_dev_hashtable[i]->next;
275 free(ino_dev_hashtable[i]);
276 ino_dev_hashtable[i] = bucket;
277 }
278 }
279}
280
Erik Andersen23dea9b2000-05-13 00:28:25 +0000281#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000282
Glenn L McGrath5e23e3d2001-01-03 01:38:29 +0000283#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000284/*
285 * Return TRUE if a fileName is a directory.
286 * Nonexistant files return FALSE.
287 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000288int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000289{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000291 int didMalloc = 0;
292
293 if (statBuf == NULL) {
294 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
295 ++didMalloc;
296 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000297
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000299 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000300 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000301 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000302
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000303 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
304 status = FALSE;
305 }
306 else status = TRUE;
307
308 if (didMalloc) {
309 free(statBuf);
310 statBuf = NULL;
311 }
312 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000313}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000314#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000315
Eric Andersen70c60862001-01-25 00:00:04 +0000316#if defined BB_AR || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000317/*
Mark Whitley47583682000-12-05 20:03:17 +0000318 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000319 */
Mark Whitley47583682000-12-05 20:03:17 +0000320int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000321{
322 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000323 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000324
Mark Whitley47583682000-12-05 20:03:17 +0000325 while (chunksize > 0) {
326 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000327 size = BUFSIZ;
328 else
Mark Whitley47583682000-12-05 20:03:17 +0000329 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000330 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000331 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000332 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000333 }
334 return (TRUE);
335}
336#endif
337
338
Glenn L McGrathde556612001-02-10 14:53:44 +0000339#if defined (BB_CP_MV) || defined BB_DPKG
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000340/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000341 * Copy one file to another, while possibly preserving its modes, times, and
342 * modes. Returns TRUE if successful, or FALSE on a failure with an error
343 * message output. (Failure is not indicated if attributes cannot be set.)
344 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000345 */
346int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000347copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000348 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000349{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 int rfd;
351 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 struct stat srcStatBuf;
354 struct stat dstStatBuf;
355 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000356
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 if (followLinks == TRUE)
358 status = stat(srcName, &srcStatBuf);
359 else
360 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000361
Erik Andersenfac10d72000-02-07 05:29:42 +0000362 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000363 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000365 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366
367 if (followLinks == TRUE)
368 status = stat(destName, &dstStatBuf);
369 else
370 status = lstat(destName, &dstStatBuf);
371
Erik Andersen59b9e872000-05-10 05:05:45 +0000372 if (status < 0 || forceFlag==TRUE) {
373 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 dstStatBuf.st_ino = -1;
375 dstStatBuf.st_dev = -1;
376 }
377
378 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
379 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000380 error_msg("Copying file \"%s\" to itself", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 return FALSE;
382 }
383
384 if (S_ISDIR(srcStatBuf.st_mode)) {
385 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
386 /* Make sure the directory is writable */
387 status = mkdir(destName, 0777777 ^ umask(0));
388 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000389 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 return FALSE;
391 }
392 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000393 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 int link_size;
395
396 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000397 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
398 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 if (link_size < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000400 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 return FALSE;
402 }
403 link_val[link_size] = '\0';
404 status = symlink(link_val, destName);
405 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000406 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 return FALSE;
408 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000409#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000411 /* Try to set owner, but fail silently like GNU cp */
412 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000414#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415 return TRUE;
416 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
417 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
418 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000419 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 return FALSE;
421 }
422 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
423 || S_ISSOCK(srcStatBuf.st_mode)) {
424 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
425 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000426 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 return FALSE;
428 }
429 } else if (S_ISREG(srcStatBuf.st_mode)) {
430 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
431 rfd = open(srcName, O_RDONLY);
432 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000433 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 return FALSE;
435 }
436
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000437 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 srcStatBuf.st_mode);
439 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000440 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 close(rfd);
442 return FALSE;
443 }
444
Mark Whitley47583682000-12-05 20:03:17 +0000445 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000446 goto error_exit;
447
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 close(rfd);
449 if (close(wfd) < 0) {
450 return FALSE;
451 }
452 }
453
454 if (setModes == TRUE) {
455 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000456 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
457 perror_msg_and_die("%s", destName);
458 if (chmod(destName, srcStatBuf.st_mode) < 0)
459 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000460 times.actime = srcStatBuf.st_atime;
461 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000462 if (utime(destName, &times) < 0)
463 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 }
465
Erik Andersenfac10d72000-02-07 05:29:42 +0000466 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000467
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000469 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000470 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000472
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000474}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000475#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000476
477
478
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000479#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000480
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000481#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
482#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
483
484/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
485static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 0, 0, S_ISUID,
487 0, 0, S_ISGID,
488 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000489};
490
491/* The 9 mode bits to test */
492static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 S_IRUSR, S_IWUSR, S_IXUSR,
494 S_IRGRP, S_IWGRP, S_IXGRP,
495 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000496};
497
Mark Whitley26a3adb2000-12-05 20:10:27 +0000498static const char MODE1[] = "rwxrwxrwx";
499static const char MODE0[] = "---------";
500static const char SMODE1[] = "..s..s..t";
501static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000502
Eric Andersencc8ed391999-10-05 16:24:54 +0000503/*
504 * Return the standard ls-like mode string from a file mode.
505 * This is static and so is overwritten on each call.
506 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000507const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000508{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000510
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 int i;
512
513 buf[0] = TYPECHAR(mode);
514 for (i = 0; i < 9; i++) {
515 if (mode & SBIT[i])
516 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
517 else
518 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
519 }
520 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000521}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000522#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000523
524
Pavel Roskin21ddb382000-07-17 19:31:54 +0000525#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000526/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000527 * Return the standard ls-like time string from a time_t
528 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000529 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000530const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000531{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532 time_t now;
533 char *str;
534 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000535
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000537
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000539
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 strcpy(buf, &str[4]);
541 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000542
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
544 strcpy(&buf[7], &str[20]);
545 buf[11] = '\0';
546 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000549}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000550#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000551
Eric Andersen70c60862001-01-25 00:00:04 +0000552#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 +0000553/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000554 * Write all of the supplied buffer out to a file.
555 * This does multiple writes as necessary.
556 * Returns the amount written, or -1 on an error.
557 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000558int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000559{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 int cc;
561 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000562
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000564
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 while (len > 0) {
566 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000567
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 if (cc < 0)
569 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000570
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 buf += cc;
572 total += cc;
573 len -= cc;
574 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000575
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000577}
Matt Kraai24ac0172000-12-18 21:38:57 +0000578#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000579
Matt Kraai24ac0172000-12-18 21:38:57 +0000580#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000581/*
582 * Read all of the supplied buffer from a file.
583 * This does multiple reads as necessary.
584 * Returns the amount read, or -1 on an error.
585 * A short read is returned on an end of file.
586 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000587int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000588{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 int cc;
590 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000591
Erik Andersene49d5ec2000-02-08 19:58:47 +0000592 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000593
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 while (len > 0) {
595 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000596
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 if (cc < 0)
598 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000599
Erik Andersene49d5ec2000-02-08 19:58:47 +0000600 if (cc == 0)
601 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000602
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 buf += cc;
604 total += cc;
605 len -= cc;
606 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000607
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000609}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000610#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000611
612
Erik Andersen23dea9b2000-05-13 00:28:25 +0000613#if defined (BB_CHMOD_CHOWN_CHGRP) \
614 || defined (BB_CP_MV) \
615 || defined (BB_FIND) \
616 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000617 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000618 || defined (BB_RM) \
619 || defined (BB_TAR)
620
Eric Andersencc8ed391999-10-05 16:24:54 +0000621/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000622 * Walk down all the directories under the specified
623 * location, and do something (something specified
624 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000625 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000626 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000627 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000628 * and so isn't sufficiently portable to take over since glibc2.1
629 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000630 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000631int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632 int recurse, int followLinks, int depthFirst,
633 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000634 struct stat * statbuf,
635 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000637 struct stat * statbuf,
638 void* userData),
639 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000640{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641 int status;
642 struct stat statbuf;
643 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000644
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645 if (followLinks == TRUE)
646 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000647 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000649
Eric Andersen2b69c401999-10-05 22:58:32 +0000650 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651#ifdef BB_DEBUG_PRINT_SCAFFOLD
652 fprintf(stderr,
653 "status=%d followLinks=%d TRUE=%d\n",
654 status, followLinks, TRUE);
655#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000656 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000657 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000658 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659
660 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
661 if (fileAction == NULL)
662 return TRUE;
663 else
Erik Andersen3364d782000-03-28 00:58:14 +0000664 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 }
666
667 if (recurse == FALSE) {
668 if (S_ISDIR(statbuf.st_mode)) {
669 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000670 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000671 else
672 return TRUE;
673 }
674 }
675
676 if (S_ISDIR(statbuf.st_mode)) {
677 DIR *dir;
678
Erik Andersene49d5ec2000-02-08 19:58:47 +0000679 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000680 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000681 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000682 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000683 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000684 } else if (status == SKIP)
685 return TRUE;
686 }
687 dir = opendir(fileName);
688 if (!dir) {
689 perror_msg("%s", fileName);
690 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 }
692 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000693 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000694
695 if ((strcmp(next->d_name, "..") == 0)
696 || (strcmp(next->d_name, ".") == 0)) {
697 continue;
698 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000699 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000700 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000701 return FALSE;
702 }
Erik Andersenc3660502000-04-18 21:18:57 +0000703 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000704 sprintf(nextFile, "%s/%s", fileName, next->d_name);
705 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000706 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000707 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000708 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 closedir(dir);
710 return FALSE;
711 }
712 }
713 status = closedir(dir);
714 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000715 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 return FALSE;
717 }
718 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000719 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000721 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 return FALSE;
723 }
724 }
725 } else {
726 if (fileAction == NULL)
727 return TRUE;
728 else
Erik Andersen3364d782000-03-28 00:58:14 +0000729 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730 }
731 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000732}
733
Erik Andersen23dea9b2000-05-13 00:28:25 +0000734#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000735
Eric Andersenf6be9441999-10-13 21:12:06 +0000736
737
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000738#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000739/*
740 * Attempt to create the directories along the specified path, except for
741 * the final component. The mode is given for the final directory only,
742 * while all previous ones get default protections. Errors are not reported
743 * here, as failures to restore files can be reported later.
744 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000745extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000746{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 char *cp;
748 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000749 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000750 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000751
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 strcpy(buf, name);
753 for (cp = buf; *cp == '/'; cp++);
754 cp = strchr(cp, '/');
755 while (cp) {
756 cpOld = cp;
757 cp = strchr(cp + 1, '/');
758 *cpOld = '\0';
759 retVal = mkdir(buf, cp ? 0777 : mode);
760 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000761 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 return FALSE;
763 }
764 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000765 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000767}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000768#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000769
770
771
Pavel Roskin21ddb382000-07-17 19:31:54 +0000772#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000773 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000774/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000775
Erik Andersene49d5ec2000-02-08 19:58:47 +0000776extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000777{
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000778 static const mode_t group_set[] = {
779 S_ISUID | S_IRWXU, /* u */
780 S_ISGID | S_IRWXG, /* g */
781 S_IRWXO, /* o */
782 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
783 };
Erik Andersene49d5ec2000-02-08 19:58:47 +0000784
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000785 static const mode_t mode_set[] = {
786 S_IRUSR | S_IRGRP | S_IROTH, /* r */
787 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
788 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
789 S_ISUID | S_ISGID, /* s */
790 S_ISVTX /* t */
791 };
792
793 static const char group_string[] = "ugoa";
794 static const char mode_string[] = "rwxst";
795
796 const char *p;
797
798 mode_t andMode =
Erik Andersene49d5ec2000-02-08 19:58:47 +0000799 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000800 mode_t orMode = 0;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000801 mode_t mode;
802 mode_t groups;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000803 char type;
804 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000805
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000806 if (s==NULL) {
Eric Andersen4e61db22000-09-01 00:06:17 +0000807 return (FALSE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000808 }
Eric Andersen4e61db22000-09-01 00:06:17 +0000809
Eric Andersenf6be9441999-10-13 21:12:06 +0000810 do {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000811 mode = 0;
812 groups = 0;
813 NEXT_GROUP:
814 if ((c = *s++) == '\0') {
815 return -1;
816 }
817 for (p=group_string ; *p ; p++) {
818 if (*p == c) {
819 groups |= group_set[(int)(p-group_string)];
820 goto NEXT_GROUP;
821 }
822 }
823 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000824 case '=':
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000825 case '+':
Eric Andersenf6be9441999-10-13 21:12:06 +0000826 case '-':
827 type = c;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000828 if (groups == 0) { /* The default is "all" */
829 groups |= S_ISUID | S_ISGID | S_ISVTX
830 | S_IRWXU | S_IRWXG | S_IRWXO;
831 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000832 break;
833 default:
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000834 if ((c < '0') || (c > '7') || (mode | groups)) {
835 return (FALSE);
836 } else {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000837 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000838 return (TRUE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000839 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000840 }
841
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000842 NEXT_MODE:
843 if (((c = *s++) != '\0') && (c != ',')) {
844 for (p=mode_string ; *p ; p++) {
845 if (*p == c) {
846 mode |= mode_set[(int)(p-mode_string)];
847 goto NEXT_MODE;
848 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000849 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000850 break; /* We're done so break out of loop.*/
Eric Andersenf6be9441999-10-13 21:12:06 +0000851 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 switch (type) {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000853 case '=':
854 andMode &= ~(groups); /* Now fall through. */
855 case '+':
856 orMode |= mode & groups;
857 break;
858 case '-':
859 andMode &= ~(mode & groups);
860 orMode &= ~(mode & groups);
861 break;
Eric Andersenf6be9441999-10-13 21:12:06 +0000862 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 } while (c == ',');
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000864
Eric Andersence8f3b91999-10-20 07:03:36 +0000865 *theMode &= andMode;
866 *theMode |= orMode;
Eric Andersence8f3b91999-10-20 07:03:36 +0000867
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000868 return TRUE;
869}
Eric Andersence8f3b91999-10-20 07:03:36 +0000870
Pavel Roskin21ddb382000-07-17 19:31:54 +0000871#endif
872/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000873
Eric Andersene674eb71999-10-19 20:52:57 +0000874
875
Erik Andersen23dea9b2000-05-13 00:28:25 +0000876
877
Pavel Roskin21ddb382000-07-17 19:31:54 +0000878#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
879 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000880 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000881/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000882long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000883{
Eric Andersenab050f52001-01-27 06:01:43 +0000884 struct passwd *myuser;
885
886 myuser = getpwnam(name);
887 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000888 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000889
890 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000891}
892
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000893/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000894long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000895{
Eric Andersenab050f52001-01-27 06:01:43 +0000896 struct group *mygroup;
897
898 mygroup = getgrnam(name);
899 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000900 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000901
902 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000903}
904
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000905/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000906void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000907{
Eric Andersenab050f52001-01-27 06:01:43 +0000908 struct passwd *myuser;
909
910 myuser = getpwuid(uid);
911 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000912 sprintf(name, "%-8ld ", (long)uid);
913 else
914 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000915}
916
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000917/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000918void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000919{
Eric Andersenab050f52001-01-27 06:01:43 +0000920 struct group *mygroup;
921
922 mygroup = getgrgid(gid);
923 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000924 sprintf(group, "%-8ld ", (long)gid);
925 else
926 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000927}
928
Matt Kraaie6e81832000-12-30 07:46:23 +0000929#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000930/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000931long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000932{
Eric Andersenab050f52001-01-27 06:01:43 +0000933 struct group *mygroup;
934 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000935
Eric Andersenab050f52001-01-27 06:01:43 +0000936 myuser=getpwnam(name);
937 if (myuser==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000938 error_msg_and_die( "unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000939
940 mygroup = getgrgid(myuser->pw_gid);
941 if (mygroup==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000942 error_msg_and_die( "unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000943
944 return mygroup->gr_gid;
945}
946#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000947#endif
948 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
949 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000950
951
Eric Andersenf86aee72000-07-06 17:20:46 +0000952#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000953
Eric Andersenbd22ed82000-07-08 18:55:24 +0000954/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000955static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
956static const int KB_84 = 0x01;
957static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000958
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000960{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961 char arg;
962
963 arg = 0;
964 return (ioctl(fd, KDGKBTYPE, &arg) == 0
965 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000966}
967
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000969{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000970 int fd;
971
972 /* try read-only */
973 fd = open(fnam, O_RDWR);
974
975 /* if failed, try read-only */
976 if (fd < 0 && errno == EACCES)
977 fd = open(fnam, O_RDONLY);
978
979 /* if failed, try write-only */
980 if (fd < 0 && errno == EACCES)
981 fd = open(fnam, O_WRONLY);
982
983 /* if failed, fail */
984 if (fd < 0)
985 return -1;
986
987 /* if not a console, fail */
988 if (!is_a_console(fd)) {
989 close(fd);
990 return -1;
991 }
992
993 /* success */
994 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000995}
996
997/*
998 * Get an fd for use with kbd/console ioctls.
999 * We try several things because opening /dev/console will fail
1000 * if someone else used X (which does a chown on /dev/console).
1001 *
1002 * if tty_name is non-NULL, try this one instead.
1003 */
1004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001006{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001008
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 if (tty_name) {
1010 if (-1 == (fd = open_a_console(tty_name)))
1011 return -1;
1012 else
1013 return fd;
1014 }
1015
1016 fd = open_a_console("/dev/tty");
1017 if (fd >= 0)
1018 return fd;
1019
1020 fd = open_a_console("/dev/tty0");
1021 if (fd >= 0)
1022 return fd;
1023
1024 fd = open_a_console("/dev/console");
1025 if (fd >= 0)
1026 return fd;
1027
1028 for (fd = 0; fd < 3; fd++)
1029 if (is_a_console(fd))
1030 return fd;
1031
Matt Kraaidd19c692001-01-31 19:00:21 +00001032 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001034}
1035
1036
Eric Andersenf86aee72000-07-06 17:20:46 +00001037#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001038
1039
Eric Andersen9f16d612000-06-12 23:11:16 +00001040#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001041/*
1042 * Routine to see if a text string is matched by a wildcard pattern.
1043 * Returns TRUE if the text is matched, or FALSE if it is not matched
1044 * or if the pattern is invalid.
1045 * * matches zero or more characters
1046 * ? matches a single character
1047 * [abc] matches 'a', 'b' or 'c'
1048 * \c quotes character c
1049 * Adapted from code written by Ingo Wilken, and
1050 * then taken from sash, Copyright (c) 1999 by David I. Bell
1051 * Permission is granted to use, distribute, or modify this source,
1052 * provided that this copyright notice remains intact.
1053 * Permission to distribute this code under the GPL has been granted.
1054 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001056{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057 const char *retryPat;
1058 const char *retryText;
1059 int ch;
1060 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001061 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001062
Erik Andersene49d5ec2000-02-08 19:58:47 +00001063 retryPat = NULL;
1064 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001065
Erik Andersene49d5ec2000-02-08 19:58:47 +00001066 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001067 ch = *pattern++;
1068
Erik Andersene49d5ec2000-02-08 19:58:47 +00001069 switch (ch) {
1070 case '*':
1071 retryPat = pattern;
1072 retryText = text;
1073 break;
1074
1075 case '[':
1076 found = FALSE;
1077
1078 while ((ch = *pattern++) != ']') {
1079 if (ch == '\\')
1080 ch = *pattern++;
1081
1082 if (ch == '\0')
1083 return FALSE;
1084
1085 if (*text == ch)
1086 found = TRUE;
1087 }
Erik Andersen632bb572000-04-18 22:09:06 +00001088 len=strlen(text);
1089 if (found == FALSE && len!=0) {
1090 return FALSE;
1091 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001093 if (strlen(pattern)==0 && len==1) {
1094 return TRUE;
1095 }
1096 if (len!=0) {
1097 text++;
1098 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001099 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001100 }
1101
1102 /* fall into next case */
1103
1104 case '?':
1105 if (*text++ == '\0')
1106 return FALSE;
1107
1108 break;
1109
1110 case '\\':
1111 ch = *pattern++;
1112
1113 if (ch == '\0')
1114 return FALSE;
1115
1116 /* fall into next case */
1117
1118 default:
1119 if (*text == ch) {
1120 if (*text)
1121 text++;
1122 break;
1123 }
1124
1125 if (*text) {
1126 pattern = retryPat;
1127 text = ++retryText;
1128 break;
1129 }
1130
Eric Andersenb186d981999-12-03 09:19:54 +00001131 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001132 }
1133
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 if (pattern == NULL)
1135 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001136 }
1137
Erik Andersene49d5ec2000-02-08 19:58:47 +00001138 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001139}
Eric Andersen9f16d612000-06-12 23:11:16 +00001140#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001141
1142
Eric Andersen29d2e361999-11-06 06:07:27 +00001143
1144
Erik Andersen23dea9b2000-05-13 00:28:25 +00001145#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001146#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001147/*
1148 * Given a block device, find the mount table entry if that block device
1149 * is mounted.
1150 *
1151 * Given any other file (or directory), find the mount table entry for its
1152 * filesystem.
1153 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001154extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001155{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 struct stat s;
1157 dev_t mountDevice;
1158 FILE *mountTable;
1159 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001160
Erik Andersene49d5ec2000-02-08 19:58:47 +00001161 if (stat(name, &s) != 0)
1162 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001163
Erik Andersene49d5ec2000-02-08 19:58:47 +00001164 if ((s.st_mode & S_IFMT) == S_IFBLK)
1165 mountDevice = s.st_rdev;
1166 else
1167 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001168
1169
Erik Andersene49d5ec2000-02-08 19:58:47 +00001170 if ((mountTable = setmntent(table, "r")) == 0)
1171 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001172
Erik Andersene49d5ec2000-02-08 19:58:47 +00001173 while ((mountEntry = getmntent(mountTable)) != 0) {
1174 if (strcmp(name, mountEntry->mnt_dir) == 0
1175 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1176 break;
1177 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1178 break;
1179 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1180 break;
1181 }
1182 endmntent(mountTable);
1183 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001184}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001185#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001186
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001187#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001188/* try to open up the specified device */
1189extern int device_open(char *device, int mode)
1190{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001192
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001194
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 /* Retry up to 5 times */
1196 for (f = 0; f < 5; f++)
1197 if ((fd = open(device, m, 0600)) >= 0)
1198 break;
1199 if (fd < 0)
1200 return fd;
1201 /* Reset original flags. */
1202 if (m != mode)
1203 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001204 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001205}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001206#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001207
1208
Erik Andersen23dea9b2000-05-13 00:28:25 +00001209#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001210#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001211#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001212#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001213
Erik Andersen23dea9b2000-05-13 00:28:25 +00001214#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001215/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001216 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001217 * This finds the pid of the specified process,
1218 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001219 *
Erik Andersen825aead2000-04-07 06:00:07 +00001220 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001221 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001222extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001223{
Erik Andersen825aead2000-04-07 06:00:07 +00001224 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001225 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001226 pid_t num_pids;
1227 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001228 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001229
1230 /* open device */
1231 fd = open(device, O_RDONLY);
1232 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001233 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001234
1235 /* Find out how many processes there are */
1236 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001237 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001238
1239 /* Allocate some memory -- grab a few extras just in case
1240 * some new processes start up while we wait. The kernel will
1241 * just ignore any extras if we give it too many, and will trunc.
1242 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001243 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001244 pid_array[0] = num_pids+10;
1245
1246 /* Now grab the pid list */
1247 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001248 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001249
1250 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001251 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001252 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001253 struct pid_info info;
1254
1255 info.pid = pid_array[i];
1256 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001257 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001258
Erik Andersen0a64de92000-04-18 20:00:03 +00001259 /* Make sure we only match on the process name */
1260 p=info.command_line+1;
1261 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1262 (p)++;
1263 }
1264 if (isspace(*(p)))
1265 *p='\0';
1266
Erik Andersenb9167cb2000-04-18 22:41:30 +00001267 if ((strstr(info.command_line, pidName) != NULL)
1268 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001269 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001270 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001271 }
1272 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001273 if (pidList)
1274 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001275
1276 /* Free memory */
1277 free( pid_array);
1278
1279 /* close device */
1280 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001281 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001282
Erik Andersen825aead2000-04-07 06:00:07 +00001283 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001284}
Erik Andersen269bba22000-03-08 14:50:47 +00001285#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001286
Mark Whitleyf57c9442000-12-07 19:56:48 +00001287/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001288 *
1289 * This finds the pid of the specified process.
1290 * Currently, it's implemented by rummaging through
1291 * the proc filesystem.
1292 *
Erik Andersen825aead2000-04-07 06:00:07 +00001293 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001294 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001295extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001296{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001297 DIR *dir;
1298 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001299 pid_t* pidList=NULL;
1300 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001301
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001302 dir = opendir("/proc");
1303 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001304 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001305
1306 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001307 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001308 char filename[256];
1309 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001310
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001311 /* If it isn't a number, we don't want it */
1312 if (!isdigit(*next->d_name))
1313 continue;
1314
Eric Andersen31e68292000-09-14 21:12:20 +00001315 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001316 status = fopen(filename, "r");
1317 if (!status) {
1318 continue;
1319 }
1320 fgets(buffer, 256, status);
1321 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001322
Eric Andersen31e68292000-09-14 21:12:20 +00001323 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001324 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001325 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001326 }
John Beppuf95ca971999-12-09 22:10:18 +00001327 }
Eric Andersen31e68292000-09-14 21:12:20 +00001328
Erik Andersen0a64de92000-04-18 20:00:03 +00001329 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001330 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001331 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001332}
Erik Andersen269bba22000-03-08 14:50:47 +00001333#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001334#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001335
Eric Andersenae6eae02000-07-19 17:35:54 +00001336#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001337/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001338extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001339{
Eric Andersenae6eae02000-07-19 17:35:54 +00001340 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001341
Eric Andersenae6eae02000-07-19 17:35:54 +00001342 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001343 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001344 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001345}
1346
Eric Andersen501c88b2000-07-28 15:14:45 +00001347extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001348{
1349 void *ptr = realloc(old, size);
1350 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001351 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001352 return ptr;
1353}
Eric Andersen501c88b2000-07-28 15:14:45 +00001354
1355extern void *xcalloc(size_t nmemb, size_t size)
1356{
1357 void *ptr = calloc(nmemb, size);
1358 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001359 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001360 return ptr;
1361}
Eric Andersenae6eae02000-07-19 17:35:54 +00001362#endif
1363
Eric Andersen15f29e72001-01-25 00:53:46 +00001364#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET || \
1365 defined BB_DPKG_DEB || defined BB_TAR
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
1390 t = xmalloc(n+1);
1391 strncpy(t,s,n);
1392 t[n] = 0;
1393
1394 return t;
1395}
1396#endif
1397
Eric Andersenec455952001-02-14 08:11:27 +00001398#if defined BB_IFCONFIG || defined BB_ROUTE
1399/* Like strncpy but make sure the resulting string is always 0 terminated. */
1400extern char * safe_strncpy(char *dst, const char *src, size_t size)
1401{
1402 dst[size-1] = '\0';
1403 return strncpy(dst, src, size-1);
1404}
1405#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001406
Erik Andersen23dea9b2000-05-13 00:28:25 +00001407#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001408extern int vdprintf(int d, const char *format, va_list ap)
1409{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001410 char buf[BUF_SIZE];
1411 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001412
Erik Andersene49d5ec2000-02-08 19:58:47 +00001413 len = vsprintf(buf, format, ap);
1414 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001415}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001416#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001417
Eric Andersen624cc772000-09-21 02:04:51 +00001418
Erik Andersen23dea9b2000-05-13 00:28:25 +00001419#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001420#include <fcntl.h>
1421#include "loop.h" /* Pull in loop device support */
1422
Erik Andersen5cbdd712000-01-26 20:06:48 +00001423extern int del_loop(const char *device)
1424{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001425 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001426
Erik Andersene49d5ec2000-02-08 19:58:47 +00001427 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001428 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001429 return (FALSE);
1430 }
1431 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001432 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001433 return (FALSE);
1434 }
1435 close(fd);
1436 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001437}
Erik Andersende7965c2000-01-26 23:49:21 +00001438
Erik Andersene49d5ec2000-02-08 19:58:47 +00001439extern int set_loop(const char *device, const char *file, int offset,
1440 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001441{
1442 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001443 int fd, ffd, mode;
1444
Erik Andersende7965c2000-01-26 23:49:21 +00001445 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001446 if ((ffd = open(file, mode)) < 0 && !*loopro
1447 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001448 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001450 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001451 if ((fd = open(device, mode)) < 0) {
1452 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001453 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001455 }
1456 *loopro = (mode == O_RDONLY);
1457
1458 memset(&loopinfo, 0, sizeof(loopinfo));
1459 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001460 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001461
1462 loopinfo.lo_offset = offset;
1463
1464 loopinfo.lo_encrypt_key_size = 0;
1465 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001466 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001467 close(fd);
1468 close(ffd);
1469 return 1;
1470 }
1471 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1472 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001473 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001474 close(fd);
1475 close(ffd);
1476 return 1;
1477 }
1478 close(fd);
1479 close(ffd);
1480 return 0;
1481}
1482
Erik Andersene49d5ec2000-02-08 19:58:47 +00001483extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001484{
1485 char dev[20];
1486 int i, fd;
1487 struct stat statbuf;
1488 struct loop_info loopinfo;
1489
Erik Andersene49d5ec2000-02-08 19:58:47 +00001490 for (i = 0; i <= 7; i++) {
1491 sprintf(dev, "/dev/loop%d", i);
1492 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1493 if ((fd = open(dev, O_RDONLY)) >= 0) {
1494 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1495 if (errno == ENXIO) { /* probably free */
1496 close(fd);
1497 return strdup(dev);
1498 }
1499 }
1500 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001501 }
Erik Andersende7965c2000-01-26 23:49:21 +00001502 }
Erik Andersende7965c2000-01-26 23:49:21 +00001503 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001504 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001505}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001506#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001507
Erik Andersen23dea9b2000-05-13 00:28:25 +00001508#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001509extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001510{
Erik Andersen016ffe92000-03-22 00:58:54 +00001511 DIR *dir;
1512 struct dirent *entry;
1513 struct stat statBuf, rootStat;
1514 char fileName[BUFSIZ];
1515
Erik Andersenec5bd902000-03-22 07:12:05 +00001516 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001517 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001518 return( FALSE);
1519 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001520
Erik Andersenec5bd902000-03-22 07:12:05 +00001521 dir = opendir("/dev");
1522 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001523 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001524 return( FALSE);
1525 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001526
1527 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001528
Erik Andersen016ffe92000-03-22 00:58:54 +00001529 /* Must skip ".." since that is "/", and so we
1530 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001531 if (strcmp(entry->d_name, "..") == 0)
1532 continue;
1533
Eric Andersenf9ca6532000-08-11 17:45:02 +00001534 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001535
1536 if (stat(fileName, &statBuf) != 0)
1537 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001538 /* Some char devices have the same dev_t as block
1539 * devices, so make sure this is a block device */
1540 if (! S_ISBLK(statBuf.st_mode))
1541 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001542 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001543 strcpy(name, fileName);
1544 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001545 }
1546 }
1547
Erik Andersenec5bd902000-03-22 07:12:05 +00001548 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001549}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001550#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001551
Mark Whitley1ca41772000-06-28 22:15:26 +00001552
1553/* get_line_from_file() - This function reads an entire line from a text file
1554 * up to a newline. It returns a malloc'ed char * which must be stored and
1555 * free'ed by the caller. */
1556extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001557{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001558 static const int GROWBY = 80; /* how large we will grow strings by */
1559
Mark Whitley1ca41772000-06-28 22:15:26 +00001560 int ch;
1561 int idx = 0;
1562 char *linebuf = NULL;
1563 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001564
Mark Whitley1ca41772000-06-28 22:15:26 +00001565 while (1) {
1566 ch = fgetc(file);
1567 if (ch == EOF)
1568 break;
1569 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001570 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001571 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001572 linebuf[idx++] = (char)ch;
1573 if ((char)ch == '\n')
1574 break;
1575 }
John Beppu5a728cf2000-04-17 04:22:09 +00001576
Mark Whitley1ca41772000-06-28 22:15:26 +00001577 if (idx == 0)
1578 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001579
Mark Whitley1ca41772000-06-28 22:15:26 +00001580 linebuf[idx] = 0;
1581 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001582}
Erik Andersen016ffe92000-03-22 00:58:54 +00001583
Eric Andersen944be902000-09-25 16:58:39 +00001584#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001585extern void print_file(FILE *file)
1586{
1587 int c;
1588
1589 while ((c = getc(file)) != EOF)
1590 putc(c, stdout);
1591 fclose(file);
1592 fflush(stdout);
1593}
1594
1595extern int print_file_by_name(char *filename)
1596{
1597 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001598 if ((file = wfopen(filename, "r")) == NULL)
1599 return FALSE;
1600 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001601 return TRUE;
1602}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001603#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001604
Matt Kraaicabafae2000-11-30 04:55:40 +00001605#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001606char process_escape_sequence(char **ptr)
1607{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001608 static const char charmap[] = {
1609 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1610 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001611
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001612 const char *p;
1613 char *q;
1614 int num_digits;
1615 unsigned int n;
1616
1617 n = 0;
1618 q = *ptr;
1619
1620 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1621 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1622 break;
1623 }
1624 n = n * 8 + (*q++ - '0');
1625 }
1626
1627 if (num_digits == 0) { /* mnemonic escape sequence? */
1628 for (p=charmap ; *p ; p++) {
1629 if (*p == *q) {
1630 q++;
1631 break;
1632 }
1633 }
1634 n = *(p+(sizeof(charmap)/2));
1635 }
1636
1637 /* doesn't hurt to fall through to here from mnemonic case */
1638 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1639 n /= 8; /* adjust value and */
1640 --q; /* back up one char */
1641 }
1642
1643 *ptr = q;
1644 return (char) n;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001645}
1646#endif
1647
Eric Andersen70c60862001-01-25 00:00:04 +00001648#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001649 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001650char *get_last_path_component(char *path)
1651{
1652 char *s=path+strlen(path)-1;
1653
1654 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001655 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001656 *s-- = '\0';
1657 }
1658
1659 /* find last component */
1660 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001661 if (s == NULL || s[1] == '\0')
1662 return path;
1663 else
1664 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001665}
1666#endif
1667
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001668#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001669#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001670void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001671{
1672 int ret;
1673 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1674 int errmsgsz = regerror(ret, preg, NULL, 0);
1675 char *errmsg = xmalloc(errmsgsz);
1676 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001677 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001678 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001679}
1680#endif
1681
Eric Andersen7a86e612000-10-09 18:21:44 +00001682#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001683FILE *wfopen(const char *path, const char *mode)
1684{
1685 FILE *fp;
1686 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001687 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001688 errno = 0;
1689 }
1690 return fp;
1691}
1692#endif
1693
Eric Andersene790ddb2000-11-17 06:50:42 +00001694#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001695 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001696 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
1697 || defined BB_MOUNT
Matt Kraaie0bcce02000-09-27 02:29:39 +00001698FILE *xfopen(const char *path, const char *mode)
1699{
1700 FILE *fp;
1701 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001702 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001703 return fp;
1704}
1705#endif
1706
Matt Kraaif2cc2762001-02-01 19:21:20 +00001707static int applet_name_compare(const void *x, const void *y)
Eric Andersene5aef922000-11-17 18:25:26 +00001708{
Matt Kraaif2cc2762001-02-01 19:21:20 +00001709 const char *name = x;
1710 const struct BB_applet *applet = y;
Eric Andersene5aef922000-11-17 18:25:26 +00001711
Matt Kraaif2cc2762001-02-01 19:21:20 +00001712 return strcmp(name, applet->name);
1713}
1714
1715extern size_t NUM_APPLETS;
1716
1717struct BB_applet *find_applet_by_name(const char *name)
1718{
1719 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1720 applet_name_compare);
Eric Andersene5aef922000-11-17 18:25:26 +00001721}
1722
Eric Andersen67991cf2001-02-14 21:23:06 +00001723void run_applet_by_name(const char *name, int argc, char **argv)
1724{
1725 /* Do a binary search to find the applet entry given the name. */
1726 if ((applet_using = find_applet_by_name(name)) != NULL) {
1727 applet_name = applet_using->name;
1728 if (argv[1] && strcmp(argv[1], "--help") == 0)
1729 show_usage();
1730 exit((*(applet_using->main)) (argc, argv));
1731 }
1732}
1733
Matt Kraai24ac0172000-12-18 21:38:57 +00001734#if defined BB_DD || defined BB_TAIL
Matt Kraaia164c642001-02-05 17:50:03 +00001735unsigned long parse_number(const char *numstr,
1736 const struct suffix_mult *suffixes)
Matt Kraai24ac0172000-12-18 21:38:57 +00001737{
Matt Kraaia164c642001-02-05 17:50:03 +00001738 const struct suffix_mult *sm;
Matt Kraai24ac0172000-12-18 21:38:57 +00001739 unsigned long int ret;
1740 int len;
1741 char *end;
1742
1743 ret = strtoul(numstr, &end, 10);
1744 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001745 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001746 while (end[0] != '\0') {
1747 for (sm = suffixes; sm->suffix != NULL; sm++) {
1748 len = strlen(sm->suffix);
1749 if (strncmp(sm->suffix, end, len) == 0) {
1750 ret *= sm->mult;
1751 end += len;
1752 break;
1753 }
1754 }
1755 if (sm->suffix == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001756 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001757 }
1758 return ret;
1759}
1760#endif
1761
Eric Andersen23a93f52001-01-24 23:47:21 +00001762#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001763ssize_t safe_read(int fd, void *buf, size_t count)
1764{
1765 ssize_t n;
1766
1767 do {
1768 n = read(fd, buf, count);
1769 } while (n < 0 && errno == EINTR);
1770
1771 return n;
1772}
1773#endif
1774
Richard June6d0921c2001-01-22 22:35:38 +00001775#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001776const char *format(unsigned long val, unsigned long hr)
Richard June6d0921c2001-01-22 22:35:38 +00001777{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001778 static const char strings[] = { '0', 0, 'k', 0, 'M', 0, 'G', 0 };
1779 static const char fmt[] = "%lu";
1780 static const char fmt_u[] = "%lu.%lu%s";
Richard June6d0921c2001-01-22 22:35:38 +00001781
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001782 static char str[10];
1783
1784 unsigned long frac __attribute__ ((unused)); /* 'may be uninitialized' warning is ok */
1785 const char *u;
1786 const char *f;
1787
1788#if 1
1789 if(val == 0) { /* This may be omitted to reduce size */
1790 return strings; /* at the cost of speed. */
1791 }
1792#endif
1793
1794 u = strings;
1795 f = fmt;
1796 if (hr) {
1797 val /= hr;
1798 } else {
1799 while ((val >= KILOBYTE) && (*u != 'G')) {
1800 f = fmt_u;
1801 u += 2;
1802 frac = (((val % KILOBYTE) * 10) + (KILOBYTE/2)) / KILOBYTE;
1803 val /= KILOBYTE;
1804 if (frac >= 10) { /* We need to round up here. */
1805 ++val;
1806 frac = 0;
1807 }
1808 }
1809 }
1810
1811 /* If f==fmt then 'frac' and 'u' are ignored and need not be set. */
1812 snprintf(str, sizeof(str), f, val, frac, u);
1813
1814 return str;
Richard June6d0921c2001-01-22 22:35:38 +00001815}
1816#endif
1817
Matt Kraai05e782d2001-02-01 16:49:30 +00001818#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS)
1819void chomp(char *s)
1820{
1821 size_t len = strlen(s);
1822
1823 if (len == 0)
1824 return;
1825
1826 if (s[len-1] == '\n')
1827 s[len-1] = '\0';
1828}
1829#endif
1830
Eric Andersencc8ed391999-10-05 16:24:54 +00001831/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001832/*
1833Local Variables:
1834c-file-style: "linux"
1835c-basic-offset: 4
1836tab-width: 4
1837End:
1838*/