blob: fa2067af3860c7f78c9502b1202ffda3c02a19e7 [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
Matt Kraaie6e81832000-12-30 07:46:23 +0000965#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000966/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000967long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000968{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000969 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000970 my_getid("/etc/passwd", name, -1, &gid);
971 return gid;
972}
Matt Kraaie6e81832000-12-30 07:46:23 +0000973#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +0000974
Pavel Roskin21ddb382000-07-17 19:31:54 +0000975#endif
976 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
977 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000978
979
Eric Andersenf86aee72000-07-06 17:20:46 +0000980#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000981
Eric Andersenbd22ed82000-07-08 18:55:24 +0000982/* From <linux/kd.h> */
983#define KDGKBTYPE 0x4B33 /* get keyboard type */
984#define KB_84 0x01
985#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000986
Erik Andersene49d5ec2000-02-08 19:58:47 +0000987int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000988{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 char arg;
990
991 arg = 0;
992 return (ioctl(fd, KDGKBTYPE, &arg) == 0
993 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000994}
995
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000997{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 int fd;
999
1000 /* try read-only */
1001 fd = open(fnam, O_RDWR);
1002
1003 /* if failed, try read-only */
1004 if (fd < 0 && errno == EACCES)
1005 fd = open(fnam, O_RDONLY);
1006
1007 /* if failed, try write-only */
1008 if (fd < 0 && errno == EACCES)
1009 fd = open(fnam, O_WRONLY);
1010
1011 /* if failed, fail */
1012 if (fd < 0)
1013 return -1;
1014
1015 /* if not a console, fail */
1016 if (!is_a_console(fd)) {
1017 close(fd);
1018 return -1;
1019 }
1020
1021 /* success */
1022 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001023}
1024
1025/*
1026 * Get an fd for use with kbd/console ioctls.
1027 * We try several things because opening /dev/console will fail
1028 * if someone else used X (which does a chown on /dev/console).
1029 *
1030 * if tty_name is non-NULL, try this one instead.
1031 */
1032
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001034{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001036
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 if (tty_name) {
1038 if (-1 == (fd = open_a_console(tty_name)))
1039 return -1;
1040 else
1041 return fd;
1042 }
1043
1044 fd = open_a_console("/dev/tty");
1045 if (fd >= 0)
1046 return fd;
1047
1048 fd = open_a_console("/dev/tty0");
1049 if (fd >= 0)
1050 return fd;
1051
1052 fd = open_a_console("/dev/console");
1053 if (fd >= 0)
1054 return fd;
1055
1056 for (fd = 0; fd < 3; fd++)
1057 if (is_a_console(fd))
1058 return fd;
1059
Mark Whitleyf57c9442000-12-07 19:56:48 +00001060 error_msg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001062}
1063
1064
Eric Andersenf86aee72000-07-06 17:20:46 +00001065#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001066
1067
Eric Andersen9f16d612000-06-12 23:11:16 +00001068#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001069/*
1070 * Routine to see if a text string is matched by a wildcard pattern.
1071 * Returns TRUE if the text is matched, or FALSE if it is not matched
1072 * or if the pattern is invalid.
1073 * * matches zero or more characters
1074 * ? matches a single character
1075 * [abc] matches 'a', 'b' or 'c'
1076 * \c quotes character c
1077 * Adapted from code written by Ingo Wilken, and
1078 * then taken from sash, Copyright (c) 1999 by David I. Bell
1079 * Permission is granted to use, distribute, or modify this source,
1080 * provided that this copyright notice remains intact.
1081 * Permission to distribute this code under the GPL has been granted.
1082 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001084{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085 const char *retryPat;
1086 const char *retryText;
1087 int ch;
1088 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001089 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001090
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 retryPat = NULL;
1092 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001093
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001095 ch = *pattern++;
1096
Erik Andersene49d5ec2000-02-08 19:58:47 +00001097 switch (ch) {
1098 case '*':
1099 retryPat = pattern;
1100 retryText = text;
1101 break;
1102
1103 case '[':
1104 found = FALSE;
1105
1106 while ((ch = *pattern++) != ']') {
1107 if (ch == '\\')
1108 ch = *pattern++;
1109
1110 if (ch == '\0')
1111 return FALSE;
1112
1113 if (*text == ch)
1114 found = TRUE;
1115 }
Erik Andersen632bb572000-04-18 22:09:06 +00001116 len=strlen(text);
1117 if (found == FALSE && len!=0) {
1118 return FALSE;
1119 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001120 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001121 if (strlen(pattern)==0 && len==1) {
1122 return TRUE;
1123 }
1124 if (len!=0) {
1125 text++;
1126 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001127 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 }
1129
1130 /* fall into next case */
1131
1132 case '?':
1133 if (*text++ == '\0')
1134 return FALSE;
1135
1136 break;
1137
1138 case '\\':
1139 ch = *pattern++;
1140
1141 if (ch == '\0')
1142 return FALSE;
1143
1144 /* fall into next case */
1145
1146 default:
1147 if (*text == ch) {
1148 if (*text)
1149 text++;
1150 break;
1151 }
1152
1153 if (*text) {
1154 pattern = retryPat;
1155 text = ++retryText;
1156 break;
1157 }
1158
Eric Andersenb186d981999-12-03 09:19:54 +00001159 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001160 }
1161
Erik Andersene49d5ec2000-02-08 19:58:47 +00001162 if (pattern == NULL)
1163 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001164 }
1165
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001167}
Eric Andersen9f16d612000-06-12 23:11:16 +00001168#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001169
1170
Eric Andersen29d2e361999-11-06 06:07:27 +00001171
1172
Erik Andersen23dea9b2000-05-13 00:28:25 +00001173#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001174/*
1175 * Given a block device, find the mount table entry if that block device
1176 * is mounted.
1177 *
1178 * Given any other file (or directory), find the mount table entry for its
1179 * filesystem.
1180 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001181extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001182{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 struct stat s;
1184 dev_t mountDevice;
1185 FILE *mountTable;
1186 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001187
Erik Andersene49d5ec2000-02-08 19:58:47 +00001188 if (stat(name, &s) != 0)
1189 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001190
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 if ((s.st_mode & S_IFMT) == S_IFBLK)
1192 mountDevice = s.st_rdev;
1193 else
1194 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001195
1196
Erik Andersene49d5ec2000-02-08 19:58:47 +00001197 if ((mountTable = setmntent(table, "r")) == 0)
1198 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001199
Erik Andersene49d5ec2000-02-08 19:58:47 +00001200 while ((mountEntry = getmntent(mountTable)) != 0) {
1201 if (strcmp(name, mountEntry->mnt_dir) == 0
1202 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1203 break;
1204 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1205 break;
1206 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1207 break;
1208 }
1209 endmntent(mountTable);
1210 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001211}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001212#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001213
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001214#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001215/* try to open up the specified device */
1216extern int device_open(char *device, int mode)
1217{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001218 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001219
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001221
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 /* Retry up to 5 times */
1223 for (f = 0; f < 5; f++)
1224 if ((fd = open(device, m, 0600)) >= 0)
1225 break;
1226 if (fd < 0)
1227 return fd;
1228 /* Reset original flags. */
1229 if (m != mode)
1230 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001231 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001232}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001233#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001234
1235
Erik Andersen23dea9b2000-05-13 00:28:25 +00001236#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001237#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001238#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001239#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001240
Erik Andersen23dea9b2000-05-13 00:28:25 +00001241#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001242/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001243 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001244 * This finds the pid of the specified process,
1245 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001246 *
Erik Andersen825aead2000-04-07 06:00:07 +00001247 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001248 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001249extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001250{
Erik Andersen825aead2000-04-07 06:00:07 +00001251 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001252 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001253 pid_t num_pids;
1254 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001255 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001256
1257 /* open device */
1258 fd = open(device, O_RDONLY);
1259 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001260 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001261
1262 /* Find out how many processes there are */
1263 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001264 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001265
1266 /* Allocate some memory -- grab a few extras just in case
1267 * some new processes start up while we wait. The kernel will
1268 * just ignore any extras if we give it too many, and will trunc.
1269 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001270 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001271 pid_array[0] = num_pids+10;
1272
1273 /* Now grab the pid list */
1274 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001275 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001276
1277 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001278 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001279 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001280 struct pid_info info;
1281
1282 info.pid = pid_array[i];
1283 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001284 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001285
Erik Andersen0a64de92000-04-18 20:00:03 +00001286 /* Make sure we only match on the process name */
1287 p=info.command_line+1;
1288 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1289 (p)++;
1290 }
1291 if (isspace(*(p)))
1292 *p='\0';
1293
Erik Andersenb9167cb2000-04-18 22:41:30 +00001294 if ((strstr(info.command_line, pidName) != NULL)
1295 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001296 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001297 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001298 }
1299 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001300 if (pidList)
1301 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001302
1303 /* Free memory */
1304 free( pid_array);
1305
1306 /* close device */
1307 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001308 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309
Erik Andersen825aead2000-04-07 06:00:07 +00001310 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001311}
Erik Andersen269bba22000-03-08 14:50:47 +00001312#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001313#if ! defined BB_FEATURE_USE_PROCFS
1314#error Sorry, I depend on the /proc filesystem right now.
1315#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001316
Mark Whitleyf57c9442000-12-07 19:56:48 +00001317/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001318 *
1319 * This finds the pid of the specified process.
1320 * Currently, it's implemented by rummaging through
1321 * the proc filesystem.
1322 *
Erik Andersen825aead2000-04-07 06:00:07 +00001323 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001324 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001325extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001326{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001327 DIR *dir;
1328 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001329 pid_t* pidList=NULL;
1330 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001331
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001332 dir = opendir("/proc");
1333 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001334 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001335
1336 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001337 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001338 char filename[256];
1339 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001340
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001341 /* If it isn't a number, we don't want it */
1342 if (!isdigit(*next->d_name))
1343 continue;
1344
Eric Andersen31e68292000-09-14 21:12:20 +00001345 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001346 status = fopen(filename, "r");
1347 if (!status) {
1348 continue;
1349 }
1350 fgets(buffer, 256, status);
1351 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001352
Eric Andersen31e68292000-09-14 21:12:20 +00001353 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001354 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001355 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001356 }
John Beppuf95ca971999-12-09 22:10:18 +00001357 }
Eric Andersen31e68292000-09-14 21:12:20 +00001358
Erik Andersen0a64de92000-04-18 20:00:03 +00001359 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001360 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001361 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001362}
Erik Andersen269bba22000-03-08 14:50:47 +00001363#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001364#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001365
Eric Andersenae6eae02000-07-19 17:35:54 +00001366#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001367/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001368extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001369{
Eric Andersenae6eae02000-07-19 17:35:54 +00001370 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001371
Eric Andersenae6eae02000-07-19 17:35:54 +00001372 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001373 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001374 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001375}
1376
Eric Andersen501c88b2000-07-28 15:14:45 +00001377extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001378{
1379 void *ptr = realloc(old, size);
1380 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001381 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001382 return ptr;
1383}
Eric Andersen501c88b2000-07-28 15:14:45 +00001384
1385extern void *xcalloc(size_t nmemb, size_t size)
1386{
1387 void *ptr = calloc(nmemb, size);
1388 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001389 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001390 return ptr;
1391}
Eric Andersenae6eae02000-07-19 17:35:54 +00001392#endif
1393
Eric Andersenf3b2b522000-12-07 22:42:11 +00001394#if defined BB_FEATURE_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET
Eric Andersenae6eae02000-07-19 17:35:54 +00001395# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001396extern char * xstrdup (const char *s) {
1397 char *t;
1398
1399 if (s == NULL)
1400 return NULL;
1401
1402 t = strdup (s);
1403
1404 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001405 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001406
1407 return t;
1408}
Eric Andersenae6eae02000-07-19 17:35:54 +00001409# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001410#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001411
Matt Kraai88cc3052000-08-26 16:52:28 +00001412#if defined BB_FEATURE_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001413extern char * xstrndup (const char *s, int n) {
1414 char *t;
1415
1416 if (s == NULL)
Matt Kraai0dab8292000-12-18 03:08:29 +00001417 error_msg_and_die("xstrndup bug\n");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001418
1419 t = xmalloc(n+1);
1420 strncpy(t,s,n);
1421 t[n] = 0;
1422
1423 return t;
1424}
1425#endif
1426
1427
Erik Andersen23dea9b2000-05-13 00:28:25 +00001428#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001429extern int vdprintf(int d, const char *format, va_list ap)
1430{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001431 char buf[BUF_SIZE];
1432 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001433
Erik Andersene49d5ec2000-02-08 19:58:47 +00001434 len = vsprintf(buf, format, ap);
1435 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001436}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001437#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001438
Eric Andersen624cc772000-09-21 02:04:51 +00001439
Erik Andersen23dea9b2000-05-13 00:28:25 +00001440#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001441#include <fcntl.h>
1442#include "loop.h" /* Pull in loop device support */
1443
Erik Andersen5cbdd712000-01-26 20:06:48 +00001444extern int del_loop(const char *device)
1445{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001446 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001447
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001449 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001450 return (FALSE);
1451 }
1452 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001453 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 return (FALSE);
1455 }
1456 close(fd);
1457 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001458}
Erik Andersende7965c2000-01-26 23:49:21 +00001459
Erik Andersene49d5ec2000-02-08 19:58:47 +00001460extern int set_loop(const char *device, const char *file, int offset,
1461 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001462{
1463 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001464 int fd, ffd, mode;
1465
Erik Andersende7965c2000-01-26 23:49:21 +00001466 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001467 if ((ffd = open(file, mode)) < 0 && !*loopro
1468 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001469 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001470 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001471 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001472 if ((fd = open(device, mode)) < 0) {
1473 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001474 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001475 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001476 }
1477 *loopro = (mode == O_RDONLY);
1478
1479 memset(&loopinfo, 0, sizeof(loopinfo));
1480 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001481 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001482
1483 loopinfo.lo_offset = offset;
1484
1485 loopinfo.lo_encrypt_key_size = 0;
1486 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001487 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001488 close(fd);
1489 close(ffd);
1490 return 1;
1491 }
1492 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1493 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001494 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001495 close(fd);
1496 close(ffd);
1497 return 1;
1498 }
1499 close(fd);
1500 close(ffd);
1501 return 0;
1502}
1503
Erik Andersene49d5ec2000-02-08 19:58:47 +00001504extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001505{
1506 char dev[20];
1507 int i, fd;
1508 struct stat statbuf;
1509 struct loop_info loopinfo;
1510
Erik Andersene49d5ec2000-02-08 19:58:47 +00001511 for (i = 0; i <= 7; i++) {
1512 sprintf(dev, "/dev/loop%d", i);
1513 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1514 if ((fd = open(dev, O_RDONLY)) >= 0) {
1515 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1516 if (errno == ENXIO) { /* probably free */
1517 close(fd);
1518 return strdup(dev);
1519 }
1520 }
1521 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001522 }
Erik Andersende7965c2000-01-26 23:49:21 +00001523 }
Erik Andersende7965c2000-01-26 23:49:21 +00001524 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001525 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001526}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001527#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001528
Erik Andersen23dea9b2000-05-13 00:28:25 +00001529#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001530extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001531{
Erik Andersen016ffe92000-03-22 00:58:54 +00001532 DIR *dir;
1533 struct dirent *entry;
1534 struct stat statBuf, rootStat;
1535 char fileName[BUFSIZ];
1536
Erik Andersenec5bd902000-03-22 07:12:05 +00001537 if (stat("/", &rootStat) != 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001538 error_msg("could not stat '/'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001539 return( FALSE);
1540 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001541
Erik Andersenec5bd902000-03-22 07:12:05 +00001542 dir = opendir("/dev");
1543 if (!dir) {
Mark Whitleyf57c9442000-12-07 19:56:48 +00001544 error_msg("could not open '/dev'\n");
Erik Andersenec5bd902000-03-22 07:12:05 +00001545 return( FALSE);
1546 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001547
1548 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001549
Erik Andersen016ffe92000-03-22 00:58:54 +00001550 /* Must skip ".." since that is "/", and so we
1551 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001552 if (strcmp(entry->d_name, "..") == 0)
1553 continue;
1554
Eric Andersenf9ca6532000-08-11 17:45:02 +00001555 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001556
1557 if (stat(fileName, &statBuf) != 0)
1558 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001559 /* Some char devices have the same dev_t as block
1560 * devices, so make sure this is a block device */
1561 if (! S_ISBLK(statBuf.st_mode))
1562 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001563 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001564 strcpy(name, fileName);
1565 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001566 }
1567 }
1568
Erik Andersenec5bd902000-03-22 07:12:05 +00001569 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001570}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001571#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001572
Mark Whitley1ca41772000-06-28 22:15:26 +00001573
1574/* get_line_from_file() - This function reads an entire line from a text file
1575 * up to a newline. It returns a malloc'ed char * which must be stored and
1576 * free'ed by the caller. */
1577extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001578{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001579 static const int GROWBY = 80; /* how large we will grow strings by */
1580
Mark Whitley1ca41772000-06-28 22:15:26 +00001581 int ch;
1582 int idx = 0;
1583 char *linebuf = NULL;
1584 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001585
Mark Whitley1ca41772000-06-28 22:15:26 +00001586 while (1) {
1587 ch = fgetc(file);
1588 if (ch == EOF)
1589 break;
1590 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001591 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001592 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001593 linebuf[idx++] = (char)ch;
1594 if ((char)ch == '\n')
1595 break;
1596 }
John Beppu5a728cf2000-04-17 04:22:09 +00001597
Mark Whitley1ca41772000-06-28 22:15:26 +00001598 if (idx == 0)
1599 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001600
Mark Whitley1ca41772000-06-28 22:15:26 +00001601 linebuf[idx] = 0;
1602 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001603}
Erik Andersen016ffe92000-03-22 00:58:54 +00001604
Eric Andersen944be902000-09-25 16:58:39 +00001605#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001606extern void print_file(FILE *file)
1607{
1608 int c;
1609
1610 while ((c = getc(file)) != EOF)
1611 putc(c, stdout);
1612 fclose(file);
1613 fflush(stdout);
1614}
1615
1616extern int print_file_by_name(char *filename)
1617{
1618 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001619 if ((file = wfopen(filename, "r")) == NULL)
1620 return FALSE;
1621 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001622 return TRUE;
1623}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001624#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001625
Matt Kraaicabafae2000-11-30 04:55:40 +00001626#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001627char process_escape_sequence(char **ptr)
1628{
1629 char c;
1630
1631 switch (c = *(*ptr)++) {
1632 case 'a':
1633 c = '\a';
1634 break;
1635 case 'b':
1636 c = '\b';
1637 break;
1638 case 'f':
1639 c = '\f';
1640 break;
1641 case 'n':
1642 c = '\n';
1643 break;
Eric Andersen9bd71e82000-11-29 23:07:57 +00001644 case 'r':
1645 c = '\r';
1646 break;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001647 case 't':
1648 c = '\t';
1649 break;
1650 case 'v':
1651 c = '\v';
1652 break;
1653 case '\\':
1654 c = '\\';
1655 break;
1656 case '0': case '1': case '2': case '3':
1657 case '4': case '5': case '6': case '7':
1658 c -= '0';
1659 if ('0' <= **ptr && **ptr <= '7') {
1660 c = c * 8 + (*(*ptr)++ - '0');
1661 if ('0' <= **ptr && **ptr <= '7')
1662 c = c * 8 + (*(*ptr)++ - '0');
1663 }
1664 break;
1665 default:
1666 (*ptr)--;
1667 c = '\\';
1668 break;
1669 }
1670 return c;
1671}
1672#endif
1673
Eric Andersenba1ff9b2000-10-01 17:01:36 +00001674#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 +00001675char *get_last_path_component(char *path)
1676{
1677 char *s=path+strlen(path)-1;
1678
1679 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001680 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001681 *s-- = '\0';
1682 }
1683
1684 /* find last component */
1685 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001686 if (s == NULL || s[1] == '\0')
1687 return path;
1688 else
1689 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001690}
1691#endif
1692
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001693#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001694void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001695{
1696 int ret;
1697 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1698 int errmsgsz = regerror(ret, preg, NULL, 0);
1699 char *errmsg = xmalloc(errmsgsz);
1700 regerror(ret, preg, errmsg, errmsgsz);
Mark Whitleyf57c9442000-12-07 19:56:48 +00001701 error_msg_and_die("xregcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001702 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001703}
1704#endif
1705
Eric Andersen7a86e612000-10-09 18:21:44 +00001706#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001707FILE *wfopen(const char *path, const char *mode)
1708{
1709 FILE *fp;
1710 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001711 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001712 errno = 0;
1713 }
1714 return fp;
1715}
1716#endif
1717
Eric Andersene790ddb2000-11-17 06:50:42 +00001718#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
1719 || defined BB_SED || defined BB_SH || defined BB_UNIQ \
1720 || defined BB_WC || defined BB_CMP
Matt Kraaie0bcce02000-09-27 02:29:39 +00001721FILE *xfopen(const char *path, const char *mode)
1722{
1723 FILE *fp;
1724 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001725 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001726 return fp;
1727}
1728#endif
1729
Eric Andersene5aef922000-11-17 18:25:26 +00001730int applet_name_compare(const void *x, const void *y)
1731{
1732 const struct BB_applet *applet1 = x;
1733 const struct BB_applet *applet2 = y;
1734
1735 return strcmp(applet1->name, applet2->name);
1736}
1737
Matt Kraai24ac0172000-12-18 21:38:57 +00001738#if defined BB_DD || defined BB_TAIL
1739unsigned long parse_number(const char *numstr, struct suffix_mult *suffixes)
1740{
1741 struct suffix_mult *sm;
1742 unsigned long int ret;
1743 int len;
1744 char *end;
1745
1746 ret = strtoul(numstr, &end, 10);
1747 if (numstr == end)
1748 error_msg_and_die("invalid number `%s'\n", numstr);
1749 while (end[0] != '\0') {
1750 for (sm = suffixes; sm->suffix != NULL; sm++) {
1751 len = strlen(sm->suffix);
1752 if (strncmp(sm->suffix, end, len) == 0) {
1753 ret *= sm->mult;
1754 end += len;
1755 break;
1756 }
1757 }
1758 if (sm->suffix == NULL)
1759 error_msg_and_die("invalid number `%s'\n", numstr);
1760 }
1761 return ret;
1762}
1763#endif
1764
1765#if defined BB_DD || defined BB_NC
Matt Kraaibfa79672000-12-15 22:34:34 +00001766ssize_t safe_read(int fd, void *buf, size_t count)
1767{
1768 ssize_t n;
1769
1770 do {
1771 n = read(fd, buf, count);
1772 } while (n < 0 && errno == EINTR);
1773
1774 return n;
1775}
1776#endif
1777
Eric Andersencc8ed391999-10-05 16:24:54 +00001778/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001779/*
1780Local Variables:
1781c-file-style: "linux"
1782c-basic-offset: 4
1783tab-width: 4
1784End:
1785*/