blob: 0ace3eb341681fa507b379680dd24842304fb5ff [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 }
706 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000707 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708
709 if ((strcmp(next->d_name, "..") == 0)
710 || (strcmp(next->d_name, ".") == 0)) {
711 continue;
712 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000713 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000714 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 return FALSE;
716 }
Erik Andersenc3660502000-04-18 21:18:57 +0000717 memset(nextFile, 0, sizeof(nextFile));
Mark Whitleyae201282001-03-07 17:27:04 +0000718 if (fileName[strlen(fileName)-1] == '/')
719 sprintf(nextFile, "%s%s", fileName, next->d_name);
720 else
721 sprintf(nextFile, "%s/%s", fileName, next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000723 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000724 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000725 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 closedir(dir);
727 return FALSE;
728 }
729 }
730 status = closedir(dir);
731 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000732 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 return FALSE;
734 }
735 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000736 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000738 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000739 return FALSE;
740 }
741 }
742 } else {
743 if (fileAction == NULL)
744 return TRUE;
745 else
Erik Andersen3364d782000-03-28 00:58:14 +0000746 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 }
748 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000749}
750
Erik Andersen23dea9b2000-05-13 00:28:25 +0000751#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000752
Eric Andersenf6be9441999-10-13 21:12:06 +0000753
754
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000755#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000756/*
757 * Attempt to create the directories along the specified path, except for
758 * the final component. The mode is given for the final directory only,
759 * while all previous ones get default protections. Errors are not reported
760 * here, as failures to restore files can be reported later.
761 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000762extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000763{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000764 char *cp;
765 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000766 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000768
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 strcpy(buf, name);
770 for (cp = buf; *cp == '/'; cp++);
771 cp = strchr(cp, '/');
772 while (cp) {
773 cpOld = cp;
774 cp = strchr(cp + 1, '/');
775 *cpOld = '\0';
776 retVal = mkdir(buf, cp ? 0777 : mode);
777 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000778 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 return FALSE;
780 }
781 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000782 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000784}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000785#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000786
787
788
Pavel Roskin21ddb382000-07-17 19:31:54 +0000789#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000790 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000791/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000792
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000794{
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000795 static const mode_t group_set[] = {
796 S_ISUID | S_IRWXU, /* u */
797 S_ISGID | S_IRWXG, /* g */
798 S_IRWXO, /* o */
799 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
800 };
Erik Andersene49d5ec2000-02-08 19:58:47 +0000801
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000802 static const mode_t mode_set[] = {
803 S_IRUSR | S_IRGRP | S_IROTH, /* r */
804 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
805 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
806 S_ISUID | S_ISGID, /* s */
807 S_ISVTX /* t */
808 };
809
810 static const char group_string[] = "ugoa";
811 static const char mode_string[] = "rwxst";
812
813 const char *p;
814
815 mode_t andMode =
Erik Andersene49d5ec2000-02-08 19:58:47 +0000816 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000817 mode_t orMode = 0;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000818 mode_t mode;
819 mode_t groups;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 char type;
821 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000822
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000823 if (s==NULL) {
Eric Andersen4e61db22000-09-01 00:06:17 +0000824 return (FALSE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000825 }
Eric Andersen4e61db22000-09-01 00:06:17 +0000826
Eric Andersenf6be9441999-10-13 21:12:06 +0000827 do {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000828 mode = 0;
829 groups = 0;
830 NEXT_GROUP:
831 if ((c = *s++) == '\0') {
832 return -1;
833 }
834 for (p=group_string ; *p ; p++) {
835 if (*p == c) {
836 groups |= group_set[(int)(p-group_string)];
837 goto NEXT_GROUP;
838 }
839 }
840 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000841 case '=':
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000842 case '+':
Eric Andersenf6be9441999-10-13 21:12:06 +0000843 case '-':
844 type = c;
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000845 if (groups == 0) { /* The default is "all" */
846 groups |= S_ISUID | S_ISGID | S_ISVTX
847 | S_IRWXU | S_IRWXG | S_IRWXO;
848 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000849 break;
850 default:
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000851 if ((c < '0') || (c > '7') || (mode | groups)) {
852 return (FALSE);
853 } else {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000854 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000855 return (TRUE);
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000856 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000857 }
858
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000859 NEXT_MODE:
860 if (((c = *s++) != '\0') && (c != ',')) {
861 for (p=mode_string ; *p ; p++) {
862 if (*p == c) {
863 mode |= mode_set[(int)(p-mode_string)];
864 goto NEXT_MODE;
865 }
Eric Andersenf6be9441999-10-13 21:12:06 +0000866 }
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000867 break; /* We're done so break out of loop.*/
Eric Andersenf6be9441999-10-13 21:12:06 +0000868 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 switch (type) {
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000870 case '=':
871 andMode &= ~(groups); /* Now fall through. */
872 case '+':
873 orMode |= mode & groups;
874 break;
875 case '-':
876 andMode &= ~(mode & groups);
877 orMode &= ~(mode & groups);
878 break;
Eric Andersenf6be9441999-10-13 21:12:06 +0000879 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 } while (c == ',');
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000881
Eric Andersence8f3b91999-10-20 07:03:36 +0000882 *theMode &= andMode;
883 *theMode |= orMode;
Eric Andersence8f3b91999-10-20 07:03:36 +0000884
Eric Andersenfc83c4c2001-02-14 07:15:30 +0000885 return TRUE;
886}
Eric Andersence8f3b91999-10-20 07:03:36 +0000887
Pavel Roskin21ddb382000-07-17 19:31:54 +0000888#endif
889/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000890
Eric Andersene674eb71999-10-19 20:52:57 +0000891
892
Erik Andersen23dea9b2000-05-13 00:28:25 +0000893
894
Pavel Roskin21ddb382000-07-17 19:31:54 +0000895#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
896 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000897 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Eric Andersen4142d4d2001-02-27 18:22:03 +0000898
899#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000900/* returns a uid given a username */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000901long my_getpwnam(const char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000902{
Eric Andersenab050f52001-01-27 06:01:43 +0000903 struct passwd *myuser;
904
905 myuser = getpwnam(name);
906 if (myuser==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000907 error_msg_and_die("unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000908
909 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000910}
911
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000912/* returns a gid given a group name */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000913long my_getgrnam(const char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000914{
Eric Andersenab050f52001-01-27 06:01:43 +0000915 struct group *mygroup;
916
917 mygroup = getgrnam(name);
918 if (mygroup==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000919 error_msg_and_die("unknown group name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000920
921 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000922}
Eric Andersen4142d4d2001-02-27 18:22:03 +0000923#endif
Eric Andersend23f9ba1999-10-20 19:18:15 +0000924
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000925/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000926void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000927{
Eric Andersenab050f52001-01-27 06:01:43 +0000928 struct passwd *myuser;
929
930 myuser = getpwuid(uid);
931 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000932 sprintf(name, "%-8ld ", (long)uid);
933 else
934 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000935}
936
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000937/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000938void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000939{
Eric Andersenab050f52001-01-27 06:01:43 +0000940 struct group *mygroup;
941
942 mygroup = getgrgid(gid);
943 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000944 sprintf(group, "%-8ld ", (long)gid);
945 else
946 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000947}
948
Matt Kraaie6e81832000-12-30 07:46:23 +0000949#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000950/* gets a gid given a user name */
Eric Andersen4142d4d2001-02-27 18:22:03 +0000951long my_getpwnamegid(const char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000952{
Eric Andersenab050f52001-01-27 06:01:43 +0000953 struct group *mygroup;
954 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000955
Eric Andersenab050f52001-01-27 06:01:43 +0000956 myuser=getpwnam(name);
957 if (myuser==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000958 error_msg_and_die("unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000959
960 mygroup = getgrgid(myuser->pw_gid);
961 if (mygroup==NULL)
Eric Andersen4142d4d2001-02-27 18:22:03 +0000962 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000963
964 return mygroup->gr_gid;
965}
966#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000967#endif
968 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
969 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000970
971
Eric Andersenf86aee72000-07-06 17:20:46 +0000972#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000973
Eric Andersenbd22ed82000-07-08 18:55:24 +0000974/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000975static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
976static const int KB_84 = 0x01;
977static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000978
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000980{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000981 char arg;
982
983 arg = 0;
984 return (ioctl(fd, KDGKBTYPE, &arg) == 0
985 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000986}
987
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000989{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 int fd;
991
992 /* try read-only */
993 fd = open(fnam, O_RDWR);
994
995 /* if failed, try read-only */
996 if (fd < 0 && errno == EACCES)
997 fd = open(fnam, O_RDONLY);
998
999 /* if failed, try write-only */
1000 if (fd < 0 && errno == EACCES)
1001 fd = open(fnam, O_WRONLY);
1002
1003 /* if failed, fail */
1004 if (fd < 0)
1005 return -1;
1006
1007 /* if not a console, fail */
1008 if (!is_a_console(fd)) {
1009 close(fd);
1010 return -1;
1011 }
1012
1013 /* success */
1014 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001015}
1016
1017/*
1018 * Get an fd for use with kbd/console ioctls.
1019 * We try several things because opening /dev/console will fail
1020 * if someone else used X (which does a chown on /dev/console).
1021 *
1022 * if tty_name is non-NULL, try this one instead.
1023 */
1024
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001026{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001027 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001028
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 if (tty_name) {
1030 if (-1 == (fd = open_a_console(tty_name)))
1031 return -1;
1032 else
1033 return fd;
1034 }
1035
1036 fd = open_a_console("/dev/tty");
1037 if (fd >= 0)
1038 return fd;
1039
1040 fd = open_a_console("/dev/tty0");
1041 if (fd >= 0)
1042 return fd;
1043
1044 fd = open_a_console("/dev/console");
1045 if (fd >= 0)
1046 return fd;
1047
1048 for (fd = 0; fd < 3; fd++)
1049 if (is_a_console(fd))
1050 return fd;
1051
Matt Kraaidd19c692001-01-31 19:00:21 +00001052 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001053 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001054}
1055
1056
Eric Andersenf86aee72000-07-06 17:20:46 +00001057#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001058
1059
Eric Andersen9f16d612000-06-12 23:11:16 +00001060#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001061/*
1062 * Routine to see if a text string is matched by a wildcard pattern.
1063 * Returns TRUE if the text is matched, or FALSE if it is not matched
1064 * or if the pattern is invalid.
1065 * * matches zero or more characters
1066 * ? matches a single character
1067 * [abc] matches 'a', 'b' or 'c'
1068 * \c quotes character c
1069 * Adapted from code written by Ingo Wilken, and
1070 * then taken from sash, Copyright (c) 1999 by David I. Bell
1071 * Permission is granted to use, distribute, or modify this source,
1072 * provided that this copyright notice remains intact.
1073 * Permission to distribute this code under the GPL has been granted.
1074 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001075extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001076{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001077 const char *retryPat;
1078 const char *retryText;
1079 int ch;
1080 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001081 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001082
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 retryPat = NULL;
1084 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001085
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001087 ch = *pattern++;
1088
Erik Andersene49d5ec2000-02-08 19:58:47 +00001089 switch (ch) {
1090 case '*':
1091 retryPat = pattern;
1092 retryText = text;
1093 break;
1094
1095 case '[':
1096 found = FALSE;
1097
1098 while ((ch = *pattern++) != ']') {
1099 if (ch == '\\')
1100 ch = *pattern++;
1101
1102 if (ch == '\0')
1103 return FALSE;
1104
1105 if (*text == ch)
1106 found = TRUE;
1107 }
Erik Andersen632bb572000-04-18 22:09:06 +00001108 len=strlen(text);
1109 if (found == FALSE && len!=0) {
1110 return FALSE;
1111 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001112 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001113 if (strlen(pattern)==0 && len==1) {
1114 return TRUE;
1115 }
1116 if (len!=0) {
1117 text++;
1118 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001119 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001120 }
1121
1122 /* fall into next case */
1123
1124 case '?':
1125 if (*text++ == '\0')
1126 return FALSE;
1127
1128 break;
1129
1130 case '\\':
1131 ch = *pattern++;
1132
1133 if (ch == '\0')
1134 return FALSE;
1135
1136 /* fall into next case */
1137
1138 default:
1139 if (*text == ch) {
1140 if (*text)
1141 text++;
1142 break;
1143 }
1144
1145 if (*text) {
1146 pattern = retryPat;
1147 text = ++retryText;
1148 break;
1149 }
1150
Eric Andersenb186d981999-12-03 09:19:54 +00001151 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001152 }
1153
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 if (pattern == NULL)
1155 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001156 }
1157
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001159}
Eric Andersen9f16d612000-06-12 23:11:16 +00001160#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001161
1162
Eric Andersen29d2e361999-11-06 06:07:27 +00001163
1164
Erik Andersen23dea9b2000-05-13 00:28:25 +00001165#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001166#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001167/*
1168 * Given a block device, find the mount table entry if that block device
1169 * is mounted.
1170 *
1171 * Given any other file (or directory), find the mount table entry for its
1172 * filesystem.
1173 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001174extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001175{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001176 struct stat s;
1177 dev_t mountDevice;
1178 FILE *mountTable;
1179 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001180
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 if (stat(name, &s) != 0)
1182 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001183
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 if ((s.st_mode & S_IFMT) == S_IFBLK)
1185 mountDevice = s.st_rdev;
1186 else
1187 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001188
1189
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 if ((mountTable = setmntent(table, "r")) == 0)
1191 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001192
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 while ((mountEntry = getmntent(mountTable)) != 0) {
1194 if (strcmp(name, mountEntry->mnt_dir) == 0
1195 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1196 break;
1197 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1198 break;
1199 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1200 break;
1201 }
1202 endmntent(mountTable);
1203 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001204}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001205#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001206
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001207#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001208/* try to open up the specified device */
1209extern int device_open(char *device, int mode)
1210{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001211 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001212
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001214
Erik Andersene49d5ec2000-02-08 19:58:47 +00001215 /* Retry up to 5 times */
1216 for (f = 0; f < 5; f++)
1217 if ((fd = open(device, m, 0600)) >= 0)
1218 break;
1219 if (fd < 0)
1220 return fd;
1221 /* Reset original flags. */
1222 if (m != mode)
1223 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001224 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001225}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001226#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001227
1228
Erik Andersen23dea9b2000-05-13 00:28:25 +00001229#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001230#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001231#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001232#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001233
Erik Andersen23dea9b2000-05-13 00:28:25 +00001234#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001235/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001236 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001237 * This finds the pid of the specified process,
1238 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001239 *
Erik Andersen825aead2000-04-07 06:00:07 +00001240 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001241 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001242extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001243{
Erik Andersen825aead2000-04-07 06:00:07 +00001244 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001245 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001246 pid_t num_pids;
1247 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001248 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001249
1250 /* open device */
1251 fd = open(device, O_RDONLY);
1252 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001253 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001254
1255 /* Find out how many processes there are */
1256 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001257 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001258
1259 /* Allocate some memory -- grab a few extras just in case
1260 * some new processes start up while we wait. The kernel will
1261 * just ignore any extras if we give it too many, and will trunc.
1262 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001263 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001264 pid_array[0] = num_pids+10;
1265
1266 /* Now grab the pid list */
1267 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001268 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001269
1270 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001271 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001272 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001273 struct pid_info info;
1274
1275 info.pid = pid_array[i];
1276 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001277 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001278
Erik Andersen0a64de92000-04-18 20:00:03 +00001279 /* Make sure we only match on the process name */
1280 p=info.command_line+1;
1281 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1282 (p)++;
1283 }
1284 if (isspace(*(p)))
1285 *p='\0';
1286
Erik Andersenb9167cb2000-04-18 22:41:30 +00001287 if ((strstr(info.command_line, pidName) != NULL)
1288 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001289 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001290 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001291 }
1292 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001293 if (pidList)
1294 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001295
1296 /* Free memory */
1297 free( pid_array);
1298
1299 /* close device */
1300 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001301 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001302
Erik Andersen825aead2000-04-07 06:00:07 +00001303 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001304}
Erik Andersen269bba22000-03-08 14:50:47 +00001305#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001306
Mark Whitleyf57c9442000-12-07 19:56:48 +00001307/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001308 *
1309 * This finds the pid of the specified process.
1310 * Currently, it's implemented by rummaging through
1311 * the proc filesystem.
1312 *
Erik Andersen825aead2000-04-07 06:00:07 +00001313 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001314 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001315extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001316{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001317 DIR *dir;
1318 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001319 pid_t* pidList=NULL;
1320 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001321
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001322 dir = opendir("/proc");
1323 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001324 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001325
1326 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001327 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001328 char filename[256];
1329 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001330
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001331 /* If it isn't a number, we don't want it */
1332 if (!isdigit(*next->d_name))
1333 continue;
1334
Eric Andersen31e68292000-09-14 21:12:20 +00001335 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001336 status = fopen(filename, "r");
1337 if (!status) {
1338 continue;
1339 }
1340 fgets(buffer, 256, status);
1341 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001342
Eric Andersen31e68292000-09-14 21:12:20 +00001343 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001344 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001345 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001346 }
John Beppuf95ca971999-12-09 22:10:18 +00001347 }
Eric Andersen31e68292000-09-14 21:12:20 +00001348
Erik Andersen0a64de92000-04-18 20:00:03 +00001349 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001350 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001351 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001352}
Erik Andersen269bba22000-03-08 14:50:47 +00001353#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001354#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001355
Eric Andersenae6eae02000-07-19 17:35:54 +00001356#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001357/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001358extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001359{
Eric Andersenae6eae02000-07-19 17:35:54 +00001360 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001361
Eric Andersenae6eae02000-07-19 17:35:54 +00001362 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001363 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001364 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001365}
1366
Eric Andersen501c88b2000-07-28 15:14:45 +00001367extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001368{
1369 void *ptr = realloc(old, size);
1370 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001371 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001372 return ptr;
1373}
Eric Andersen501c88b2000-07-28 15:14:45 +00001374
1375extern void *xcalloc(size_t nmemb, size_t size)
1376{
1377 void *ptr = calloc(nmemb, size);
1378 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001379 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001380 return ptr;
1381}
Eric Andersenae6eae02000-07-19 17:35:54 +00001382#endif
1383
Eric Andersenca0f4582001-02-16 18:43:54 +00001384#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || \
1385 defined BB_WGET || defined BB_DPKG_DEB || defined BB_TAR || \
1386 defined BB_LN
Eric Andersenae6eae02000-07-19 17:35:54 +00001387# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001388extern char * xstrdup (const char *s) {
1389 char *t;
1390
1391 if (s == NULL)
1392 return NULL;
1393
1394 t = strdup (s);
1395
1396 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001397 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001398
1399 return t;
1400}
Eric Andersenae6eae02000-07-19 17:35:54 +00001401# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001402#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001403
Eric Andersenf5d5e772001-01-24 23:34:48 +00001404#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001405extern char * xstrndup (const char *s, int n) {
1406 char *t;
1407
1408 if (s == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001409 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001410
Eric Andersen12dceb12001-02-18 20:15:36 +00001411 t = xmalloc(++n);
1412
1413 return safe_strncpy(t,s,n);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001414}
1415#endif
1416
Eric Andersen12dceb12001-02-18 20:15:36 +00001417#if defined BB_IFCONFIG || defined BB_ROUTE || defined BB_NFSMOUNT || \
1418 defined BB_FEATURE_MOUNT_LOOP
Eric Andersenec455952001-02-14 08:11:27 +00001419/* Like strncpy but make sure the resulting string is always 0 terminated. */
1420extern char * safe_strncpy(char *dst, const char *src, size_t size)
1421{
1422 dst[size-1] = '\0';
1423 return strncpy(dst, src, size-1);
1424}
1425#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001426
Erik Andersen23dea9b2000-05-13 00:28:25 +00001427#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001428extern int vdprintf(int d, const char *format, va_list ap)
1429{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001430 char buf[BUF_SIZE];
1431 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001432
Erik Andersene49d5ec2000-02-08 19:58:47 +00001433 len = vsprintf(buf, format, ap);
1434 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001435}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001436#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001437
Eric Andersen624cc772000-09-21 02:04:51 +00001438
Erik Andersen23dea9b2000-05-13 00:28:25 +00001439#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001440#include <fcntl.h>
1441#include "loop.h" /* Pull in loop device support */
1442
Erik Andersen5cbdd712000-01-26 20:06:48 +00001443extern int del_loop(const char *device)
1444{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001445 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001446
Erik Andersene49d5ec2000-02-08 19:58:47 +00001447 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001448 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 return (FALSE);
1450 }
1451 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001452 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001453 return (FALSE);
1454 }
1455 close(fd);
1456 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001457}
Erik Andersende7965c2000-01-26 23:49:21 +00001458
Erik Andersene49d5ec2000-02-08 19:58:47 +00001459extern int set_loop(const char *device, const char *file, int offset,
1460 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001461{
1462 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001463 int fd, ffd, mode;
1464
Erik Andersende7965c2000-01-26 23:49:21 +00001465 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001466 if ((ffd = open(file, mode)) < 0 && !*loopro
1467 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001468 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001469 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001470 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001471 if ((fd = open(device, mode)) < 0) {
1472 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001473 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001474 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001475 }
1476 *loopro = (mode == O_RDONLY);
1477
1478 memset(&loopinfo, 0, sizeof(loopinfo));
Eric Andersen12dceb12001-02-18 20:15:36 +00001479 safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersende7965c2000-01-26 23:49:21 +00001480
1481 loopinfo.lo_offset = offset;
1482
1483 loopinfo.lo_encrypt_key_size = 0;
1484 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001485 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001486 close(fd);
1487 close(ffd);
1488 return 1;
1489 }
1490 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1491 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001492 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001493 close(fd);
1494 close(ffd);
1495 return 1;
1496 }
1497 close(fd);
1498 close(ffd);
1499 return 0;
1500}
1501
Erik Andersene49d5ec2000-02-08 19:58:47 +00001502extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001503{
1504 char dev[20];
1505 int i, fd;
1506 struct stat statbuf;
1507 struct loop_info loopinfo;
1508
Erik Andersene49d5ec2000-02-08 19:58:47 +00001509 for (i = 0; i <= 7; i++) {
1510 sprintf(dev, "/dev/loop%d", i);
1511 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1512 if ((fd = open(dev, O_RDONLY)) >= 0) {
Eric Andersen1a046d52001-03-06 19:35:58 +00001513 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001514 if (errno == ENXIO) { /* probably free */
1515 close(fd);
1516 return strdup(dev);
1517 }
1518 }
1519 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001520 }
Erik Andersende7965c2000-01-26 23:49:21 +00001521 }
Erik Andersende7965c2000-01-26 23:49:21 +00001522 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001523 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001524}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001525#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001526
Erik Andersen23dea9b2000-05-13 00:28:25 +00001527#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001528extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001529{
Erik Andersen016ffe92000-03-22 00:58:54 +00001530 DIR *dir;
1531 struct dirent *entry;
1532 struct stat statBuf, rootStat;
1533 char fileName[BUFSIZ];
1534
Erik Andersenec5bd902000-03-22 07:12:05 +00001535 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001536 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001537 return( FALSE);
1538 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001539
Erik Andersenec5bd902000-03-22 07:12:05 +00001540 dir = opendir("/dev");
1541 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001542 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001543 return( FALSE);
1544 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001545
1546 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001547
Erik Andersen016ffe92000-03-22 00:58:54 +00001548 /* Must skip ".." since that is "/", and so we
1549 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001550 if (strcmp(entry->d_name, "..") == 0)
1551 continue;
1552
Eric Andersenf9ca6532000-08-11 17:45:02 +00001553 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001554
1555 if (stat(fileName, &statBuf) != 0)
1556 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001557 /* Some char devices have the same dev_t as block
1558 * devices, so make sure this is a block device */
1559 if (! S_ISBLK(statBuf.st_mode))
1560 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001561 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001562 strcpy(name, fileName);
1563 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001564 }
1565 }
1566
Erik Andersenec5bd902000-03-22 07:12:05 +00001567 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001568}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001569#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001570
Mark Whitley1ca41772000-06-28 22:15:26 +00001571
1572/* get_line_from_file() - This function reads an entire line from a text file
1573 * up to a newline. It returns a malloc'ed char * which must be stored and
1574 * free'ed by the caller. */
1575extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001576{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001577 static const int GROWBY = 80; /* how large we will grow strings by */
1578
Mark Whitley1ca41772000-06-28 22:15:26 +00001579 int ch;
1580 int idx = 0;
1581 char *linebuf = NULL;
1582 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001583
Mark Whitley1ca41772000-06-28 22:15:26 +00001584 while (1) {
1585 ch = fgetc(file);
1586 if (ch == EOF)
1587 break;
1588 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001589 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001590 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001591 linebuf[idx++] = (char)ch;
1592 if ((char)ch == '\n')
1593 break;
1594 }
John Beppu5a728cf2000-04-17 04:22:09 +00001595
Mark Whitley1ca41772000-06-28 22:15:26 +00001596 if (idx == 0)
1597 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001598
Mark Whitley1ca41772000-06-28 22:15:26 +00001599 linebuf[idx] = 0;
1600 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001601}
Erik Andersen016ffe92000-03-22 00:58:54 +00001602
Eric Andersen944be902000-09-25 16:58:39 +00001603#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001604extern void print_file(FILE *file)
1605{
1606 int c;
1607
1608 while ((c = getc(file)) != EOF)
1609 putc(c, stdout);
1610 fclose(file);
1611 fflush(stdout);
1612}
1613
1614extern int print_file_by_name(char *filename)
1615{
1616 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001617 if ((file = wfopen(filename, "r")) == NULL)
1618 return FALSE;
1619 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001620 return TRUE;
1621}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001622#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001623
Matt Kraaicabafae2000-11-30 04:55:40 +00001624#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001625char process_escape_sequence(char **ptr)
1626{
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001627 static const char charmap[] = {
1628 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
1629 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001630
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001631 const char *p;
1632 char *q;
1633 int num_digits;
1634 unsigned int n;
1635
1636 n = 0;
1637 q = *ptr;
1638
1639 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) {
1640 if ((*q < '0') || (*q > '7')) { /* not a digit? */
1641 break;
1642 }
1643 n = n * 8 + (*q++ - '0');
1644 }
1645
1646 if (num_digits == 0) { /* mnemonic escape sequence? */
1647 for (p=charmap ; *p ; p++) {
1648 if (*p == *q) {
1649 q++;
1650 break;
1651 }
1652 }
1653 n = *(p+(sizeof(charmap)/2));
1654 }
1655
1656 /* doesn't hurt to fall through to here from mnemonic case */
1657 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
1658 n /= 8; /* adjust value and */
1659 --q; /* back up one char */
1660 }
1661
1662 *ptr = q;
1663 return (char) n;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001664}
1665#endif
1666
Eric Andersen70c60862001-01-25 00:00:04 +00001667#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001668 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001669char *get_last_path_component(char *path)
1670{
1671 char *s=path+strlen(path)-1;
1672
1673 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001674 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001675 *s-- = '\0';
1676 }
1677
1678 /* find last component */
1679 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001680 if (s == NULL || s[1] == '\0')
1681 return path;
1682 else
1683 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001684}
1685#endif
1686
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001687#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001688#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001689void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001690{
1691 int ret;
1692 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1693 int errmsgsz = regerror(ret, preg, NULL, 0);
1694 char *errmsg = xmalloc(errmsgsz);
1695 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001696 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001697 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001698}
1699#endif
1700
Eric Andersen7a86e612000-10-09 18:21:44 +00001701#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001702FILE *wfopen(const char *path, const char *mode)
1703{
1704 FILE *fp;
1705 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001706 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001707 errno = 0;
1708 }
1709 return fp;
1710}
1711#endif
1712
Eric Andersene790ddb2000-11-17 06:50:42 +00001713#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001714 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001715 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
Eric Andersen12dceb12001-02-18 20:15:36 +00001716 || defined BB_MOUNT || defined BB_ROUTE
Matt Kraaie0bcce02000-09-27 02:29:39 +00001717FILE *xfopen(const char *path, const char *mode)
1718{
1719 FILE *fp;
1720 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001721 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001722 return fp;
1723}
1724#endif
1725
Matt Kraaif2cc2762001-02-01 19:21:20 +00001726static int applet_name_compare(const void *x, const void *y)
Eric Andersene5aef922000-11-17 18:25:26 +00001727{
Matt Kraaif2cc2762001-02-01 19:21:20 +00001728 const char *name = x;
1729 const struct BB_applet *applet = y;
Eric Andersene5aef922000-11-17 18:25:26 +00001730
Matt Kraaif2cc2762001-02-01 19:21:20 +00001731 return strcmp(name, applet->name);
1732}
1733
1734extern size_t NUM_APPLETS;
1735
1736struct BB_applet *find_applet_by_name(const char *name)
1737{
1738 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1739 applet_name_compare);
Eric Andersene5aef922000-11-17 18:25:26 +00001740}
1741
Eric Andersen67991cf2001-02-14 21:23:06 +00001742void run_applet_by_name(const char *name, int argc, char **argv)
1743{
1744 /* Do a binary search to find the applet entry given the name. */
1745 if ((applet_using = find_applet_by_name(name)) != NULL) {
1746 applet_name = applet_using->name;
1747 if (argv[1] && strcmp(argv[1], "--help") == 0)
1748 show_usage();
1749 exit((*(applet_using->main)) (argc, argv));
1750 }
1751}
1752
Eric Andersenca0f4582001-02-16 18:43:54 +00001753#if defined BB_DD || defined BB_TAIL || defined BB_STTY
Matt Kraaia164c642001-02-05 17:50:03 +00001754unsigned long parse_number(const char *numstr,
1755 const struct suffix_mult *suffixes)
Matt Kraai24ac0172000-12-18 21:38:57 +00001756{
Matt Kraaia164c642001-02-05 17:50:03 +00001757 const struct suffix_mult *sm;
Matt Kraai24ac0172000-12-18 21:38:57 +00001758 unsigned long int ret;
1759 int len;
1760 char *end;
1761
1762 ret = strtoul(numstr, &end, 10);
1763 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001764 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001765 while (end[0] != '\0') {
Eric Andersen12dceb12001-02-18 20:15:36 +00001766 sm = suffixes;
1767 while ( sm != 0 ) {
1768 if(sm->suffix) {
1769 len = strlen(sm->suffix);
1770 if (strncmp(sm->suffix, end, len) == 0) {
1771 ret *= sm->mult;
1772 end += len;
1773 break;
1774 }
1775 sm++;
1776
1777 } else
1778 sm = 0;
Matt Kraai24ac0172000-12-18 21:38:57 +00001779 }
Eric Andersen12dceb12001-02-18 20:15:36 +00001780 if (sm == 0)
Matt Kraaidd19c692001-01-31 19:00:21 +00001781 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001782 }
1783 return ret;
1784}
1785#endif
1786
Eric Andersen23a93f52001-01-24 23:47:21 +00001787#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001788ssize_t safe_read(int fd, void *buf, size_t count)
1789{
1790 ssize_t n;
1791
1792 do {
1793 n = read(fd, buf, count);
1794 } while (n < 0 && errno == EINTR);
1795
1796 return n;
1797}
1798#endif
1799
Richard June6d0921c2001-01-22 22:35:38 +00001800#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001801const char *format(unsigned long val, unsigned long hr)
Richard June6d0921c2001-01-22 22:35:38 +00001802{
Eric Andersen7850f1e2001-03-07 03:50:57 +00001803 int i=0;
1804 static char str[10] = "\0";
1805 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
1806 unsigned long divisor = 1;
Richard June6d0921c2001-01-22 22:35:38 +00001807
Eric Andersen7850f1e2001-03-07 03:50:57 +00001808 if(val == 0)
1809 return("0");
1810 if(hr)
1811 snprintf(str, 9, "%ld", val/hr);
1812 else {
1813 while(val >= divisor && i <= 4) {
1814 divisor=divisor<<10, i++;
1815 }
1816 divisor=divisor>>10, i--;
1817 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
Eric Andersenfc83c4c2001-02-14 07:15:30 +00001818 }
Eric Andersen7850f1e2001-03-07 03:50:57 +00001819 return(str);
Richard June6d0921c2001-01-22 22:35:38 +00001820}
1821#endif
1822
Matt Kraai05e782d2001-02-01 16:49:30 +00001823#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS)
1824void chomp(char *s)
1825{
1826 size_t len = strlen(s);
1827
1828 if (len == 0)
1829 return;
1830
1831 if (s[len-1] == '\n')
1832 s[len-1] = '\0';
1833}
1834#endif
1835
Eric Andersencc8ed391999-10-05 16:24:54 +00001836/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001837/*
1838Local Variables:
1839c-file-style: "linux"
1840c-basic-offset: 4
1841tab-width: 4
1842End:
1843*/