blob: 5c08e5b010ee130f3c03982165a9b8a38d1b5888 [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 Andersenbd22ed82000-07-08 18:55:24 +000053#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000054#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersen246cc6d2000-03-07 07:41:42 +000056/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
57 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000058#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000059//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
60#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
61#endif
62
Erik Andersen5cbdd712000-01-26 20:06:48 +000063
Eric Andersend0246fb1999-11-04 21:18:07 +000064#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000065# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000066const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000067# else
68# if defined BB_FEATURE_USE_PROCFS
69const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070# else
Erik Andersen269bba22000-03-08 14:50:47 +000071# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000072const char mtab_file[] = "/dev/mtab";
73# else
Erik Andersen269bba22000-03-08 14:50:47 +000074# 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 +000075# endif
76# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000077# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000078#endif
79
Eric Andersend73dc5b1999-11-10 23:13:02 +000080extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000081{
Eric Andersenc73a7f92001-01-20 21:41:11 +000082 fprintf(stderr, "%s\n\nUsage: %s\n", full_version, usage);
Matt Kraaie0bcce02000-09-27 02:29:39 +000083 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000084}
85
Mark Whitleyf57c9442000-12-07 19:56:48 +000086static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +000087{
88 fflush(stdout);
89 fprintf(stderr, "%s: ", applet_name);
90 vfprintf(stderr, s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +000091}
92
Mark Whitleyf57c9442000-12-07 19:56:48 +000093extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000094{
95 va_list p;
96
97 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +000098 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000099 va_end(p);
100}
101
Mark Whitleyf57c9442000-12-07 19:56:48 +0000102extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000103{
104 va_list p;
105
106 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000107 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000108 va_end(p);
109 exit(EXIT_FAILURE);
110}
111
Mark Whitleyf57c9442000-12-07 19:56:48 +0000112static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000113{
Eric Andersenc73a7f92001-01-20 21:41:11 +0000114 int err=errno;
115 if(s == 0) s = "";
116 verror_msg(s, p);
117 if (*s) s = ": ";
118 fprintf(stderr, "%s%s\n", s, strerror(err));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000119}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000120
Mark Whitleyf57c9442000-12-07 19:56:48 +0000121extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000122{
123 va_list p;
124
125 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000126 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000127 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000128}
129
Mark Whitleyf57c9442000-12-07 19:56:48 +0000130extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000131{
132 va_list p;
133
134 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000135 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000136 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000137 exit(EXIT_FAILURE);
138}
139
Eric Andersenf5d5e772001-01-24 23:34:48 +0000140#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000141/* Returns kernel version encoded as major*65536 + minor*256 + patch,
142 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000143 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000144 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000145extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000146{
Erik Andersend07ee462000-02-21 21:26:32 +0000147 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000149
Erik Andersend07ee462000-02-21 21:26:32 +0000150 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000151 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 return (0);
153 }
Eric Andersenef936da2000-10-30 17:22:04 +0000154 major = atoi(strtok(name.release, "."));
155 minor = atoi(strtok(NULL, "."));
156 patch = atoi(strtok(NULL, "."));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000158}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000159#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000160
Eric Andersen10dc9d42000-06-26 10:45:52 +0000161
162
163#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000164_syscall1(int, sysinfo, struct sysinfo *, info);
165#endif /* BB_INIT */
166
Eric Andersena57ba4d2000-07-08 19:20:49 +0000167#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000168
169#ifndef __NR_umount2
Mark Whitley59ab0252001-01-23 22:30:04 +0000170static const int __NR_umount2 = 52;
Eric Andersena57ba4d2000-07-08 19:20:49 +0000171#endif
172
173/* Include our own version of <sys/mount.h>, since libc5 doesn't
174 * know about umount2 */
175extern _syscall1(int, umount, const char *, special_file);
176extern _syscall2(int, umount2, const char *, special_file, int, flags);
177extern _syscall5(int, mount, const char *, special_file, const char *, dir,
178 const char *, fstype, unsigned long int, rwflag, const void *, data);
179#endif
180
Eric Andersenc86e5e62000-08-21 19:38:49 +0000181#if defined BB_INSMOD || defined BB_LSMOD
182#ifndef __NR_query_module
Mark Whitley59ab0252001-01-23 22:30:04 +0000183static const int __NR_query_module = 167;
Eric Andersenc86e5e62000-08-21 19:38:49 +0000184#endif
185_syscall5(int, query_module, const char *, name, int, which,
186 void *, buf, size_t, bufsize, size_t*, ret);
187#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000188
189
Erik Andersen23dea9b2000-05-13 00:28:25 +0000190#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000191
Erik Andersen029011b2000-03-04 21:19:32 +0000192#define HASH_SIZE 311 /* Should be prime */
193#define hash_inode(i) ((i) % HASH_SIZE)
194
195static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
196
197/*
198 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
199 * `ino_dev_hashtable', else return 0
200 *
201 * If NAME is a non-NULL pointer to a character pointer, and there is
202 * a match, then set *NAME to the value of the name slot in that
203 * bucket.
204 */
205int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
206{
207 ino_dev_hashtable_bucket_t *bucket;
208
209 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
210 while (bucket != NULL) {
211 if ((bucket->ino == statbuf->st_ino) &&
212 (bucket->dev == statbuf->st_dev))
213 {
214 if (name) *name = bucket->name;
215 return 1;
216 }
217 bucket = bucket->next;
218 }
219 return 0;
220}
221
222/* Add statbuf to statbuf hash table */
223void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
224{
225 int i;
226 size_t s;
227 ino_dev_hashtable_bucket_t *bucket;
228
229 i = hash_inode(statbuf->st_ino);
230 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000231 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000232 bucket->ino = statbuf->st_ino;
233 bucket->dev = statbuf->st_dev;
234 if (name)
235 strcpy(bucket->name, name);
236 else
237 bucket->name[0] = '\0';
238 bucket->next = ino_dev_hashtable[i];
239 ino_dev_hashtable[i] = bucket;
240}
241
242/* Clear statbuf hash table */
243void reset_ino_dev_hashtable(void)
244{
245 int i;
246 ino_dev_hashtable_bucket_t *bucket;
247
248 for (i = 0; i < HASH_SIZE; i++) {
249 while (ino_dev_hashtable[i] != NULL) {
250 bucket = ino_dev_hashtable[i]->next;
251 free(ino_dev_hashtable[i]);
252 ino_dev_hashtable[i] = bucket;
253 }
254 }
255}
256
Erik Andersen23dea9b2000-05-13 00:28:25 +0000257#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000258
Glenn L McGrath5e23e3d2001-01-03 01:38:29 +0000259#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000260/*
261 * Return TRUE if a fileName is a directory.
262 * Nonexistant files return FALSE.
263 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000264int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000265{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000267 int didMalloc = 0;
268
269 if (statBuf == NULL) {
270 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
271 ++didMalloc;
272 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000275 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000277 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000278
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000279 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
280 status = FALSE;
281 }
282 else status = TRUE;
283
284 if (didMalloc) {
285 free(statBuf);
286 statBuf = NULL;
287 }
288 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000289}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000290#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000291
Eric Andersenebee3012000-09-05 22:17:22 +0000292#if defined (BB_AR) || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000293/*
Mark Whitley47583682000-12-05 20:03:17 +0000294 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000295 */
Mark Whitley47583682000-12-05 20:03:17 +0000296int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000297{
298 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000299 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000300
Mark Whitley47583682000-12-05 20:03:17 +0000301 while (chunksize > 0) {
302 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000303 size = BUFSIZ;
304 else
Mark Whitley47583682000-12-05 20:03:17 +0000305 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000306 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000307 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000308 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000309 }
310 return (TRUE);
311}
312#endif
313
314
Erik Andersen23dea9b2000-05-13 00:28:25 +0000315#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000316/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000317 * Copy one file to another, while possibly preserving its modes, times, and
318 * modes. Returns TRUE if successful, or FALSE on a failure with an error
319 * message output. (Failure is not indicated if attributes cannot be set.)
320 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000321 */
322int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000323copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000324 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000325{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 int rfd;
327 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 struct stat srcStatBuf;
330 struct stat dstStatBuf;
331 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000332
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 if (followLinks == TRUE)
334 status = stat(srcName, &srcStatBuf);
335 else
336 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000337
Erik Andersenfac10d72000-02-07 05:29:42 +0000338 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000339 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000341 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342
343 if (followLinks == TRUE)
344 status = stat(destName, &dstStatBuf);
345 else
346 status = lstat(destName, &dstStatBuf);
347
Erik Andersen59b9e872000-05-10 05:05:45 +0000348 if (status < 0 || forceFlag==TRUE) {
349 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 dstStatBuf.st_ino = -1;
351 dstStatBuf.st_dev = -1;
352 }
353
354 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
355 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000356 error_msg("Copying file \"%s\" to itself\n", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 return FALSE;
358 }
359
360 if (S_ISDIR(srcStatBuf.st_mode)) {
361 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
362 /* Make sure the directory is writable */
363 status = mkdir(destName, 0777777 ^ umask(0));
364 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000365 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 return FALSE;
367 }
368 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000369 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 int link_size;
371
372 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000373 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
374 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 if (link_size < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000376 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 return FALSE;
378 }
379 link_val[link_size] = '\0';
380 status = symlink(link_val, destName);
381 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000382 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 return FALSE;
384 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000385#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000387 /* Try to set owner, but fail silently like GNU cp */
388 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000390#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000391 return TRUE;
392 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
393 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
394 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000395 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 return FALSE;
397 }
398 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
399 || S_ISSOCK(srcStatBuf.st_mode)) {
400 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
401 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000402 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 return FALSE;
404 }
405 } else if (S_ISREG(srcStatBuf.st_mode)) {
406 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
407 rfd = open(srcName, O_RDONLY);
408 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000409 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 return FALSE;
411 }
412
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000413 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000414 srcStatBuf.st_mode);
415 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000416 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 close(rfd);
418 return FALSE;
419 }
420
Mark Whitley47583682000-12-05 20:03:17 +0000421 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000422 goto error_exit;
423
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 close(rfd);
425 if (close(wfd) < 0) {
426 return FALSE;
427 }
428 }
429
430 if (setModes == TRUE) {
431 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000432 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
433 perror_msg_and_die("%s", destName);
434 if (chmod(destName, srcStatBuf.st_mode) < 0)
435 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 times.actime = srcStatBuf.st_atime;
437 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000438 if (utime(destName, &times) < 0)
439 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 }
441
Erik Andersenfac10d72000-02-07 05:29:42 +0000442 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000443
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000445 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000446 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000448
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000450}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000451#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000452
453
454
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000455#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000456
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000457#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
458#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
459
460/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
461static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462 0, 0, S_ISUID,
463 0, 0, S_ISGID,
464 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000465};
466
467/* The 9 mode bits to test */
468static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 S_IRUSR, S_IWUSR, S_IXUSR,
470 S_IRGRP, S_IWGRP, S_IXGRP,
471 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000472};
473
Mark Whitley26a3adb2000-12-05 20:10:27 +0000474static const char MODE1[] = "rwxrwxrwx";
475static const char MODE0[] = "---------";
476static const char SMODE1[] = "..s..s..t";
477static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000478
Eric Andersencc8ed391999-10-05 16:24:54 +0000479/*
480 * Return the standard ls-like mode string from a file mode.
481 * This is static and so is overwritten on each call.
482 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000483const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000484{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000486
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 int i;
488
489 buf[0] = TYPECHAR(mode);
490 for (i = 0; i < 9; i++) {
491 if (mode & SBIT[i])
492 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
493 else
494 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
495 }
496 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000497}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000498#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000499
500
Pavel Roskin21ddb382000-07-17 19:31:54 +0000501#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000502/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000503 * Return the standard ls-like time string from a time_t
504 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000505 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000506const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000507{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 time_t now;
509 char *str;
510 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000511
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000513
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000515
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 strcpy(buf, &str[4]);
517 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
520 strcpy(&buf[7], &str[20]);
521 buf[11] = '\0';
522 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000523
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000525}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000526#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000527
Eric Andersen23a93f52001-01-24 23:47:21 +0000528#if defined BB_DD || defined BB_NC || defined BB_TAIL || defined BB_TAR
Eric Andersen17d49ef1999-10-06 20:25:32 +0000529/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000530 * Write all of the supplied buffer out to a file.
531 * This does multiple writes as necessary.
532 * Returns the amount written, or -1 on an error.
533 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000534int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000535{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 int cc;
537 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000538
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000540
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 while (len > 0) {
542 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000543
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 if (cc < 0)
545 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000546
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 buf += cc;
548 total += cc;
549 len -= cc;
550 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000553}
Matt Kraai24ac0172000-12-18 21:38:57 +0000554#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000555
Matt Kraai24ac0172000-12-18 21:38:57 +0000556#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000557/*
558 * Read all of the supplied buffer from a file.
559 * This does multiple reads as necessary.
560 * Returns the amount read, or -1 on an error.
561 * A short read is returned on an end of file.
562 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000563int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000564{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 int cc;
566 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000567
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000569
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570 while (len > 0) {
571 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000572
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 if (cc < 0)
574 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000575
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 if (cc == 0)
577 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000578
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 buf += cc;
580 total += cc;
581 len -= cc;
582 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000583
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000585}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000586#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000587
588
Erik Andersen23dea9b2000-05-13 00:28:25 +0000589#if defined (BB_CHMOD_CHOWN_CHGRP) \
590 || defined (BB_CP_MV) \
591 || defined (BB_FIND) \
592 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000593 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000594 || defined (BB_RM) \
595 || defined (BB_TAR)
596
Eric Andersencc8ed391999-10-05 16:24:54 +0000597/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000598 * Walk down all the directories under the specified
599 * location, and do something (something specified
600 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000601 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000602 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000603 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000604 * and so isn't sufficiently portable to take over since glibc2.1
605 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000606 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000607int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 int recurse, int followLinks, int depthFirst,
609 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000610 struct stat * statbuf,
611 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000613 struct stat * statbuf,
614 void* userData),
615 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000616{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 int status;
618 struct stat statbuf;
619 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000620
Erik Andersene49d5ec2000-02-08 19:58:47 +0000621 if (followLinks == TRUE)
622 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000623 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000625
Eric Andersen2b69c401999-10-05 22:58:32 +0000626 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627#ifdef BB_DEBUG_PRINT_SCAFFOLD
628 fprintf(stderr,
629 "status=%d followLinks=%d TRUE=%d\n",
630 status, followLinks, TRUE);
631#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000632 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000633 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000634 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635
636 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
637 if (fileAction == NULL)
638 return TRUE;
639 else
Erik Andersen3364d782000-03-28 00:58:14 +0000640 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641 }
642
643 if (recurse == FALSE) {
644 if (S_ISDIR(statbuf.st_mode)) {
645 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000646 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 else
648 return TRUE;
649 }
650 }
651
652 if (S_ISDIR(statbuf.st_mode)) {
653 DIR *dir;
654
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000656 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000658 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000660 } else if (status == SKIP)
661 return TRUE;
662 }
663 dir = opendir(fileName);
664 if (!dir) {
665 perror_msg("%s", fileName);
666 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667 }
668 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000669 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670
671 if ((strcmp(next->d_name, "..") == 0)
672 || (strcmp(next->d_name, ".") == 0)) {
673 continue;
674 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000675 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000676 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 return FALSE;
678 }
Erik Andersenc3660502000-04-18 21:18:57 +0000679 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680 sprintf(nextFile, "%s/%s", fileName, next->d_name);
681 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000682 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000683 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000684 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 closedir(dir);
686 return FALSE;
687 }
688 }
689 status = closedir(dir);
690 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000691 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 return FALSE;
693 }
694 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000695 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000696 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000697 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000698 return FALSE;
699 }
700 }
701 } else {
702 if (fileAction == NULL)
703 return TRUE;
704 else
Erik Andersen3364d782000-03-28 00:58:14 +0000705 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 }
707 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000708}
709
Erik Andersen23dea9b2000-05-13 00:28:25 +0000710#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000711
Eric Andersenf6be9441999-10-13 21:12:06 +0000712
713
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000714#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000715/*
716 * Attempt to create the directories along the specified path, except for
717 * the final component. The mode is given for the final directory only,
718 * while all previous ones get default protections. Errors are not reported
719 * here, as failures to restore files can be reported later.
720 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000721extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000722{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 char *cp;
724 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000725 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000727
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 strcpy(buf, name);
729 for (cp = buf; *cp == '/'; cp++);
730 cp = strchr(cp, '/');
731 while (cp) {
732 cpOld = cp;
733 cp = strchr(cp + 1, '/');
734 *cpOld = '\0';
735 retVal = mkdir(buf, cp ? 0777 : mode);
736 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000737 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 return FALSE;
739 }
740 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000741 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000743}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000744#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000745
746
747
Pavel Roskin21ddb382000-07-17 19:31:54 +0000748#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000749 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000750/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000751
752
753
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000755{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000756 mode_t andMode =
757
758 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000759 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760 mode_t mode = 0;
761 mode_t groups = 0;
762 char type;
763 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000764
Eric Andersen4e61db22000-09-01 00:06:17 +0000765 if (s==NULL)
766 return (FALSE);
767
Eric Andersenf6be9441999-10-13 21:12:06 +0000768 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 for (;;) {
770 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000771 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000772 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000775 continue;
776 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000778 continue;
779 case 'o':
780 groups |= S_IRWXO;
781 continue;
782 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000784 continue;
785 case '+':
786 case '=':
787 case '-':
788 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 if (groups == 0) /* The default is "all" */
790 groups |=
791 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000792 break;
793 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 if (isdigit(c) && c >= '0' && c <= '7' &&
795 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000796 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000797 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000799 return (FALSE);
800 }
801 break;
802 }
803
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 while ((c = *s++) != '\0') {
805 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000806 case ',':
807 break;
808 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000810 continue;
811 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000812 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000813 continue;
814 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000816 continue;
817 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000819 continue;
820 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000821 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000822 continue;
823 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000824 *theMode &= andMode;
825 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000827 }
828 break;
829 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000831 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000832 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000833 /* fall through */
834 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000835 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000836 break;
837 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000838 andMode &= ~(mode & groups);
839 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000840 break;
841 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000843 *theMode &= andMode;
844 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000845 return (TRUE);
846}
Eric Andersence8f3b91999-10-20 07:03:36 +0000847
848
Pavel Roskin21ddb382000-07-17 19:31:54 +0000849#endif
850/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000851
Eric Andersene674eb71999-10-19 20:52:57 +0000852
853
Erik Andersen23dea9b2000-05-13 00:28:25 +0000854
855
Pavel Roskin21ddb382000-07-17 19:31:54 +0000856#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
857 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000858 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Erik Andersen23dea9b2000-05-13 00:28:25 +0000859
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000860/* This parses entries in /etc/passwd and /etc/group. This is desirable
861 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
862 * increases target size and is often not needed or wanted for embedded
863 * systems.
864 *
865 * /etc/passwd entries look like this:
866 * root:x:0:0:root:/root:/bin/bash
867 * and /etc/group entries look like this:
868 * root:x:0:
869 *
870 * This uses buf as storage to hold things.
871 *
872 */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000873unsigned long my_getid(const char *filename, char *name, long id, long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000874{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000875 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000876 char *rname, *start, *end, buf[128];
Eric Andersenfad04fd2000-07-14 06:49:52 +0000877 long rid;
878 long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000879
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000881 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000882 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000883 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000884 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000885 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000886
Erik Andersene49d5ec2000-02-08 19:58:47 +0000887 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000888 if (buf[0] == '#')
889 continue;
890
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000891 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000892 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000894 if (end == NULL)
895 continue;
896 *end = '\0';
897 rname = start;
898
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000899 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000900 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000902 if (end == NULL)
903 continue;
904
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000905 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000906 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000907 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000908 if (end == start)
909 continue;
910
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000911 /* gid in passwd */
912 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000913 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000914
Eric Andersend23f9ba1999-10-20 19:18:15 +0000915 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000916 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000917 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 fclose(file);
919 return (rid);
920 }
921 }
922 if (id != -1 && id == rid) {
923 strncpy(name, rname, 8);
Eric Andersen2d6282b2000-12-11 20:33:46 +0000924 name[8]='\0';
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000925 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 fclose(file);
927 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000928 }
929 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000930 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000931 return (-1);
932}
933
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000934/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000935long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000936{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000937 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000938}
939
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000940/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000941long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000942{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000943 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000944}
945
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000946/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000947void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000948{
Eric Andersenbd193a42000-12-13 01:52:39 +0000949 name[0] = '\0';
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000950 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000951}
952
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000953/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000954void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000955{
Eric Andersenbd193a42000-12-13 01:52:39 +0000956 group[0] = '\0';
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000957 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000958}
959
Matt Kraaie6e81832000-12-30 07:46:23 +0000960#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000961/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000962long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000963{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000964 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000965 my_getid("/etc/passwd", name, -1, &gid);
966 return gid;
967}
Matt Kraaie6e81832000-12-30 07:46:23 +0000968#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +0000969
Pavel Roskin21ddb382000-07-17 19:31:54 +0000970#endif
971 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
972 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000973
974
Eric Andersenf86aee72000-07-06 17:20:46 +0000975#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000976
Eric Andersenbd22ed82000-07-08 18:55:24 +0000977/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000978static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
979static const int KB_84 = 0x01;
980static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000981
Erik Andersene49d5ec2000-02-08 19:58:47 +0000982int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000983{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000984 char arg;
985
986 arg = 0;
987 return (ioctl(fd, KDGKBTYPE, &arg) == 0
988 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000989}
990
Erik Andersene49d5ec2000-02-08 19:58:47 +0000991static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000992{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000993 int fd;
994
995 /* try read-only */
996 fd = open(fnam, O_RDWR);
997
998 /* if failed, try read-only */
999 if (fd < 0 && errno == EACCES)
1000 fd = open(fnam, O_RDONLY);
1001
1002 /* if failed, try write-only */
1003 if (fd < 0 && errno == EACCES)
1004 fd = open(fnam, O_WRONLY);
1005
1006 /* if failed, fail */
1007 if (fd < 0)
1008 return -1;
1009
1010 /* if not a console, fail */
1011 if (!is_a_console(fd)) {
1012 close(fd);
1013 return -1;
1014 }
1015
1016 /* success */
1017 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001018}
1019
1020/*
1021 * Get an fd for use with kbd/console ioctls.
1022 * We try several things because opening /dev/console will fail
1023 * if someone else used X (which does a chown on /dev/console).
1024 *
1025 * if tty_name is non-NULL, try this one instead.
1026 */
1027
Erik Andersene49d5ec2000-02-08 19:58:47 +00001028int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001029{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001030 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001031
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 if (tty_name) {
1033 if (-1 == (fd = open_a_console(tty_name)))
1034 return -1;
1035 else
1036 return fd;
1037 }
1038
1039 fd = open_a_console("/dev/tty");
1040 if (fd >= 0)
1041 return fd;
1042
1043 fd = open_a_console("/dev/tty0");
1044 if (fd >= 0)
1045 return fd;
1046
1047 fd = open_a_console("/dev/console");
1048 if (fd >= 0)
1049 return fd;
1050
1051 for (fd = 0; fd < 3; fd++)
1052 if (is_a_console(fd))
1053 return fd;
1054
Mark Whitleyf57c9442000-12-07 19:56:48 +00001055 error_msg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001057}
1058
1059
Eric Andersenf86aee72000-07-06 17:20:46 +00001060#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001061
1062
Eric Andersen9f16d612000-06-12 23:11:16 +00001063#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001064/*
1065 * Routine to see if a text string is matched by a wildcard pattern.
1066 * Returns TRUE if the text is matched, or FALSE if it is not matched
1067 * or if the pattern is invalid.
1068 * * matches zero or more characters
1069 * ? matches a single character
1070 * [abc] matches 'a', 'b' or 'c'
1071 * \c quotes character c
1072 * Adapted from code written by Ingo Wilken, and
1073 * then taken from sash, Copyright (c) 1999 by David I. Bell
1074 * Permission is granted to use, distribute, or modify this source,
1075 * provided that this copyright notice remains intact.
1076 * Permission to distribute this code under the GPL has been granted.
1077 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001079{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 const char *retryPat;
1081 const char *retryText;
1082 int ch;
1083 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001084 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001085
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 retryPat = NULL;
1087 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001088
Erik Andersene49d5ec2000-02-08 19:58:47 +00001089 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001090 ch = *pattern++;
1091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 switch (ch) {
1093 case '*':
1094 retryPat = pattern;
1095 retryText = text;
1096 break;
1097
1098 case '[':
1099 found = FALSE;
1100
1101 while ((ch = *pattern++) != ']') {
1102 if (ch == '\\')
1103 ch = *pattern++;
1104
1105 if (ch == '\0')
1106 return FALSE;
1107
1108 if (*text == ch)
1109 found = TRUE;
1110 }
Erik Andersen632bb572000-04-18 22:09:06 +00001111 len=strlen(text);
1112 if (found == FALSE && len!=0) {
1113 return FALSE;
1114 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001116 if (strlen(pattern)==0 && len==1) {
1117 return TRUE;
1118 }
1119 if (len!=0) {
1120 text++;
1121 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001122 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001123 }
1124
1125 /* fall into next case */
1126
1127 case '?':
1128 if (*text++ == '\0')
1129 return FALSE;
1130
1131 break;
1132
1133 case '\\':
1134 ch = *pattern++;
1135
1136 if (ch == '\0')
1137 return FALSE;
1138
1139 /* fall into next case */
1140
1141 default:
1142 if (*text == ch) {
1143 if (*text)
1144 text++;
1145 break;
1146 }
1147
1148 if (*text) {
1149 pattern = retryPat;
1150 text = ++retryText;
1151 break;
1152 }
1153
Eric Andersenb186d981999-12-03 09:19:54 +00001154 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001155 }
1156
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 if (pattern == NULL)
1158 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001159 }
1160
Erik Andersene49d5ec2000-02-08 19:58:47 +00001161 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001162}
Eric Andersen9f16d612000-06-12 23:11:16 +00001163#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001164
1165
Eric Andersen29d2e361999-11-06 06:07:27 +00001166
1167
Erik Andersen23dea9b2000-05-13 00:28:25 +00001168#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001169/*
1170 * Given a block device, find the mount table entry if that block device
1171 * is mounted.
1172 *
1173 * Given any other file (or directory), find the mount table entry for its
1174 * filesystem.
1175 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001176extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001177{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001178 struct stat s;
1179 dev_t mountDevice;
1180 FILE *mountTable;
1181 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001182
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 if (stat(name, &s) != 0)
1184 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001185
Erik Andersene49d5ec2000-02-08 19:58:47 +00001186 if ((s.st_mode & S_IFMT) == S_IFBLK)
1187 mountDevice = s.st_rdev;
1188 else
1189 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001190
1191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 if ((mountTable = setmntent(table, "r")) == 0)
1193 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001194
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 while ((mountEntry = getmntent(mountTable)) != 0) {
1196 if (strcmp(name, mountEntry->mnt_dir) == 0
1197 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1198 break;
1199 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1200 break;
1201 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1202 break;
1203 }
1204 endmntent(mountTable);
1205 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001206}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001207#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001208
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001209#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001210/* try to open up the specified device */
1211extern int device_open(char *device, int mode)
1212{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001214
Erik Andersene49d5ec2000-02-08 19:58:47 +00001215 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001216
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 /* Retry up to 5 times */
1218 for (f = 0; f < 5; f++)
1219 if ((fd = open(device, m, 0600)) >= 0)
1220 break;
1221 if (fd < 0)
1222 return fd;
1223 /* Reset original flags. */
1224 if (m != mode)
1225 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001226 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001227}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001228#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001229
1230
Erik Andersen23dea9b2000-05-13 00:28:25 +00001231#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001232#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001233#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001234#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001235
Erik Andersen23dea9b2000-05-13 00:28:25 +00001236#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001237/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001238 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001239 * This finds the pid of the specified process,
1240 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001241 *
Erik Andersen825aead2000-04-07 06:00:07 +00001242 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001243 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001244extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001245{
Erik Andersen825aead2000-04-07 06:00:07 +00001246 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001247 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001248 pid_t num_pids;
1249 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001250 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001251
1252 /* open device */
1253 fd = open(device, O_RDONLY);
1254 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001255 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001256
1257 /* Find out how many processes there are */
1258 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001259 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001260
1261 /* Allocate some memory -- grab a few extras just in case
1262 * some new processes start up while we wait. The kernel will
1263 * just ignore any extras if we give it too many, and will trunc.
1264 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001265 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001266 pid_array[0] = num_pids+10;
1267
1268 /* Now grab the pid list */
1269 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001270 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001271
1272 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001273 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001274 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001275 struct pid_info info;
1276
1277 info.pid = pid_array[i];
1278 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001279 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001280
Erik Andersen0a64de92000-04-18 20:00:03 +00001281 /* Make sure we only match on the process name */
1282 p=info.command_line+1;
1283 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1284 (p)++;
1285 }
1286 if (isspace(*(p)))
1287 *p='\0';
1288
Erik Andersenb9167cb2000-04-18 22:41:30 +00001289 if ((strstr(info.command_line, pidName) != NULL)
1290 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001291 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001292 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001293 }
1294 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001295 if (pidList)
1296 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001297
1298 /* Free memory */
1299 free( pid_array);
1300
1301 /* close device */
1302 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001303 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001304
Erik Andersen825aead2000-04-07 06:00:07 +00001305 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001306}
Erik Andersen269bba22000-03-08 14:50:47 +00001307#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001308#if ! defined BB_FEATURE_USE_PROCFS
1309#error Sorry, I depend on the /proc filesystem right now.
1310#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001311
Mark Whitleyf57c9442000-12-07 19:56:48 +00001312/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001313 *
1314 * This finds the pid of the specified process.
1315 * Currently, it's implemented by rummaging through
1316 * the proc filesystem.
1317 *
Erik Andersen825aead2000-04-07 06:00:07 +00001318 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001319 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001320extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001321{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001322 DIR *dir;
1323 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001324 pid_t* pidList=NULL;
1325 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001326
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001327 dir = opendir("/proc");
1328 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001329 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001330
1331 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001332 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001333 char filename[256];
1334 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001335
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001336 /* If it isn't a number, we don't want it */
1337 if (!isdigit(*next->d_name))
1338 continue;
1339
Eric Andersen31e68292000-09-14 21:12:20 +00001340 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001341 status = fopen(filename, "r");
1342 if (!status) {
1343 continue;
1344 }
1345 fgets(buffer, 256, status);
1346 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001347
Eric Andersen31e68292000-09-14 21:12:20 +00001348 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001349 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001350 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001351 }
John Beppuf95ca971999-12-09 22:10:18 +00001352 }
Eric Andersen31e68292000-09-14 21:12:20 +00001353
Erik Andersen0a64de92000-04-18 20:00:03 +00001354 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001355 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001356 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001357}
Erik Andersen269bba22000-03-08 14:50:47 +00001358#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001359#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001360
Eric Andersenae6eae02000-07-19 17:35:54 +00001361#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001362/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001363extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001364{
Eric Andersenae6eae02000-07-19 17:35:54 +00001365 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001366
Eric Andersenae6eae02000-07-19 17:35:54 +00001367 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001368 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001369 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001370}
1371
Eric Andersen501c88b2000-07-28 15:14:45 +00001372extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001373{
1374 void *ptr = realloc(old, size);
1375 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001376 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001377 return ptr;
1378}
Eric Andersen501c88b2000-07-28 15:14:45 +00001379
1380extern void *xcalloc(size_t nmemb, size_t size)
1381{
1382 void *ptr = calloc(nmemb, size);
1383 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001384 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001385 return ptr;
1386}
Eric Andersenae6eae02000-07-19 17:35:54 +00001387#endif
1388
Eric Andersenf5d5e772001-01-24 23:34:48 +00001389#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET
Eric Andersenae6eae02000-07-19 17:35:54 +00001390# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001391extern char * xstrdup (const char *s) {
1392 char *t;
1393
1394 if (s == NULL)
1395 return NULL;
1396
1397 t = strdup (s);
1398
1399 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001400 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001401
1402 return t;
1403}
Eric Andersenae6eae02000-07-19 17:35:54 +00001404# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001405#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001406
Eric Andersenf5d5e772001-01-24 23:34:48 +00001407#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001408extern char * xstrndup (const char *s, int n) {
1409 char *t;
1410
1411 if (s == NULL)
Matt Kraai0dab8292000-12-18 03:08:29 +00001412 error_msg_and_die("xstrndup bug\n");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001413
1414 t = xmalloc(n+1);
1415 strncpy(t,s,n);
1416 t[n] = 0;
1417
1418 return t;
1419}
1420#endif
1421
1422
Erik Andersen23dea9b2000-05-13 00:28:25 +00001423#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001424extern int vdprintf(int d, const char *format, va_list ap)
1425{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001426 char buf[BUF_SIZE];
1427 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001428
Erik Andersene49d5ec2000-02-08 19:58:47 +00001429 len = vsprintf(buf, format, ap);
1430 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001431}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001432#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001433
Eric Andersen624cc772000-09-21 02:04:51 +00001434
Erik Andersen23dea9b2000-05-13 00:28:25 +00001435#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001436#include <fcntl.h>
1437#include "loop.h" /* Pull in loop device support */
1438
Erik Andersen5cbdd712000-01-26 20:06:48 +00001439extern int del_loop(const char *device)
1440{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001441 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001442
Erik Andersene49d5ec2000-02-08 19:58:47 +00001443 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001444 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001445 return (FALSE);
1446 }
1447 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001448 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 return (FALSE);
1450 }
1451 close(fd);
1452 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001453}
Erik Andersende7965c2000-01-26 23:49:21 +00001454
Erik Andersene49d5ec2000-02-08 19:58:47 +00001455extern int set_loop(const char *device, const char *file, int offset,
1456 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001457{
1458 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001459 int fd, ffd, mode;
1460
Erik Andersende7965c2000-01-26 23:49:21 +00001461 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001462 if ((ffd = open(file, mode)) < 0 && !*loopro
1463 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001464 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001465 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001466 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001467 if ((fd = open(device, mode)) < 0) {
1468 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001469 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001470 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001471 }
1472 *loopro = (mode == O_RDONLY);
1473
1474 memset(&loopinfo, 0, sizeof(loopinfo));
1475 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001476 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001477
1478 loopinfo.lo_offset = offset;
1479
1480 loopinfo.lo_encrypt_key_size = 0;
1481 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001482 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001483 close(fd);
1484 close(ffd);
1485 return 1;
1486 }
1487 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1488 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001489 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001490 close(fd);
1491 close(ffd);
1492 return 1;
1493 }
1494 close(fd);
1495 close(ffd);
1496 return 0;
1497}
1498
Erik Andersene49d5ec2000-02-08 19:58:47 +00001499extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001500{
1501 char dev[20];
1502 int i, fd;
1503 struct stat statbuf;
1504 struct loop_info loopinfo;
1505
Erik Andersene49d5ec2000-02-08 19:58:47 +00001506 for (i = 0; i <= 7; i++) {
1507 sprintf(dev, "/dev/loop%d", i);
1508 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1509 if ((fd = open(dev, O_RDONLY)) >= 0) {
1510 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1511 if (errno == ENXIO) { /* probably free */
1512 close(fd);
1513 return strdup(dev);
1514 }
1515 }
1516 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001517 }
Erik Andersende7965c2000-01-26 23:49:21 +00001518 }
Erik Andersende7965c2000-01-26 23:49:21 +00001519 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001520 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001521}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001522#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001523
Erik Andersen23dea9b2000-05-13 00:28:25 +00001524#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001525extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001526{
Erik Andersen016ffe92000-03-22 00:58:54 +00001527 DIR *dir;
1528 struct dirent *entry;
1529 struct stat statBuf, rootStat;
1530 char fileName[BUFSIZ];
1531
Erik Andersenec5bd902000-03-22 07:12:05 +00001532 if (stat("/", &rootStat) != 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001533 error_msg("could not stat '/'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001534 return( FALSE);
1535 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001536
Erik Andersenec5bd902000-03-22 07:12:05 +00001537 dir = opendir("/dev");
1538 if (!dir) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001539 error_msg("could not open '/dev'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001540 return( FALSE);
1541 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001542
1543 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001544
Erik Andersen016ffe92000-03-22 00:58:54 +00001545 /* Must skip ".." since that is "/", and so we
1546 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001547 if (strcmp(entry->d_name, "..") == 0)
1548 continue;
1549
Eric Andersenf9ca6532000-08-11 17:45:02 +00001550 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001551
1552 if (stat(fileName, &statBuf) != 0)
1553 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001554 /* Some char devices have the same dev_t as block
1555 * devices, so make sure this is a block device */
1556 if (! S_ISBLK(statBuf.st_mode))
1557 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001558 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001559 strcpy(name, fileName);
1560 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001561 }
1562 }
1563
Erik Andersenec5bd902000-03-22 07:12:05 +00001564 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001565}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001566#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001567
Mark Whitley1ca41772000-06-28 22:15:26 +00001568
1569/* get_line_from_file() - This function reads an entire line from a text file
1570 * up to a newline. It returns a malloc'ed char * which must be stored and
1571 * free'ed by the caller. */
1572extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001573{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001574 static const int GROWBY = 80; /* how large we will grow strings by */
1575
Mark Whitley1ca41772000-06-28 22:15:26 +00001576 int ch;
1577 int idx = 0;
1578 char *linebuf = NULL;
1579 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001580
Mark Whitley1ca41772000-06-28 22:15:26 +00001581 while (1) {
1582 ch = fgetc(file);
1583 if (ch == EOF)
1584 break;
1585 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001586 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001587 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001588 linebuf[idx++] = (char)ch;
1589 if ((char)ch == '\n')
1590 break;
1591 }
John Beppu5a728cf2000-04-17 04:22:09 +00001592
Mark Whitley1ca41772000-06-28 22:15:26 +00001593 if (idx == 0)
1594 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001595
Mark Whitley1ca41772000-06-28 22:15:26 +00001596 linebuf[idx] = 0;
1597 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001598}
Erik Andersen016ffe92000-03-22 00:58:54 +00001599
Eric Andersen944be902000-09-25 16:58:39 +00001600#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001601extern void print_file(FILE *file)
1602{
1603 int c;
1604
1605 while ((c = getc(file)) != EOF)
1606 putc(c, stdout);
1607 fclose(file);
1608 fflush(stdout);
1609}
1610
1611extern int print_file_by_name(char *filename)
1612{
1613 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001614 if ((file = wfopen(filename, "r")) == NULL)
1615 return FALSE;
1616 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001617 return TRUE;
1618}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001619#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001620
Matt Kraaicabafae2000-11-30 04:55:40 +00001621#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001622char process_escape_sequence(char **ptr)
1623{
1624 char c;
1625
1626 switch (c = *(*ptr)++) {
1627 case 'a':
1628 c = '\a';
1629 break;
1630 case 'b':
1631 c = '\b';
1632 break;
1633 case 'f':
1634 c = '\f';
1635 break;
1636 case 'n':
1637 c = '\n';
1638 break;
Eric Andersen9bd71e82000-11-29 23:07:57 +00001639 case 'r':
1640 c = '\r';
1641 break;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001642 case 't':
1643 c = '\t';
1644 break;
1645 case 'v':
1646 c = '\v';
1647 break;
1648 case '\\':
1649 c = '\\';
1650 break;
1651 case '0': case '1': case '2': case '3':
1652 case '4': case '5': case '6': case '7':
1653 c -= '0';
1654 if ('0' <= **ptr && **ptr <= '7') {
1655 c = c * 8 + (*(*ptr)++ - '0');
1656 if ('0' <= **ptr && **ptr <= '7')
1657 c = c * 8 + (*(*ptr)++ - '0');
1658 }
1659 break;
1660 default:
1661 (*ptr)--;
1662 c = '\\';
1663 break;
1664 }
1665 return c;
1666}
1667#endif
1668
Eric Andersenba1ff9b2000-10-01 17:01:36 +00001669#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || defined BB_FEATURE_USE_PROCFS
Eric Andersen0b4551f2000-07-10 16:44:03 +00001670char *get_last_path_component(char *path)
1671{
1672 char *s=path+strlen(path)-1;
1673
1674 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001675 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001676 *s-- = '\0';
1677 }
1678
1679 /* find last component */
1680 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001681 if (s == NULL || s[1] == '\0')
1682 return path;
1683 else
1684 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001685}
1686#endif
1687
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001688#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001689void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001690{
1691 int ret;
1692 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1693 int errmsgsz = regerror(ret, preg, NULL, 0);
1694 char *errmsg = xmalloc(errmsgsz);
1695 regerror(ret, preg, errmsg, errmsgsz);
Mark Whitleyf57c9442000-12-07 19:56:48 +00001696 error_msg_and_die("xregcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001697 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001698}
1699#endif
1700
Eric Andersen7a86e612000-10-09 18:21:44 +00001701#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001702FILE *wfopen(const char *path, const char *mode)
1703{
1704 FILE *fp;
1705 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001706 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001707 errno = 0;
1708 }
1709 return fp;
1710}
1711#endif
1712
Eric Andersene790ddb2000-11-17 06:50:42 +00001713#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001714 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersen23a93f52001-01-24 23:47:21 +00001715 || defined BB_WC || defined BB_CMP || defined BB_SORT
Matt Kraaie0bcce02000-09-27 02:29:39 +00001716FILE *xfopen(const char *path, const char *mode)
1717{
1718 FILE *fp;
1719 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001720 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001721 return fp;
1722}
1723#endif
1724
Eric Andersene5aef922000-11-17 18:25:26 +00001725int applet_name_compare(const void *x, const void *y)
1726{
1727 const struct BB_applet *applet1 = x;
1728 const struct BB_applet *applet2 = y;
1729
1730 return strcmp(applet1->name, applet2->name);
1731}
1732
Matt Kraai24ac0172000-12-18 21:38:57 +00001733#if defined BB_DD || defined BB_TAIL
1734unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes)
1735{
1736 struct suffix_mult *sm;
1737 unsigned long int ret;
1738 int len;
1739 char *end;
1740
1741 ret = strtoul(numstr, &end, 10);
1742 if (numstr == end)
1743 error_msg_and_die("invalid number `%s'\n", numstr);
1744 while (end[0] != '\0') {
1745 for (sm = suffixes; sm->suffix != NULL; sm++) {
1746 len = strlen(sm->suffix);
1747 if (strncmp(sm->suffix, end, len) == 0) {
1748 ret *= sm->mult;
1749 end += len;
1750 break;
1751 }
1752 }
1753 if (sm->suffix == NULL)
1754 error_msg_and_die("invalid number `%s'\n", numstr);
1755 }
1756 return ret;
1757}
1758#endif
1759
Eric Andersen23a93f52001-01-24 23:47:21 +00001760#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001761ssize_t safe_read(int fd, void *buf, size_t count)
1762{
1763 ssize_t n;
1764
1765 do {
1766 n = read(fd, buf, count);
1767 } while (n < 0 && errno == EINTR);
1768
1769 return n;
1770}
1771#endif
1772
Richard June6d0921c2001-01-22 22:35:38 +00001773#ifdef BB_FEATURE_HUMAN_READABLE
1774char *format(unsigned long val, unsigned long hr)
1775{
1776 static char str[10] = "\0";
1777
1778 if(val == 0)
1779 return("0");
1780 if(hr)
1781 snprintf(str, 9, "%ld", val/hr);
1782 else if(val >= GIGABYTE)
1783 snprintf(str, 9, "%.1LfG", ((long double)(val)/GIGABYTE));
1784 else if(val >= MEGABYTE)
1785 snprintf(str, 9, "%.1LfM", ((long double)(val)/MEGABYTE));
1786 else if(val >= KILOBYTE)
1787 snprintf(str, 9, "%.1Lfk", ((long double)(val)/KILOBYTE));
1788 else
1789 snprintf(str, 9, "%ld", (val));
1790 return(str);
1791}
1792#endif
1793
Eric Andersencc8ed391999-10-05 16:24:54 +00001794/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001795/*
1796Local Variables:
1797c-file-style: "linux"
1798c-basic-offset: 4
1799tab-width: 4
1800End:
1801*/