blob: 9f0bca97112557a9c85ebc62c1eabfc7ddcf06af [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
3 * Utility routines.
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
Eric Andersen3570a342000-09-25 21:45:58 +000028#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000029#if defined (BB_CHMOD_CHOWN_CHGRP) \
30 || defined (BB_CP_MV) \
31 || defined (BB_FIND) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000032 || defined (BB_INSMOD) \
Erik Andersenfac10d72000-02-07 05:29:42 +000033 || defined (BB_LS) \
Pavel Roskin21ddb382000-07-17 19:31:54 +000034 || defined (BB_RM) \
35 || defined (BB_TAR)
Mark Whitleyf57c9442000-12-07 19:56:48 +000036/* same conditions as recursive_action */
Erik Andersenfac10d72000-02-07 05:29:42 +000037#define bb_need_name_too_long
38#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000039#define bb_need_memory_exhausted
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000040#define bb_need_full_version
Erik Andersenfac10d72000-02-07 05:29:42 +000041#define BB_DECLARE_EXTERN
42#include "messages.c"
43
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <stdio.h>
45#include <string.h>
46#include <errno.h>
47#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000048#include <dirent.h>
49#include <time.h>
50#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000051#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000052#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000053#include <stdlib.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000054#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000055#include <sys/utsname.h> /* for uname(2) */
Eric Andersened3ef502001-01-27 08:24:39 +000056
Eric Andersenab050f52001-01-27 06:01:43 +000057#include "pwd_grp/pwd.h"
58#include "pwd_grp/grp.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000059
Eric Andersened3ef502001-01-27 08:24:39 +000060/* for the _syscall() macros */
61#include <sys/syscall.h>
62#include <linux/unistd.h>
63
Erik Andersen246cc6d2000-03-07 07:41:42 +000064/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
65 * list of available filesystems used for the -t auto option */
Eric Andersend0246fb1999-11-04 21:18:07 +000066#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000067# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000068const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000069# else
Erik Andersen269bba22000-03-08 14:50:47 +000070# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000071const char mtab_file[] = "/dev/mtab";
72# else
Eric Andersenaf4ac772001-02-01 22:43:49 +000073const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000074# endif
75# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000076#endif
77
Eric Andersend73dc5b1999-11-10 23:13:02 +000078extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000079{
Eric Andersenc73a7f92001-01-20 21:41:11 +000080 fprintf(stderr, "%s\n\nUsage: %s\n", full_version, usage);
Matt Kraaie0bcce02000-09-27 02:29:39 +000081 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000082}
83
Mark Whitleyf57c9442000-12-07 19:56:48 +000084static void verror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +000085{
86 fflush(stdout);
87 fprintf(stderr, "%s: ", applet_name);
88 vfprintf(stderr, s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +000089}
90
Mark Whitleyf57c9442000-12-07 19:56:48 +000091extern void error_msg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000092{
93 va_list p;
94
95 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +000096 verror_msg(s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000097 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +000098 putc('\n', stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000099}
100
Mark Whitleyf57c9442000-12-07 19:56:48 +0000101extern void error_msg_and_die(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000102{
103 va_list p;
104
105 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000106 verror_msg(s, p);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000107 va_end(p);
Matt Kraaidd19c692001-01-31 19:00:21 +0000108 putc('\n', stderr);
Mark Whitleycf61fe12000-12-05 19:27:13 +0000109 exit(EXIT_FAILURE);
110}
111
Mark Whitleyf57c9442000-12-07 19:56:48 +0000112static void vperror_msg(const char *s, va_list p)
Mark Whitleycf61fe12000-12-05 19:27:13 +0000113{
Eric Andersenc73a7f92001-01-20 21:41:11 +0000114 int err=errno;
115 if(s == 0) s = "";
116 verror_msg(s, p);
117 if (*s) s = ": ";
118 fprintf(stderr, "%s%s\n", s, strerror(err));
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000119}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000120
Mark Whitleyf57c9442000-12-07 19:56:48 +0000121extern void perror_msg(const char *s, ...)
Matt Kraaief5529b2000-10-25 17:00:36 +0000122{
123 va_list p;
124
125 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000126 vperror_msg(s, p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000127 va_end(p);
Matt Kraaief5529b2000-10-25 17:00:36 +0000128}
129
Mark Whitleyf57c9442000-12-07 19:56:48 +0000130extern void perror_msg_and_die(const char *s, ...)
Matt Kraai324a7782000-10-25 15:10:08 +0000131{
132 va_list p;
133
134 va_start(p, s);
Mark Whitleyf57c9442000-12-07 19:56:48 +0000135 vperror_msg(s, p);
Matt Kraai324a7782000-10-25 15:10:08 +0000136 va_end(p);
Matt Kraai324a7782000-10-25 15:10:08 +0000137 exit(EXIT_FAILURE);
138}
139
Eric Andersenf5d5e772001-01-24 23:34:48 +0000140#if defined BB_INIT || defined BB_MKSWAP || defined BB_MOUNT || defined BB_NFSMOUNT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000141/* Returns kernel version encoded as major*65536 + minor*256 + patch,
142 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000143 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000144 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000145extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000146{
Erik Andersend07ee462000-02-21 21:26:32 +0000147 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000149
Erik Andersend07ee462000-02-21 21:26:32 +0000150 if (uname(&name) == -1) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000151 perror_msg("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 return (0);
153 }
Eric Andersenef936da2000-10-30 17:22:04 +0000154 major = atoi(strtok(name.release, "."));
155 minor = atoi(strtok(NULL, "."));
156 patch = atoi(strtok(NULL, "."));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000158}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000159#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000160
Eric Andersen10dc9d42000-06-26 10:45:52 +0000161
162
163#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000164_syscall1(int, sysinfo, struct sysinfo *, info);
165#endif /* BB_INIT */
166
Eric Andersena57ba4d2000-07-08 19:20:49 +0000167#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000168
169#ifndef __NR_umount2
Mark Whitley59ab0252001-01-23 22:30:04 +0000170static const int __NR_umount2 = 52;
Eric Andersena57ba4d2000-07-08 19:20:49 +0000171#endif
172
173/* Include our own version of <sys/mount.h>, since libc5 doesn't
174 * know about umount2 */
175extern _syscall1(int, umount, const char *, special_file);
176extern _syscall2(int, umount2, const char *, special_file, int, flags);
177extern _syscall5(int, mount, const char *, special_file, const char *, dir,
178 const char *, fstype, unsigned long int, rwflag, const void *, data);
179#endif
180
Eric Andersenc86e5e62000-08-21 19:38:49 +0000181#if defined BB_INSMOD || defined BB_LSMOD
182#ifndef __NR_query_module
Mark Whitley59ab0252001-01-23 22:30:04 +0000183static const int __NR_query_module = 167;
Eric Andersenc86e5e62000-08-21 19:38:49 +0000184#endif
185_syscall5(int, query_module, const char *, name, int, which,
186 void *, buf, size_t, bufsize, size_t*, ret);
187#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000188
189
Erik Andersen23dea9b2000-05-13 00:28:25 +0000190#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000191
Erik Andersen029011b2000-03-04 21:19:32 +0000192#define HASH_SIZE 311 /* Should be prime */
193#define hash_inode(i) ((i) % HASH_SIZE)
194
195static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
196
197/*
198 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
199 * `ino_dev_hashtable', else return 0
200 *
201 * If NAME is a non-NULL pointer to a character pointer, and there is
202 * a match, then set *NAME to the value of the name slot in that
203 * bucket.
204 */
205int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
206{
207 ino_dev_hashtable_bucket_t *bucket;
208
209 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
210 while (bucket != NULL) {
211 if ((bucket->ino == statbuf->st_ino) &&
212 (bucket->dev == statbuf->st_dev))
213 {
214 if (name) *name = bucket->name;
215 return 1;
216 }
217 bucket = bucket->next;
218 }
219 return 0;
220}
221
222/* Add statbuf to statbuf hash table */
223void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
224{
225 int i;
226 size_t s;
227 ino_dev_hashtable_bucket_t *bucket;
228
229 i = hash_inode(statbuf->st_ino);
230 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000231 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000232 bucket->ino = statbuf->st_ino;
233 bucket->dev = statbuf->st_dev;
234 if (name)
235 strcpy(bucket->name, name);
236 else
237 bucket->name[0] = '\0';
238 bucket->next = ino_dev_hashtable[i];
239 ino_dev_hashtable[i] = bucket;
240}
241
242/* Clear statbuf hash table */
243void reset_ino_dev_hashtable(void)
244{
245 int i;
246 ino_dev_hashtable_bucket_t *bucket;
247
248 for (i = 0; i < HASH_SIZE; i++) {
249 while (ino_dev_hashtable[i] != NULL) {
250 bucket = ino_dev_hashtable[i]->next;
251 free(ino_dev_hashtable[i]);
252 ino_dev_hashtable[i] = bucket;
253 }
254 }
255}
256
Erik Andersen23dea9b2000-05-13 00:28:25 +0000257#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000258
Glenn L McGrath5e23e3d2001-01-03 01:38:29 +0000259#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_DPKG_DEB)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000260/*
261 * Return TRUE if a fileName is a directory.
262 * Nonexistant files return FALSE.
263 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000264int is_directory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000265{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000267 int didMalloc = 0;
268
269 if (statBuf == NULL) {
270 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
271 ++didMalloc;
272 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000275 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000277 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000278
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000279 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
280 status = FALSE;
281 }
282 else status = TRUE;
283
284 if (didMalloc) {
285 free(statBuf);
286 statBuf = NULL;
287 }
288 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000289}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000290#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000291
Eric Andersen70c60862001-01-25 00:00:04 +0000292#if defined BB_AR || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000293/*
Mark Whitley47583682000-12-05 20:03:17 +0000294 * Copy chunksize bytes between two file descriptors
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000295 */
Mark Whitley47583682000-12-05 20:03:17 +0000296int copy_file_chunk(int srcfd, int dstfd, size_t chunksize)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000297{
298 size_t size;
Mark Whitley47583682000-12-05 20:03:17 +0000299 char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000300
Mark Whitley47583682000-12-05 20:03:17 +0000301 while (chunksize > 0) {
302 if (chunksize > BUFSIZ)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000303 size = BUFSIZ;
304 else
Mark Whitley47583682000-12-05 20:03:17 +0000305 size = chunksize;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000306 if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000307 return(FALSE);
Mark Whitley47583682000-12-05 20:03:17 +0000308 chunksize -= size;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000309 }
310 return (TRUE);
311}
312#endif
313
314
Glenn L McGrathde556612001-02-10 14:53:44 +0000315#if defined (BB_CP_MV) || defined BB_DPKG
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000316/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000317 * Copy one file to another, while possibly preserving its modes, times, and
318 * modes. Returns TRUE if successful, or FALSE on a failure with an error
319 * message output. (Failure is not indicated if attributes cannot be set.)
320 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000321 */
322int
Mark Whitleyf57c9442000-12-07 19:56:48 +0000323copy_file(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000324 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000325{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 int rfd;
327 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 struct stat srcStatBuf;
330 struct stat dstStatBuf;
331 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000332
Erik Andersene49d5ec2000-02-08 19:58:47 +0000333 if (followLinks == TRUE)
334 status = stat(srcName, &srcStatBuf);
335 else
336 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000337
Erik Andersenfac10d72000-02-07 05:29:42 +0000338 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000339 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000341 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342
343 if (followLinks == TRUE)
344 status = stat(destName, &dstStatBuf);
345 else
346 status = lstat(destName, &dstStatBuf);
347
Erik Andersen59b9e872000-05-10 05:05:45 +0000348 if (status < 0 || forceFlag==TRUE) {
349 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 dstStatBuf.st_ino = -1;
351 dstStatBuf.st_dev = -1;
352 }
353
354 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
355 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000356 error_msg("Copying file \"%s\" to itself", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 return FALSE;
358 }
359
360 if (S_ISDIR(srcStatBuf.st_mode)) {
361 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
362 /* Make sure the directory is writable */
363 status = mkdir(destName, 0777777 ^ umask(0));
364 if (status < 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000365 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 return FALSE;
367 }
368 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000369 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 int link_size;
371
372 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000373 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
374 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 if (link_size < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000376 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 return FALSE;
378 }
379 link_val[link_size] = '\0';
380 status = symlink(link_val, destName);
381 if (status < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000382 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 return FALSE;
384 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000385#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000387 /* Try to set owner, but fail silently like GNU cp */
388 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000390#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000391 return TRUE;
392 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
393 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
394 if (mkfifo(destName, 0644) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000395 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 return FALSE;
397 }
398 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
399 || S_ISSOCK(srcStatBuf.st_mode)) {
400 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
401 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000402 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 return FALSE;
404 }
405 } else if (S_ISREG(srcStatBuf.st_mode)) {
406 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
407 rfd = open(srcName, O_RDONLY);
408 if (rfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000409 perror_msg("%s", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 return FALSE;
411 }
412
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000413 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000414 srcStatBuf.st_mode);
415 if (wfd < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000416 perror_msg("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 close(rfd);
418 return FALSE;
419 }
420
Mark Whitley47583682000-12-05 20:03:17 +0000421 if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000422 goto error_exit;
423
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 close(rfd);
425 if (close(wfd) < 0) {
426 return FALSE;
427 }
428 }
429
430 if (setModes == TRUE) {
431 /* This is fine, since symlinks never get here */
Matt Kraaia9819b22000-12-22 01:48:07 +0000432 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
433 perror_msg_and_die("%s", destName);
434 if (chmod(destName, srcStatBuf.st_mode) < 0)
435 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 times.actime = srcStatBuf.st_atime;
437 times.modtime = srcStatBuf.st_mtime;
Matt Kraaia9819b22000-12-22 01:48:07 +0000438 if (utime(destName, &times) < 0)
439 perror_msg_and_die("%s", destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 }
441
Erik Andersenfac10d72000-02-07 05:29:42 +0000442 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000443
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 error_exit:
Matt Kraaia9819b22000-12-22 01:48:07 +0000445 perror_msg("%s", destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000446 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000448
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000450}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000451#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000452
453
454
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000455#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000456
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000457#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
458#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
459
460/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
461static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462 0, 0, S_ISUID,
463 0, 0, S_ISGID,
464 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000465};
466
467/* The 9 mode bits to test */
468static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 S_IRUSR, S_IWUSR, S_IXUSR,
470 S_IRGRP, S_IWGRP, S_IXGRP,
471 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000472};
473
Mark Whitley26a3adb2000-12-05 20:10:27 +0000474static const char MODE1[] = "rwxrwxrwx";
475static const char MODE0[] = "---------";
476static const char SMODE1[] = "..s..s..t";
477static const char SMODE0[] = "..S..S..T";
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000478
Eric Andersencc8ed391999-10-05 16:24:54 +0000479/*
480 * Return the standard ls-like mode string from a file mode.
481 * This is static and so is overwritten on each call.
482 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000483const char *mode_string(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000484{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000486
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 int i;
488
489 buf[0] = TYPECHAR(mode);
490 for (i = 0; i < 9; i++) {
491 if (mode & SBIT[i])
492 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
493 else
494 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
495 }
496 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000497}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000498#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000499
500
Pavel Roskin21ddb382000-07-17 19:31:54 +0000501#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000502/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000503 * Return the standard ls-like time string from a time_t
504 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000505 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000506const char *time_string(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000507{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 time_t now;
509 char *str;
510 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000511
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000513
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000515
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 strcpy(buf, &str[4]);
517 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
520 strcpy(&buf[7], &str[20]);
521 buf[11] = '\0';
522 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000523
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000525}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000526#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000527
Eric Andersen70c60862001-01-25 00:00:04 +0000528#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 +0000529/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000530 * Write all of the supplied buffer out to a file.
531 * This does multiple writes as necessary.
532 * Returns the amount written, or -1 on an error.
533 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000534int full_write(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000535{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 int cc;
537 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000538
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000540
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 while (len > 0) {
542 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000543
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 if (cc < 0)
545 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000546
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 buf += cc;
548 total += cc;
549 len -= cc;
550 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000553}
Matt Kraai24ac0172000-12-18 21:38:57 +0000554#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000555
Matt Kraai24ac0172000-12-18 21:38:57 +0000556#if defined BB_AR || defined BB_CP_MV || defined BB_SH || defined BB_TAR
Eric Andersencc8ed391999-10-05 16:24:54 +0000557/*
558 * Read all of the supplied buffer from a file.
559 * This does multiple reads as necessary.
560 * Returns the amount read, or -1 on an error.
561 * A short read is returned on an end of file.
562 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000563int full_read(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000564{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 int cc;
566 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000567
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000569
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570 while (len > 0) {
571 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000572
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 if (cc < 0)
574 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000575
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 if (cc == 0)
577 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000578
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 buf += cc;
580 total += cc;
581 len -= cc;
582 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000583
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000585}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000586#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000587
588
Erik Andersen23dea9b2000-05-13 00:28:25 +0000589#if defined (BB_CHMOD_CHOWN_CHGRP) \
590 || defined (BB_CP_MV) \
591 || defined (BB_FIND) \
592 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000593 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000594 || defined (BB_RM) \
595 || defined (BB_TAR)
596
Eric Andersencc8ed391999-10-05 16:24:54 +0000597/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000598 * Walk down all the directories under the specified
599 * location, and do something (something specified
600 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000601 *
Eric Andersenf3b2b522000-12-07 22:42:11 +0000602 * Unfortunately, while nftw(3) could replace this and reduce
Eric Andersen63a0e531999-10-22 05:12:14 +0000603 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000604 * and so isn't sufficiently portable to take over since glibc2.1
605 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000606 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000607int recursive_action(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 int recurse, int followLinks, int depthFirst,
609 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000610 struct stat * statbuf,
611 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000613 struct stat * statbuf,
614 void* userData),
615 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000616{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 int status;
618 struct stat statbuf;
619 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000620
Erik Andersene49d5ec2000-02-08 19:58:47 +0000621 if (followLinks == TRUE)
622 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000623 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000625
Eric Andersen2b69c401999-10-05 22:58:32 +0000626 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627#ifdef BB_DEBUG_PRINT_SCAFFOLD
628 fprintf(stderr,
629 "status=%d followLinks=%d TRUE=%d\n",
630 status, followLinks, TRUE);
631#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000632 perror_msg("%s", fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000633 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000634 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635
636 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
637 if (fileAction == NULL)
638 return TRUE;
639 else
Erik Andersen3364d782000-03-28 00:58:14 +0000640 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641 }
642
643 if (recurse == FALSE) {
644 if (S_ISDIR(statbuf.st_mode)) {
645 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000646 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 else
648 return TRUE;
649 }
650 }
651
652 if (S_ISDIR(statbuf.st_mode)) {
653 DIR *dir;
654
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000656 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000658 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 return FALSE;
Matt Kraaia1f97752000-12-19 06:24:08 +0000660 } else if (status == SKIP)
661 return TRUE;
662 }
663 dir = opendir(fileName);
664 if (!dir) {
665 perror_msg("%s", fileName);
666 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667 }
668 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000669 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670
671 if ((strcmp(next->d_name, "..") == 0)
672 || (strcmp(next->d_name, ".") == 0)) {
673 continue;
674 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000675 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000676 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 return FALSE;
678 }
Erik Andersenc3660502000-04-18 21:18:57 +0000679 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680 sprintf(nextFile, "%s/%s", fileName, next->d_name);
681 status =
Mark Whitleyf57c9442000-12-07 19:56:48 +0000682 recursive_action(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000683 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000684 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 closedir(dir);
686 return FALSE;
687 }
688 }
689 status = closedir(dir);
690 if (status < 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000691 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 return FALSE;
693 }
694 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000695 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000696 if (status == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000697 perror_msg("%s", fileName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000698 return FALSE;
699 }
700 }
701 } else {
702 if (fileAction == NULL)
703 return TRUE;
704 else
Erik Andersen3364d782000-03-28 00:58:14 +0000705 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 }
707 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000708}
709
Erik Andersen23dea9b2000-05-13 00:28:25 +0000710#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000711
Eric Andersenf6be9441999-10-13 21:12:06 +0000712
713
Glenn L McGrathe699b9f2000-12-15 06:50:54 +0000714#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000715/*
716 * Attempt to create the directories along the specified path, except for
717 * the final component. The mode is given for the final directory only,
718 * while all previous ones get default protections. Errors are not reported
719 * here, as failures to restore files can be reported later.
720 */
Mark Whitleyf57c9442000-12-07 19:56:48 +0000721extern int create_path(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000722{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 char *cp;
724 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000725 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000727
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 strcpy(buf, name);
729 for (cp = buf; *cp == '/'; cp++);
730 cp = strchr(cp, '/');
731 while (cp) {
732 cpOld = cp;
733 cp = strchr(cp + 1, '/');
734 *cpOld = '\0';
735 retVal = mkdir(buf, cp ? 0777 : mode);
736 if (retVal != 0 && errno != EEXIST) {
Matt Kraaia9819b22000-12-22 01:48:07 +0000737 perror_msg("%s", buf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 return FALSE;
739 }
740 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000741 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000743}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000744#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000745
746
747
Pavel Roskin21ddb382000-07-17 19:31:54 +0000748#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000749 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000750/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000751
752
753
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000755{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000756 mode_t andMode =
757
758 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000759 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760 mode_t mode = 0;
761 mode_t groups = 0;
762 char type;
763 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000764
Eric Andersen4e61db22000-09-01 00:06:17 +0000765 if (s==NULL)
766 return (FALSE);
767
Eric Andersenf6be9441999-10-13 21:12:06 +0000768 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 for (;;) {
770 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000771 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000772 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000773 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000775 continue;
776 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000778 continue;
779 case 'o':
780 groups |= S_IRWXO;
781 continue;
782 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000784 continue;
785 case '+':
786 case '=':
787 case '-':
788 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 if (groups == 0) /* The default is "all" */
790 groups |=
791 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000792 break;
793 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 if (isdigit(c) && c >= '0' && c <= '7' &&
795 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000796 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000797 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000799 return (FALSE);
800 }
801 break;
802 }
803
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 while ((c = *s++) != '\0') {
805 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000806 case ',':
807 break;
808 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000810 continue;
811 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000812 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000813 continue;
814 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000816 continue;
817 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000819 continue;
820 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000821 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000822 continue;
823 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000824 *theMode &= andMode;
825 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000827 }
828 break;
829 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000831 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000832 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000833 /* fall through */
834 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000835 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000836 break;
837 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000838 andMode &= ~(mode & groups);
839 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000840 break;
841 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000843 *theMode &= andMode;
844 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000845 return (TRUE);
846}
Eric Andersence8f3b91999-10-20 07:03:36 +0000847
848
Pavel Roskin21ddb382000-07-17 19:31:54 +0000849#endif
850/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000851
Eric Andersene674eb71999-10-19 20:52:57 +0000852
853
Erik Andersen23dea9b2000-05-13 00:28:25 +0000854
855
Pavel Roskin21ddb382000-07-17 19:31:54 +0000856#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
857 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
Eric Andersena2d61e72000-12-12 16:55:56 +0000858 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000859/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000860long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000861{
Eric Andersenab050f52001-01-27 06:01:43 +0000862 struct passwd *myuser;
863
864 myuser = getpwnam(name);
865 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000866 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000867
868 return myuser->pw_uid;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000869}
870
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000871/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000872long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000873{
Eric Andersenab050f52001-01-27 06:01:43 +0000874 struct group *mygroup;
875
876 mygroup = getgrnam(name);
877 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000878 return(-1);
Eric Andersenab050f52001-01-27 06:01:43 +0000879
880 return (mygroup->gr_gid);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000881}
882
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000883/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000884void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000885{
Eric Andersenab050f52001-01-27 06:01:43 +0000886 struct passwd *myuser;
887
888 myuser = getpwuid(uid);
889 if (myuser==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000890 sprintf(name, "%-8ld ", (long)uid);
891 else
892 strcpy(name, myuser->pw_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000893}
894
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000895/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000896void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000897{
Eric Andersenab050f52001-01-27 06:01:43 +0000898 struct group *mygroup;
899
900 mygroup = getgrgid(gid);
901 if (mygroup==NULL)
Eric Andersene57d54b2001-01-30 18:03:11 +0000902 sprintf(group, "%-8ld ", (long)gid);
903 else
904 strcpy(group, mygroup->gr_name);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000905}
906
Matt Kraaie6e81832000-12-30 07:46:23 +0000907#if defined BB_ID
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000908/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000909long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000910{
Eric Andersenab050f52001-01-27 06:01:43 +0000911 struct group *mygroup;
912 struct passwd *myuser;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000913
Eric Andersenab050f52001-01-27 06:01:43 +0000914 myuser=getpwnam(name);
915 if (myuser==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000916 error_msg_and_die( "unknown user name: %s", name);
Eric Andersenab050f52001-01-27 06:01:43 +0000917
918 mygroup = getgrgid(myuser->pw_gid);
919 if (mygroup==NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +0000920 error_msg_and_die( "unknown gid %ld", (long)myuser->pw_gid);
Eric Andersenab050f52001-01-27 06:01:43 +0000921
922 return mygroup->gr_gid;
923}
924#endif /* BB_ID */
Pavel Roskin21ddb382000-07-17 19:31:54 +0000925#endif
926 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
927 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000928
929
Eric Andersenf86aee72000-07-06 17:20:46 +0000930#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000931
Eric Andersenbd22ed82000-07-08 18:55:24 +0000932/* From <linux/kd.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +0000933static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
934static const int KB_84 = 0x01;
935static const int KB_101 = 0x02; /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000936
Erik Andersene49d5ec2000-02-08 19:58:47 +0000937int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000938{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939 char arg;
940
941 arg = 0;
942 return (ioctl(fd, KDGKBTYPE, &arg) == 0
943 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000944}
945
Erik Andersene49d5ec2000-02-08 19:58:47 +0000946static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000947{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948 int fd;
949
950 /* try read-only */
951 fd = open(fnam, O_RDWR);
952
953 /* if failed, try read-only */
954 if (fd < 0 && errno == EACCES)
955 fd = open(fnam, O_RDONLY);
956
957 /* if failed, try write-only */
958 if (fd < 0 && errno == EACCES)
959 fd = open(fnam, O_WRONLY);
960
961 /* if failed, fail */
962 if (fd < 0)
963 return -1;
964
965 /* if not a console, fail */
966 if (!is_a_console(fd)) {
967 close(fd);
968 return -1;
969 }
970
971 /* success */
972 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000973}
974
975/*
976 * Get an fd for use with kbd/console ioctls.
977 * We try several things because opening /dev/console will fail
978 * if someone else used X (which does a chown on /dev/console).
979 *
980 * if tty_name is non-NULL, try this one instead.
981 */
982
Erik Andersene49d5ec2000-02-08 19:58:47 +0000983int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +0000984{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000986
Erik Andersene49d5ec2000-02-08 19:58:47 +0000987 if (tty_name) {
988 if (-1 == (fd = open_a_console(tty_name)))
989 return -1;
990 else
991 return fd;
992 }
993
994 fd = open_a_console("/dev/tty");
995 if (fd >= 0)
996 return fd;
997
998 fd = open_a_console("/dev/tty0");
999 if (fd >= 0)
1000 return fd;
1001
1002 fd = open_a_console("/dev/console");
1003 if (fd >= 0)
1004 return fd;
1005
1006 for (fd = 0; fd < 3; fd++)
1007 if (is_a_console(fd))
1008 return fd;
1009
Matt Kraaidd19c692001-01-31 19:00:21 +00001010 error_msg("Couldnt get a file descriptor referring to the console");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001012}
1013
1014
Eric Andersenf86aee72000-07-06 17:20:46 +00001015#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001016
1017
Eric Andersen9f16d612000-06-12 23:11:16 +00001018#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001019/*
1020 * Routine to see if a text string is matched by a wildcard pattern.
1021 * Returns TRUE if the text is matched, or FALSE if it is not matched
1022 * or if the pattern is invalid.
1023 * * matches zero or more characters
1024 * ? matches a single character
1025 * [abc] matches 'a', 'b' or 'c'
1026 * \c quotes character c
1027 * Adapted from code written by Ingo Wilken, and
1028 * then taken from sash, Copyright (c) 1999 by David I. Bell
1029 * Permission is granted to use, distribute, or modify this source,
1030 * provided that this copyright notice remains intact.
1031 * Permission to distribute this code under the GPL has been granted.
1032 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001034{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 const char *retryPat;
1036 const char *retryText;
1037 int ch;
1038 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001039 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001040
Erik Andersene49d5ec2000-02-08 19:58:47 +00001041 retryPat = NULL;
1042 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001043
Erik Andersene49d5ec2000-02-08 19:58:47 +00001044 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001045 ch = *pattern++;
1046
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047 switch (ch) {
1048 case '*':
1049 retryPat = pattern;
1050 retryText = text;
1051 break;
1052
1053 case '[':
1054 found = FALSE;
1055
1056 while ((ch = *pattern++) != ']') {
1057 if (ch == '\\')
1058 ch = *pattern++;
1059
1060 if (ch == '\0')
1061 return FALSE;
1062
1063 if (*text == ch)
1064 found = TRUE;
1065 }
Erik Andersen632bb572000-04-18 22:09:06 +00001066 len=strlen(text);
1067 if (found == FALSE && len!=0) {
1068 return FALSE;
1069 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001070 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001071 if (strlen(pattern)==0 && len==1) {
1072 return TRUE;
1073 }
1074 if (len!=0) {
1075 text++;
1076 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001077 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 }
1079
1080 /* fall into next case */
1081
1082 case '?':
1083 if (*text++ == '\0')
1084 return FALSE;
1085
1086 break;
1087
1088 case '\\':
1089 ch = *pattern++;
1090
1091 if (ch == '\0')
1092 return FALSE;
1093
1094 /* fall into next case */
1095
1096 default:
1097 if (*text == ch) {
1098 if (*text)
1099 text++;
1100 break;
1101 }
1102
1103 if (*text) {
1104 pattern = retryPat;
1105 text = ++retryText;
1106 break;
1107 }
1108
Eric Andersenb186d981999-12-03 09:19:54 +00001109 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001110 }
1111
Erik Andersene49d5ec2000-02-08 19:58:47 +00001112 if (pattern == NULL)
1113 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001114 }
1115
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001117}
Eric Andersen9f16d612000-06-12 23:11:16 +00001118#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001119
1120
Eric Andersen29d2e361999-11-06 06:07:27 +00001121
1122
Erik Andersen23dea9b2000-05-13 00:28:25 +00001123#if defined BB_DF || defined BB_MTAB
Eric Andersened3ef502001-01-27 08:24:39 +00001124#include <mntent.h>
Eric Andersen29d2e361999-11-06 06:07:27 +00001125/*
1126 * Given a block device, find the mount table entry if that block device
1127 * is mounted.
1128 *
1129 * Given any other file (or directory), find the mount table entry for its
1130 * filesystem.
1131 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001132extern struct mntent *find_mount_point(const char *name, const char *table)
Eric Andersen29d2e361999-11-06 06:07:27 +00001133{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 struct stat s;
1135 dev_t mountDevice;
1136 FILE *mountTable;
1137 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001138
Erik Andersene49d5ec2000-02-08 19:58:47 +00001139 if (stat(name, &s) != 0)
1140 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001141
Erik Andersene49d5ec2000-02-08 19:58:47 +00001142 if ((s.st_mode & S_IFMT) == S_IFBLK)
1143 mountDevice = s.st_rdev;
1144 else
1145 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001146
1147
Erik Andersene49d5ec2000-02-08 19:58:47 +00001148 if ((mountTable = setmntent(table, "r")) == 0)
1149 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001150
Erik Andersene49d5ec2000-02-08 19:58:47 +00001151 while ((mountEntry = getmntent(mountTable)) != 0) {
1152 if (strcmp(name, mountEntry->mnt_dir) == 0
1153 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1154 break;
1155 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1156 break;
1157 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1158 break;
1159 }
1160 endmntent(mountTable);
1161 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001162}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001163#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001164
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001165#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001166/* try to open up the specified device */
1167extern int device_open(char *device, int mode)
1168{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001170
Erik Andersene49d5ec2000-02-08 19:58:47 +00001171 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001172
Erik Andersene49d5ec2000-02-08 19:58:47 +00001173 /* Retry up to 5 times */
1174 for (f = 0; f < 5; f++)
1175 if ((fd = open(device, m, 0600)) >= 0)
1176 break;
1177 if (fd < 0)
1178 return fd;
1179 /* Reset original flags. */
1180 if (m != mode)
1181 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001182 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001183}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001184#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001185
1186
Erik Andersen23dea9b2000-05-13 00:28:25 +00001187#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001188#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001189#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001190#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001191
Erik Andersen23dea9b2000-05-13 00:28:25 +00001192#if defined BB_FEATURE_USE_DEVPS_PATCH
Mark Whitleyf57c9442000-12-07 19:56:48 +00001193/* find_pid_by_name()
John Beppuf95ca971999-12-09 22:10:18 +00001194 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001195 * This finds the pid of the specified process,
1196 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001197 *
Erik Andersen825aead2000-04-07 06:00:07 +00001198 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001199 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001200extern pid_t* find_pid_by_name( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001201{
Erik Andersen825aead2000-04-07 06:00:07 +00001202 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001203 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001204 pid_t num_pids;
1205 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001206 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001207
1208 /* open device */
1209 fd = open(device, O_RDONLY);
1210 if (fd < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001211 perror_msg_and_die("open failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001212
1213 /* Find out how many processes there are */
1214 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001215 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001216
1217 /* Allocate some memory -- grab a few extras just in case
1218 * some new processes start up while we wait. The kernel will
1219 * just ignore any extras if we give it too many, and will trunc.
1220 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001221 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001222 pid_array[0] = num_pids+10;
1223
1224 /* Now grab the pid list */
1225 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001226 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001227
1228 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001229 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001230 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001231 struct pid_info info;
1232
1233 info.pid = pid_array[i];
1234 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001235 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
Erik Andersen246cc6d2000-03-07 07:41:42 +00001236
Erik Andersen0a64de92000-04-18 20:00:03 +00001237 /* Make sure we only match on the process name */
1238 p=info.command_line+1;
1239 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1240 (p)++;
1241 }
1242 if (isspace(*(p)))
1243 *p='\0';
1244
Erik Andersenb9167cb2000-04-18 22:41:30 +00001245 if ((strstr(info.command_line, pidName) != NULL)
1246 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001247 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001248 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001249 }
1250 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001251 if (pidList)
1252 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001253
1254 /* Free memory */
1255 free( pid_array);
1256
1257 /* close device */
1258 if (close (fd) != 0)
Matt Kraai24ac0172000-12-18 21:38:57 +00001259 perror_msg_and_die("close failed for `%s'", device);
Erik Andersen246cc6d2000-03-07 07:41:42 +00001260
Erik Andersen825aead2000-04-07 06:00:07 +00001261 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001262}
Erik Andersen269bba22000-03-08 14:50:47 +00001263#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001264
Mark Whitleyf57c9442000-12-07 19:56:48 +00001265/* find_pid_by_name()
Erik Andersen246cc6d2000-03-07 07:41:42 +00001266 *
1267 * This finds the pid of the specified process.
1268 * Currently, it's implemented by rummaging through
1269 * the proc filesystem.
1270 *
Erik Andersen825aead2000-04-07 06:00:07 +00001271 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001272 */
Mark Whitleyf57c9442000-12-07 19:56:48 +00001273extern pid_t* find_pid_by_name( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001274{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001275 DIR *dir;
1276 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001277 pid_t* pidList=NULL;
1278 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001279
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001280 dir = opendir("/proc");
1281 if (!dir)
Matt Kraai24ac0172000-12-18 21:38:57 +00001282 perror_msg_and_die("Cannot open /proc");
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001283
1284 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001285 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001286 char filename[256];
1287 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001288
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001289 /* If it isn't a number, we don't want it */
1290 if (!isdigit(*next->d_name))
1291 continue;
1292
Eric Andersen31e68292000-09-14 21:12:20 +00001293 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001294 status = fopen(filename, "r");
1295 if (!status) {
1296 continue;
1297 }
1298 fgets(buffer, 256, status);
1299 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001300
Eric Andersen31e68292000-09-14 21:12:20 +00001301 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001302 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001303 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001304 }
John Beppuf95ca971999-12-09 22:10:18 +00001305 }
Eric Andersen31e68292000-09-14 21:12:20 +00001306
Erik Andersen0a64de92000-04-18 20:00:03 +00001307 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001308 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001309 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001310}
Erik Andersen269bba22000-03-08 14:50:47 +00001311#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001312#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001313
Eric Andersenae6eae02000-07-19 17:35:54 +00001314#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001315/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001316extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001317{
Eric Andersenae6eae02000-07-19 17:35:54 +00001318 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001319
Eric Andersenae6eae02000-07-19 17:35:54 +00001320 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001321 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001322 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001323}
1324
Eric Andersen501c88b2000-07-28 15:14:45 +00001325extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001326{
1327 void *ptr = realloc(old, size);
1328 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001329 error_msg_and_die(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001330 return ptr;
1331}
Eric Andersen501c88b2000-07-28 15:14:45 +00001332
1333extern void *xcalloc(size_t nmemb, size_t size)
1334{
1335 void *ptr = calloc(nmemb, size);
1336 if (!ptr)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001337 error_msg_and_die(memory_exhausted);
Eric Andersen501c88b2000-07-28 15:14:45 +00001338 return ptr;
1339}
Eric Andersenae6eae02000-07-19 17:35:54 +00001340#endif
1341
Eric Andersen15f29e72001-01-25 00:53:46 +00001342#if defined BB_NFSMOUNT || defined BB_LS || defined BB_SH || defined BB_WGET || \
1343 defined BB_DPKG_DEB || defined BB_TAR
Eric Andersenae6eae02000-07-19 17:35:54 +00001344# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001345extern char * xstrdup (const char *s) {
1346 char *t;
1347
1348 if (s == NULL)
1349 return NULL;
1350
1351 t = strdup (s);
1352
1353 if (t == NULL)
Mark Whitleyf57c9442000-12-07 19:56:48 +00001354 error_msg_and_die(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001355
1356 return t;
1357}
Eric Andersenae6eae02000-07-19 17:35:54 +00001358# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001359#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001360
Eric Andersenf5d5e772001-01-24 23:34:48 +00001361#if defined BB_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001362extern char * xstrndup (const char *s, int n) {
1363 char *t;
1364
1365 if (s == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001366 error_msg_and_die("xstrndup bug");
Eric Andersenda1d1e72000-07-10 23:39:44 +00001367
1368 t = xmalloc(n+1);
1369 strncpy(t,s,n);
1370 t[n] = 0;
1371
1372 return t;
1373}
1374#endif
1375
1376
Erik Andersen23dea9b2000-05-13 00:28:25 +00001377#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001378extern int vdprintf(int d, const char *format, va_list ap)
1379{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001380 char buf[BUF_SIZE];
1381 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001382
Erik Andersene49d5ec2000-02-08 19:58:47 +00001383 len = vsprintf(buf, format, ap);
1384 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001385}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001386#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001387
Eric Andersen624cc772000-09-21 02:04:51 +00001388
Erik Andersen23dea9b2000-05-13 00:28:25 +00001389#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001390#include <fcntl.h>
1391#include "loop.h" /* Pull in loop device support */
1392
Erik Andersen5cbdd712000-01-26 20:06:48 +00001393extern int del_loop(const char *device)
1394{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001395 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001396
Erik Andersene49d5ec2000-02-08 19:58:47 +00001397 if ((fd = open(device, O_RDONLY)) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001398 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001399 return (FALSE);
1400 }
1401 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001402 perror_msg("ioctl: LOOP_CLR_FD");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001403 return (FALSE);
1404 }
1405 close(fd);
1406 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001407}
Erik Andersende7965c2000-01-26 23:49:21 +00001408
Erik Andersene49d5ec2000-02-08 19:58:47 +00001409extern int set_loop(const char *device, const char *file, int offset,
1410 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001411{
1412 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001413 int fd, ffd, mode;
1414
Erik Andersende7965c2000-01-26 23:49:21 +00001415 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001416 if ((ffd = open(file, mode)) < 0 && !*loopro
1417 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001418 perror_msg("%s", file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001419 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001420 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001421 if ((fd = open(device, mode)) < 0) {
1422 close(ffd);
Matt Kraaia9819b22000-12-22 01:48:07 +00001423 perror_msg("%s", device);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001424 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001425 }
1426 *loopro = (mode == O_RDONLY);
1427
1428 memset(&loopinfo, 0, sizeof(loopinfo));
1429 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001430 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001431
1432 loopinfo.lo_offset = offset;
1433
1434 loopinfo.lo_encrypt_key_size = 0;
1435 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +00001436 perror_msg("ioctl: LOOP_SET_FD");
Erik Andersende7965c2000-01-26 23:49:21 +00001437 close(fd);
1438 close(ffd);
1439 return 1;
1440 }
1441 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1442 (void) ioctl(fd, LOOP_CLR_FD, 0);
Matt Kraaia9819b22000-12-22 01:48:07 +00001443 perror_msg("ioctl: LOOP_SET_STATUS");
Erik Andersende7965c2000-01-26 23:49:21 +00001444 close(fd);
1445 close(ffd);
1446 return 1;
1447 }
1448 close(fd);
1449 close(ffd);
1450 return 0;
1451}
1452
Erik Andersene49d5ec2000-02-08 19:58:47 +00001453extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001454{
1455 char dev[20];
1456 int i, fd;
1457 struct stat statbuf;
1458 struct loop_info loopinfo;
1459
Erik Andersene49d5ec2000-02-08 19:58:47 +00001460 for (i = 0; i <= 7; i++) {
1461 sprintf(dev, "/dev/loop%d", i);
1462 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1463 if ((fd = open(dev, O_RDONLY)) >= 0) {
1464 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1465 if (errno == ENXIO) { /* probably free */
1466 close(fd);
1467 return strdup(dev);
1468 }
1469 }
1470 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001471 }
Erik Andersende7965c2000-01-26 23:49:21 +00001472 }
Erik Andersende7965c2000-01-26 23:49:21 +00001473 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001474 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001475}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001476#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001477
Erik Andersen23dea9b2000-05-13 00:28:25 +00001478#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001479extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001480{
Erik Andersen016ffe92000-03-22 00:58:54 +00001481 DIR *dir;
1482 struct dirent *entry;
1483 struct stat statBuf, rootStat;
1484 char fileName[BUFSIZ];
1485
Erik Andersenec5bd902000-03-22 07:12:05 +00001486 if (stat("/", &rootStat) != 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001487 error_msg("could not stat '/'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001488 return( FALSE);
1489 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001490
Erik Andersenec5bd902000-03-22 07:12:05 +00001491 dir = opendir("/dev");
1492 if (!dir) {
Matt Kraaidd19c692001-01-31 19:00:21 +00001493 error_msg("could not open '/dev'");
Erik Andersenec5bd902000-03-22 07:12:05 +00001494 return( FALSE);
1495 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001496
1497 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001498
Erik Andersen016ffe92000-03-22 00:58:54 +00001499 /* Must skip ".." since that is "/", and so we
1500 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001501 if (strcmp(entry->d_name, "..") == 0)
1502 continue;
1503
Eric Andersenf9ca6532000-08-11 17:45:02 +00001504 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001505
1506 if (stat(fileName, &statBuf) != 0)
1507 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001508 /* Some char devices have the same dev_t as block
1509 * devices, so make sure this is a block device */
1510 if (! S_ISBLK(statBuf.st_mode))
1511 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001512 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001513 strcpy(name, fileName);
1514 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001515 }
1516 }
1517
Erik Andersenec5bd902000-03-22 07:12:05 +00001518 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001519}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001520#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001521
Mark Whitley1ca41772000-06-28 22:15:26 +00001522
1523/* get_line_from_file() - This function reads an entire line from a text file
1524 * up to a newline. It returns a malloc'ed char * which must be stored and
1525 * free'ed by the caller. */
1526extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001527{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001528 static const int GROWBY = 80; /* how large we will grow strings by */
1529
Mark Whitley1ca41772000-06-28 22:15:26 +00001530 int ch;
1531 int idx = 0;
1532 char *linebuf = NULL;
1533 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001534
Mark Whitley1ca41772000-06-28 22:15:26 +00001535 while (1) {
1536 ch = fgetc(file);
1537 if (ch == EOF)
1538 break;
1539 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001540 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001541 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001542 linebuf[idx++] = (char)ch;
1543 if ((char)ch == '\n')
1544 break;
1545 }
John Beppu5a728cf2000-04-17 04:22:09 +00001546
Mark Whitley1ca41772000-06-28 22:15:26 +00001547 if (idx == 0)
1548 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001549
Mark Whitley1ca41772000-06-28 22:15:26 +00001550 linebuf[idx] = 0;
1551 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001552}
Erik Andersen016ffe92000-03-22 00:58:54 +00001553
Eric Andersen944be902000-09-25 16:58:39 +00001554#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001555extern void print_file(FILE *file)
1556{
1557 int c;
1558
1559 while ((c = getc(file)) != EOF)
1560 putc(c, stdout);
1561 fclose(file);
1562 fflush(stdout);
1563}
1564
1565extern int print_file_by_name(char *filename)
1566{
1567 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001568 if ((file = wfopen(filename, "r")) == NULL)
1569 return FALSE;
1570 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001571 return TRUE;
1572}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001573#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001574
Matt Kraaicabafae2000-11-30 04:55:40 +00001575#if defined BB_ECHO || defined BB_SH || defined BB_TR
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001576char process_escape_sequence(char **ptr)
1577{
1578 char c;
1579
1580 switch (c = *(*ptr)++) {
1581 case 'a':
1582 c = '\a';
1583 break;
1584 case 'b':
1585 c = '\b';
1586 break;
1587 case 'f':
1588 c = '\f';
1589 break;
1590 case 'n':
1591 c = '\n';
1592 break;
Eric Andersen9bd71e82000-11-29 23:07:57 +00001593 case 'r':
1594 c = '\r';
1595 break;
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001596 case 't':
1597 c = '\t';
1598 break;
1599 case 'v':
1600 c = '\v';
1601 break;
1602 case '\\':
1603 c = '\\';
1604 break;
1605 case '0': case '1': case '2': case '3':
1606 case '4': case '5': case '6': case '7':
1607 c -= '0';
1608 if ('0' <= **ptr && **ptr <= '7') {
1609 c = c * 8 + (*(*ptr)++ - '0');
1610 if ('0' <= **ptr && **ptr <= '7')
1611 c = c * 8 + (*(*ptr)++ - '0');
1612 }
1613 break;
1614 default:
1615 (*ptr)--;
1616 c = '\\';
1617 break;
1618 }
1619 return c;
1620}
1621#endif
1622
Eric Andersen70c60862001-01-25 00:00:04 +00001623#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001624 ! defined BB_FEATURE_USE_DEVPS_PATCH || defined BB_WGET
Eric Andersen0b4551f2000-07-10 16:44:03 +00001625char *get_last_path_component(char *path)
1626{
1627 char *s=path+strlen(path)-1;
1628
1629 /* strip trailing slashes */
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001630 while (s != path && *s == '/') {
Eric Andersen0b4551f2000-07-10 16:44:03 +00001631 *s-- = '\0';
1632 }
1633
1634 /* find last component */
1635 s = strrchr(path, '/');
Eric Andersen69a6b2d2000-12-12 23:13:54 +00001636 if (s == NULL || s[1] == '\0')
1637 return path;
1638 else
1639 return s+1;
Eric Andersen0b4551f2000-07-10 16:44:03 +00001640}
1641#endif
1642
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001643#if defined BB_GREP || defined BB_SED
Eric Andersened3ef502001-01-27 08:24:39 +00001644#include <regex.h>
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001645void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001646{
1647 int ret;
1648 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1649 int errmsgsz = regerror(ret, preg, NULL, 0);
1650 char *errmsg = xmalloc(errmsgsz);
1651 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaidd19c692001-01-31 19:00:21 +00001652 error_msg_and_die("xregcomp: %s", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001653 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001654}
1655#endif
1656
Eric Andersen7a86e612000-10-09 18:21:44 +00001657#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001658FILE *wfopen(const char *path, const char *mode)
1659{
1660 FILE *fp;
1661 if ((fp = fopen(path, mode)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001662 perror_msg("%s", path);
Matt Kraaic0321f92000-09-27 04:09:22 +00001663 errno = 0;
1664 }
1665 return fp;
1666}
1667#endif
1668
Eric Andersene790ddb2000-11-17 06:50:42 +00001669#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \
Matt Kraai3b3f5c32001-01-22 20:49:00 +00001670 || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \
Eric Andersenaf4ac772001-02-01 22:43:49 +00001671 || defined BB_WC || defined BB_CMP || defined BB_SORT || defined BB_WGET \
1672 || defined BB_MOUNT
Matt Kraaie0bcce02000-09-27 02:29:39 +00001673FILE *xfopen(const char *path, const char *mode)
1674{
1675 FILE *fp;
1676 if ((fp = fopen(path, mode)) == NULL)
Matt Kraai1fa1ade2000-12-18 03:57:16 +00001677 perror_msg_and_die("%s", path);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001678 return fp;
1679}
1680#endif
1681
Matt Kraaif2cc2762001-02-01 19:21:20 +00001682static int applet_name_compare(const void *x, const void *y)
Eric Andersene5aef922000-11-17 18:25:26 +00001683{
Matt Kraaif2cc2762001-02-01 19:21:20 +00001684 const char *name = x;
1685 const struct BB_applet *applet = y;
Eric Andersene5aef922000-11-17 18:25:26 +00001686
Matt Kraaif2cc2762001-02-01 19:21:20 +00001687 return strcmp(name, applet->name);
1688}
1689
1690extern size_t NUM_APPLETS;
1691
1692struct BB_applet *find_applet_by_name(const char *name)
1693{
1694 return bsearch(name, applets, NUM_APPLETS, sizeof(struct BB_applet),
1695 applet_name_compare);
Eric Andersene5aef922000-11-17 18:25:26 +00001696}
1697
Matt Kraai24ac0172000-12-18 21:38:57 +00001698#if defined BB_DD || defined BB_TAIL
Matt Kraaia164c642001-02-05 17:50:03 +00001699unsigned long parse_number(const char *numstr,
1700 const struct suffix_mult *suffixes)
Matt Kraai24ac0172000-12-18 21:38:57 +00001701{
Matt Kraaia164c642001-02-05 17:50:03 +00001702 const struct suffix_mult *sm;
Matt Kraai24ac0172000-12-18 21:38:57 +00001703 unsigned long int ret;
1704 int len;
1705 char *end;
1706
1707 ret = strtoul(numstr, &end, 10);
1708 if (numstr == end)
Matt Kraaidd19c692001-01-31 19:00:21 +00001709 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001710 while (end[0] != '\0') {
1711 for (sm = suffixes; sm->suffix != NULL; sm++) {
1712 len = strlen(sm->suffix);
1713 if (strncmp(sm->suffix, end, len) == 0) {
1714 ret *= sm->mult;
1715 end += len;
1716 break;
1717 }
1718 }
1719 if (sm->suffix == NULL)
Matt Kraaidd19c692001-01-31 19:00:21 +00001720 error_msg_and_die("invalid number `%s'", numstr);
Matt Kraai24ac0172000-12-18 21:38:57 +00001721 }
1722 return ret;
1723}
1724#endif
1725
Eric Andersen23a93f52001-01-24 23:47:21 +00001726#if defined BB_DD || defined BB_NC || defined BB_TAIL
Matt Kraaibfa79672000-12-15 22:34:34 +00001727ssize_t safe_read(int fd, void *buf, size_t count)
1728{
1729 ssize_t n;
1730
1731 do {
1732 n = read(fd, buf, count);
1733 } while (n < 0 && errno == EINTR);
1734
1735 return n;
1736}
1737#endif
1738
Richard June6d0921c2001-01-22 22:35:38 +00001739#ifdef BB_FEATURE_HUMAN_READABLE
1740char *format(unsigned long val, unsigned long hr)
1741{
1742 static char str[10] = "\0";
1743
1744 if(val == 0)
1745 return("0");
1746 if(hr)
1747 snprintf(str, 9, "%ld", val/hr);
1748 else if(val >= GIGABYTE)
1749 snprintf(str, 9, "%.1LfG", ((long double)(val)/GIGABYTE));
1750 else if(val >= MEGABYTE)
1751 snprintf(str, 9, "%.1LfM", ((long double)(val)/MEGABYTE));
1752 else if(val >= KILOBYTE)
1753 snprintf(str, 9, "%.1Lfk", ((long double)(val)/KILOBYTE));
1754 else
1755 snprintf(str, 9, "%ld", (val));
1756 return(str);
1757}
1758#endif
1759
Matt Kraai05e782d2001-02-01 16:49:30 +00001760#if defined(BB_GREP) || defined(BB_HOSTNAME) || defined(BB_SED) || defined(BB_TAR) || defined(BB_WGET) || defined(BB_XARGS)
1761void chomp(char *s)
1762{
1763 size_t len = strlen(s);
1764
1765 if (len == 0)
1766 return;
1767
1768 if (s[len-1] == '\n')
1769 s[len-1] = '\0';
1770}
1771#endif
1772
Eric Andersencc8ed391999-10-05 16:24:54 +00001773/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001774/*
1775Local Variables:
1776c-file-style: "linux"
1777c-basic-offset: 4
1778tab-width: 4
1779End:
1780*/