blob: 092cfdb10d20b069fd135800e95ebd126badff91 [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 Andersencbe31da2001-02-20 06:14:08 +000028#include <stdio.h>
29#include <string.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <dirent.h>
33#include <time.h>
34#include <utime.h>
35#include <unistd.h>
36#include <ctype.h>
37#include <stdlib.h>
38#include <limits.h>
David Kimdon68c1d842001-02-23 03:55:24 +000039#include <stdarg.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000040#include <sys/ioctl.h>
41#include <sys/utsname.h> /* for uname(2) */
42
Eric Andersen3570a342000-09-25 21:45:58 +000043#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000044#if defined (BB_CHMOD_CHOWN_CHGRP) \
45 || defined (BB_CP_MV) \
46 || defined (BB_FIND) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000047 || defined (BB_INSMOD) \
Erik Andersenfac10d72000-02-07 05:29:42 +000048 || defined (BB_LS) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000049 || defined (BB_RM) \
50 || defined (BB_TAR)
Mark Whitleyf57c9442000-12-07 19:56:48 +000051/* same conditions as recursive_action */
Erik Andersenfac10d72000-02-07 05:29:42 +000052#define bb_need_name_too_long
53#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000054#define bb_need_memory_exhausted
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000055#define bb_need_full_version
Erik Andersenfac10d72000-02-07 05:29:42 +000056#define BB_DECLARE_EXTERN
57#include "messages.c"
58
Eric Andersenab050f52001-01-27 06:01:43 +000059#include "pwd_grp/pwd.h"
60#include "pwd_grp/grp.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Eric Andersened3ef502001-01-27 08:24:39 +000062/* for the _syscall() macros */
63#include <sys/syscall.h>
64#include <linux/unistd.h>
65
Eric Andersen7b91f022001-03-01 07:50:04 +000066/* Busybox mount uses either /proc/mounts or /dev/mtab to
67 * get the list of currently mounted filesystems */
Eric Andersend0246fb1999-11-04 21:18:07 +000068#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000069# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000071# else
Erik Andersen269bba22000-03-08 14:50:47 +000072# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000073const char mtab_file[] = "/dev/mtab";
74# else
Eric Andersenaf4ac772001-02-01 22:43:49 +000075const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000076# endif
77# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000078#endif
79
Eric Andersen67991cf2001-02-14 21:23:06 +000080static struct BB_applet *applet_using;
81
82extern void show_usage(void)
Eric Andersenb0e9a701999-10-18 22:28:26 +000083{
Eric Andersen7e9276b2001-02-22 01:15:47 +000084 const char *format_string;
85 const char *usage_string = usage_messages;
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000086 int i;
Eric Andersen67991cf2001-02-14 21:23:06 +000087
Eric Andersen7e9276b2001-02-22 01:15:47 +000088 for (i = applet_using - applets; i > 0; ) {
89 if (!*usage_string++) {
90 --i;
Eric Andersen3ec8ffb2001-02-14 21:46:30 +000091 }
Eric Andersen67991cf2001-02-14 21:23:06 +000092 }
Eric Andersen7e9276b2001-02-22 01:15:47 +000093 format_string = "%s\n\nUsage: %s %s\n\n";
94 if(*usage_string == 0)
95 format_string = "%s\n\nNo help available.\n\n";
96 fprintf(stderr, format_string,
97 full_version, applet_using->name, usage_string);
Matt Kraaie0bcce02000-09-27 02:29:39 +000098 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000099}
100
Eric Andersen67991cf2001-02-14 21:23:06 +0000101
Mark Whitleyf57c9442000-12-07 19:56:48 +0000102static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000103{
104 fflush(stdout);
105 fprintf(stderr, "%s: ", applet_name);
106 vfprintf(stderr, s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000107}
108
Mark Whitleyf57c9442000-12-07 19:56:48 +0000109extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000110{
111 va_list p;
112
113 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000114 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000115 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000116 putc('\n', stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000117}
118
Mark Whitleyf57c9442000-12-07 19:56:48 +0000119extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000120{
121 va_list p;
122
123 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000124 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000125 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000126 putc('\n', stderr);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000127 exit(EXIT_FAILURE);
128}
129
Mark Whitleyf57c9442000-12-07 19:56:48 +0000130static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000131{
Eric Andersenc73a7f92001-01-20 21:41:11 +0000132 int err=errno;
133 if(s == 0) s = "";
134 verror_msg(s, p);
135 if (*s) s = ": ";
136 fprintf(stderr, "%s%s\n", s, strerror(err));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000137}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000138
Mark Whitleyf57c9442000-12-07 19:56:48 +0000139extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000140{
141 va_list p;
142
143 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000144 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000145 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000146}
147
Mark Whitleyf57c9442000-12-07 19:56:48 +0000148extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000149{
150 va_list p;
151
152 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000153 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000154 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000155 exit(EXIT_FAILURE);
156}
157
Eric Andersenf5d5e772001-01-24 23:34:48 +0000158#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000159/* Returns kernel version encoded as major*65536 + minor*256 + patch,
160 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000161 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000162 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000163extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000164{
Erik Andersend07ee462000-02-21 21:26:32 +0000165 struct utsname name;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000166 char *s;
167 int i, r;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000168
Erik Andersend07ee462000-02-21 21:26:32 +0000169 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000170 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 return (0);
172 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000173
174 s = name.release;
175 r = 0;
176 for (i=0 ; i<3 ; i++) {
177 r = r * 256 + atoi(strtok(s, "."));
178 s = NULL;
179 }
180 return r;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000181}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000182#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000183
Eric Andersen10dc9d42000-06-26 10:45:52 +0000184
185
186#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000187_syscall1(int, sysinfo, struct sysinfo *, info);
188#endif /* BB_INIT */
189
Eric Andersena57ba4d2000-07-08 19:20:49 +0000190#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000191
Eric Andersena57ba4d2000-07-08 19:20:49 +0000192/* Include our own version of <sys/mount.h>, since libc5 doesn't
193 * know about umount2 */
194extern _syscall1(int, umount, const char *, special_file);
Eric Andersena57ba4d2000-07-08 19:20:49 +0000195extern _syscall5(int, mount, const char *, special_file, const char *, dir,
196 const char *, fstype, unsigned long int, rwflag, const void *, data);
Eric Andersen076cdc22001-03-04 06:07:34 +0000197#ifndef __NR_umount2
198# warning This kernel does not support the umount2 syscall
199# warning The umount2 system call is being stubbed out...
200 int umount2(const char * special_file, int flags)
201 {
202 /* BusyBox was compiled against a kernel that did not support
203 * the umount2 system call. To make this application work,
204 * you will need to recompile with a kernel supporting the
205 * umount2 system call.
206 */
207 fprintf(stderr, "\n\nTo make this application work, you will need to recompile\n");
208 fprintf(stderr, "with a kernel supporting the umount2 system call. -Erik\n\n");
209 errno=ENOSYS;
210 return -1;
211 }
212# else
213 extern _syscall2(int, umount2, const char *, special_file, int, flags);
214# endif
Eric Andersena57ba4d2000-07-08 19:20:49 +0000215#endif
216
Eric Andersen076cdc22001-03-04 06:07:34 +0000217
218
Eric Andersen8079b152001-02-23 02:04:33 +0000219#if defined BB_FEATURE_NEW_MODULE_INTERFACE && ( defined BB_INSMOD || defined BB_LSMOD )
Eric Andersenc86e5e62000-08-21 19:38:49 +0000220#ifndef __NR_query_module
Mark Whitley59ab0252001-01-23 22:30:04 +0000221static const int __NR_query_module = 167;
Eric Andersenc86e5e62000-08-21 19:38:49 +0000222#endif
223_syscall5(int, query_module, const char *, name, int, which,
224 void *, buf, size_t, bufsize, size_t*, ret);
225#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000226
227
Erik Andersen23dea9b2000-05-13 00:28:25 +0000228#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000229
Erik Andersen029011b2000-03-04 21:19:32 +0000230#define HASH_SIZE 311 /* Should be prime */
231#define hash_inode(i) ((i) % HASH_SIZE)
232
233static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
234
235/*
236 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
237 * `ino_dev_hashtable', else return 0
238 *
239 * If NAME is a non-NULL pointer to a character pointer, and there is
240 * a match, then set *NAME to the value of the name slot in that
241 * bucket.
242 */
243int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
244{
245 ino_dev_hashtable_bucket_t *bucket;
246
247 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
248 while (bucket != NULL) {
249 if ((bucket->ino == statbuf->st_ino) &&
250 (bucket->dev == statbuf->st_dev))
251 {
252 if (name) *name = bucket->name;
253 return 1;
254 }
255 bucket = bucket->next;
256 }
257 return 0;
258}
259
260/* Add statbuf to statbuf hash table */
261void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
262{
263 int i;
264 size_t s;
265 ino_dev_hashtable_bucket_t *bucket;
266
267 i = hash_inode(statbuf->st_ino);
268 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000269 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000270 bucket->ino = statbuf->st_ino;
271 bucket->dev = statbuf->st_dev;
272 if (name)
273 strcpy(bucket->name, name);
274 else
275 bucket->name[0] = '\0';
276 bucket->next = ino_dev_hashtable[i];
277 ino_dev_hashtable[i] = bucket;
278}
279
280/* Clear statbuf hash table */
281void reset_ino_dev_hashtable(void)
282{
283 int i;
284 ino_dev_hashtable_bucket_t *bucket;
285
286 for (i = 0; i < HASH_SIZE; i++) {
287 while (ino_dev_hashtable[i] != NULL) {
288 bucket = ino_dev_hashtable[i]->next;
289 free(ino_dev_hashtable[i]);
290 ino_dev_hashtable[i] = bucket;
291 }
292 }
293}
294
Erik Andersen23dea9b2000-05-13 00:28:25 +0000295#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000296
Glenn L McGrath5e23e3d2001-01-03 01:38:29 +0000297#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000298/*
299 * Return TRUE if a fileName is a directory.
300 * Nonexistant files return FALSE.
301 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000302int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000303{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000305 int didMalloc = 0;
306
307 if (statBuf == NULL) {
308 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
309 ++didMalloc;
310 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000311
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000313 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000315 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000316
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000317 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
318 status = FALSE;
319 }
320 else status = TRUE;
321
322 if (didMalloc) {
323 free(statBuf);
324 statBuf = NULL;
325 }
326 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000327}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000328#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000329
Eric Andersen70c60862001-01-25 00:00:04 +0000330#if defined BB_AR || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000331/*
Mark Whitley47583682000-12-05 20:03:17 +0000332 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000333 */
Mark Whitley47583682000-12-05 20:03:17 +0000334int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000335{
336 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000337 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000338
Mark Whitley47583682000-12-05 20:03:17 +0000339 while (chunksize > 0) {
340 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000341 size = BUFSIZ;
342 else
Mark Whitley47583682000-12-05 20:03:17 +0000343 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000344 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000345 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000346 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000347 }
348 return (TRUE);
349}
350#endif
351
352
Glenn L McGrathde556612001-02-10 14:53:44 +0000353#if defined (BB_CP_MV) || defined BB_DPKG
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000354/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000355 * Copy one file to another, while possibly preserving its modes, times, and
356 * modes. Returns TRUE if successful, or FALSE on a failure with an error
357 * message output. (Failure is not indicated if attributes cannot be set.)
358 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000359 */
360int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000361copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000362 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000363{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 int rfd;
365 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 struct stat srcStatBuf;
368 struct stat dstStatBuf;
369 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000370
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 if (followLinks == TRUE)
372 status = stat(srcName, &srcStatBuf);
373 else
374 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000375
Erik Andersenfac10d72000-02-07 05:29:42 +0000376 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000377 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000379 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380
381 if (followLinks == TRUE)
382 status = stat(destName, &dstStatBuf);
383 else
384 status = lstat(destName, &dstStatBuf);
385
Erik Andersen59b9e872000-05-10 05:05:45 +0000386 if (status < 0 || forceFlag==TRUE) {
387 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 dstStatBuf.st_ino = -1;
389 dstStatBuf.st_dev = -1;
390 }
391
392 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
393 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000394 error_msg("Copying file \"%s\" to itself", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000395 return FALSE;
396 }
397
398 if (S_ISDIR(srcStatBuf.st_mode)) {
399 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
400 /* Make sure the directory is writable */
401 status = mkdir(destName, 0777777 ^ umask(0));
402 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000403 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 return FALSE;
405 }
406 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000407 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 int link_size;
409
410 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000411 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
412 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 if (link_size < 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 link_val[link_size] = '\0';
418 status = symlink(link_val, destName);
419 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000420 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 return FALSE;
422 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000423#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000425 /* Try to set owner, but fail silently like GNU cp */
426 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000428#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 return TRUE;
430 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
431 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
432 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000433 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 return FALSE;
435 }
436 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
437 || S_ISSOCK(srcStatBuf.st_mode)) {
438 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
439 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000440 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 return FALSE;
442 }
443 } else if (S_ISREG(srcStatBuf.st_mode)) {
444 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
445 rfd = open(srcName, O_RDONLY);
446 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000447 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 return FALSE;
449 }
450
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000451 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452 srcStatBuf.st_mode);
453 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000454 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 close(rfd);
456 return FALSE;
457 }
458
Mark Whitley47583682000-12-05 20:03:17 +0000459 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000460 goto error_exit;
461
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462 close(rfd);
463 if (close(wfd) < 0) {
464 return FALSE;
465 }
466 }
467
468 if (setModes == TRUE) {
469 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000470 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
471 perror_msg_and_die("%s", destName);
472 if (chmod(destName, srcStatBuf.st_mode) < 0)
473 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 times.actime = srcStatBuf.st_atime;
475 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000476 if (utime(destName, &times) < 0)
477 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 }
479
Erik Andersenfac10d72000-02-07 05:29:42 +0000480 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000481
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000483 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000484 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000486
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000488}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000489#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000490
491
492
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000493#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000494
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000495#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
496#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
497
498/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
499static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 0, 0, S_ISUID,
501 0, 0, S_ISGID,
502 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000503};
504
505/* The 9 mode bits to test */
506static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 S_IRUSR, S_IWUSR, S_IXUSR,
508 S_IRGRP, S_IWGRP, S_IXGRP,
509 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000510};
511
Mark Whitley26a3adb2000-12-05 20:10:27 +0000512static const char MODE1[] = "rwxrwxrwx";
513static const char MODE0[] = "---------";
514static const char SMODE1[] = "..s..s..t";
515static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000516
Eric Andersencc8ed391999-10-05 16:24:54 +0000517/*
518 * Return the standard ls-like mode string from a file mode.
519 * This is static and so is overwritten on each call.
520 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000521const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000522{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000524
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 int i;
526
527 buf[0] = TYPECHAR(mode);
528 for (i = 0; i < 9; i++) {
529 if (mode & SBIT[i])
530 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
531 else
532 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
533 }
534 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000535}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000536#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000537
538
Pavel Roskin21ddb382000-07-17 19:31:54 +0000539#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000540/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000541 * Return the standard ls-like time string from a time_t
542 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000543 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000544const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000545{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 time_t now;
547 char *str;
548 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000553
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 strcpy(buf, &str[4]);
555 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000556
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
558 strcpy(&buf[7], &str[20]);
559 buf[11] = '\0';
560 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000561
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000563}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000564#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000565
Eric Andersen70c60862001-01-25 00:00:04 +0000566#if defined BB_DD || defined BB_NC || defined BB_TAIL || defined BB_TAR || defined BB_AR || defined BB_CP_MV
Eric Andersen17d49ef1999-10-06 20:25:32 +0000567/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000568 * Write all of the supplied buffer out to a file.
569 * This does multiple writes as necessary.
570 * Returns the amount written, or -1 on an error.
571 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000572int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000573{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 int cc;
575 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000576
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000578
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 while (len > 0) {
580 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000581
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 if (cc < 0)
583 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000584
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 buf += cc;
586 total += cc;
587 len -= cc;
588 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000589
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000591}
Matt Kraai24ac0172000-12-18 21:38:57 +0000592#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000593
Matt Kraai24ac0172000-12-18 21:38:57 +0000594#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000595/*
596 * Read all of the supplied buffer from a file.
597 * This does multiple reads as necessary.
598 * Returns the amount read, or -1 on an error.
599 * A short read is returned on an end of file.
600 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000601int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000602{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 int cc;
604 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000605
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000607
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 while (len > 0) {
609 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000610
Erik Andersene49d5ec2000-02-08 19:58:47 +0000611 if (cc < 0)
612 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000613
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614 if (cc == 0)
615 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000616
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 buf += cc;
618 total += cc;
619 len -= cc;
620 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000621
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000623}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000624#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000625
626
Erik Andersen23dea9b2000-05-13 00:28:25 +0000627#if defined (BB_CHMOD_CHOWN_CHGRP) \
628 || defined (BB_CP_MV) \
629 || defined (BB_FIND) \
630 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000631 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000632 || defined (BB_RM) \
633 || defined (BB_TAR)
634
Eric Andersencc8ed391999-10-05 16:24:54 +0000635/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000636 * Walk down all the directories under the specified
637 * location, and do something (something specified
638 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000639 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000640 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000641 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000642 * and so isn't sufficiently portable to take over since glibc2.1
643 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000644 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000645int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 int recurse, int followLinks, int depthFirst,
647 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000648 struct stat * statbuf,
649 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000651 struct stat * statbuf,
652 void* userData),
653 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000654{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 int status;
656 struct stat statbuf;
657 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000658
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 if (followLinks == TRUE)
660 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000661 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000663
Eric Andersen2b69c401999-10-05 22:58:32 +0000664 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665#ifdef BB_DEBUG_PRINT_SCAFFOLD
666 fprintf(stderr,
667 "status=%d followLinks=%d TRUE=%d\n",
668 status, followLinks, TRUE);
669#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000670 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000671 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000672 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673
674 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
675 if (fileAction == NULL)
676 return TRUE;
677 else
Erik Andersen3364d782000-03-28 00:58:14 +0000678 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000679 }
680
681 if (recurse == FALSE) {
682 if (S_ISDIR(statbuf.st_mode)) {
683 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000684 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 else
686 return TRUE;
687 }
688 }
689
690 if (S_ISDIR(statbuf.st_mode)) {
691 DIR *dir;
692
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000694 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000696 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000698 } else if (status == SKIP)
699 return TRUE;
700 }
701 dir = opendir(fileName);
702 if (!dir) {
703 perror_msg("%s", fileName);
704 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 }
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000706 status = TRUE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 while ((next = readdir(dir)) != NULL) {
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000708 char nextFile[PATH_MAX];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709
710 if ((strcmp(next->d_name, "..") == 0)
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000711 || (strcmp(next->d_name, ".") == 0)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712 continue;
713 }
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000714 if (strlen(fileName) + strlen(next->d_name) + 1 > PATH_MAX) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000715 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 return FALSE;
717 }
Erik Andersenc3660502000-04-18 21:18:57 +0000718 memset(nextFile, 0, sizeof(nextFile));
Mark Whitleyae201282001-03-07 17:27:04 +0000719 if (fileName[strlen(fileName)-1] == '/')
720 sprintf(nextFile, "%s%s", fileName, next->d_name);
721 else
722 sprintf(nextFile, "%s/%s", fileName, next->d_name);
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000723 if (recursive_action(nextFile, TRUE, followLinks, depthFirst,
724 fileAction, dirAction, userData) == FALSE) {
725 status = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 }
727 }
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000728 closedir(dir);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 if (dirAction != NULL && depthFirst == TRUE) {
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000730 if (dirAction(fileName, &statbuf, userData) == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000731 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 return FALSE;
733 }
734 }
Mark Whitleye2c44fc2001-03-08 16:54:44 +0000735 if (status == FALSE)
736 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 } else {
738 if (fileAction == NULL)
739 return TRUE;
740 else
Erik Andersen3364d782000-03-28 00:58:14 +0000741 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 }
743 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000744}
745
Erik Andersen23dea9b2000-05-13 00:28:25 +0000746#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000747
Eric Andersenf6be9441999-10-13 21:12:06 +0000748
749
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000750#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000751/*
752 * Attempt to create the directories along the specified path, except for
753 * the final component. The mode is given for the final directory only,
754 * while all previous ones get default protections. Errors are not reported
755 * here, as failures to restore files can be reported later.
756 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000757extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000758{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 char *cp;
760 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000761 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000763
Erik Andersene49d5ec2000-02-08 19:58:47 +0000764 strcpy(buf, name);
765 for (cp = buf; *cp == '/'; cp++);
766 cp = strchr(cp, '/');
767 while (cp) {
768 cpOld = cp;
769 cp = strchr(cp + 1, '/');
770 *cpOld = '\0';
771 retVal = mkdir(buf, cp ? 0777 : mode);
772 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000773 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 return FALSE;
775 }
776 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000777 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000778 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000779}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000780#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000781
782
783
Pavel Roskin21ddb382000-07-17 19:31:54 +0000784#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000785 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000786/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000787
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000789{
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000790 static const mode_t group_set[] = {
791 S_ISUID | S_IRWXU, /* u */
792 S_ISGID | S_IRWXG, /* g */
793 S_IRWXO, /* o */
794 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
795 };
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000797 static const mode_t mode_set[] = {
798 S_IRUSR | S_IRGRP | S_IROTH, /* r */
799 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
800 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
801 S_ISUID | S_ISGID, /* s */
802 S_ISVTX /* t */
803 };
804
805 static const char group_string[] = "ugoa";
806 static const char mode_string[] = "rwxst";
807
808 const char *p;
809
810 mode_t andMode =
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000812 mode_t orMode = 0;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000813 mode_t mode;
814 mode_t groups;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 char type;
816 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000817
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000818 if (s==NULL) {
Eric Andersen4e61db22000-09-01 00:06:17 +0000819 return (FALSE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000820 }
Eric Andersen4e61db22000-09-01 00:06:17 +0000821
Eric Andersenf6be9441999-10-13 21:12:06 +0000822 do {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000823 mode = 0;
824 groups = 0;
825 NEXT_GROUP:
826 if ((c = *s++) == '\0') {
827 return -1;
828 }
829 for (p=group_string ; *p ; p++) {
830 if (*p == c) {
831 groups |= group_set[(int)(p-group_string)];
832 goto NEXT_GROUP;
833 }
834 }
835 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000836 case '=':
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000837 case '+':
Eric Andersenf6be9441999-10-13 21:12:06 +0000838 case '-':
839 type = c;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000840 if (groups == 0) { /* The default is "all" */
841 groups |= S_ISUID | S_ISGID | S_ISVTX
842 | S_IRWXU | S_IRWXG | S_IRWXO;
843 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000844 break;
845 default:
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000846 if ((c < '0') || (c > '7') || (mode | groups)) {
847 return (FALSE);
848 } else {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000849 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000850 return (TRUE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000851 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000852 }
853
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000854 NEXT_MODE:
855 if (((c = *s++) != '\0') && (c != ',')) {
856 for (p=mode_string ; *p ; p++) {
857 if (*p == c) {
858 mode |= mode_set[(int)(p-mode_string)];
859 goto NEXT_MODE;
860 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000861 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000862 break; /* We're done so break out of loop.*/
Eric Andersenf6be9441999-10-13 21:12:06 +0000863 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 switch (type) {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000865 case '=':
866 andMode &= ~(groups); /* Now fall through. */
867 case '+':
868 orMode |= mode & groups;
869 break;
870 case '-':
871 andMode &= ~(mode & groups);
872 orMode &= ~(mode & groups);
873 break;
Eric Andersenf6be9441999-10-13 21:12:06 +0000874 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000875 } while (c == ',');
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000876
Eric Andersence8f3b91999-10-20 07:03:36 +0000877 *theMode &= andMode;
878 *theMode |= orMode;
Eric Andersence8f3b91999-10-20 07:03:36 +0000879
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000880 return TRUE;
881}
Eric Andersence8f3b91999-10-20 07:03:36 +0000882
Pavel Roskin21ddb382000-07-17 19:31:54 +0000883#endif
884/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000885
Eric Andersene674eb71999-10-19 20:52:57 +0000886
887
Erik Andersen23dea9b2000-05-13 00:28:25 +0000888
889
Pavel Roskin21ddb382000-07-17 19:31:54 +0000890#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
891 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000892 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Eric Andersen4142d4d2001-02-27 18:22:03 +0000893
894#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000895/* returns a uid given a username */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000896long my_getpwnam(const char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000897{
Eric Andersenab050f52001-01-27 06:01:43 +0000898 struct passwd *myuser;
899
900 myuser = getpwnam(name);
901 if (myuser==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000902 error_msg_and_die("unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000903
904 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000905}
906
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000907/* returns a gid given a group name */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000908long my_getgrnam(const char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000909{
Eric Andersenab050f52001-01-27 06:01:43 +0000910 struct group *mygroup;
911
912 mygroup = getgrnam(name);
913 if (mygroup==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000914 error_msg_and_die("unknown group name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000915
916 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000917}
Eric Andersen4142d4d2001-02-27 18:22:03 +0000918#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +0000919
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000920/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000921void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000922{
Eric Andersenab050f52001-01-27 06:01:43 +0000923 struct passwd *myuser;
924
925 myuser = getpwuid(uid);
926 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000927 sprintf(name, "%-8ld ", (long)uid);
928 else
929 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000930}
931
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000932/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000933void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000934{
Eric Andersenab050f52001-01-27 06:01:43 +0000935 struct group *mygroup;
936
937 mygroup = getgrgid(gid);
938 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000939 sprintf(group, "%-8ld ", (long)gid);
940 else
941 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000942}
943
Matt Kraaie6e81832000-12-30 07:46:23 +0000944#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000945/* gets a gid given a user name */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000946long my_getpwnamegid(const char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000947{
Eric Andersenab050f52001-01-27 06:01:43 +0000948 struct group *mygroup;
949 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000950
Eric Andersenab050f52001-01-27 06:01:43 +0000951 myuser=getpwnam(name);
952 if (myuser==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000953 error_msg_and_die("unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000954
955 mygroup = getgrgid(myuser->pw_gid);
956 if (mygroup==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000957 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000958
959 return mygroup->gr_gid;
960}
961#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000962#endif
963 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
964 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000965
966
Eric Andersenf86aee72000-07-06 17:20:46 +0000967#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000968
Eric Andersenbd22ed82000-07-08 18:55:24 +0000969/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000970static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
971static const int KB_84 = 0x01;
972static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000973
Erik Andersene49d5ec2000-02-08 19:58:47 +0000974int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000975{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976 char arg;
977
978 arg = 0;
979 return (ioctl(fd, KDGKBTYPE, &arg) == 0
980 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000981}
982
Erik Andersene49d5ec2000-02-08 19:58:47 +0000983static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000984{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 int fd;
986
987 /* try read-only */
988 fd = open(fnam, O_RDWR);
989
990 /* if failed, try read-only */
991 if (fd < 0 && errno == EACCES)
992 fd = open(fnam, O_RDONLY);
993
994 /* if failed, try write-only */
995 if (fd < 0 && errno == EACCES)
996 fd = open(fnam, O_WRONLY);
997
998 /* if failed, fail */
999 if (fd < 0)
1000 return -1;
1001
1002 /* if not a console, fail */
1003 if (!is_a_console(fd)) {
1004 close(fd);
1005 return -1;
1006 }
1007
1008 /* success */
1009 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001010}
1011
1012/*
1013 * Get an fd for use with kbd/console ioctls.
1014 * We try several things because opening /dev/console will fail
1015 * if someone else used X (which does a chown on /dev/console).
1016 *
1017 * if tty_name is non-NULL, try this one instead.
1018 */
1019
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001021{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001022 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001023
Erik Andersene49d5ec2000-02-08 19:58:47 +00001024 if (tty_name) {
1025 if (-1 == (fd = open_a_console(tty_name)))
1026 return -1;
1027 else
1028 return fd;
1029 }
1030
1031 fd = open_a_console("/dev/tty");
1032 if (fd >= 0)
1033 return fd;
1034
1035 fd = open_a_console("/dev/tty0");
1036 if (fd >= 0)
1037 return fd;
1038
1039 fd = open_a_console("/dev/console");
1040 if (fd >= 0)
1041 return fd;
1042
1043 for (fd = 0; fd < 3; fd++)
1044 if (is_a_console(fd))
1045 return fd;
1046
Matt Kraaidd19c692001-01-31 19:00:21 +00001047 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001048 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001049}
1050
1051
Eric Andersenf86aee72000-07-06 17:20:46 +00001052#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001053
1054
Eric Andersen9f16d612000-06-12 23:11:16 +00001055#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001056/*
1057 * Routine to see if a text string is matched by a wildcard pattern.
1058 * Returns TRUE if the text is matched, or FALSE if it is not matched
1059 * or if the pattern is invalid.
1060 * * matches zero or more characters
1061 * ? matches a single character
1062 * [abc] matches 'a', 'b' or 'c'
1063 * \c quotes character c
1064 * Adapted from code written by Ingo Wilken, and
1065 * then taken from sash, Copyright (c) 1999 by David I. Bell
1066 * Permission is granted to use, distribute, or modify this source,
1067 * provided that this copyright notice remains intact.
1068 * Permission to distribute this code under the GPL has been granted.
1069 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001070extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001071{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 const char *retryPat;
1073 const char *retryText;
1074 int ch;
1075 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001076 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001077
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 retryPat = NULL;
1079 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001080
Erik Andersene49d5ec2000-02-08 19:58:47 +00001081 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001082 ch = *pattern++;
1083
Erik Andersene49d5ec2000-02-08 19:58:47 +00001084 switch (ch) {
1085 case '*':
1086 retryPat = pattern;
1087 retryText = text;
1088 break;
1089
1090 case '[':
1091 found = FALSE;
1092
1093 while ((ch = *pattern++) != ']') {
1094 if (ch == '\\')
1095 ch = *pattern++;
1096
1097 if (ch == '\0')
1098 return FALSE;
1099
1100 if (*text == ch)
1101 found = TRUE;
1102 }
Erik Andersen632bb572000-04-18 22:09:06 +00001103 len=strlen(text);
1104 if (found == FALSE && len!=0) {
1105 return FALSE;
1106 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001108 if (strlen(pattern)==0 && len==1) {
1109 return TRUE;
1110 }
1111 if (len!=0) {
1112 text++;
1113 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001114 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 }
1116
1117 /* fall into next case */
1118
1119 case '?':
1120 if (*text++ == '\0')
1121 return FALSE;
1122
1123 break;
1124
1125 case '\\':
1126 ch = *pattern++;
1127
1128 if (ch == '\0')
1129 return FALSE;
1130
1131 /* fall into next case */
1132
1133 default:
1134 if (*text == ch) {
1135 if (*text)
1136 text++;
1137 break;
1138 }
1139
1140 if (*text) {
1141 pattern = retryPat;
1142 text = ++retryText;
1143 break;
1144 }
1145
Eric Andersenb186d981999-12-03 09:19:54 +00001146 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001147 }
1148
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 if (pattern == NULL)
1150 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001151 }
1152
Erik Andersene49d5ec2000-02-08 19:58:47 +00001153 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001154}
Eric Andersen9f16d612000-06-12 23:11:16 +00001155#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001156
1157
Eric Andersen29d2e361999-11-06 06:07:27 +00001158
1159
Erik Andersen23dea9b2000-05-13 00:28:25 +00001160#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001161#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001162/*
1163 * Given a block device, find the mount table entry if that block device
1164 * is mounted.
1165 *
1166 * Given any other file (or directory), find the mount table entry for its
1167 * filesystem.
1168 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001169extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001170{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001171 struct stat s;
1172 dev_t mountDevice;
1173 FILE *mountTable;
1174 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001175
Erik Andersene49d5ec2000-02-08 19:58:47 +00001176 if (stat(name, &s) != 0)
1177 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001178
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 if ((s.st_mode & S_IFMT) == S_IFBLK)
1180 mountDevice = s.st_rdev;
1181 else
1182 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001183
1184
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 if ((mountTable = setmntent(table, "r")) == 0)
1186 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001187
Erik Andersene49d5ec2000-02-08 19:58:47 +00001188 while ((mountEntry = getmntent(mountTable)) != 0) {
1189 if (strcmp(name, mountEntry->mnt_dir) == 0
1190 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1191 break;
1192 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1193 break;
1194 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1195 break;
1196 }
1197 endmntent(mountTable);
1198 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001199}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001200#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001201
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001202#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001203/* try to open up the specified device */
1204extern int device_open(char *device, int mode)
1205{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001206 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001207
Erik Andersene49d5ec2000-02-08 19:58:47 +00001208 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001209
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210 /* Retry up to 5 times */
1211 for (f = 0; f < 5; f++)
1212 if ((fd = open(device, m, 0600)) >= 0)
1213 break;
1214 if (fd < 0)
1215 return fd;
1216 /* Reset original flags. */
1217 if (m != mode)
1218 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001219 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001220}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001221#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001222
1223
Erik Andersen23dea9b2000-05-13 00:28:25 +00001224#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001225#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001226#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001227#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001228
Erik Andersen23dea9b2000-05-13 00:28:25 +00001229#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001230/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001231 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001232 * This finds the pid of the specified process,
1233 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001234 *
Erik Andersen825aead2000-04-07 06:00:07 +00001235 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001236 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001237extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001238{
Erik Andersen825aead2000-04-07 06:00:07 +00001239 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001240 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001241 pid_t num_pids;
1242 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001243 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001244
1245 /* open device */
1246 fd = open(device, O_RDONLY);
1247 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001248 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001249
1250 /* Find out how many processes there are */
1251 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001252 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001253
1254 /* Allocate some memory -- grab a few extras just in case
1255 * some new processes start up while we wait. The kernel will
1256 * just ignore any extras if we give it too many, and will trunc.
1257 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001258 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001259 pid_array[0] = num_pids+10;
1260
1261 /* Now grab the pid list */
1262 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001263 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001264
1265 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001266 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001267 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001268 struct pid_info info;
1269
1270 info.pid = pid_array[i];
1271 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001272 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001273
Erik Andersen0a64de92000-04-18 20:00:03 +00001274 /* Make sure we only match on the process name */
1275 p=info.command_line+1;
1276 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1277 (p)++;
1278 }
1279 if (isspace(*(p)))
1280 *p='\0';
1281
Erik Andersenb9167cb2000-04-18 22:41:30 +00001282 if ((strstr(info.command_line, pidName) != NULL)
1283 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001284 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001285 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001286 }
1287 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001288 if (pidList)
1289 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001290
1291 /* Free memory */
1292 free( pid_array);
1293
1294 /* close device */
1295 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001296 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001297
Erik Andersen825aead2000-04-07 06:00:07 +00001298 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001299}
Erik Andersen269bba22000-03-08 14:50:47 +00001300#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001301
Mark Whitleyf57c9442000-12-07 19:56:48 +00001302/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001303 *
1304 * This finds the pid of the specified process.
1305 * Currently, it's implemented by rummaging through
1306 * the proc filesystem.
1307 *
Erik Andersen825aead2000-04-07 06:00:07 +00001308 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001309 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001310extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001311{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001312 DIR *dir;
1313 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001314 pid_t* pidList=NULL;
1315 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001316
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001317 dir = opendir("/proc");
1318 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001319 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001320
1321 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001322 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001323 char filename[256];
1324 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001325
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001326 /* If it isn't a number, we don't want it */
1327 if (!isdigit(*next->d_name))
1328 continue;
1329
Eric Andersen31e68292000-09-14 21:12:20 +00001330 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001331 status = fopen(filename, "r");
1332 if (!status) {
1333 continue;
1334 }
1335 fgets(buffer, 256, status);
1336 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001337
Eric Andersen31e68292000-09-14 21:12:20 +00001338 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001339 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001340 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001341 }
John Beppuf95ca971999-12-09 22:10:18 +00001342 }
Eric Andersen31e68292000-09-14 21:12:20 +00001343
Erik Andersen0a64de92000-04-18 20:00:03 +00001344 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001345 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001346 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001347}
Erik Andersen269bba22000-03-08 14:50:47 +00001348#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001349#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001350
Eric Andersenae6eae02000-07-19 17:35:54 +00001351#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001352/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001353extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001354{
Eric Andersenae6eae02000-07-19 17:35:54 +00001355 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001356
Eric Andersenae6eae02000-07-19 17:35:54 +00001357 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001358 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001359 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001360}
1361
Eric Andersen501c88b2000-07-28 15:14:45 +00001362extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001363{
1364 void *ptr = realloc(old, size);
1365 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001366 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001367 return ptr;
1368}
Eric Andersen501c88b2000-07-28 15:14:45 +00001369
1370extern void *xcalloc(size_t nmemb, size_t size)
1371{
1372 void *ptr = calloc(nmemb, size);
1373 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001374 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001375 return ptr;
1376}
Eric Andersenae6eae02000-07-19 17:35:54 +00001377#endif
1378
Eric Andersenca0f4582001-02-16 18:43:54 +00001379#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || \
1380 defined BB_WGET || defined BB_DPKG_DEB || defined BB_TAR || \
1381 defined BB_LN
Eric Andersenae6eae02000-07-19 17:35:54 +00001382# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001383extern char * xstrdup (const char *s) {
1384 char *t;
1385
1386 if (s == NULL)
1387 return NULL;
1388
1389 t = strdup (s);
1390
1391 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001392 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001393
1394 return t;
1395}
Eric Andersenae6eae02000-07-19 17:35:54 +00001396# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001397#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001398
Eric Andersenf5d5e772001-01-24 23:34:48 +00001399#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001400extern char * xstrndup (const char *s, int n) {
1401 char *t;
1402
1403 if (s == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001404 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001405
Eric Andersen12dceb12001-02-18 20:15:36 +00001406 t = xmalloc(++n);
1407
1408 return safe_strncpy(t,s,n);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001409}
1410#endif
1411
Eric Andersen12dceb12001-02-18 20:15:36 +00001412#if defined BB_IFCONFIG || defined BB_ROUTE || defined BB_NFSMOUNT || \
1413 defined BB_FEATURE_MOUNT_LOOP
Eric Andersenec455952001-02-14 08:11:27 +00001414/* Like strncpy but make sure the resulting string is always 0 terminated. */
1415extern char * safe_strncpy(char *dst, const char *src, size_t size)
1416{
1417 dst[size-1] = '\0';
1418 return strncpy(dst, src, size-1);
1419}
1420#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001421
Erik Andersen23dea9b2000-05-13 00:28:25 +00001422#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001423extern int vdprintf(int d, const char *format, va_list ap)
1424{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001425 char buf[BUF_SIZE];
1426 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001427
Erik Andersene49d5ec2000-02-08 19:58:47 +00001428 len = vsprintf(buf, format, ap);
1429 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001430}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001431#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001432
Eric Andersen624cc772000-09-21 02:04:51 +00001433
Erik Andersen23dea9b2000-05-13 00:28:25 +00001434#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001435#include <fcntl.h>
1436#include "loop.h" /* Pull in loop device support */
1437
Erik Andersen5cbdd712000-01-26 20:06:48 +00001438extern int del_loop(const char *device)
1439{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001440 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001441
Erik Andersene49d5ec2000-02-08 19:58:47 +00001442 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001443 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001444 return (FALSE);
1445 }
1446 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001447 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448 return (FALSE);
1449 }
1450 close(fd);
1451 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001452}
Erik Andersende7965c2000-01-26 23:49:21 +00001453
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454extern int set_loop(const char *device, const char *file, int offset,
1455 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001456{
1457 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001458 int fd, ffd, mode;
1459
Erik Andersende7965c2000-01-26 23:49:21 +00001460 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001461 if ((ffd = open(file, mode)) < 0 && !*loopro
1462 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001463 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001464 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001465 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001466 if ((fd = open(device, mode)) < 0) {
1467 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001468 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001469 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001470 }
1471 *loopro = (mode == O_RDONLY);
1472
1473 memset(&loopinfo, 0, sizeof(loopinfo));
Eric Andersen12dceb12001-02-18 20:15:36 +00001474 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersende7965c2000-01-26 23:49:21 +00001475
1476 loopinfo.lo_offset = offset;
1477
1478 loopinfo.lo_encrypt_key_size = 0;
1479 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001480 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001481 close(fd);
1482 close(ffd);
1483 return 1;
1484 }
1485 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1486 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001487 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001488 close(fd);
1489 close(ffd);
1490 return 1;
1491 }
1492 close(fd);
1493 close(ffd);
1494 return 0;
1495}
1496
Erik Andersene49d5ec2000-02-08 19:58:47 +00001497extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001498{
1499 char dev[20];
1500 int i, fd;
1501 struct stat statbuf;
1502 struct loop_info loopinfo;
1503
Erik Andersene49d5ec2000-02-08 19:58:47 +00001504 for (i = 0; i <= 7; i++) {
1505 sprintf(dev, "/dev/loop%d", i);
1506 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1507 if ((fd = open(dev, O_RDONLY)) >= 0) {
Eric Andersen1a046d52001-03-06 19:35:58 +00001508 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001509 if (errno == ENXIO) { /* probably free */
1510 close(fd);
1511 return strdup(dev);
1512 }
1513 }
1514 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001515 }
Erik Andersende7965c2000-01-26 23:49:21 +00001516 }
Erik Andersende7965c2000-01-26 23:49:21 +00001517 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001518 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001519}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001520#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001521
Erik Andersen23dea9b2000-05-13 00:28:25 +00001522#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001523extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001524{
Erik Andersen016ffe92000-03-22 00:58:54 +00001525 DIR *dir;
1526 struct dirent *entry;
1527 struct stat statBuf, rootStat;
1528 char fileName[BUFSIZ];
1529
Erik Andersenec5bd902000-03-22 07:12:05 +00001530 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001531 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001532 return( FALSE);
1533 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001534
Erik Andersenec5bd902000-03-22 07:12:05 +00001535 dir = opendir("/dev");
1536 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001537 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001538 return( FALSE);
1539 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001540
1541 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001542
Erik Andersen016ffe92000-03-22 00:58:54 +00001543 /* Must skip ".." since that is "/", and so we
1544 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001545 if (strcmp(entry->d_name, "..") == 0)
1546 continue;
1547
Eric Andersenf9ca6532000-08-11 17:45:02 +00001548 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001549
1550 if (stat(fileName, &statBuf) != 0)
1551 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001552 /* Some char devices have the same dev_t as block
1553 * devices, so make sure this is a block device */
1554 if (! S_ISBLK(statBuf.st_mode))
1555 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001556 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001557 strcpy(name, fileName);
1558 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001559 }
1560 }
1561
Erik Andersenec5bd902000-03-22 07:12:05 +00001562 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001563}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001564#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001565
Mark Whitley1ca41772000-06-28 22:15:26 +00001566
1567/* get_line_from_file() - This function reads an entire line from a text file
1568 * up to a newline. It returns a malloc'ed char * which must be stored and
1569 * free'ed by the caller. */
1570extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001571{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001572 static const int GROWBY = 80; /* how large we will grow strings by */
1573
Mark Whitley1ca41772000-06-28 22:15:26 +00001574 int ch;
1575 int idx = 0;
1576 char *linebuf = NULL;
1577 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001578
Mark Whitley1ca41772000-06-28 22:15:26 +00001579 while (1) {
1580 ch = fgetc(file);
1581 if (ch == EOF)
1582 break;
1583 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001584 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001585 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001586 linebuf[idx++] = (char)ch;
1587 if ((char)ch == '\n')
1588 break;
1589 }
John Beppu5a728cf2000-04-17 04:22:09 +00001590
Mark Whitley1ca41772000-06-28 22:15:26 +00001591 if (idx == 0)
1592 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001593
Mark Whitley1ca41772000-06-28 22:15:26 +00001594 linebuf[idx] = 0;
1595 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001596}
Erik Andersen016ffe92000-03-22 00:58:54 +00001597
Eric Andersen944be902000-09-25 16:58:39 +00001598#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001599extern void print_file(FILE *file)
1600{
1601 int c;
1602
1603 while ((c = getc(file)) != EOF)
1604 putc(c, stdout);
1605 fclose(file);
1606 fflush(stdout);
1607}
1608
1609extern int print_file_by_name(char *filename)
1610{
1611 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001612 if ((file = wfopen(filename, "r")) == NULL)
1613 return FALSE;
1614 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001615 return TRUE;
1616}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001617#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001618
Matt Kraaicabafae2000-11-30 04:55:40 +00001619#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001620char process_escape_sequence(char **ptr)
1621{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001622 static const char charmap[] = {
1623 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1624 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001625
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001626 const char *p;
1627 char *q;
1628 int num_digits;
1629 unsigned int n;
1630
1631 n = 0;
1632 q = *ptr;
1633
1634 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1635 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1636 break;
1637 }
1638 n = n * 8 + (*q++ - '0');
1639 }
1640
1641 if (num_digits == 0) { /* mnemonic escape sequence? */
1642 for (p=charmap ; *p ; p++) {
1643 if (*p == *q) {
1644 q++;
1645 break;
1646 }
1647 }
1648 n = *(p+(sizeof(charmap)/2));
1649 }
1650
1651 /* doesn't hurt to fall through to here from mnemonic case */
1652 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1653 n /= 8; /* adjust value and */
1654 --q; /* back up one char */
1655 }
1656
1657 *ptr = q;
1658 return (char) n;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001659}
1660#endif
1661
Eric Andersen70c60862001-01-25 00:00:04 +00001662#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001663 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001664char *get_last_path_component(char *path)
1665{
1666 char *s=path+strlen(path)-1;
1667
1668 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001669 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001670 *s-- = '\0';
1671 }
1672
1673 /* find last component */
1674 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001675 if (s == NULL || s[1] == '\0')
1676 return path;
1677 else
1678 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001679}
1680#endif
1681
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001682#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001683#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001684void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001685{
1686 int ret;
1687 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1688 int errmsgsz = regerror(ret, preg, NULL, 0);
1689 char *errmsg = xmalloc(errmsgsz);
1690 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001691 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001692 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001693}
1694#endif
1695
Eric Andersen7a86e612000-10-09 18:21:44 +00001696#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001697FILE *wfopen(const char *path, const char *mode)
1698{
1699 FILE *fp;
1700 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001701 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001702 errno = 0;
1703 }
1704 return fp;
1705}
1706#endif
1707
Eric Andersene790ddb2000-11-17 06:50:42 +00001708#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001709 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001710 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
Eric Andersen12dceb12001-02-18 20:15:36 +00001711 || defined BB_MOUNT || defined BB_ROUTE
Matt Kraaie0bcce02000-09-27 02:29:39 +00001712FILE *xfopen(const char *path, const char *mode)
1713{
1714 FILE *fp;
1715 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001716 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001717 return fp;
1718}
1719#endif
1720
Matt Kraaif2cc2762001-02-01 19:21:20 +00001721static int applet_name_compare(const void *x, const void *y)
Eric Andersene5aef922000-11-17 18:25:26 +00001722{
Matt Kraaif2cc2762001-02-01 19:21:20 +00001723 const char *name = x;
1724 const struct BB_applet *applet = y;
Eric Andersene5aef922000-11-17 18:25:26 +00001725
Matt Kraaif2cc2762001-02-01 19:21:20 +00001726 return strcmp(name, applet->name);
1727}
1728
1729extern size_t NUM_APPLETS;
1730
1731struct BB_applet *find_applet_by_name(const char *name)
1732{
1733 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1734 applet_name_compare);
Eric Andersene5aef922000-11-17 18:25:26 +00001735}
1736
Eric Andersen67991cf2001-02-14 21:23:06 +00001737void run_applet_by_name(const char *name, int argc, char **argv)
1738{
1739 /* Do a binary search to find the applet entry given the name. */
1740 if ((applet_using = find_applet_by_name(name)) != NULL) {
1741 applet_name = applet_using->name;
1742 if (argv[1] && strcmp(argv[1], "--help") == 0)
1743 show_usage();
1744 exit((*(applet_using->main)) (argc, argv));
1745 }
1746}
1747
Eric Andersenca0f4582001-02-16 18:43:54 +00001748#if defined BB_DD || defined BB_TAIL || defined BB_STTY
Matt Kraaia164c642001-02-05 17:50:03 +00001749unsigned long parse_number(const char *numstr,
1750 const struct suffix_mult *suffixes)
Matt Kraai24ac0172000-12-18 21:38:57 +00001751{
Matt Kraaia164c642001-02-05 17:50:03 +00001752 const struct suffix_mult *sm;
Matt Kraai24ac0172000-12-18 21:38:57 +00001753 unsigned long int ret;
1754 int len;
1755 char *end;
1756
1757 ret = strtoul(numstr, &end, 10);
1758 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001759 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001760 while (end[0] != '\0') {
Eric Andersen12dceb12001-02-18 20:15:36 +00001761 sm = suffixes;
1762 while ( sm != 0 ) {
1763 if(sm->suffix) {
1764 len = strlen(sm->suffix);
1765 if (strncmp(sm->suffix, end, len) == 0) {
1766 ret *= sm->mult;
1767 end += len;
1768 break;
1769 }
1770 sm++;
1771
1772 } else
1773 sm = 0;
Matt Kraai24ac0172000-12-18 21:38:57 +00001774 }
Eric Andersen12dceb12001-02-18 20:15:36 +00001775 if (sm == 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001776 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001777 }
1778 return ret;
1779}
1780#endif
1781
Eric Andersen23a93f52001-01-24 23:47:21 +00001782#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001783ssize_t safe_read(int fd, void *buf, size_t count)
1784{
1785 ssize_t n;
1786
1787 do {
1788 n = read(fd, buf, count);
1789 } while (n < 0 && errno == EINTR);
1790
1791 return n;
1792}
1793#endif
1794
Richard June6d0921c2001-01-22 22:35:38 +00001795#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +00001796const char *make_human_readable_str(unsigned long val, unsigned long hr)
Richard June6d0921c2001-01-22 22:35:38 +00001797{
Eric Andersen7850f1e2001-03-07 03:50:57 +00001798 int i=0;
1799 static char str[10] = "\0";
1800 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
1801 unsigned long divisor = 1;
Richard June6d0921c2001-01-22 22:35:38 +00001802
Eric Andersen7850f1e2001-03-07 03:50:57 +00001803 if(val == 0)
1804 return("0");
1805 if(hr)
1806 snprintf(str, 9, "%ld", val/hr);
1807 else {
1808 while(val >= divisor && i <= 4) {
1809 divisor=divisor<<10, i++;
1810 }
1811 divisor=divisor>>10, i--;
1812 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001813 }
Eric Andersen7850f1e2001-03-07 03:50:57 +00001814 return(str);
Richard June6d0921c2001-01-22 22:35:38 +00001815}
1816#endif
1817
Eric Andersenca604592001-03-08 17:17:13 +00001818#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS) || defined(BB_SH)
Matt Kraai05e782d2001-02-01 16:49:30 +00001819void chomp(char *s)
1820{
1821 size_t len = strlen(s);
1822
1823 if (len == 0)
1824 return;
1825
1826 if (s[len-1] == '\n')
1827 s[len-1] = '\0';
1828}
1829#endif
1830
Eric Andersen13d1fa12001-03-08 23:59:45 +00001831#if defined(BB_SH)
1832void trim(char *s)
1833{
Mark Whitleyafffbfe2001-03-09 00:55:34 +00001834 /* trim trailing whitespace */
1835 while (isspace(s[strlen(s)-1]))
1836 s[strlen(s)-1]='\0';
1837
Eric Andersen13d1fa12001-03-08 23:59:45 +00001838 /* trim leading whitespace */
1839 memmove(s, &s[strspn(s, " \n\r\t\v")], strlen(s));
1840
Eric Andersen13d1fa12001-03-08 23:59:45 +00001841}
1842#endif
1843
Eric Andersencc8ed391999-10-05 16:24:54 +00001844/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001845/*
1846Local Variables:
1847c-file-style: "linux"
1848c-basic-offset: 4
1849tab-width: 4
1850End:
1851*/