blob: 2f3486c13663456c1a7ee8258753ee71559dd0e9 [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
Erik Andersen23dea9b2000-05-13 00:28:25 +0000145#if defined BB_INIT
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) {
156 perror("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 McGrathac19b7e2000-09-11 05:06:13 +0000264#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_AR)
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) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 perror(srcName);
345 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) {
370 perror(destName);
371 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) {
381 perror(srcName);
382 return FALSE;
383 }
384 link_val[link_size] = '\0';
385 status = symlink(link_val, destName);
386 if (status < 0) {
387 perror(destName);
388 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) {
400 perror(destName);
401 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) {
407 perror(destName);
408 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) {
414 perror(srcName);
415 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) {
421 perror(destName);
422 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 */
437 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
438 perror(destName);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000439 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 }
441 if (chmod(destName, srcStatBuf.st_mode) < 0) {
442 perror(destName);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000443 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 }
445 times.actime = srcStatBuf.st_atime;
446 times.modtime = srcStatBuf.st_mtime;
447 if (utime(destName, &times) < 0) {
448 perror(destName);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000449 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
451 }
452
Erik Andersenfac10d72000-02-07 05:29:42 +0000453 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000454
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 error_exit:
456 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000457 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000459
Erik Andersene49d5ec2000-02-08 19:58:47 +0000460 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000461}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000462#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000463
464
465
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000466#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000467
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000468#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
469#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
470
471/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
472static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 0, 0, S_ISUID,
474 0, 0, S_ISGID,
475 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000476};
477
478/* The 9 mode bits to test */
479static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 S_IRUSR, S_IWUSR, S_IXUSR,
481 S_IRGRP, S_IWGRP, S_IXGRP,
482 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000483};
484
Mark Whitley26a3adb2000-12-05 20:10:27 +0000485static const char MODE1[] = "rwxrwxrwx";
486static const char MODE0[] = "---------";
487static const char SMODE1[] = "..s..s..t";
488static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000489
Eric Andersencc8ed391999-10-05 16:24:54 +0000490/*
491 * Return the standard ls-like mode string from a file mode.
492 * This is static and so is overwritten on each call.
493 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000494const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000495{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000497
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 int i;
499
500 buf[0] = TYPECHAR(mode);
501 for (i = 0; i < 9; i++) {
502 if (mode & SBIT[i])
503 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
504 else
505 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
506 }
507 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000508}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000509#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000510
511
Pavel Roskin21ddb382000-07-17 19:31:54 +0000512#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000513/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000514 * Return the standard ls-like time string from a time_t
515 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000516 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000517const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000518{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 time_t now;
520 char *str;
521 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000522
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000524
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000526
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527 strcpy(buf, &str[4]);
528 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000529
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
531 strcpy(&buf[7], &str[20]);
532 buf[11] = '\0';
533 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000534
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000536}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000537#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000538
Pavel Roskin21ddb382000-07-17 19:31:54 +0000539#if defined BB_TAR || defined BB_CP_MV || defined BB_AR
Eric Andersen17d49ef1999-10-06 20:25:32 +0000540/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000541 * Write all of the supplied buffer out to a file.
542 * This does multiple writes as necessary.
543 * Returns the amount written, or -1 on an error.
544 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000545int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000546{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 int cc;
548 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 while (len > 0) {
553 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000554
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 if (cc < 0)
556 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000557
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 buf += cc;
559 total += cc;
560 len -= cc;
561 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000562
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000564}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000565#endif /* BB_TAR || BB_CP_MV || BB_AR */
Eric Andersencc8ed391999-10-05 16:24:54 +0000566
567
Eric Andersenebee3012000-09-05 22:17:22 +0000568#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV
Eric Andersencc8ed391999-10-05 16:24:54 +0000569/*
570 * Read all of the supplied buffer from a file.
571 * This does multiple reads as necessary.
572 * Returns the amount read, or -1 on an error.
573 * A short read is returned on an end of file.
574 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000575int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000576{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 int cc;
578 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000579
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000581
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 while (len > 0) {
583 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000584
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 if (cc < 0)
586 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000587
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588 if (cc == 0)
589 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000590
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591 buf += cc;
592 total += cc;
593 len -= cc;
594 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000595
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000597}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000598#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000599
600
Erik Andersen23dea9b2000-05-13 00:28:25 +0000601#if defined (BB_CHMOD_CHOWN_CHGRP) \
602 || defined (BB_CP_MV) \
603 || defined (BB_FIND) \
604 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000605 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000606 || defined (BB_RM) \
607 || defined (BB_TAR)
608
Eric Andersencc8ed391999-10-05 16:24:54 +0000609/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000610 * Walk down all the directories under the specified
611 * location, and do something (something specified
612 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000613 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000614 * Unfortunatly, while nftw(3) could replace this and reduce
615 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000616 * and so isn't sufficiently portable to take over since glibc2.1
617 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000618 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000619int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620 int recurse, int followLinks, int depthFirst,
621 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000622 struct stat * statbuf,
623 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000625 struct stat * statbuf,
626 void* userData),
627 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000628{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 int status;
630 struct stat statbuf;
631 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000632
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 if (followLinks == TRUE)
634 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000635 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000637
Eric Andersen2b69c401999-10-05 22:58:32 +0000638 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639#ifdef BB_DEBUG_PRINT_SCAFFOLD
640 fprintf(stderr,
641 "status=%d followLinks=%d TRUE=%d\n",
642 status, followLinks, TRUE);
643#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000644 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000645 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000646 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647
648 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
649 if (fileAction == NULL)
650 return TRUE;
651 else
Erik Andersen3364d782000-03-28 00:58:14 +0000652 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 }
654
655 if (recurse == FALSE) {
656 if (S_ISDIR(statbuf.st_mode)) {
657 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000658 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 else
660 return TRUE;
661 }
662 }
663
664 if (S_ISDIR(statbuf.st_mode)) {
665 DIR *dir;
666
667 dir = opendir(fileName);
668 if (!dir) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000669 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 return FALSE;
671 }
672 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000673 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000675 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 return FALSE;
677 }
678 }
679 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000680 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000681
682 if ((strcmp(next->d_name, "..") == 0)
683 || (strcmp(next->d_name, ".") == 0)) {
684 continue;
685 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000686 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000687 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000688 return FALSE;
689 }
Erik Andersenc3660502000-04-18 21:18:57 +0000690 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 sprintf(nextFile, "%s/%s", fileName, next->d_name);
692 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000693 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000694 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000695 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000696 closedir(dir);
697 return FALSE;
698 }
699 }
700 status = closedir(dir);
701 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000702 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 return FALSE;
704 }
705 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000706 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000708 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 return FALSE;
710 }
711 }
712 } else {
713 if (fileAction == NULL)
714 return TRUE;
715 else
Erik Andersen3364d782000-03-28 00:58:14 +0000716 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 }
718 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000719}
720
Erik Andersen23dea9b2000-05-13 00:28:25 +0000721#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000722
Eric Andersenf6be9441999-10-13 21:12:06 +0000723
724
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000725#if defined (BB_TAR) || defined (BB_MKDIR) || defined (BB_AR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000726/*
727 * Attempt to create the directories along the specified path, except for
728 * the final component. The mode is given for the final directory only,
729 * while all previous ones get default protections. Errors are not reported
730 * here, as failures to restore files can be reported later.
731 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000732extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000733{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000734 char *cp;
735 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000736 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000738
Erik Andersene49d5ec2000-02-08 19:58:47 +0000739 strcpy(buf, name);
740 for (cp = buf; *cp == '/'; cp++);
741 cp = strchr(cp, '/');
742 while (cp) {
743 cpOld = cp;
744 cp = strchr(cp + 1, '/');
745 *cpOld = '\0';
746 retVal = mkdir(buf, cp ? 0777 : mode);
747 if (retVal != 0 && errno != EEXIST) {
748 perror(buf);
749 return FALSE;
750 }
751 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000752 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000753 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000754}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000755#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000756
757
758
Pavel Roskin21ddb382000-07-17 19:31:54 +0000759#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000760 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000761/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000762
763
764
Erik Andersene49d5ec2000-02-08 19:58:47 +0000765extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000766{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 mode_t andMode =
768
769 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000770 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 mode_t mode = 0;
772 mode_t groups = 0;
773 char type;
774 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000775
Eric Andersen4e61db22000-09-01 00:06:17 +0000776 if (s==NULL)
777 return (FALSE);
778
Eric Andersenf6be9441999-10-13 21:12:06 +0000779 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 for (;;) {
781 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000782 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000783 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000784 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000785 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000786 continue;
787 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000789 continue;
790 case 'o':
791 groups |= S_IRWXO;
792 continue;
793 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000795 continue;
796 case '+':
797 case '=':
798 case '-':
799 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000800 if (groups == 0) /* The default is "all" */
801 groups |=
802 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000803 break;
804 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000805 if (isdigit(c) && c >= '0' && c <= '7' &&
806 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000807 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000808 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000810 return (FALSE);
811 }
812 break;
813 }
814
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 while ((c = *s++) != '\0') {
816 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000817 case ',':
818 break;
819 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000821 continue;
822 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000824 continue;
825 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000827 continue;
828 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000830 continue;
831 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000832 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000833 continue;
834 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000835 *theMode &= andMode;
836 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000837 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000838 }
839 break;
840 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000842 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000843 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000844 /* fall through */
845 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000846 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000847 break;
848 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000849 andMode &= ~(mode & groups);
850 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000851 break;
852 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000853 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000854 *theMode &= andMode;
855 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000856 return (TRUE);
857}
Eric Andersence8f3b91999-10-20 07:03:36 +0000858
859
Pavel Roskin21ddb382000-07-17 19:31:54 +0000860#endif
861/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000862
Eric Andersene674eb71999-10-19 20:52:57 +0000863
864
Erik Andersen23dea9b2000-05-13 00:28:25 +0000865
866
Pavel Roskin21ddb382000-07-17 19:31:54 +0000867#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
868 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
869 || defined BB_LOGNAME || defined BB_WHOAMI
Erik Andersen23dea9b2000-05-13 00:28:25 +0000870
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000871/* This parses entries in /etc/passwd and /etc/group. This is desirable
872 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
873 * increases target size and is often not needed or wanted for embedded
874 * systems.
875 *
876 * /etc/passwd entries look like this:
877 * root:x:0:0:root:/root:/bin/bash
878 * and /etc/group entries look like this:
879 * root:x:0:
880 *
881 * This uses buf as storage to hold things.
882 *
883 */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000884unsigned long my_getid(const char *filename, char *name, long id, long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000885{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000886 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000887 char *rname, *start, *end, buf[128];
Eric Andersenfad04fd2000-07-14 06:49:52 +0000888 long rid;
889 long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000890
Erik Andersene49d5ec2000-02-08 19:58:47 +0000891 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000892 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000893 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000894 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000895 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000896 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000897
Erik Andersene49d5ec2000-02-08 19:58:47 +0000898 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000899 if (buf[0] == '#')
900 continue;
901
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000902 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000903 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000905 if (end == NULL)
906 continue;
907 *end = '\0';
908 rname = start;
909
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000910 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000911 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000912 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000913 if (end == NULL)
914 continue;
915
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000916 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000917 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000918 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000919 if (end == start)
920 continue;
921
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000922 /* gid in passwd */
923 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000924 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000925
Eric Andersend23f9ba1999-10-20 19:18:15 +0000926 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000928 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 fclose(file);
930 return (rid);
931 }
932 }
933 if (id != -1 && id == rid) {
934 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000935 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 fclose(file);
937 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000938 }
939 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000940 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000941 return (-1);
942}
943
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000944/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000945long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000946{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000947 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000948}
949
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000950/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000951long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000952{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000953 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000954}
955
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000956/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000957void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000958{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000959 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000960}
961
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000962/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000963void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000964{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000965 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000966}
967
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000968/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000969long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000970{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000971 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000972 my_getid("/etc/passwd", name, -1, &gid);
973 return gid;
974}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000975
Pavel Roskin21ddb382000-07-17 19:31:54 +0000976#endif
977 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
978 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000979
980
Eric Andersenf86aee72000-07-06 17:20:46 +0000981#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000982
Eric Andersenbd22ed82000-07-08 18:55:24 +0000983/* From <linux/kd.h> */
984#define KDGKBTYPE 0x4B33 /* get keyboard type */
985#define KB_84 0x01
986#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000987
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000989{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 char arg;
991
992 arg = 0;
993 return (ioctl(fd, KDGKBTYPE, &arg) == 0
994 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000995}
996
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000998{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999 int fd;
1000
1001 /* try read-only */
1002 fd = open(fnam, O_RDWR);
1003
1004 /* if failed, try read-only */
1005 if (fd < 0 && errno == EACCES)
1006 fd = open(fnam, O_RDONLY);
1007
1008 /* if failed, try write-only */
1009 if (fd < 0 && errno == EACCES)
1010 fd = open(fnam, O_WRONLY);
1011
1012 /* if failed, fail */
1013 if (fd < 0)
1014 return -1;
1015
1016 /* if not a console, fail */
1017 if (!is_a_console(fd)) {
1018 close(fd);
1019 return -1;
1020 }
1021
1022 /* success */
1023 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001024}
1025
1026/*
1027 * Get an fd for use with kbd/console ioctls.
1028 * We try several things because opening /dev/console will fail
1029 * if someone else used X (which does a chown on /dev/console).
1030 *
1031 * if tty_name is non-NULL, try this one instead.
1032 */
1033
Erik Andersene49d5ec2000-02-08 19:58:47 +00001034int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001035{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001037
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 if (tty_name) {
1039 if (-1 == (fd = open_a_console(tty_name)))
1040 return -1;
1041 else
1042 return fd;
1043 }
1044
1045 fd = open_a_console("/dev/tty");
1046 if (fd >= 0)
1047 return fd;
1048
1049 fd = open_a_console("/dev/tty0");
1050 if (fd >= 0)
1051 return fd;
1052
1053 fd = open_a_console("/dev/console");
1054 if (fd >= 0)
1055 return fd;
1056
1057 for (fd = 0; fd < 3; fd++)
1058 if (is_a_console(fd))
1059 return fd;
1060
Mark Whitleyf57c9442000-12-07 19:56:48 +00001061 error_msg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001062 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001063}
1064
1065
Eric Andersenf86aee72000-07-06 17:20:46 +00001066#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001067
1068
Eric Andersen9f16d612000-06-12 23:11:16 +00001069#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001070/*
1071 * Routine to see if a text string is matched by a wildcard pattern.
1072 * Returns TRUE if the text is matched, or FALSE if it is not matched
1073 * or if the pattern is invalid.
1074 * * matches zero or more characters
1075 * ? matches a single character
1076 * [abc] matches 'a', 'b' or 'c'
1077 * \c quotes character c
1078 * Adapted from code written by Ingo Wilken, and
1079 * then taken from sash, Copyright (c) 1999 by David I. Bell
1080 * Permission is granted to use, distribute, or modify this source,
1081 * provided that this copyright notice remains intact.
1082 * Permission to distribute this code under the GPL has been granted.
1083 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001084extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001085{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 const char *retryPat;
1087 const char *retryText;
1088 int ch;
1089 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001090 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 retryPat = NULL;
1093 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001094
Erik Andersene49d5ec2000-02-08 19:58:47 +00001095 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001096 ch = *pattern++;
1097
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 switch (ch) {
1099 case '*':
1100 retryPat = pattern;
1101 retryText = text;
1102 break;
1103
1104 case '[':
1105 found = FALSE;
1106
1107 while ((ch = *pattern++) != ']') {
1108 if (ch == '\\')
1109 ch = *pattern++;
1110
1111 if (ch == '\0')
1112 return FALSE;
1113
1114 if (*text == ch)
1115 found = TRUE;
1116 }
Erik Andersen632bb572000-04-18 22:09:06 +00001117 len=strlen(text);
1118 if (found == FALSE && len!=0) {
1119 return FALSE;
1120 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001122 if (strlen(pattern)==0 && len==1) {
1123 return TRUE;
1124 }
1125 if (len!=0) {
1126 text++;
1127 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001128 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 }
1130
1131 /* fall into next case */
1132
1133 case '?':
1134 if (*text++ == '\0')
1135 return FALSE;
1136
1137 break;
1138
1139 case '\\':
1140 ch = *pattern++;
1141
1142 if (ch == '\0')
1143 return FALSE;
1144
1145 /* fall into next case */
1146
1147 default:
1148 if (*text == ch) {
1149 if (*text)
1150 text++;
1151 break;
1152 }
1153
1154 if (*text) {
1155 pattern = retryPat;
1156 text = ++retryText;
1157 break;
1158 }
1159
Eric Andersenb186d981999-12-03 09:19:54 +00001160 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001161 }
1162
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 if (pattern == NULL)
1164 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001165 }
1166
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001168}
Eric Andersen9f16d612000-06-12 23:11:16 +00001169#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001170
1171
Eric Andersen29d2e361999-11-06 06:07:27 +00001172
1173
Erik Andersen23dea9b2000-05-13 00:28:25 +00001174#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001175/*
1176 * Given a block device, find the mount table entry if that block device
1177 * is mounted.
1178 *
1179 * Given any other file (or directory), find the mount table entry for its
1180 * filesystem.
1181 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001182extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001183{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 struct stat s;
1185 dev_t mountDevice;
1186 FILE *mountTable;
1187 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 if (stat(name, &s) != 0)
1190 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 if ((s.st_mode & S_IFMT) == S_IFBLK)
1193 mountDevice = s.st_rdev;
1194 else
1195 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001196
1197
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198 if ((mountTable = setmntent(table, "r")) == 0)
1199 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001200
Erik Andersene49d5ec2000-02-08 19:58:47 +00001201 while ((mountEntry = getmntent(mountTable)) != 0) {
1202 if (strcmp(name, mountEntry->mnt_dir) == 0
1203 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1204 break;
1205 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1206 break;
1207 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1208 break;
1209 }
1210 endmntent(mountTable);
1211 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001212}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001213#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001214
1215
1216
Erik Andersen23dea9b2000-05-13 00:28:25 +00001217#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001218/*
1219 * Read a number with a possible multiplier.
1220 * Returns -1 if the number format is illegal.
1221 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001222extern long atoi_w_units(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001223{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001224 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001225
Mark Whitleyf57c9442000-12-07 19:56:48 +00001226 if (!is_decimal(*cp))
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001228
Erik Andersene49d5ec2000-02-08 19:58:47 +00001229 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001230
Mark Whitleyf57c9442000-12-07 19:56:48 +00001231 while (is_decimal(*cp))
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001233
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 switch (*cp++) {
1235 case 'M':
1236 case 'm': /* `tail' uses it traditionally */
1237 value *= 1048576;
1238 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001239
Erik Andersene49d5ec2000-02-08 19:58:47 +00001240 case 'k':
1241 value *= 1024;
1242 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001243
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 case 'b':
1245 value *= 512;
1246 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001247
Erik Andersene49d5ec2000-02-08 19:58:47 +00001248 case 'w':
1249 value *= 2;
1250 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001251
Erik Andersene49d5ec2000-02-08 19:58:47 +00001252 case '\0':
1253 return value;
1254
1255 default:
1256 return -1;
1257 }
1258
1259 if (*cp)
1260 return -1;
1261
Eric Andersen1792f8c1999-12-09 06:11:36 +00001262 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001263}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001264#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001265
Eric Andersen6805d5d1999-12-09 22:39:55 +00001266
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001267#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001268/* try to open up the specified device */
1269extern int device_open(char *device, int mode)
1270{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001271 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001272
Erik Andersene49d5ec2000-02-08 19:58:47 +00001273 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001274
Erik Andersene49d5ec2000-02-08 19:58:47 +00001275 /* Retry up to 5 times */
1276 for (f = 0; f < 5; f++)
1277 if ((fd = open(device, m, 0600)) >= 0)
1278 break;
1279 if (fd < 0)
1280 return fd;
1281 /* Reset original flags. */
1282 if (m != mode)
1283 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001284 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001285}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001286#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001287
1288
Erik Andersen23dea9b2000-05-13 00:28:25 +00001289#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001290#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001291#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001292#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001293
Erik Andersen23dea9b2000-05-13 00:28:25 +00001294#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001295/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001296 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001297 * This finds the pid of the specified process,
1298 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001299 *
Erik Andersen825aead2000-04-07 06:00:07 +00001300 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001301 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001302extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001303{
Erik Andersen825aead2000-04-07 06:00:07 +00001304 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001305 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001306 pid_t num_pids;
1307 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001308 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309
1310 /* open device */
1311 fd = open(device, O_RDONLY);
1312 if (fd < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001313 error_msg_and_die( "open failed for `%s': %s\n", device, strerror (errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001314
1315 /* Find out how many processes there are */
1316 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001317 error_msg_and_die( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001318
1319 /* Allocate some memory -- grab a few extras just in case
1320 * some new processes start up while we wait. The kernel will
1321 * just ignore any extras if we give it too many, and will trunc.
1322 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001323 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001324 pid_array[0] = num_pids+10;
1325
1326 /* Now grab the pid list */
1327 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001328 error_msg_and_die( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001329
1330 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001331 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001332 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001333 struct pid_info info;
1334
1335 info.pid = pid_array[i];
1336 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001337 error_msg_and_die( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001338
Erik Andersen0a64de92000-04-18 20:00:03 +00001339 /* Make sure we only match on the process name */
1340 p=info.command_line+1;
1341 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1342 (p)++;
1343 }
1344 if (isspace(*(p)))
1345 *p='\0';
1346
Erik Andersenb9167cb2000-04-18 22:41:30 +00001347 if ((strstr(info.command_line, pidName) != NULL)
1348 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001349 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001350 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001351 }
1352 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001353 if (pidList)
1354 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001355
1356 /* Free memory */
1357 free( pid_array);
1358
1359 /* close device */
1360 if (close (fd) != 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001361 error_msg_and_die( "close failed for `%s': %s\n",device, strerror (errno));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001362
Erik Andersen825aead2000-04-07 06:00:07 +00001363 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001364}
Erik Andersen269bba22000-03-08 14:50:47 +00001365#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001366#if ! defined BB_FEATURE_USE_PROCFS
1367#error Sorry, I depend on the /proc filesystem right now.
1368#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001369
Mark Whitleyf57c9442000-12-07 19:56:48 +00001370/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001371 *
1372 * This finds the pid of the specified process.
1373 * Currently, it's implemented by rummaging through
1374 * the proc filesystem.
1375 *
Erik Andersen825aead2000-04-07 06:00:07 +00001376 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001377 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001378extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001379{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001380 DIR *dir;
1381 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001382 pid_t* pidList=NULL;
1383 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001384
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001385 dir = opendir("/proc");
1386 if (!dir)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001387 error_msg_and_die( "Cannot open /proc: %s\n", strerror (errno));
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001388
1389 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001390 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001391 char filename[256];
1392 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001393
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001394 /* If it isn't a number, we don't want it */
1395 if (!isdigit(*next->d_name))
1396 continue;
1397
Eric Andersen31e68292000-09-14 21:12:20 +00001398 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001399 status = fopen(filename, "r");
1400 if (!status) {
1401 continue;
1402 }
1403 fgets(buffer, 256, status);
1404 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001405
Eric Andersen31e68292000-09-14 21:12:20 +00001406 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001407 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001408 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001409 }
John Beppuf95ca971999-12-09 22:10:18 +00001410 }
Eric Andersen31e68292000-09-14 21:12:20 +00001411
Erik Andersen0a64de92000-04-18 20:00:03 +00001412 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001413 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001414 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001415}
Erik Andersen269bba22000-03-08 14:50:47 +00001416#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001417#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001418
Eric Andersenae6eae02000-07-19 17:35:54 +00001419#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001420/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001421extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001422{
Eric Andersenae6eae02000-07-19 17:35:54 +00001423 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001424
Eric Andersenae6eae02000-07-19 17:35:54 +00001425 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001426 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001427 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001428}
1429
Eric Andersen501c88b2000-07-28 15:14:45 +00001430extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001431{
1432 void *ptr = realloc(old, size);
1433 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001434 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001435 return ptr;
1436}
Eric Andersen501c88b2000-07-28 15:14:45 +00001437
1438extern void *xcalloc(size_t nmemb, size_t size)
1439{
1440 void *ptr = calloc(nmemb, size);
1441 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001442 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001443 return ptr;
1444}
Eric Andersenae6eae02000-07-19 17:35:54 +00001445#endif
1446
Eric Andersen11c65522000-09-07 17:24:47 +00001447#if defined BB_FEATURE_NFSMOUNT || defined BB_SH || defined BB_LS
Eric Andersenae6eae02000-07-19 17:35:54 +00001448# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001449extern char * xstrdup (const char *s) {
1450 char *t;
1451
1452 if (s == NULL)
1453 return NULL;
1454
1455 t = strdup (s);
1456
1457 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001458 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001459
1460 return t;
1461}
Eric Andersenae6eae02000-07-19 17:35:54 +00001462# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001463#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001464
Matt Kraai88cc3052000-08-26 16:52:28 +00001465#if defined BB_FEATURE_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001466extern char * xstrndup (const char *s, int n) {
1467 char *t;
1468
1469 if (s == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001470 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001471
1472 t = xmalloc(n+1);
1473 strncpy(t,s,n);
1474 t[n] = 0;
1475
1476 return t;
1477}
1478#endif
1479
1480
Erik Andersen23dea9b2000-05-13 00:28:25 +00001481#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001482extern int vdprintf(int d, const char *format, va_list ap)
1483{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001484 char buf[BUF_SIZE];
1485 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001486
Erik Andersene49d5ec2000-02-08 19:58:47 +00001487 len = vsprintf(buf, format, ap);
1488 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001489}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001490#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001491
Eric Andersen624cc772000-09-21 02:04:51 +00001492
Erik Andersen23dea9b2000-05-13 00:28:25 +00001493#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001494#include <fcntl.h>
1495#include "loop.h" /* Pull in loop device support */
1496
Erik Andersen5cbdd712000-01-26 20:06:48 +00001497extern int del_loop(const char *device)
1498{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001499 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001500
Erik Andersene49d5ec2000-02-08 19:58:47 +00001501 if ((fd = open(device, O_RDONLY)) < 0) {
1502 perror(device);
1503 return (FALSE);
1504 }
1505 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1506 perror("ioctl: LOOP_CLR_FD");
1507 return (FALSE);
1508 }
1509 close(fd);
1510 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001511}
Erik Andersende7965c2000-01-26 23:49:21 +00001512
Erik Andersene49d5ec2000-02-08 19:58:47 +00001513extern int set_loop(const char *device, const char *file, int offset,
1514 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001515{
1516 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001517 int fd, ffd, mode;
1518
Erik Andersende7965c2000-01-26 23:49:21 +00001519 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001520 if ((ffd = open(file, mode)) < 0 && !*loopro
1521 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1522 perror(file);
1523 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001524 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001525 if ((fd = open(device, mode)) < 0) {
1526 close(ffd);
1527 perror(device);
1528 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001529 }
1530 *loopro = (mode == O_RDONLY);
1531
1532 memset(&loopinfo, 0, sizeof(loopinfo));
1533 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001534 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001535
1536 loopinfo.lo_offset = offset;
1537
1538 loopinfo.lo_encrypt_key_size = 0;
1539 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1540 perror("ioctl: LOOP_SET_FD");
1541 close(fd);
1542 close(ffd);
1543 return 1;
1544 }
1545 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1546 (void) ioctl(fd, LOOP_CLR_FD, 0);
1547 perror("ioctl: LOOP_SET_STATUS");
1548 close(fd);
1549 close(ffd);
1550 return 1;
1551 }
1552 close(fd);
1553 close(ffd);
1554 return 0;
1555}
1556
Erik Andersene49d5ec2000-02-08 19:58:47 +00001557extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001558{
1559 char dev[20];
1560 int i, fd;
1561 struct stat statbuf;
1562 struct loop_info loopinfo;
1563
Erik Andersene49d5ec2000-02-08 19:58:47 +00001564 for (i = 0; i <= 7; i++) {
1565 sprintf(dev, "/dev/loop%d", i);
1566 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1567 if ((fd = open(dev, O_RDONLY)) >= 0) {
1568 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1569 if (errno == ENXIO) { /* probably free */
1570 close(fd);
1571 return strdup(dev);
1572 }
1573 }
1574 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001575 }
Erik Andersende7965c2000-01-26 23:49:21 +00001576 }
Erik Andersende7965c2000-01-26 23:49:21 +00001577 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001578 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001579}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001580#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001581
Erik Andersen23dea9b2000-05-13 00:28:25 +00001582#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001583extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001584{
Erik Andersen016ffe92000-03-22 00:58:54 +00001585 DIR *dir;
1586 struct dirent *entry;
1587 struct stat statBuf, rootStat;
1588 char fileName[BUFSIZ];
1589
Erik Andersenec5bd902000-03-22 07:12:05 +00001590 if (stat("/", &rootStat) != 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001591 error_msg("could not stat '/'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001592 return( FALSE);
1593 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001594
Erik Andersenec5bd902000-03-22 07:12:05 +00001595 dir = opendir("/dev");
1596 if (!dir) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001597 error_msg("could not open '/dev'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001598 return( FALSE);
1599 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001600
1601 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001602
Erik Andersen016ffe92000-03-22 00:58:54 +00001603 /* Must skip ".." since that is "/", and so we
1604 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001605 if (strcmp(entry->d_name, "..") == 0)
1606 continue;
1607
Eric Andersenf9ca6532000-08-11 17:45:02 +00001608 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001609
1610 if (stat(fileName, &statBuf) != 0)
1611 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001612 /* Some char devices have the same dev_t as block
1613 * devices, so make sure this is a block device */
1614 if (! S_ISBLK(statBuf.st_mode))
1615 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001616 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001617 strcpy(name, fileName);
1618 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001619 }
1620 }
1621
Erik Andersenec5bd902000-03-22 07:12:05 +00001622 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001623}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001624#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001625
Mark Whitley1ca41772000-06-28 22:15:26 +00001626
1627/* get_line_from_file() - This function reads an entire line from a text file
1628 * up to a newline. It returns a malloc'ed char * which must be stored and
1629 * free'ed by the caller. */
1630extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001631{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001632 static const int GROWBY = 80; /* how large we will grow strings by */
1633
Mark Whitley1ca41772000-06-28 22:15:26 +00001634 int ch;
1635 int idx = 0;
1636 char *linebuf = NULL;
1637 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001638
Mark Whitley1ca41772000-06-28 22:15:26 +00001639 while (1) {
1640 ch = fgetc(file);
1641 if (ch == EOF)
1642 break;
1643 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001644 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001645 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001646 linebuf[idx++] = (char)ch;
1647 if ((char)ch == '\n')
1648 break;
1649 }
John Beppu5a728cf2000-04-17 04:22:09 +00001650
Mark Whitley1ca41772000-06-28 22:15:26 +00001651 if (idx == 0)
1652 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001653
Mark Whitley1ca41772000-06-28 22:15:26 +00001654 linebuf[idx] = 0;
1655 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001656}
Erik Andersen016ffe92000-03-22 00:58:54 +00001657
Eric Andersen944be902000-09-25 16:58:39 +00001658#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001659extern void print_file(FILE *file)
1660{
1661 int c;
1662
1663 while ((c = getc(file)) != EOF)
1664 putc(c, stdout);
1665 fclose(file);
1666 fflush(stdout);
1667}
1668
1669extern int print_file_by_name(char *filename)
1670{
1671 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001672 if ((file = wfopen(filename, "r")) == NULL)
1673 return FALSE;
1674 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001675 return TRUE;
1676}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001677#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001678
Matt Kraaicabafae2000-11-30 04:55:40 +00001679#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001680char process_escape_sequence(char **ptr)
1681{
1682 char c;
1683
1684 switch (c = *(*ptr)++) {
1685 case 'a':
1686 c = '\a';
1687 break;
1688 case 'b':
1689 c = '\b';
1690 break;
1691 case 'f':
1692 c = '\f';
1693 break;
1694 case 'n':
1695 c = '\n';
1696 break;
Eric Andersen9bd71e82000-11-29 23:07:57 +00001697 case 'r':
1698 c = '\r';
1699 break;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001700 case 't':
1701 c = '\t';
1702 break;
1703 case 'v':
1704 c = '\v';
1705 break;
1706 case '\\':
1707 c = '\\';
1708 break;
1709 case '0': case '1': case '2': case '3':
1710 case '4': case '5': case '6': case '7':
1711 c -= '0';
1712 if ('0' <= **ptr && **ptr <= '7') {
1713 c = c * 8 + (*(*ptr)++ - '0');
1714 if ('0' <= **ptr && **ptr <= '7')
1715 c = c * 8 + (*(*ptr)++ - '0');
1716 }
1717 break;
1718 default:
1719 (*ptr)--;
1720 c = '\\';
1721 break;
1722 }
1723 return c;
1724}
1725#endif
1726
Eric Andersenba1ff9b2000-10-01 17:01:36 +00001727#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 +00001728char *get_last_path_component(char *path)
1729{
1730 char *s=path+strlen(path)-1;
1731
1732 /* strip trailing slashes */
1733 while (s && *s == '/') {
1734 *s-- = '\0';
1735 }
1736
1737 /* find last component */
1738 s = strrchr(path, '/');
1739 if (s==NULL) return path;
1740 else return s+1;
1741}
1742#endif
1743
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001744#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001745void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001746{
1747 int ret;
1748 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1749 int errmsgsz = regerror(ret, preg, NULL, 0);
1750 char *errmsg = xmalloc(errmsgsz);
1751 regerror(ret, preg, errmsg, errmsgsz);
Mark Whitleyf57c9442000-12-07 19:56:48 +00001752 error_msg_and_die("xregcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001753 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001754}
1755#endif
1756
Eric Andersen7a86e612000-10-09 18:21:44 +00001757#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001758FILE *wfopen(const char *path, const char *mode)
1759{
1760 FILE *fp;
1761 if ((fp = fopen(path, mode)) == NULL) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001762 error_msg("%s: %s\n", path, strerror(errno));
Matt Kraaic0321f92000-09-27 04:09:22 +00001763 errno = 0;
1764 }
1765 return fp;
1766}
1767#endif
1768
Eric Andersene790ddb2000-11-17 06:50:42 +00001769#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
1770 || defined BB_SED || defined BB_SH || defined BB_UNIQ \
1771 || defined BB_WC || defined BB_CMP
Matt Kraaie0bcce02000-09-27 02:29:39 +00001772FILE *xfopen(const char *path, const char *mode)
1773{
1774 FILE *fp;
1775 if ((fp = fopen(path, mode)) == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001776 error_msg_and_die("%s: %s\n", path, strerror(errno));
Matt Kraaie0bcce02000-09-27 02:29:39 +00001777 return fp;
1778}
1779#endif
1780
Eric Andersene5aef922000-11-17 18:25:26 +00001781int applet_name_compare(const void *x, const void *y)
1782{
1783 const struct BB_applet *applet1 = x;
1784 const struct BB_applet *applet2 = y;
1785
1786 return strcmp(applet1->name, applet2->name);
1787}
1788
Eric Andersencc8ed391999-10-05 16:24:54 +00001789/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001790/*
1791Local Variables:
1792c-file-style: "linux"
1793c-basic-offset: 4
1794tab-width: 4
1795End:
1796*/