blob: 31192693872f238c2200b6a87bc4e9766967b9d7 [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"
43
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <stdio.h>
45#include <string.h>
46#include <errno.h>
47#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000048#include <dirent.h>
49#include <time.h>
50#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000051#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000052#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000053#include <stdlib.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000054#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000055#include <sys/utsname.h> /* for uname(2) */
Eric Andersened3ef502001-01-27 08:24:39 +000056
Eric Andersenab050f52001-01-27 06:01:43 +000057#include "pwd_grp/pwd.h"
58#include "pwd_grp/grp.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000059
Eric Andersened3ef502001-01-27 08:24:39 +000060/* for the _syscall() macros */
61#include <sys/syscall.h>
62#include <linux/unistd.h>
63
Erik Andersen246cc6d2000-03-07 07:41:42 +000064/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
65 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000066#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000067//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
68#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
69#endif
70
Erik Andersen5cbdd712000-01-26 20:06:48 +000071
Eric Andersend0246fb1999-11-04 21:18:07 +000072#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000073# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000074const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000075# else
76# if defined BB_FEATURE_USE_PROCFS
77const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000078# else
Erik Andersen269bba22000-03-08 14:50:47 +000079# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000080const char mtab_file[] = "/dev/mtab";
81# else
Erik Andersen269bba22000-03-08 14:50:47 +000082# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or ( BB_FEATURE_USE_PROCFS | BB_FEATURE_USE_DEVPS_PATCH)
Eric Andersen0ecb54a1999-12-05 23:24:55 +000083# endif
84# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000085# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000086#endif
87
Eric Andersend73dc5b1999-11-10 23:13:02 +000088extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000089{
Eric Andersenc73a7f92001-01-20 21:41:11 +000090 fprintf(stderr, "%s\n\nUsage: %s\n", full_version, usage);
Matt Kraaie0bcce02000-09-27 02:29:39 +000091 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000092}
93
Mark Whitleyf57c9442000-12-07 19:56:48 +000094static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +000095{
96 fflush(stdout);
97 fprintf(stderr, "%s: ", applet_name);
98 vfprintf(stderr, s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +000099}
100
Mark Whitleyf57c9442000-12-07 19:56:48 +0000101extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000102{
103 va_list p;
104
105 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000106 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000107 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000108 putc('\n', stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000109}
110
Mark Whitleyf57c9442000-12-07 19:56:48 +0000111extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000112{
113 va_list p;
114
115 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000116 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000117 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000118 putc('\n', stderr);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000119 exit(EXIT_FAILURE);
120}
121
Mark Whitleyf57c9442000-12-07 19:56:48 +0000122static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000123{
Eric Andersenc73a7f92001-01-20 21:41:11 +0000124 int err=errno;
125 if(s == 0) s = "";
126 verror_msg(s, p);
127 if (*s) s = ": ";
128 fprintf(stderr, "%s%s\n", s, strerror(err));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000129}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000130
Mark Whitleyf57c9442000-12-07 19:56:48 +0000131extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000132{
133 va_list p;
134
135 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000136 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000137 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000138}
139
Mark Whitleyf57c9442000-12-07 19:56:48 +0000140extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000141{
142 va_list p;
143
144 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000145 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000146 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000147 exit(EXIT_FAILURE);
148}
149
Eric Andersenf5d5e772001-01-24 23:34:48 +0000150#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000151/* Returns kernel version encoded as major*65536 + minor*256 + patch,
152 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000153 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000154 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000155extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000156{
Erik Andersend07ee462000-02-21 21:26:32 +0000157 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000159
Erik Andersend07ee462000-02-21 21:26:32 +0000160 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000161 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 return (0);
163 }
Eric Andersenef936da2000-10-30 17:22:04 +0000164 major = atoi(strtok(name.release, "."));
165 minor = atoi(strtok(NULL, "."));
166 patch = atoi(strtok(NULL, "."));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000168}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000169#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000170
Eric Andersen10dc9d42000-06-26 10:45:52 +0000171
172
173#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000174_syscall1(int, sysinfo, struct sysinfo *, info);
175#endif /* BB_INIT */
176
Eric Andersena57ba4d2000-07-08 19:20:49 +0000177#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000178
179#ifndef __NR_umount2
Mark Whitley59ab0252001-01-23 22:30:04 +0000180static const int __NR_umount2 = 52;
Eric Andersena57ba4d2000-07-08 19:20:49 +0000181#endif
182
183/* Include our own version of <sys/mount.h>, since libc5 doesn't
184 * know about umount2 */
185extern _syscall1(int, umount, const char *, special_file);
186extern _syscall2(int, umount2, const char *, special_file, int, flags);
187extern _syscall5(int, mount, const char *, special_file, const char *, dir,
188 const char *, fstype, unsigned long int, rwflag, const void *, data);
189#endif
190
Eric Andersenc86e5e62000-08-21 19:38:49 +0000191#if defined BB_INSMOD || defined BB_LSMOD
192#ifndef __NR_query_module
Mark Whitley59ab0252001-01-23 22:30:04 +0000193static const int __NR_query_module = 167;
Eric Andersenc86e5e62000-08-21 19:38:49 +0000194#endif
195_syscall5(int, query_module, const char *, name, int, which,
196 void *, buf, size_t, bufsize, size_t*, ret);
197#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000198
199
Erik Andersen23dea9b2000-05-13 00:28:25 +0000200#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000201
Erik Andersen029011b2000-03-04 21:19:32 +0000202#define HASH_SIZE 311 /* Should be prime */
203#define hash_inode(i) ((i) % HASH_SIZE)
204
205static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
206
207/*
208 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
209 * `ino_dev_hashtable', else return 0
210 *
211 * If NAME is a non-NULL pointer to a character pointer, and there is
212 * a match, then set *NAME to the value of the name slot in that
213 * bucket.
214 */
215int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
216{
217 ino_dev_hashtable_bucket_t *bucket;
218
219 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
220 while (bucket != NULL) {
221 if ((bucket->ino == statbuf->st_ino) &&
222 (bucket->dev == statbuf->st_dev))
223 {
224 if (name) *name = bucket->name;
225 return 1;
226 }
227 bucket = bucket->next;
228 }
229 return 0;
230}
231
232/* Add statbuf to statbuf hash table */
233void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
234{
235 int i;
236 size_t s;
237 ino_dev_hashtable_bucket_t *bucket;
238
239 i = hash_inode(statbuf->st_ino);
240 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000241 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000242 bucket->ino = statbuf->st_ino;
243 bucket->dev = statbuf->st_dev;
244 if (name)
245 strcpy(bucket->name, name);
246 else
247 bucket->name[0] = '\0';
248 bucket->next = ino_dev_hashtable[i];
249 ino_dev_hashtable[i] = bucket;
250}
251
252/* Clear statbuf hash table */
253void reset_ino_dev_hashtable(void)
254{
255 int i;
256 ino_dev_hashtable_bucket_t *bucket;
257
258 for (i = 0; i < HASH_SIZE; i++) {
259 while (ino_dev_hashtable[i] != NULL) {
260 bucket = ino_dev_hashtable[i]->next;
261 free(ino_dev_hashtable[i]);
262 ino_dev_hashtable[i] = bucket;
263 }
264 }
265}
266
Erik Andersen23dea9b2000-05-13 00:28:25 +0000267#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000268
Glenn L McGrath5e23e3d2001-01-03 01:38:29 +0000269#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000270/*
271 * Return TRUE if a fileName is a directory.
272 * Nonexistant files return FALSE.
273 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000274int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000275{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000277 int didMalloc = 0;
278
279 if (statBuf == NULL) {
280 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
281 ++didMalloc;
282 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000283
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000285 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000287 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000288
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000289 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
290 status = FALSE;
291 }
292 else status = TRUE;
293
294 if (didMalloc) {
295 free(statBuf);
296 statBuf = NULL;
297 }
298 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000299}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000300#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000301
Eric Andersen70c60862001-01-25 00:00:04 +0000302#if defined BB_AR || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000303/*
Mark Whitley47583682000-12-05 20:03:17 +0000304 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000305 */
Mark Whitley47583682000-12-05 20:03:17 +0000306int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000307{
308 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000309 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000310
Mark Whitley47583682000-12-05 20:03:17 +0000311 while (chunksize > 0) {
312 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000313 size = BUFSIZ;
314 else
Mark Whitley47583682000-12-05 20:03:17 +0000315 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000316 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000317 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000318 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000319 }
320 return (TRUE);
321}
322#endif
323
324
Erik Andersen23dea9b2000-05-13 00:28:25 +0000325#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000326/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000327 * Copy one file to another, while possibly preserving its modes, times, and
328 * modes. Returns TRUE if successful, or FALSE on a failure with an error
329 * message output. (Failure is not indicated if attributes cannot be set.)
330 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000331 */
332int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000333copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000334 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000335{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 int rfd;
337 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 struct stat srcStatBuf;
340 struct stat dstStatBuf;
341 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000342
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 if (followLinks == TRUE)
344 status = stat(srcName, &srcStatBuf);
345 else
346 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000347
Erik Andersenfac10d72000-02-07 05:29:42 +0000348 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000349 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000351 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352
353 if (followLinks == TRUE)
354 status = stat(destName, &dstStatBuf);
355 else
356 status = lstat(destName, &dstStatBuf);
357
Erik Andersen59b9e872000-05-10 05:05:45 +0000358 if (status < 0 || forceFlag==TRUE) {
359 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 dstStatBuf.st_ino = -1;
361 dstStatBuf.st_dev = -1;
362 }
363
364 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
365 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000366 error_msg("Copying file \"%s\" to itself", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 return FALSE;
368 }
369
370 if (S_ISDIR(srcStatBuf.st_mode)) {
371 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
372 /* Make sure the directory is writable */
373 status = mkdir(destName, 0777777 ^ umask(0));
374 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000375 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 return FALSE;
377 }
378 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000379 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 int link_size;
381
382 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000383 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
384 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 if (link_size < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000386 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 return FALSE;
388 }
389 link_val[link_size] = '\0';
390 status = symlink(link_val, destName);
391 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000392 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000393 return FALSE;
394 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000395#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000397 /* Try to set owner, but fail silently like GNU cp */
398 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000400#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 return TRUE;
402 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
403 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
404 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000405 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 return FALSE;
407 }
408 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
409 || S_ISSOCK(srcStatBuf.st_mode)) {
410 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
411 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000412 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 return FALSE;
414 }
415 } else if (S_ISREG(srcStatBuf.st_mode)) {
416 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
417 rfd = open(srcName, O_RDONLY);
418 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000419 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 return FALSE;
421 }
422
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000423 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 srcStatBuf.st_mode);
425 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000426 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 close(rfd);
428 return FALSE;
429 }
430
Mark Whitley47583682000-12-05 20:03:17 +0000431 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000432 goto error_exit;
433
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 close(rfd);
435 if (close(wfd) < 0) {
436 return FALSE;
437 }
438 }
439
440 if (setModes == TRUE) {
441 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000442 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
443 perror_msg_and_die("%s", destName);
444 if (chmod(destName, srcStatBuf.st_mode) < 0)
445 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 times.actime = srcStatBuf.st_atime;
447 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000448 if (utime(destName, &times) < 0)
449 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
451
Erik Andersenfac10d72000-02-07 05:29:42 +0000452 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000453
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000455 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000456 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000458
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000460}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000461#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000462
463
464
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000465#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000466
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000467#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
468#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
469
470/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
471static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 0, 0, S_ISUID,
473 0, 0, S_ISGID,
474 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000475};
476
477/* The 9 mode bits to test */
478static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000479 S_IRUSR, S_IWUSR, S_IXUSR,
480 S_IRGRP, S_IWGRP, S_IXGRP,
481 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000482};
483
Mark Whitley26a3adb2000-12-05 20:10:27 +0000484static const char MODE1[] = "rwxrwxrwx";
485static const char MODE0[] = "---------";
486static const char SMODE1[] = "..s..s..t";
487static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000488
Eric Andersencc8ed391999-10-05 16:24:54 +0000489/*
490 * Return the standard ls-like mode string from a file mode.
491 * This is static and so is overwritten on each call.
492 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000493const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000494{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000496
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 int i;
498
499 buf[0] = TYPECHAR(mode);
500 for (i = 0; i < 9; i++) {
501 if (mode & SBIT[i])
502 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
503 else
504 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
505 }
506 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000507}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000508#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000509
510
Pavel Roskin21ddb382000-07-17 19:31:54 +0000511#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000512/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000513 * Return the standard ls-like time string from a time_t
514 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000515 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000516const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000517{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000518 time_t now;
519 char *str;
520 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000521
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000523
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000525
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 strcpy(buf, &str[4]);
527 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000528
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
530 strcpy(&buf[7], &str[20]);
531 buf[11] = '\0';
532 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000533
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000535}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000536#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000537
Eric Andersen70c60862001-01-25 00:00:04 +0000538#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 +0000539/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000540 * Write all of the supplied buffer out to a file.
541 * This does multiple writes as necessary.
542 * Returns the amount written, or -1 on an error.
543 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000544int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000545{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 int cc;
547 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000548
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000550
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 while (len > 0) {
552 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000553
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 if (cc < 0)
555 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000556
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 buf += cc;
558 total += cc;
559 len -= cc;
560 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000561
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000563}
Matt Kraai24ac0172000-12-18 21:38:57 +0000564#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000565
Matt Kraai24ac0172000-12-18 21:38:57 +0000566#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000567/*
568 * Read all of the supplied buffer from a file.
569 * This does multiple reads as necessary.
570 * Returns the amount read, or -1 on an error.
571 * A short read is returned on an end of file.
572 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000573int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000574{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 int cc;
576 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000577
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000579
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 while (len > 0) {
581 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000582
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583 if (cc < 0)
584 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000585
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 if (cc == 0)
587 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000588
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 buf += cc;
590 total += cc;
591 len -= cc;
592 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000593
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000595}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000596#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000597
598
Erik Andersen23dea9b2000-05-13 00:28:25 +0000599#if defined (BB_CHMOD_CHOWN_CHGRP) \
600 || defined (BB_CP_MV) \
601 || defined (BB_FIND) \
602 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000603 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000604 || defined (BB_RM) \
605 || defined (BB_TAR)
606
Eric Andersencc8ed391999-10-05 16:24:54 +0000607/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000608 * Walk down all the directories under the specified
609 * location, and do something (something specified
610 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000611 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000612 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000613 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000614 * and so isn't sufficiently portable to take over since glibc2.1
615 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000616 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000617int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 int recurse, int followLinks, int depthFirst,
619 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000620 struct stat * statbuf,
621 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000623 struct stat * statbuf,
624 void* userData),
625 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000626{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 int status;
628 struct stat statbuf;
629 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000630
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 if (followLinks == TRUE)
632 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000633 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000635
Eric Andersen2b69c401999-10-05 22:58:32 +0000636 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000637#ifdef BB_DEBUG_PRINT_SCAFFOLD
638 fprintf(stderr,
639 "status=%d followLinks=%d TRUE=%d\n",
640 status, followLinks, TRUE);
641#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000642 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000643 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000644 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645
646 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
647 if (fileAction == NULL)
648 return TRUE;
649 else
Erik Andersen3364d782000-03-28 00:58:14 +0000650 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651 }
652
653 if (recurse == FALSE) {
654 if (S_ISDIR(statbuf.st_mode)) {
655 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000656 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 else
658 return TRUE;
659 }
660 }
661
662 if (S_ISDIR(statbuf.st_mode)) {
663 DIR *dir;
664
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000666 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000668 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000670 } else if (status == SKIP)
671 return TRUE;
672 }
673 dir = opendir(fileName);
674 if (!dir) {
675 perror_msg("%s", fileName);
676 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 }
678 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000679 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680
681 if ((strcmp(next->d_name, "..") == 0)
682 || (strcmp(next->d_name, ".") == 0)) {
683 continue;
684 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000685 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000686 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 return FALSE;
688 }
Erik Andersenc3660502000-04-18 21:18:57 +0000689 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 sprintf(nextFile, "%s/%s", fileName, next->d_name);
691 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000692 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000693 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000694 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 closedir(dir);
696 return FALSE;
697 }
698 }
699 status = closedir(dir);
700 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000701 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702 return FALSE;
703 }
704 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000705 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000707 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 return FALSE;
709 }
710 }
711 } else {
712 if (fileAction == NULL)
713 return TRUE;
714 else
Erik Andersen3364d782000-03-28 00:58:14 +0000715 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 }
717 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000718}
719
Erik Andersen23dea9b2000-05-13 00:28:25 +0000720#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000721
Eric Andersenf6be9441999-10-13 21:12:06 +0000722
723
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000724#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000725/*
726 * Attempt to create the directories along the specified path, except for
727 * the final component. The mode is given for the final directory only,
728 * while all previous ones get default protections. Errors are not reported
729 * here, as failures to restore files can be reported later.
730 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000731extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000732{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 char *cp;
734 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000735 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000737
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 strcpy(buf, name);
739 for (cp = buf; *cp == '/'; cp++);
740 cp = strchr(cp, '/');
741 while (cp) {
742 cpOld = cp;
743 cp = strchr(cp + 1, '/');
744 *cpOld = '\0';
745 retVal = mkdir(buf, cp ? 0777 : mode);
746 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000747 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000748 return FALSE;
749 }
750 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000751 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000753}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000754#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000755
756
757
Pavel Roskin21ddb382000-07-17 19:31:54 +0000758#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000759 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000760/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000761
762
763
Erik Andersene49d5ec2000-02-08 19:58:47 +0000764extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000765{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 mode_t andMode =
767
768 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000769 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 mode_t mode = 0;
771 mode_t groups = 0;
772 char type;
773 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000774
Eric Andersen4e61db22000-09-01 00:06:17 +0000775 if (s==NULL)
776 return (FALSE);
777
Eric Andersenf6be9441999-10-13 21:12:06 +0000778 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 for (;;) {
780 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000781 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000782 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000783 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000784 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000785 continue;
786 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000788 continue;
789 case 'o':
790 groups |= S_IRWXO;
791 continue;
792 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000794 continue;
795 case '+':
796 case '=':
797 case '-':
798 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000799 if (groups == 0) /* The default is "all" */
800 groups |=
801 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000802 break;
803 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 if (isdigit(c) && c >= '0' && c <= '7' &&
805 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000806 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000807 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000809 return (FALSE);
810 }
811 break;
812 }
813
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 while ((c = *s++) != '\0') {
815 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000816 case ',':
817 break;
818 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000819 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000820 continue;
821 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000822 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000823 continue;
824 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000825 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000826 continue;
827 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000828 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000829 continue;
830 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000831 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000832 continue;
833 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000834 *theMode &= andMode;
835 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000837 }
838 break;
839 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000840 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000841 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000842 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000843 /* fall through */
844 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000845 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000846 break;
847 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000848 andMode &= ~(mode & groups);
849 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000850 break;
851 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000853 *theMode &= andMode;
854 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000855 return (TRUE);
856}
Eric Andersence8f3b91999-10-20 07:03:36 +0000857
858
Pavel Roskin21ddb382000-07-17 19:31:54 +0000859#endif
860/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000861
Eric Andersene674eb71999-10-19 20:52:57 +0000862
863
Erik Andersen23dea9b2000-05-13 00:28:25 +0000864
865
Pavel Roskin21ddb382000-07-17 19:31:54 +0000866#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
867 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000868 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000869/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000870long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000871{
Eric Andersenab050f52001-01-27 06:01:43 +0000872 struct passwd *myuser;
873
874 myuser = getpwnam(name);
875 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000876 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000877
878 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000879}
880
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000881/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000882long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000883{
Eric Andersenab050f52001-01-27 06:01:43 +0000884 struct group *mygroup;
885
886 mygroup = getgrnam(name);
887 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000888 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000889
890 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000891}
892
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000893/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000894void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000895{
Eric Andersenab050f52001-01-27 06:01:43 +0000896 struct passwd *myuser;
897
898 myuser = getpwuid(uid);
899 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000900 sprintf(name, "%-8ld ", (long)uid);
901 else
902 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000903}
904
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000905/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000906void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000907{
Eric Andersenab050f52001-01-27 06:01:43 +0000908 struct group *mygroup;
909
910 mygroup = getgrgid(gid);
911 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000912 sprintf(group, "%-8ld ", (long)gid);
913 else
914 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000915}
916
Matt Kraaie6e81832000-12-30 07:46:23 +0000917#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000918/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000919long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000920{
Eric Andersenab050f52001-01-27 06:01:43 +0000921 struct group *mygroup;
922 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000923
Eric Andersenab050f52001-01-27 06:01:43 +0000924 myuser=getpwnam(name);
925 if (myuser==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000926 error_msg_and_die( "unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000927
928 mygroup = getgrgid(myuser->pw_gid);
929 if (mygroup==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000930 error_msg_and_die( "unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000931
932 return mygroup->gr_gid;
933}
934#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000935#endif
936 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
937 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000938
939
Eric Andersenf86aee72000-07-06 17:20:46 +0000940#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000941
Eric Andersenbd22ed82000-07-08 18:55:24 +0000942/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000943static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
944static const int KB_84 = 0x01;
945static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000946
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000948{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000949 char arg;
950
951 arg = 0;
952 return (ioctl(fd, KDGKBTYPE, &arg) == 0
953 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000954}
955
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000957{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 int fd;
959
960 /* try read-only */
961 fd = open(fnam, O_RDWR);
962
963 /* if failed, try read-only */
964 if (fd < 0 && errno == EACCES)
965 fd = open(fnam, O_RDONLY);
966
967 /* if failed, try write-only */
968 if (fd < 0 && errno == EACCES)
969 fd = open(fnam, O_WRONLY);
970
971 /* if failed, fail */
972 if (fd < 0)
973 return -1;
974
975 /* if not a console, fail */
976 if (!is_a_console(fd)) {
977 close(fd);
978 return -1;
979 }
980
981 /* success */
982 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000983}
984
985/*
986 * Get an fd for use with kbd/console ioctls.
987 * We try several things because opening /dev/console will fail
988 * if someone else used X (which does a chown on /dev/console).
989 *
990 * if tty_name is non-NULL, try this one instead.
991 */
992
Erik Andersene49d5ec2000-02-08 19:58:47 +0000993int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000994{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000995 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000996
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997 if (tty_name) {
998 if (-1 == (fd = open_a_console(tty_name)))
999 return -1;
1000 else
1001 return fd;
1002 }
1003
1004 fd = open_a_console("/dev/tty");
1005 if (fd >= 0)
1006 return fd;
1007
1008 fd = open_a_console("/dev/tty0");
1009 if (fd >= 0)
1010 return fd;
1011
1012 fd = open_a_console("/dev/console");
1013 if (fd >= 0)
1014 return fd;
1015
1016 for (fd = 0; fd < 3; fd++)
1017 if (is_a_console(fd))
1018 return fd;
1019
Matt Kraaidd19c692001-01-31 19:00:21 +00001020 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001022}
1023
1024
Eric Andersenf86aee72000-07-06 17:20:46 +00001025#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001026
1027
Eric Andersen9f16d612000-06-12 23:11:16 +00001028#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001029/*
1030 * Routine to see if a text string is matched by a wildcard pattern.
1031 * Returns TRUE if the text is matched, or FALSE if it is not matched
1032 * or if the pattern is invalid.
1033 * * matches zero or more characters
1034 * ? matches a single character
1035 * [abc] matches 'a', 'b' or 'c'
1036 * \c quotes character c
1037 * Adapted from code written by Ingo Wilken, and
1038 * then taken from sash, Copyright (c) 1999 by David I. Bell
1039 * Permission is granted to use, distribute, or modify this source,
1040 * provided that this copyright notice remains intact.
1041 * Permission to distribute this code under the GPL has been granted.
1042 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001044{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001045 const char *retryPat;
1046 const char *retryText;
1047 int ch;
1048 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001049 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001050
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051 retryPat = NULL;
1052 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001053
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001055 ch = *pattern++;
1056
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057 switch (ch) {
1058 case '*':
1059 retryPat = pattern;
1060 retryText = text;
1061 break;
1062
1063 case '[':
1064 found = FALSE;
1065
1066 while ((ch = *pattern++) != ']') {
1067 if (ch == '\\')
1068 ch = *pattern++;
1069
1070 if (ch == '\0')
1071 return FALSE;
1072
1073 if (*text == ch)
1074 found = TRUE;
1075 }
Erik Andersen632bb572000-04-18 22:09:06 +00001076 len=strlen(text);
1077 if (found == FALSE && len!=0) {
1078 return FALSE;
1079 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001081 if (strlen(pattern)==0 && len==1) {
1082 return TRUE;
1083 }
1084 if (len!=0) {
1085 text++;
1086 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001087 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 }
1089
1090 /* fall into next case */
1091
1092 case '?':
1093 if (*text++ == '\0')
1094 return FALSE;
1095
1096 break;
1097
1098 case '\\':
1099 ch = *pattern++;
1100
1101 if (ch == '\0')
1102 return FALSE;
1103
1104 /* fall into next case */
1105
1106 default:
1107 if (*text == ch) {
1108 if (*text)
1109 text++;
1110 break;
1111 }
1112
1113 if (*text) {
1114 pattern = retryPat;
1115 text = ++retryText;
1116 break;
1117 }
1118
Eric Andersenb186d981999-12-03 09:19:54 +00001119 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001120 }
1121
Erik Andersene49d5ec2000-02-08 19:58:47 +00001122 if (pattern == NULL)
1123 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001124 }
1125
Erik Andersene49d5ec2000-02-08 19:58:47 +00001126 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001127}
Eric Andersen9f16d612000-06-12 23:11:16 +00001128#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001129
1130
Eric Andersen29d2e361999-11-06 06:07:27 +00001131
1132
Erik Andersen23dea9b2000-05-13 00:28:25 +00001133#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001134#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001135/*
1136 * Given a block device, find the mount table entry if that block device
1137 * is mounted.
1138 *
1139 * Given any other file (or directory), find the mount table entry for its
1140 * filesystem.
1141 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001142extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001143{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001144 struct stat s;
1145 dev_t mountDevice;
1146 FILE *mountTable;
1147 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001148
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 if (stat(name, &s) != 0)
1150 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001151
Erik Andersene49d5ec2000-02-08 19:58:47 +00001152 if ((s.st_mode & S_IFMT) == S_IFBLK)
1153 mountDevice = s.st_rdev;
1154 else
1155 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001156
1157
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 if ((mountTable = setmntent(table, "r")) == 0)
1159 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001160
Erik Andersene49d5ec2000-02-08 19:58:47 +00001161 while ((mountEntry = getmntent(mountTable)) != 0) {
1162 if (strcmp(name, mountEntry->mnt_dir) == 0
1163 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1164 break;
1165 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1166 break;
1167 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1168 break;
1169 }
1170 endmntent(mountTable);
1171 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001172}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001173#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001174
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001175#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001176/* try to open up the specified device */
1177extern int device_open(char *device, int mode)
1178{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001180
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001182
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 /* Retry up to 5 times */
1184 for (f = 0; f < 5; f++)
1185 if ((fd = open(device, m, 0600)) >= 0)
1186 break;
1187 if (fd < 0)
1188 return fd;
1189 /* Reset original flags. */
1190 if (m != mode)
1191 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001192 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001193}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001194#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001195
1196
Erik Andersen23dea9b2000-05-13 00:28:25 +00001197#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001198#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001199#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001200#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001201
Erik Andersen23dea9b2000-05-13 00:28:25 +00001202#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001203/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001204 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001205 * This finds the pid of the specified process,
1206 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001207 *
Erik Andersen825aead2000-04-07 06:00:07 +00001208 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001209 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001210extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001211{
Erik Andersen825aead2000-04-07 06:00:07 +00001212 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001213 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001214 pid_t num_pids;
1215 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001216 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001217
1218 /* open device */
1219 fd = open(device, O_RDONLY);
1220 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001221 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001222
1223 /* Find out how many processes there are */
1224 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001225 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001226
1227 /* Allocate some memory -- grab a few extras just in case
1228 * some new processes start up while we wait. The kernel will
1229 * just ignore any extras if we give it too many, and will trunc.
1230 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001231 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001232 pid_array[0] = num_pids+10;
1233
1234 /* Now grab the pid list */
1235 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001236 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001237
1238 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001239 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001240 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001241 struct pid_info info;
1242
1243 info.pid = pid_array[i];
1244 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001245 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001246
Erik Andersen0a64de92000-04-18 20:00:03 +00001247 /* Make sure we only match on the process name */
1248 p=info.command_line+1;
1249 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1250 (p)++;
1251 }
1252 if (isspace(*(p)))
1253 *p='\0';
1254
Erik Andersenb9167cb2000-04-18 22:41:30 +00001255 if ((strstr(info.command_line, pidName) != NULL)
1256 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001257 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001258 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001259 }
1260 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001261 if (pidList)
1262 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001263
1264 /* Free memory */
1265 free( pid_array);
1266
1267 /* close device */
1268 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001269 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001270
Erik Andersen825aead2000-04-07 06:00:07 +00001271 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001272}
Erik Andersen269bba22000-03-08 14:50:47 +00001273#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001274#if ! defined BB_FEATURE_USE_PROCFS
1275#error Sorry, I depend on the /proc filesystem right now.
1276#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001277
Mark Whitleyf57c9442000-12-07 19:56:48 +00001278/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001279 *
1280 * This finds the pid of the specified process.
1281 * Currently, it's implemented by rummaging through
1282 * the proc filesystem.
1283 *
Erik Andersen825aead2000-04-07 06:00:07 +00001284 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001285 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001286extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001287{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001288 DIR *dir;
1289 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001290 pid_t* pidList=NULL;
1291 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001292
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001293 dir = opendir("/proc");
1294 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001295 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001296
1297 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001298 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001299 char filename[256];
1300 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001301
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001302 /* If it isn't a number, we don't want it */
1303 if (!isdigit(*next->d_name))
1304 continue;
1305
Eric Andersen31e68292000-09-14 21:12:20 +00001306 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001307 status = fopen(filename, "r");
1308 if (!status) {
1309 continue;
1310 }
1311 fgets(buffer, 256, status);
1312 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001313
Eric Andersen31e68292000-09-14 21:12:20 +00001314 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001315 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001316 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001317 }
John Beppuf95ca971999-12-09 22:10:18 +00001318 }
Eric Andersen31e68292000-09-14 21:12:20 +00001319
Erik Andersen0a64de92000-04-18 20:00:03 +00001320 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001321 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001322 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001323}
Erik Andersen269bba22000-03-08 14:50:47 +00001324#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001325#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001326
Eric Andersenae6eae02000-07-19 17:35:54 +00001327#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001328/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001329extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001330{
Eric Andersenae6eae02000-07-19 17:35:54 +00001331 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001332
Eric Andersenae6eae02000-07-19 17:35:54 +00001333 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001334 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001335 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001336}
1337
Eric Andersen501c88b2000-07-28 15:14:45 +00001338extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001339{
1340 void *ptr = realloc(old, size);
1341 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001342 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001343 return ptr;
1344}
Eric Andersen501c88b2000-07-28 15:14:45 +00001345
1346extern void *xcalloc(size_t nmemb, size_t size)
1347{
1348 void *ptr = calloc(nmemb, size);
1349 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001350 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001351 return ptr;
1352}
Eric Andersenae6eae02000-07-19 17:35:54 +00001353#endif
1354
Eric Andersen15f29e72001-01-25 00:53:46 +00001355#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET || \
1356 defined BB_DPKG_DEB || defined BB_TAR
Eric Andersenae6eae02000-07-19 17:35:54 +00001357# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001358extern char * xstrdup (const char *s) {
1359 char *t;
1360
1361 if (s == NULL)
1362 return NULL;
1363
1364 t = strdup (s);
1365
1366 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001367 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001368
1369 return t;
1370}
Eric Andersenae6eae02000-07-19 17:35:54 +00001371# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001372#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001373
Eric Andersenf5d5e772001-01-24 23:34:48 +00001374#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001375extern char * xstrndup (const char *s, int n) {
1376 char *t;
1377
1378 if (s == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001379 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001380
1381 t = xmalloc(n+1);
1382 strncpy(t,s,n);
1383 t[n] = 0;
1384
1385 return t;
1386}
1387#endif
1388
1389
Erik Andersen23dea9b2000-05-13 00:28:25 +00001390#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001391extern int vdprintf(int d, const char *format, va_list ap)
1392{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001393 char buf[BUF_SIZE];
1394 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001395
Erik Andersene49d5ec2000-02-08 19:58:47 +00001396 len = vsprintf(buf, format, ap);
1397 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001398}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001399#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001400
Eric Andersen624cc772000-09-21 02:04:51 +00001401
Erik Andersen23dea9b2000-05-13 00:28:25 +00001402#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001403#include <fcntl.h>
1404#include "loop.h" /* Pull in loop device support */
1405
Erik Andersen5cbdd712000-01-26 20:06:48 +00001406extern int del_loop(const char *device)
1407{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001408 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001409
Erik Andersene49d5ec2000-02-08 19:58:47 +00001410 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001411 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001412 return (FALSE);
1413 }
1414 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001415 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001416 return (FALSE);
1417 }
1418 close(fd);
1419 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001420}
Erik Andersende7965c2000-01-26 23:49:21 +00001421
Erik Andersene49d5ec2000-02-08 19:58:47 +00001422extern int set_loop(const char *device, const char *file, int offset,
1423 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001424{
1425 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001426 int fd, ffd, mode;
1427
Erik Andersende7965c2000-01-26 23:49:21 +00001428 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001429 if ((ffd = open(file, mode)) < 0 && !*loopro
1430 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001431 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001433 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001434 if ((fd = open(device, mode)) < 0) {
1435 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001436 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001437 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001438 }
1439 *loopro = (mode == O_RDONLY);
1440
1441 memset(&loopinfo, 0, sizeof(loopinfo));
1442 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001443 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001444
1445 loopinfo.lo_offset = offset;
1446
1447 loopinfo.lo_encrypt_key_size = 0;
1448 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001449 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001450 close(fd);
1451 close(ffd);
1452 return 1;
1453 }
1454 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1455 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001456 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001457 close(fd);
1458 close(ffd);
1459 return 1;
1460 }
1461 close(fd);
1462 close(ffd);
1463 return 0;
1464}
1465
Erik Andersene49d5ec2000-02-08 19:58:47 +00001466extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001467{
1468 char dev[20];
1469 int i, fd;
1470 struct stat statbuf;
1471 struct loop_info loopinfo;
1472
Erik Andersene49d5ec2000-02-08 19:58:47 +00001473 for (i = 0; i <= 7; i++) {
1474 sprintf(dev, "/dev/loop%d", i);
1475 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1476 if ((fd = open(dev, O_RDONLY)) >= 0) {
1477 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1478 if (errno == ENXIO) { /* probably free */
1479 close(fd);
1480 return strdup(dev);
1481 }
1482 }
1483 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001484 }
Erik Andersende7965c2000-01-26 23:49:21 +00001485 }
Erik Andersende7965c2000-01-26 23:49:21 +00001486 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001487 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001488}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001489#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001490
Erik Andersen23dea9b2000-05-13 00:28:25 +00001491#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001492extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001493{
Erik Andersen016ffe92000-03-22 00:58:54 +00001494 DIR *dir;
1495 struct dirent *entry;
1496 struct stat statBuf, rootStat;
1497 char fileName[BUFSIZ];
1498
Erik Andersenec5bd902000-03-22 07:12:05 +00001499 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001500 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001501 return( FALSE);
1502 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001503
Erik Andersenec5bd902000-03-22 07:12:05 +00001504 dir = opendir("/dev");
1505 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001506 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001507 return( FALSE);
1508 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001509
1510 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001511
Erik Andersen016ffe92000-03-22 00:58:54 +00001512 /* Must skip ".." since that is "/", and so we
1513 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001514 if (strcmp(entry->d_name, "..") == 0)
1515 continue;
1516
Eric Andersenf9ca6532000-08-11 17:45:02 +00001517 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001518
1519 if (stat(fileName, &statBuf) != 0)
1520 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001521 /* Some char devices have the same dev_t as block
1522 * devices, so make sure this is a block device */
1523 if (! S_ISBLK(statBuf.st_mode))
1524 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001525 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001526 strcpy(name, fileName);
1527 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001528 }
1529 }
1530
Erik Andersenec5bd902000-03-22 07:12:05 +00001531 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001532}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001533#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001534
Mark Whitley1ca41772000-06-28 22:15:26 +00001535
1536/* get_line_from_file() - This function reads an entire line from a text file
1537 * up to a newline. It returns a malloc'ed char * which must be stored and
1538 * free'ed by the caller. */
1539extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001540{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001541 static const int GROWBY = 80; /* how large we will grow strings by */
1542
Mark Whitley1ca41772000-06-28 22:15:26 +00001543 int ch;
1544 int idx = 0;
1545 char *linebuf = NULL;
1546 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001547
Mark Whitley1ca41772000-06-28 22:15:26 +00001548 while (1) {
1549 ch = fgetc(file);
1550 if (ch == EOF)
1551 break;
1552 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001553 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001554 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001555 linebuf[idx++] = (char)ch;
1556 if ((char)ch == '\n')
1557 break;
1558 }
John Beppu5a728cf2000-04-17 04:22:09 +00001559
Mark Whitley1ca41772000-06-28 22:15:26 +00001560 if (idx == 0)
1561 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001562
Mark Whitley1ca41772000-06-28 22:15:26 +00001563 linebuf[idx] = 0;
1564 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001565}
Erik Andersen016ffe92000-03-22 00:58:54 +00001566
Eric Andersen944be902000-09-25 16:58:39 +00001567#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001568extern void print_file(FILE *file)
1569{
1570 int c;
1571
1572 while ((c = getc(file)) != EOF)
1573 putc(c, stdout);
1574 fclose(file);
1575 fflush(stdout);
1576}
1577
1578extern int print_file_by_name(char *filename)
1579{
1580 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001581 if ((file = wfopen(filename, "r")) == NULL)
1582 return FALSE;
1583 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001584 return TRUE;
1585}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001586#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001587
Matt Kraaicabafae2000-11-30 04:55:40 +00001588#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001589char process_escape_sequence(char **ptr)
1590{
1591 char c;
1592
1593 switch (c = *(*ptr)++) {
1594 case 'a':
1595 c = '\a';
1596 break;
1597 case 'b':
1598 c = '\b';
1599 break;
1600 case 'f':
1601 c = '\f';
1602 break;
1603 case 'n':
1604 c = '\n';
1605 break;
Eric Andersen9bd71e82000-11-29 23:07:57 +00001606 case 'r':
1607 c = '\r';
1608 break;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001609 case 't':
1610 c = '\t';
1611 break;
1612 case 'v':
1613 c = '\v';
1614 break;
1615 case '\\':
1616 c = '\\';
1617 break;
1618 case '0': case '1': case '2': case '3':
1619 case '4': case '5': case '6': case '7':
1620 c -= '0';
1621 if ('0' <= **ptr && **ptr <= '7') {
1622 c = c * 8 + (*(*ptr)++ - '0');
1623 if ('0' <= **ptr && **ptr <= '7')
1624 c = c * 8 + (*(*ptr)++ - '0');
1625 }
1626 break;
1627 default:
1628 (*ptr)--;
1629 c = '\\';
1630 break;
1631 }
1632 return c;
1633}
1634#endif
1635
Eric Andersen70c60862001-01-25 00:00:04 +00001636#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
1637 defined BB_FEATURE_USE_PROCFS || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001638char *get_last_path_component(char *path)
1639{
1640 char *s=path+strlen(path)-1;
1641
1642 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001643 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001644 *s-- = '\0';
1645 }
1646
1647 /* find last component */
1648 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001649 if (s == NULL || s[1] == '\0')
1650 return path;
1651 else
1652 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001653}
1654#endif
1655
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001656#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001657#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001658void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001659{
1660 int ret;
1661 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1662 int errmsgsz = regerror(ret, preg, NULL, 0);
1663 char *errmsg = xmalloc(errmsgsz);
1664 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001665 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001666 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001667}
1668#endif
1669
Eric Andersen7a86e612000-10-09 18:21:44 +00001670#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001671FILE *wfopen(const char *path, const char *mode)
1672{
1673 FILE *fp;
1674 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001675 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001676 errno = 0;
1677 }
1678 return fp;
1679}
1680#endif
1681
Eric Andersene790ddb2000-11-17 06:50:42 +00001682#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001683 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersen82d5e592001-01-31 17:50:35 +00001684 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET
Matt Kraaie0bcce02000-09-27 02:29:39 +00001685FILE *xfopen(const char *path, const char *mode)
1686{
1687 FILE *fp;
1688 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001689 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001690 return fp;
1691}
1692#endif
1693
Eric Andersene5aef922000-11-17 18:25:26 +00001694int applet_name_compare(const void *x, const void *y)
1695{
1696 const struct BB_applet *applet1 = x;
1697 const struct BB_applet *applet2 = y;
1698
1699 return strcmp(applet1->name, applet2->name);
1700}
1701
Matt Kraai24ac0172000-12-18 21:38:57 +00001702#if defined BB_DD || defined BB_TAIL
1703unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes)
1704{
1705 struct suffix_mult *sm;
1706 unsigned long int ret;
1707 int len;
1708 char *end;
1709
1710 ret = strtoul(numstr, &end, 10);
1711 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001712 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001713 while (end[0] != '\0') {
1714 for (sm = suffixes; sm->suffix != NULL; sm++) {
1715 len = strlen(sm->suffix);
1716 if (strncmp(sm->suffix, end, len) == 0) {
1717 ret *= sm->mult;
1718 end += len;
1719 break;
1720 }
1721 }
1722 if (sm->suffix == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001723 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001724 }
1725 return ret;
1726}
1727#endif
1728
Eric Andersen23a93f52001-01-24 23:47:21 +00001729#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001730ssize_t safe_read(int fd, void *buf, size_t count)
1731{
1732 ssize_t n;
1733
1734 do {
1735 n = read(fd, buf, count);
1736 } while (n < 0 && errno == EINTR);
1737
1738 return n;
1739}
1740#endif
1741
Richard June6d0921c2001-01-22 22:35:38 +00001742#ifdef BB_FEATURE_HUMAN_READABLE
1743char *format(unsigned long val, unsigned long hr)
1744{
1745 static char str[10] = "\0";
1746
1747 if(val == 0)
1748 return("0");
1749 if(hr)
1750 snprintf(str, 9, "%ld", val/hr);
1751 else if(val >= GIGABYTE)
1752 snprintf(str, 9, "%.1LfG", ((long double)(val)/GIGABYTE));
1753 else if(val >= MEGABYTE)
1754 snprintf(str, 9, "%.1LfM", ((long double)(val)/MEGABYTE));
1755 else if(val >= KILOBYTE)
1756 snprintf(str, 9, "%.1Lfk", ((long double)(val)/KILOBYTE));
1757 else
1758 snprintf(str, 9, "%ld", (val));
1759 return(str);
1760}
1761#endif
1762
Eric Andersencc8ed391999-10-05 16:24:54 +00001763/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001764/*
1765Local Variables:
1766c-file-style: "linux"
1767c-basic-offset: 4
1768tab-width: 4
1769End:
1770*/