blob: f222518950b2c114f01d6cda0cfd560ef3809e51 [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{
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000082 fprintf(stderr, "%s\n\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 fprintf(stderr, "Usage: %s\n", usage);
Matt Kraaie0bcce02000-09-27 02:29:39 +000084 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000085}
86
Mark Whitleyf57c9442000-12-07 19:56:48 +000087static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +000088{
89 fflush(stdout);
90 fprintf(stderr, "%s: ", applet_name);
91 vfprintf(stderr, s, p);
92 fflush(stderr);
93}
94
Mark Whitleyf57c9442000-12-07 19:56:48 +000095extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000096{
97 va_list p;
98
99 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000100 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000101 va_end(p);
102}
103
Mark Whitleyf57c9442000-12-07 19:56:48 +0000104extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000105{
106 va_list p;
107
108 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000109 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000110 va_end(p);
111 exit(EXIT_FAILURE);
112}
113
Mark Whitleyf57c9442000-12-07 19:56:48 +0000114static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000115{
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000116 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +0000117 fprintf(stderr, "%s: ", applet_name);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000118 if (s && *s) {
119 vfprintf(stderr, s, p);
120 fputs(": ", stderr);
121 }
122 fprintf(stderr, "%s\n", strerror(errno));
Erik Andersen3522eb12000-03-12 23:49:18 +0000123 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000124}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000125
Mark Whitleyf57c9442000-12-07 19:56:48 +0000126extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000127{
128 va_list p;
129
130 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000131 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000132 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000133}
134
Mark Whitleyf57c9442000-12-07 19:56:48 +0000135extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000136{
137 va_list p;
138
139 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000140 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000141 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000142 exit(EXIT_FAILURE);
143}
144
Eric Andersena2d61e72000-12-12 16:55:56 +0000145#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000146/* Returns kernel version encoded as major*65536 + minor*256 + patch,
147 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000148 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000149 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000150extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000151{
Erik Andersend07ee462000-02-21 21:26:32 +0000152 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000154
Erik Andersend07ee462000-02-21 21:26:32 +0000155 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000156 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 return (0);
158 }
Eric Andersenef936da2000-10-30 17:22:04 +0000159 major = atoi(strtok(name.release, "."));
160 minor = atoi(strtok(NULL, "."));
161 patch = atoi(strtok(NULL, "."));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000163}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000164#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000165
Eric Andersen10dc9d42000-06-26 10:45:52 +0000166
167
168#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000169_syscall1(int, sysinfo, struct sysinfo *, info);
170#endif /* BB_INIT */
171
Eric Andersena57ba4d2000-07-08 19:20:49 +0000172#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000173
174#ifndef __NR_umount2
175#define __NR_umount2 52
176#endif
177
178/* Include our own version of <sys/mount.h>, since libc5 doesn't
179 * know about umount2 */
180extern _syscall1(int, umount, const char *, special_file);
181extern _syscall2(int, umount2, const char *, special_file, int, flags);
182extern _syscall5(int, mount, const char *, special_file, const char *, dir,
183 const char *, fstype, unsigned long int, rwflag, const void *, data);
184#endif
185
Eric Andersenc86e5e62000-08-21 19:38:49 +0000186#if defined BB_INSMOD || defined BB_LSMOD
187#ifndef __NR_query_module
188#define __NR_query_module 167
189#endif
190_syscall5(int, query_module, const char *, name, int, which,
191 void *, buf, size_t, bufsize, size_t*, ret);
192#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000193
194
Erik Andersen23dea9b2000-05-13 00:28:25 +0000195#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000196
Erik Andersen029011b2000-03-04 21:19:32 +0000197#define HASH_SIZE 311 /* Should be prime */
198#define hash_inode(i) ((i) % HASH_SIZE)
199
200static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
201
202/*
203 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
204 * `ino_dev_hashtable', else return 0
205 *
206 * If NAME is a non-NULL pointer to a character pointer, and there is
207 * a match, then set *NAME to the value of the name slot in that
208 * bucket.
209 */
210int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
211{
212 ino_dev_hashtable_bucket_t *bucket;
213
214 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
215 while (bucket != NULL) {
216 if ((bucket->ino == statbuf->st_ino) &&
217 (bucket->dev == statbuf->st_dev))
218 {
219 if (name) *name = bucket->name;
220 return 1;
221 }
222 bucket = bucket->next;
223 }
224 return 0;
225}
226
227/* Add statbuf to statbuf hash table */
228void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
229{
230 int i;
231 size_t s;
232 ino_dev_hashtable_bucket_t *bucket;
233
234 i = hash_inode(statbuf->st_ino);
235 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000236 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000237 bucket->ino = statbuf->st_ino;
238 bucket->dev = statbuf->st_dev;
239 if (name)
240 strcpy(bucket->name, name);
241 else
242 bucket->name[0] = '\0';
243 bucket->next = ino_dev_hashtable[i];
244 ino_dev_hashtable[i] = bucket;
245}
246
247/* Clear statbuf hash table */
248void reset_ino_dev_hashtable(void)
249{
250 int i;
251 ino_dev_hashtable_bucket_t *bucket;
252
253 for (i = 0; i < HASH_SIZE; i++) {
254 while (ino_dev_hashtable[i] != NULL) {
255 bucket = ino_dev_hashtable[i]->next;
256 free(ino_dev_hashtable[i]);
257 ino_dev_hashtable[i] = bucket;
258 }
259 }
260}
261
Erik Andersen23dea9b2000-05-13 00:28:25 +0000262#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000263
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000264#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000265/*
266 * Return TRUE if a fileName is a directory.
267 * Nonexistant files return FALSE.
268 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000269int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000270{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000272 int didMalloc = 0;
273
274 if (statBuf == NULL) {
275 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
276 ++didMalloc;
277 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000280 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000282 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000283
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000284 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
285 status = FALSE;
286 }
287 else status = TRUE;
288
289 if (didMalloc) {
290 free(statBuf);
291 statBuf = NULL;
292 }
293 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000294}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000295#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000296
Eric Andersenebee3012000-09-05 22:17:22 +0000297#if defined (BB_AR) || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000298/*
Mark Whitley47583682000-12-05 20:03:17 +0000299 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000300 */
Mark Whitley47583682000-12-05 20:03:17 +0000301int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000302{
303 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000304 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000305
Mark Whitley47583682000-12-05 20:03:17 +0000306 while (chunksize > 0) {
307 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000308 size = BUFSIZ;
309 else
Mark Whitley47583682000-12-05 20:03:17 +0000310 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000311 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000312 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000313 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000314 }
315 return (TRUE);
316}
317#endif
318
319
Erik Andersen23dea9b2000-05-13 00:28:25 +0000320#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000321/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000322 * Copy one file to another, while possibly preserving its modes, times, and
323 * modes. Returns TRUE if successful, or FALSE on a failure with an error
324 * message output. (Failure is not indicated if attributes cannot be set.)
325 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000326 */
327int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000328copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000329 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000330{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 int rfd;
332 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 struct stat srcStatBuf;
335 struct stat dstStatBuf;
336 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000337
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 if (followLinks == TRUE)
339 status = stat(srcName, &srcStatBuf);
340 else
341 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000342
Erik Andersenfac10d72000-02-07 05:29:42 +0000343 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000344 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000346 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347
348 if (followLinks == TRUE)
349 status = stat(destName, &dstStatBuf);
350 else
351 status = lstat(destName, &dstStatBuf);
352
Erik Andersen59b9e872000-05-10 05:05:45 +0000353 if (status < 0 || forceFlag==TRUE) {
354 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 dstStatBuf.st_ino = -1;
356 dstStatBuf.st_dev = -1;
357 }
358
359 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
360 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000361 error_msg("Copying file \"%s\" to itself\n", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 return FALSE;
363 }
364
365 if (S_ISDIR(srcStatBuf.st_mode)) {
366 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
367 /* Make sure the directory is writable */
368 status = mkdir(destName, 0777777 ^ umask(0));
369 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000370 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 return FALSE;
372 }
373 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000374 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 int link_size;
376
377 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000378 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
379 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 if (link_size < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000381 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 return FALSE;
383 }
384 link_val[link_size] = '\0';
385 status = symlink(link_val, destName);
386 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000387 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 return FALSE;
389 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000390#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000391 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000392 /* Try to set owner, but fail silently like GNU cp */
393 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000395#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 return TRUE;
397 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
398 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
399 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000400 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 return FALSE;
402 }
403 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
404 || S_ISSOCK(srcStatBuf.st_mode)) {
405 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
406 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000407 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 return FALSE;
409 }
410 } else if (S_ISREG(srcStatBuf.st_mode)) {
411 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
412 rfd = open(srcName, O_RDONLY);
413 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000414 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415 return FALSE;
416 }
417
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000418 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 srcStatBuf.st_mode);
420 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000421 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 close(rfd);
423 return FALSE;
424 }
425
Mark Whitley47583682000-12-05 20:03:17 +0000426 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000427 goto error_exit;
428
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 close(rfd);
430 if (close(wfd) < 0) {
431 return FALSE;
432 }
433 }
434
435 if (setModes == TRUE) {
436 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000437 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
438 perror_msg_and_die("%s", destName);
439 if (chmod(destName, srcStatBuf.st_mode) < 0)
440 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 times.actime = srcStatBuf.st_atime;
442 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000443 if (utime(destName, &times) < 0)
444 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 }
446
Erik Andersenfac10d72000-02-07 05:29:42 +0000447 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000448
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000450 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000451 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000453
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000455}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000456#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000457
458
459
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000460#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000461
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000462#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
463#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
464
465/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
466static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 0, 0, S_ISUID,
468 0, 0, S_ISGID,
469 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000470};
471
472/* The 9 mode bits to test */
473static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 S_IRUSR, S_IWUSR, S_IXUSR,
475 S_IRGRP, S_IWGRP, S_IXGRP,
476 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000477};
478
Mark Whitley26a3adb2000-12-05 20:10:27 +0000479static const char MODE1[] = "rwxrwxrwx";
480static const char MODE0[] = "---------";
481static const char SMODE1[] = "..s..s..t";
482static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000483
Eric Andersencc8ed391999-10-05 16:24:54 +0000484/*
485 * Return the standard ls-like mode string from a file mode.
486 * This is static and so is overwritten on each call.
487 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000488const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000489{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000491
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 int i;
493
494 buf[0] = TYPECHAR(mode);
495 for (i = 0; i < 9; i++) {
496 if (mode & SBIT[i])
497 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
498 else
499 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
500 }
501 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000502}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000503#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000504
505
Pavel Roskin21ddb382000-07-17 19:31:54 +0000506#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000507/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000508 * Return the standard ls-like time string from a time_t
509 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000510 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000511const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000512{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 time_t now;
514 char *str;
515 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000516
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000520
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 strcpy(buf, &str[4]);
522 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000523
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
525 strcpy(&buf[7], &str[20]);
526 buf[11] = '\0';
527 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000528
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000530}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000531#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000532
Matt Kraaibfa79672000-12-15 22:34:34 +0000533#if defined BB_AR || defined BB_CP_MV || defined BB_DD || defined BB_NC || defined BB_TAR
Eric Andersen17d49ef1999-10-06 20:25:32 +0000534/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000535 * Write all of the supplied buffer out to a file.
536 * This does multiple writes as necessary.
537 * Returns the amount written, or -1 on an error.
538 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000539int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000540{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 int cc;
542 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000543
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000545
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 while (len > 0) {
547 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000548
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 if (cc < 0)
550 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 buf += cc;
553 total += cc;
554 len -= cc;
555 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000556
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000558}
Matt Kraai24ac0172000-12-18 21:38:57 +0000559#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000560
Matt Kraai24ac0172000-12-18 21:38:57 +0000561#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000562/*
563 * Read all of the supplied buffer from a file.
564 * This does multiple reads as necessary.
565 * Returns the amount read, or -1 on an error.
566 * A short read is returned on an end of file.
567 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000568int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000569{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570 int cc;
571 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000572
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000574
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 while (len > 0) {
576 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000577
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 if (cc < 0)
579 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000580
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 if (cc == 0)
582 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000583
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 buf += cc;
585 total += cc;
586 len -= cc;
587 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000588
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000590}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000591#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000592
593
Erik Andersen23dea9b2000-05-13 00:28:25 +0000594#if defined (BB_CHMOD_CHOWN_CHGRP) \
595 || defined (BB_CP_MV) \
596 || defined (BB_FIND) \
597 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000598 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000599 || defined (BB_RM) \
600 || defined (BB_TAR)
601
Eric Andersencc8ed391999-10-05 16:24:54 +0000602/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000603 * Walk down all the directories under the specified
604 * location, and do something (something specified
605 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000606 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000607 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000608 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000609 * and so isn't sufficiently portable to take over since glibc2.1
610 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000611 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000612int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 int recurse, int followLinks, int depthFirst,
614 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000615 struct stat * statbuf,
616 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000618 struct stat * statbuf,
619 void* userData),
620 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000621{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622 int status;
623 struct stat statbuf;
624 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000625
Erik Andersene49d5ec2000-02-08 19:58:47 +0000626 if (followLinks == TRUE)
627 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000628 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000630
Eric Andersen2b69c401999-10-05 22:58:32 +0000631 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632#ifdef BB_DEBUG_PRINT_SCAFFOLD
633 fprintf(stderr,
634 "status=%d followLinks=%d TRUE=%d\n",
635 status, followLinks, TRUE);
636#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000637 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000638 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000639 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640
641 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
642 if (fileAction == NULL)
643 return TRUE;
644 else
Erik Andersen3364d782000-03-28 00:58:14 +0000645 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 }
647
648 if (recurse == FALSE) {
649 if (S_ISDIR(statbuf.st_mode)) {
650 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000651 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000652 else
653 return TRUE;
654 }
655 }
656
657 if (S_ISDIR(statbuf.st_mode)) {
658 DIR *dir;
659
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000661 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000663 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000665 } else if (status == SKIP)
666 return TRUE;
667 }
668 dir = opendir(fileName);
669 if (!dir) {
670 perror_msg("%s", fileName);
671 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 }
673 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000674 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675
676 if ((strcmp(next->d_name, "..") == 0)
677 || (strcmp(next->d_name, ".") == 0)) {
678 continue;
679 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000680 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000681 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 return FALSE;
683 }
Erik Andersenc3660502000-04-18 21:18:57 +0000684 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 sprintf(nextFile, "%s/%s", fileName, next->d_name);
686 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000687 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000688 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000689 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 closedir(dir);
691 return FALSE;
692 }
693 }
694 status = closedir(dir);
695 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000696 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 return FALSE;
698 }
699 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000700 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000701 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000702 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 return FALSE;
704 }
705 }
706 } else {
707 if (fileAction == NULL)
708 return TRUE;
709 else
Erik Andersen3364d782000-03-28 00:58:14 +0000710 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 }
712 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000713}
714
Erik Andersen23dea9b2000-05-13 00:28:25 +0000715#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000716
Eric Andersenf6be9441999-10-13 21:12:06 +0000717
718
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000719#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000720/*
721 * Attempt to create the directories along the specified path, except for
722 * the final component. The mode is given for the final directory only,
723 * while all previous ones get default protections. Errors are not reported
724 * here, as failures to restore files can be reported later.
725 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000726extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000727{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 char *cp;
729 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000730 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000731 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000732
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 strcpy(buf, name);
734 for (cp = buf; *cp == '/'; cp++);
735 cp = strchr(cp, '/');
736 while (cp) {
737 cpOld = cp;
738 cp = strchr(cp + 1, '/');
739 *cpOld = '\0';
740 retVal = mkdir(buf, cp ? 0777 : mode);
741 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000742 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 return FALSE;
744 }
745 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000746 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000748}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000749#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000750
751
752
Pavel Roskin21ddb382000-07-17 19:31:54 +0000753#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000754 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000755/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000756
757
758
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000760{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000761 mode_t andMode =
762
763 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000764 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000765 mode_t mode = 0;
766 mode_t groups = 0;
767 char type;
768 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000769
Eric Andersen4e61db22000-09-01 00:06:17 +0000770 if (s==NULL)
771 return (FALSE);
772
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 for (;;) {
775 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000776 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000777 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000778 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000780 continue;
781 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000783 continue;
784 case 'o':
785 groups |= S_IRWXO;
786 continue;
787 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000789 continue;
790 case '+':
791 case '=':
792 case '-':
793 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 if (groups == 0) /* The default is "all" */
795 groups |=
796 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000797 break;
798 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000799 if (isdigit(c) && c >= '0' && c <= '7' &&
800 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000801 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000802 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000803 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000804 return (FALSE);
805 }
806 break;
807 }
808
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 while ((c = *s++) != '\0') {
810 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000811 case ',':
812 break;
813 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000815 continue;
816 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000817 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000818 continue;
819 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000821 continue;
822 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000824 continue;
825 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000826 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000827 continue;
828 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000829 *theMode &= andMode;
830 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000831 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000832 }
833 break;
834 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000836 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000837 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000838 /* fall through */
839 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000840 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000841 break;
842 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000843 andMode &= ~(mode & groups);
844 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000845 break;
846 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000847 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000848 *theMode &= andMode;
849 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000850 return (TRUE);
851}
Eric Andersence8f3b91999-10-20 07:03:36 +0000852
853
Pavel Roskin21ddb382000-07-17 19:31:54 +0000854#endif
855/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000856
Eric Andersene674eb71999-10-19 20:52:57 +0000857
858
Erik Andersen23dea9b2000-05-13 00:28:25 +0000859
860
Pavel Roskin21ddb382000-07-17 19:31:54 +0000861#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
862 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000863 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Erik Andersen23dea9b2000-05-13 00:28:25 +0000864
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000865/* This parses entries in /etc/passwd and /etc/group. This is desirable
866 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
867 * increases target size and is often not needed or wanted for embedded
868 * systems.
869 *
870 * /etc/passwd entries look like this:
871 * root:x:0:0:root:/root:/bin/bash
872 * and /etc/group entries look like this:
873 * root:x:0:
874 *
875 * This uses buf as storage to hold things.
876 *
877 */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000878unsigned long my_getid(const char *filename, char *name, long id, long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000879{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000880 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000881 char *rname, *start, *end, buf[128];
Eric Andersenfad04fd2000-07-14 06:49:52 +0000882 long rid;
883 long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000884
Erik Andersene49d5ec2000-02-08 19:58:47 +0000885 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000886 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000887 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000888 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000890 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000891
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000893 if (buf[0] == '#')
894 continue;
895
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000896 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000897 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000898 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000899 if (end == NULL)
900 continue;
901 *end = '\0';
902 rname = start;
903
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000904 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000905 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000907 if (end == NULL)
908 continue;
909
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000910 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000911 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000912 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000913 if (end == start)
914 continue;
915
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000916 /* gid in passwd */
917 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000918 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000919
Eric Andersend23f9ba1999-10-20 19:18:15 +0000920 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000921 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000922 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000923 fclose(file);
924 return (rid);
925 }
926 }
927 if (id != -1 && id == rid) {
928 strncpy(name, rname, 8);
Eric Andersen2d6282b2000-12-11 20:33:46 +0000929 name[8]='\0';
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000930 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000931 fclose(file);
932 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000933 }
934 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000935 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000936 return (-1);
937}
938
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000939/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000940long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000941{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000942 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000943}
944
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000945/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000946long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000947{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000948 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000949}
950
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000951/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000952void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000953{
Eric Andersenbd193a42000-12-13 01:52:39 +0000954 name[0] = '\0';
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000955 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000956}
957
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000958/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000959void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000960{
Eric Andersenbd193a42000-12-13 01:52:39 +0000961 group[0] = '\0';
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000962 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000963}
964
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000965/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000966long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000967{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000968 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000969 my_getid("/etc/passwd", name, -1, &gid);
970 return gid;
971}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000972
Pavel Roskin21ddb382000-07-17 19:31:54 +0000973#endif
974 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
975 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000976
977
Eric Andersenf86aee72000-07-06 17:20:46 +0000978#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000979
Eric Andersenbd22ed82000-07-08 18:55:24 +0000980/* From <linux/kd.h> */
981#define KDGKBTYPE 0x4B33 /* get keyboard type */
982#define KB_84 0x01
983#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000984
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000986{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000987 char arg;
988
989 arg = 0;
990 return (ioctl(fd, KDGKBTYPE, &arg) == 0
991 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000992}
993
Erik Andersene49d5ec2000-02-08 19:58:47 +0000994static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000995{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996 int fd;
997
998 /* try read-only */
999 fd = open(fnam, O_RDWR);
1000
1001 /* if failed, try read-only */
1002 if (fd < 0 && errno == EACCES)
1003 fd = open(fnam, O_RDONLY);
1004
1005 /* if failed, try write-only */
1006 if (fd < 0 && errno == EACCES)
1007 fd = open(fnam, O_WRONLY);
1008
1009 /* if failed, fail */
1010 if (fd < 0)
1011 return -1;
1012
1013 /* if not a console, fail */
1014 if (!is_a_console(fd)) {
1015 close(fd);
1016 return -1;
1017 }
1018
1019 /* success */
1020 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001021}
1022
1023/*
1024 * Get an fd for use with kbd/console ioctls.
1025 * We try several things because opening /dev/console will fail
1026 * if someone else used X (which does a chown on /dev/console).
1027 *
1028 * if tty_name is non-NULL, try this one instead.
1029 */
1030
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001032{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001034
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 if (tty_name) {
1036 if (-1 == (fd = open_a_console(tty_name)))
1037 return -1;
1038 else
1039 return fd;
1040 }
1041
1042 fd = open_a_console("/dev/tty");
1043 if (fd >= 0)
1044 return fd;
1045
1046 fd = open_a_console("/dev/tty0");
1047 if (fd >= 0)
1048 return fd;
1049
1050 fd = open_a_console("/dev/console");
1051 if (fd >= 0)
1052 return fd;
1053
1054 for (fd = 0; fd < 3; fd++)
1055 if (is_a_console(fd))
1056 return fd;
1057
Mark Whitleyf57c9442000-12-07 19:56:48 +00001058 error_msg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001060}
1061
1062
Eric Andersenf86aee72000-07-06 17:20:46 +00001063#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001064
1065
Eric Andersen9f16d612000-06-12 23:11:16 +00001066#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001067/*
1068 * Routine to see if a text string is matched by a wildcard pattern.
1069 * Returns TRUE if the text is matched, or FALSE if it is not matched
1070 * or if the pattern is invalid.
1071 * * matches zero or more characters
1072 * ? matches a single character
1073 * [abc] matches 'a', 'b' or 'c'
1074 * \c quotes character c
1075 * Adapted from code written by Ingo Wilken, and
1076 * then taken from sash, Copyright (c) 1999 by David I. Bell
1077 * Permission is granted to use, distribute, or modify this source,
1078 * provided that this copyright notice remains intact.
1079 * Permission to distribute this code under the GPL has been granted.
1080 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001081extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001082{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 const char *retryPat;
1084 const char *retryText;
1085 int ch;
1086 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001087 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001088
Erik Andersene49d5ec2000-02-08 19:58:47 +00001089 retryPat = NULL;
1090 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001093 ch = *pattern++;
1094
Erik Andersene49d5ec2000-02-08 19:58:47 +00001095 switch (ch) {
1096 case '*':
1097 retryPat = pattern;
1098 retryText = text;
1099 break;
1100
1101 case '[':
1102 found = FALSE;
1103
1104 while ((ch = *pattern++) != ']') {
1105 if (ch == '\\')
1106 ch = *pattern++;
1107
1108 if (ch == '\0')
1109 return FALSE;
1110
1111 if (*text == ch)
1112 found = TRUE;
1113 }
Erik Andersen632bb572000-04-18 22:09:06 +00001114 len=strlen(text);
1115 if (found == FALSE && len!=0) {
1116 return FALSE;
1117 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001118 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001119 if (strlen(pattern)==0 && len==1) {
1120 return TRUE;
1121 }
1122 if (len!=0) {
1123 text++;
1124 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001125 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001126 }
1127
1128 /* fall into next case */
1129
1130 case '?':
1131 if (*text++ == '\0')
1132 return FALSE;
1133
1134 break;
1135
1136 case '\\':
1137 ch = *pattern++;
1138
1139 if (ch == '\0')
1140 return FALSE;
1141
1142 /* fall into next case */
1143
1144 default:
1145 if (*text == ch) {
1146 if (*text)
1147 text++;
1148 break;
1149 }
1150
1151 if (*text) {
1152 pattern = retryPat;
1153 text = ++retryText;
1154 break;
1155 }
1156
Eric Andersenb186d981999-12-03 09:19:54 +00001157 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001158 }
1159
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 if (pattern == NULL)
1161 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001162 }
1163
Erik Andersene49d5ec2000-02-08 19:58:47 +00001164 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001165}
Eric Andersen9f16d612000-06-12 23:11:16 +00001166#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001167
1168
Eric Andersen29d2e361999-11-06 06:07:27 +00001169
1170
Erik Andersen23dea9b2000-05-13 00:28:25 +00001171#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001172/*
1173 * Given a block device, find the mount table entry if that block device
1174 * is mounted.
1175 *
1176 * Given any other file (or directory), find the mount table entry for its
1177 * filesystem.
1178 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001179extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001180{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 struct stat s;
1182 dev_t mountDevice;
1183 FILE *mountTable;
1184 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001185
Erik Andersene49d5ec2000-02-08 19:58:47 +00001186 if (stat(name, &s) != 0)
1187 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 if ((s.st_mode & S_IFMT) == S_IFBLK)
1190 mountDevice = s.st_rdev;
1191 else
1192 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001193
1194
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 if ((mountTable = setmntent(table, "r")) == 0)
1196 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001197
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198 while ((mountEntry = getmntent(mountTable)) != 0) {
1199 if (strcmp(name, mountEntry->mnt_dir) == 0
1200 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1201 break;
1202 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1203 break;
1204 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1205 break;
1206 }
1207 endmntent(mountTable);
1208 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001209}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001210#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001211
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001212#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001213/* try to open up the specified device */
1214extern int device_open(char *device, int mode)
1215{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001217
Erik Andersene49d5ec2000-02-08 19:58:47 +00001218 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001219
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 /* Retry up to 5 times */
1221 for (f = 0; f < 5; f++)
1222 if ((fd = open(device, m, 0600)) >= 0)
1223 break;
1224 if (fd < 0)
1225 return fd;
1226 /* Reset original flags. */
1227 if (m != mode)
1228 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001229 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001230}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001231#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001232
1233
Erik Andersen23dea9b2000-05-13 00:28:25 +00001234#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001235#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001236#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001237#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001238
Erik Andersen23dea9b2000-05-13 00:28:25 +00001239#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001240/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001241 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001242 * This finds the pid of the specified process,
1243 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001244 *
Erik Andersen825aead2000-04-07 06:00:07 +00001245 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001246 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001247extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001248{
Erik Andersen825aead2000-04-07 06:00:07 +00001249 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001250 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001251 pid_t num_pids;
1252 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001253 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001254
1255 /* open device */
1256 fd = open(device, O_RDONLY);
1257 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001258 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001259
1260 /* Find out how many processes there are */
1261 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001262 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001263
1264 /* Allocate some memory -- grab a few extras just in case
1265 * some new processes start up while we wait. The kernel will
1266 * just ignore any extras if we give it too many, and will trunc.
1267 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001268 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001269 pid_array[0] = num_pids+10;
1270
1271 /* Now grab the pid list */
1272 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001273 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001274
1275 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001276 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001277 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001278 struct pid_info info;
1279
1280 info.pid = pid_array[i];
1281 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001282 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001283
Erik Andersen0a64de92000-04-18 20:00:03 +00001284 /* Make sure we only match on the process name */
1285 p=info.command_line+1;
1286 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1287 (p)++;
1288 }
1289 if (isspace(*(p)))
1290 *p='\0';
1291
Erik Andersenb9167cb2000-04-18 22:41:30 +00001292 if ((strstr(info.command_line, pidName) != NULL)
1293 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001294 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001295 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001296 }
1297 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001298 if (pidList)
1299 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001300
1301 /* Free memory */
1302 free( pid_array);
1303
1304 /* close device */
1305 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001306 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001307
Erik Andersen825aead2000-04-07 06:00:07 +00001308 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309}
Erik Andersen269bba22000-03-08 14:50:47 +00001310#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001311#if ! defined BB_FEATURE_USE_PROCFS
1312#error Sorry, I depend on the /proc filesystem right now.
1313#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001314
Mark Whitleyf57c9442000-12-07 19:56:48 +00001315/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001316 *
1317 * This finds the pid of the specified process.
1318 * Currently, it's implemented by rummaging through
1319 * the proc filesystem.
1320 *
Erik Andersen825aead2000-04-07 06:00:07 +00001321 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001322 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001323extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001324{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001325 DIR *dir;
1326 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001327 pid_t* pidList=NULL;
1328 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001329
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001330 dir = opendir("/proc");
1331 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001332 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001333
1334 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001335 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001336 char filename[256];
1337 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001338
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001339 /* If it isn't a number, we don't want it */
1340 if (!isdigit(*next->d_name))
1341 continue;
1342
Eric Andersen31e68292000-09-14 21:12:20 +00001343 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001344 status = fopen(filename, "r");
1345 if (!status) {
1346 continue;
1347 }
1348 fgets(buffer, 256, status);
1349 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001350
Eric Andersen31e68292000-09-14 21:12:20 +00001351 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001352 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001353 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001354 }
John Beppuf95ca971999-12-09 22:10:18 +00001355 }
Eric Andersen31e68292000-09-14 21:12:20 +00001356
Erik Andersen0a64de92000-04-18 20:00:03 +00001357 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001358 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001359 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001360}
Erik Andersen269bba22000-03-08 14:50:47 +00001361#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001362#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001363
Eric Andersenae6eae02000-07-19 17:35:54 +00001364#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001365/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001366extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001367{
Eric Andersenae6eae02000-07-19 17:35:54 +00001368 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001369
Eric Andersenae6eae02000-07-19 17:35:54 +00001370 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001371 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001372 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001373}
1374
Eric Andersen501c88b2000-07-28 15:14:45 +00001375extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001376{
1377 void *ptr = realloc(old, size);
1378 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001379 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001380 return ptr;
1381}
Eric Andersen501c88b2000-07-28 15:14:45 +00001382
1383extern void *xcalloc(size_t nmemb, size_t size)
1384{
1385 void *ptr = calloc(nmemb, size);
1386 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001387 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001388 return ptr;
1389}
Eric Andersenae6eae02000-07-19 17:35:54 +00001390#endif
1391
Eric Andersenf3b2b522000-12-07 22:42:11 +00001392#if defined BB_FEATURE_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET
Eric Andersenae6eae02000-07-19 17:35:54 +00001393# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001394extern char * xstrdup (const char *s) {
1395 char *t;
1396
1397 if (s == NULL)
1398 return NULL;
1399
1400 t = strdup (s);
1401
1402 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001403 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001404
1405 return t;
1406}
Eric Andersenae6eae02000-07-19 17:35:54 +00001407# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001408#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001409
Matt Kraai88cc3052000-08-26 16:52:28 +00001410#if defined BB_FEATURE_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001411extern char * xstrndup (const char *s, int n) {
1412 char *t;
1413
1414 if (s == NULL)
Matt Kraai0dab8292000-12-18 03:08:29 +00001415 error_msg_and_die("xstrndup bug\n");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001416
1417 t = xmalloc(n+1);
1418 strncpy(t,s,n);
1419 t[n] = 0;
1420
1421 return t;
1422}
1423#endif
1424
1425
Erik Andersen23dea9b2000-05-13 00:28:25 +00001426#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001427extern int vdprintf(int d, const char *format, va_list ap)
1428{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001429 char buf[BUF_SIZE];
1430 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001431
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 len = vsprintf(buf, format, ap);
1433 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001434}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001435#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001436
Eric Andersen624cc772000-09-21 02:04:51 +00001437
Erik Andersen23dea9b2000-05-13 00:28:25 +00001438#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001439#include <fcntl.h>
1440#include "loop.h" /* Pull in loop device support */
1441
Erik Andersen5cbdd712000-01-26 20:06:48 +00001442extern int del_loop(const char *device)
1443{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001444 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001445
Erik Andersene49d5ec2000-02-08 19:58:47 +00001446 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001447 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448 return (FALSE);
1449 }
1450 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001451 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001452 return (FALSE);
1453 }
1454 close(fd);
1455 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001456}
Erik Andersende7965c2000-01-26 23:49:21 +00001457
Erik Andersene49d5ec2000-02-08 19:58:47 +00001458extern int set_loop(const char *device, const char *file, int offset,
1459 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001460{
1461 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001462 int fd, ffd, mode;
1463
Erik Andersende7965c2000-01-26 23:49:21 +00001464 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001465 if ((ffd = open(file, mode)) < 0 && !*loopro
1466 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001467 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001468 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001469 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001470 if ((fd = open(device, mode)) < 0) {
1471 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001472 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001473 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001474 }
1475 *loopro = (mode == O_RDONLY);
1476
1477 memset(&loopinfo, 0, sizeof(loopinfo));
1478 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001479 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001480
1481 loopinfo.lo_offset = offset;
1482
1483 loopinfo.lo_encrypt_key_size = 0;
1484 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001485 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001486 close(fd);
1487 close(ffd);
1488 return 1;
1489 }
1490 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1491 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001492 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001493 close(fd);
1494 close(ffd);
1495 return 1;
1496 }
1497 close(fd);
1498 close(ffd);
1499 return 0;
1500}
1501
Erik Andersene49d5ec2000-02-08 19:58:47 +00001502extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001503{
1504 char dev[20];
1505 int i, fd;
1506 struct stat statbuf;
1507 struct loop_info loopinfo;
1508
Erik Andersene49d5ec2000-02-08 19:58:47 +00001509 for (i = 0; i <= 7; i++) {
1510 sprintf(dev, "/dev/loop%d", i);
1511 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1512 if ((fd = open(dev, O_RDONLY)) >= 0) {
1513 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1514 if (errno == ENXIO) { /* probably free */
1515 close(fd);
1516 return strdup(dev);
1517 }
1518 }
1519 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001520 }
Erik Andersende7965c2000-01-26 23:49:21 +00001521 }
Erik Andersende7965c2000-01-26 23:49:21 +00001522 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001523 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001524}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001525#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001526
Erik Andersen23dea9b2000-05-13 00:28:25 +00001527#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001528extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001529{
Erik Andersen016ffe92000-03-22 00:58:54 +00001530 DIR *dir;
1531 struct dirent *entry;
1532 struct stat statBuf, rootStat;
1533 char fileName[BUFSIZ];
1534
Erik Andersenec5bd902000-03-22 07:12:05 +00001535 if (stat("/", &rootStat) != 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001536 error_msg("could not stat '/'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001537 return( FALSE);
1538 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001539
Erik Andersenec5bd902000-03-22 07:12:05 +00001540 dir = opendir("/dev");
1541 if (!dir) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001542 error_msg("could not open '/dev'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001543 return( FALSE);
1544 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001545
1546 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001547
Erik Andersen016ffe92000-03-22 00:58:54 +00001548 /* Must skip ".." since that is "/", and so we
1549 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001550 if (strcmp(entry->d_name, "..") == 0)
1551 continue;
1552
Eric Andersenf9ca6532000-08-11 17:45:02 +00001553 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001554
1555 if (stat(fileName, &statBuf) != 0)
1556 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001557 /* Some char devices have the same dev_t as block
1558 * devices, so make sure this is a block device */
1559 if (! S_ISBLK(statBuf.st_mode))
1560 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001561 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001562 strcpy(name, fileName);
1563 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001564 }
1565 }
1566
Erik Andersenec5bd902000-03-22 07:12:05 +00001567 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001568}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001569#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001570
Mark Whitley1ca41772000-06-28 22:15:26 +00001571
1572/* get_line_from_file() - This function reads an entire line from a text file
1573 * up to a newline. It returns a malloc'ed char * which must be stored and
1574 * free'ed by the caller. */
1575extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001576{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001577 static const int GROWBY = 80; /* how large we will grow strings by */
1578
Mark Whitley1ca41772000-06-28 22:15:26 +00001579 int ch;
1580 int idx = 0;
1581 char *linebuf = NULL;
1582 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001583
Mark Whitley1ca41772000-06-28 22:15:26 +00001584 while (1) {
1585 ch = fgetc(file);
1586 if (ch == EOF)
1587 break;
1588 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001589 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001590 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001591 linebuf[idx++] = (char)ch;
1592 if ((char)ch == '\n')
1593 break;
1594 }
John Beppu5a728cf2000-04-17 04:22:09 +00001595
Mark Whitley1ca41772000-06-28 22:15:26 +00001596 if (idx == 0)
1597 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001598
Mark Whitley1ca41772000-06-28 22:15:26 +00001599 linebuf[idx] = 0;
1600 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001601}
Erik Andersen016ffe92000-03-22 00:58:54 +00001602
Eric Andersen944be902000-09-25 16:58:39 +00001603#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001604extern void print_file(FILE *file)
1605{
1606 int c;
1607
1608 while ((c = getc(file)) != EOF)
1609 putc(c, stdout);
1610 fclose(file);
1611 fflush(stdout);
1612}
1613
1614extern int print_file_by_name(char *filename)
1615{
1616 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001617 if ((file = wfopen(filename, "r")) == NULL)
1618 return FALSE;
1619 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001620 return TRUE;
1621}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001622#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001623
Matt Kraaicabafae2000-11-30 04:55:40 +00001624#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001625char process_escape_sequence(char **ptr)
1626{
1627 char c;
1628
1629 switch (c = *(*ptr)++) {
1630 case 'a':
1631 c = '\a';
1632 break;
1633 case 'b':
1634 c = '\b';
1635 break;
1636 case 'f':
1637 c = '\f';
1638 break;
1639 case 'n':
1640 c = '\n';
1641 break;
Eric Andersen9bd71e82000-11-29 23:07:57 +00001642 case 'r':
1643 c = '\r';
1644 break;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001645 case 't':
1646 c = '\t';
1647 break;
1648 case 'v':
1649 c = '\v';
1650 break;
1651 case '\\':
1652 c = '\\';
1653 break;
1654 case '0': case '1': case '2': case '3':
1655 case '4': case '5': case '6': case '7':
1656 c -= '0';
1657 if ('0' <= **ptr && **ptr <= '7') {
1658 c = c * 8 + (*(*ptr)++ - '0');
1659 if ('0' <= **ptr && **ptr <= '7')
1660 c = c * 8 + (*(*ptr)++ - '0');
1661 }
1662 break;
1663 default:
1664 (*ptr)--;
1665 c = '\\';
1666 break;
1667 }
1668 return c;
1669}
1670#endif
1671
Eric Andersenba1ff9b2000-10-01 17:01:36 +00001672#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 +00001673char *get_last_path_component(char *path)
1674{
1675 char *s=path+strlen(path)-1;
1676
1677 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001678 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001679 *s-- = '\0';
1680 }
1681
1682 /* find last component */
1683 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001684 if (s == NULL || s[1] == '\0')
1685 return path;
1686 else
1687 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001688}
1689#endif
1690
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001691#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001692void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001693{
1694 int ret;
1695 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1696 int errmsgsz = regerror(ret, preg, NULL, 0);
1697 char *errmsg = xmalloc(errmsgsz);
1698 regerror(ret, preg, errmsg, errmsgsz);
Mark Whitleyf57c9442000-12-07 19:56:48 +00001699 error_msg_and_die("xregcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001700 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001701}
1702#endif
1703
Eric Andersen7a86e612000-10-09 18:21:44 +00001704#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001705FILE *wfopen(const char *path, const char *mode)
1706{
1707 FILE *fp;
1708 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001709 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001710 errno = 0;
1711 }
1712 return fp;
1713}
1714#endif
1715
Eric Andersene790ddb2000-11-17 06:50:42 +00001716#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
1717 || defined BB_SED || defined BB_SH || defined BB_UNIQ \
1718 || defined BB_WC || defined BB_CMP
Matt Kraaie0bcce02000-09-27 02:29:39 +00001719FILE *xfopen(const char *path, const char *mode)
1720{
1721 FILE *fp;
1722 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001723 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001724 return fp;
1725}
1726#endif
1727
Eric Andersene5aef922000-11-17 18:25:26 +00001728int applet_name_compare(const void *x, const void *y)
1729{
1730 const struct BB_applet *applet1 = x;
1731 const struct BB_applet *applet2 = y;
1732
1733 return strcmp(applet1->name, applet2->name);
1734}
1735
Matt Kraai24ac0172000-12-18 21:38:57 +00001736#if defined BB_DD || defined BB_TAIL
1737unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes)
1738{
1739 struct suffix_mult *sm;
1740 unsigned long int ret;
1741 int len;
1742 char *end;
1743
1744 ret = strtoul(numstr, &end, 10);
1745 if (numstr == end)
1746 error_msg_and_die("invalid number `%s'\n", numstr);
1747 while (end[0] != '\0') {
1748 for (sm = suffixes; sm->suffix != NULL; sm++) {
1749 len = strlen(sm->suffix);
1750 if (strncmp(sm->suffix, end, len) == 0) {
1751 ret *= sm->mult;
1752 end += len;
1753 break;
1754 }
1755 }
1756 if (sm->suffix == NULL)
1757 error_msg_and_die("invalid number `%s'\n", numstr);
1758 }
1759 return ret;
1760}
1761#endif
1762
1763#if defined BB_DD || defined BB_NC
Matt Kraaibfa79672000-12-15 22:34:34 +00001764ssize_t safe_read(int fd, void *buf, size_t count)
1765{
1766 ssize_t n;
1767
1768 do {
1769 n = read(fd, buf, count);
1770 } while (n < 0 && errno == EINTR);
1771
1772 return n;
1773}
1774#endif
1775
Eric Andersencc8ed391999-10-05 16:24:54 +00001776/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001777/*
1778Local Variables:
1779c-file-style: "linux"
1780c-basic-offset: 4
1781tab-width: 4
1782End:
1783*/