blob: d61ef30e11308c817a5dfb54d4968d26cc602660 [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)
Erik Andersenfac10d72000-02-07 05:29:42 +000036/* same conditions as recursiveAction */
37#define bb_need_name_too_long
38#endif
Eric Andersen9f16d612000-06-12 23:11:16 +000039#define bb_need_memory_exhausted
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000040#define bb_need_full_version
Erik Andersenfac10d72000-02-07 05:29:42 +000041#define BB_DECLARE_EXTERN
42#include "messages.c"
43
Eric Andersen2b69c401999-10-05 22:58:32 +000044#include <stdio.h>
45#include <string.h>
46#include <errno.h>
47#include <fcntl.h>
Eric Andersen2b69c401999-10-05 22:58:32 +000048#include <dirent.h>
49#include <time.h>
50#include <utime.h>
Eric Andersenf811e071999-10-09 00:25:00 +000051#include <unistd.h>
Eric Andersence8f3b91999-10-20 07:03:36 +000052#include <ctype.h>
Eric Andersenbd22ed82000-07-08 18:55:24 +000053#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000054#include <sys/utsname.h> /* for uname(2) */
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersen246cc6d2000-03-07 07:41:42 +000056/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
57 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000058#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000059//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
60#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
61#endif
62
Erik Andersen5cbdd712000-01-26 20:06:48 +000063
Eric Andersend0246fb1999-11-04 21:18:07 +000064#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000065# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000066const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000067# else
68# if defined BB_FEATURE_USE_PROCFS
69const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000070# else
Erik Andersen269bba22000-03-08 14:50:47 +000071# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000072const char mtab_file[] = "/dev/mtab";
73# else
Erik Andersen269bba22000-03-08 14:50:47 +000074# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or ( BB_FEATURE_USE_PROCFS | BB_FEATURE_USE_DEVPS_PATCH)
Eric Andersen0ecb54a1999-12-05 23:24:55 +000075# endif
76# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000077# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000078#endif
79
Eric Andersend73dc5b1999-11-10 23:13:02 +000080extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000081{
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000082 fprintf(stderr, "%s\n\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 fprintf(stderr, "Usage: %s\n", usage);
Matt Kraaie0bcce02000-09-27 02:29:39 +000084 exit(EXIT_FAILURE);
Eric Andersenb0e9a701999-10-18 22:28:26 +000085}
86
Erik Andersen1ad302a2000-03-24 00:54:46 +000087extern void errorMsg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000088{
89 va_list p;
90
91 va_start(p, s);
92 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +000093 fprintf(stderr, "%s: ", applet_name);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000094 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000095 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +000096 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000097}
98
Erik Andersen1ad302a2000-03-24 00:54:46 +000099extern void fatalError(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000100{
101 va_list p;
102
103 va_start(p, s);
104 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +0000105 fprintf(stderr, "%s: ", applet_name);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000106 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000107 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000108 fflush(stderr);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000109 exit(EXIT_FAILURE);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000110}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000111
Matt Kraaief5529b2000-10-25 17:00:36 +0000112extern void perrorMsg(const char *s, ...)
113{
114 va_list p;
115
116 va_start(p, s);
117 fflush(stdout);
118 fprintf(stderr, "%s: ", applet_name);
119 if (s && *s) {
120 vfprintf(stderr, s, p);
121 fputs(": ", stderr);
122 }
123 fprintf(stderr, "%s\n", strerror(errno));
124 va_end(p);
125 fflush(stderr);
126}
127
Matt Kraai324a7782000-10-25 15:10:08 +0000128extern void fatalPerror(const char *s, ...)
129{
130 va_list p;
131
132 va_start(p, s);
133 fflush(stdout);
134 fprintf(stderr, "%s: ", applet_name);
135 if (s && *s) {
136 vfprintf(stderr, s, p);
137 fputs(": ", stderr);
138 }
139 fprintf(stderr, "%s\n", strerror(errno));
140 va_end(p);
141 fflush(stderr);
142 exit(EXIT_FAILURE);
143}
144
Erik Andersen23dea9b2000-05-13 00:28:25 +0000145#if defined BB_INIT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000146/* Returns kernel version encoded as major*65536 + minor*256 + patch,
147 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000148 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000149 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000150extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000151{
Erik Andersend07ee462000-02-21 21:26:32 +0000152 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000154
Erik Andersend07ee462000-02-21 21:26:32 +0000155 if (uname(&name) == -1) {
156 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 return (0);
158 }
Erik Andersend07ee462000-02-21 21:26:32 +0000159 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000161}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000162#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000163
Eric Andersen10dc9d42000-06-26 10:45:52 +0000164
165
166#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000167_syscall1(int, sysinfo, struct sysinfo *, info);
168#endif /* BB_INIT */
169
Eric Andersena57ba4d2000-07-08 19:20:49 +0000170#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000171
172#ifndef __NR_umount2
173#define __NR_umount2 52
174#endif
175
176/* Include our own version of <sys/mount.h>, since libc5 doesn't
177 * know about umount2 */
178extern _syscall1(int, umount, const char *, special_file);
179extern _syscall2(int, umount2, const char *, special_file, int, flags);
180extern _syscall5(int, mount, const char *, special_file, const char *, dir,
181 const char *, fstype, unsigned long int, rwflag, const void *, data);
182#endif
183
Eric Andersenc86e5e62000-08-21 19:38:49 +0000184#if defined BB_INSMOD || defined BB_LSMOD
185#ifndef __NR_query_module
186#define __NR_query_module 167
187#endif
188_syscall5(int, query_module, const char *, name, int, which,
189 void *, buf, size_t, bufsize, size_t*, ret);
190#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000191
192
Erik Andersen23dea9b2000-05-13 00:28:25 +0000193#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000194
Erik Andersen029011b2000-03-04 21:19:32 +0000195#define HASH_SIZE 311 /* Should be prime */
196#define hash_inode(i) ((i) % HASH_SIZE)
197
198static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
199
200/*
201 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
202 * `ino_dev_hashtable', else return 0
203 *
204 * If NAME is a non-NULL pointer to a character pointer, and there is
205 * a match, then set *NAME to the value of the name slot in that
206 * bucket.
207 */
208int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
209{
210 ino_dev_hashtable_bucket_t *bucket;
211
212 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
213 while (bucket != NULL) {
214 if ((bucket->ino == statbuf->st_ino) &&
215 (bucket->dev == statbuf->st_dev))
216 {
217 if (name) *name = bucket->name;
218 return 1;
219 }
220 bucket = bucket->next;
221 }
222 return 0;
223}
224
225/* Add statbuf to statbuf hash table */
226void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
227{
228 int i;
229 size_t s;
230 ino_dev_hashtable_bucket_t *bucket;
231
232 i = hash_inode(statbuf->st_ino);
233 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000234 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000235 bucket->ino = statbuf->st_ino;
236 bucket->dev = statbuf->st_dev;
237 if (name)
238 strcpy(bucket->name, name);
239 else
240 bucket->name[0] = '\0';
241 bucket->next = ino_dev_hashtable[i];
242 ino_dev_hashtable[i] = bucket;
243}
244
245/* Clear statbuf hash table */
246void reset_ino_dev_hashtable(void)
247{
248 int i;
249 ino_dev_hashtable_bucket_t *bucket;
250
251 for (i = 0; i < HASH_SIZE; i++) {
252 while (ino_dev_hashtable[i] != NULL) {
253 bucket = ino_dev_hashtable[i]->next;
254 free(ino_dev_hashtable[i]);
255 ino_dev_hashtable[i] = bucket;
256 }
257 }
258}
259
Erik Andersen23dea9b2000-05-13 00:28:25 +0000260#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000261
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000262#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN) || defined (BB_AR)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000263/*
264 * Return TRUE if a fileName is a directory.
265 * Nonexistant files return FALSE.
266 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000267int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000268{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000270 int didMalloc = 0;
271
272 if (statBuf == NULL) {
273 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
274 ++didMalloc;
275 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000278 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000280 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000281
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000282 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
283 status = FALSE;
284 }
285 else status = TRUE;
286
287 if (didMalloc) {
288 free(statBuf);
289 statBuf = NULL;
290 }
291 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000292}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000293#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000294
Eric Andersenebee3012000-09-05 22:17:22 +0000295#if defined (BB_AR) || defined BB_CP_MV
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000296/*
297 * Copy readSize bytes between two file descriptors
298 */
299int copySubFile(int srcFd, int dstFd, size_t remaining)
300{
301 size_t size;
302 char buffer[BUFSIZ];
303
304 while (remaining > 0) {
305 if (remaining > BUFSIZ)
306 size = BUFSIZ;
307 else
308 size = remaining;
309 if (fullWrite(dstFd, buffer, fullRead(srcFd, buffer, size)) < size)
310 return(FALSE);
311 remaining -= size;
312 }
313 return (TRUE);
314}
315#endif
316
317
Erik Andersen23dea9b2000-05-13 00:28:25 +0000318#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000319/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000320 * Copy one file to another, while possibly preserving its modes, times, and
321 * modes. Returns TRUE if successful, or FALSE on a failure with an error
322 * message output. (Failure is not indicated if attributes cannot be set.)
323 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000324 */
325int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000327 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000328{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 int rfd;
330 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 struct stat srcStatBuf;
333 struct stat dstStatBuf;
334 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000335
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 if (followLinks == TRUE)
337 status = stat(srcName, &srcStatBuf);
338 else
339 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000340
Erik Andersenfac10d72000-02-07 05:29:42 +0000341 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342 perror(srcName);
343 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000344 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345
346 if (followLinks == TRUE)
347 status = stat(destName, &dstStatBuf);
348 else
349 status = lstat(destName, &dstStatBuf);
350
Erik Andersen59b9e872000-05-10 05:05:45 +0000351 if (status < 0 || forceFlag==TRUE) {
352 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 dstStatBuf.st_ino = -1;
354 dstStatBuf.st_dev = -1;
355 }
356
357 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
358 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaid537a952000-07-14 01:51:25 +0000359 errorMsg("Copying file \"%s\" to itself\n", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 return FALSE;
361 }
362
363 if (S_ISDIR(srcStatBuf.st_mode)) {
364 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
365 /* Make sure the directory is writable */
366 status = mkdir(destName, 0777777 ^ umask(0));
367 if (status < 0 && errno != EEXIST) {
368 perror(destName);
369 return FALSE;
370 }
371 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000372 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 int link_size;
374
375 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000376 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
377 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 if (link_size < 0) {
379 perror(srcName);
380 return FALSE;
381 }
382 link_val[link_size] = '\0';
383 status = symlink(link_val, destName);
384 if (status < 0) {
385 perror(destName);
386 return FALSE;
387 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000388#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000390 /* Try to set owner, but fail silently like GNU cp */
391 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000393#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 return TRUE;
395 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
396 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
397 if (mkfifo(destName, 0644) < 0) {
398 perror(destName);
399 return FALSE;
400 }
401 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
402 || S_ISSOCK(srcStatBuf.st_mode)) {
403 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
404 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
405 perror(destName);
406 return FALSE;
407 }
408 } else if (S_ISREG(srcStatBuf.st_mode)) {
409 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
410 rfd = open(srcName, O_RDONLY);
411 if (rfd < 0) {
412 perror(srcName);
413 return FALSE;
414 }
415
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000416 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 srcStatBuf.st_mode);
418 if (wfd < 0) {
419 perror(destName);
420 close(rfd);
421 return FALSE;
422 }
423
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000424 if (copySubFile(rfd, wfd, srcStatBuf.st_size)==FALSE)
425 goto error_exit;
426
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 close(rfd);
428 if (close(wfd) < 0) {
429 return FALSE;
430 }
431 }
432
433 if (setModes == TRUE) {
434 /* This is fine, since symlinks never get here */
435 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
436 perror(destName);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000437 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 }
439 if (chmod(destName, srcStatBuf.st_mode) < 0) {
440 perror(destName);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000441 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 }
443 times.actime = srcStatBuf.st_atime;
444 times.modtime = srcStatBuf.st_mtime;
445 if (utime(destName, &times) < 0) {
446 perror(destName);
Matt Kraaie0bcce02000-09-27 02:29:39 +0000447 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 }
449 }
450
Erik Andersenfac10d72000-02-07 05:29:42 +0000451 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 error_exit:
454 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000455 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000457
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000459}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000460#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000461
462
463
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000464#if defined BB_TAR || defined BB_LS ||defined BB_AR
Erik Andersen23dea9b2000-05-13 00:28:25 +0000465
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000466#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
467#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
468
469/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
470static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 0, 0, S_ISUID,
472 0, 0, S_ISGID,
473 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000474};
475
476/* The 9 mode bits to test */
477static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 S_IRUSR, S_IWUSR, S_IXUSR,
479 S_IRGRP, S_IWGRP, S_IXGRP,
480 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000481};
482
483#define MODE1 "rwxrwxrwx"
484#define MODE0 "---------"
485#define SMODE1 "..s..s..t"
486#define SMODE0 "..S..S..T"
487
Eric Andersencc8ed391999-10-05 16:24:54 +0000488/*
489 * Return the standard ls-like mode string from a file mode.
490 * This is static and so is overwritten on each call.
491 */
Eric Andersenf811e071999-10-09 00:25:00 +0000492const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000493{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000495
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 int i;
497
498 buf[0] = TYPECHAR(mode);
499 for (i = 0; i < 9; i++) {
500 if (mode & SBIT[i])
501 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
502 else
503 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
504 }
505 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000506}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000507#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000508
509
Pavel Roskin21ddb382000-07-17 19:31:54 +0000510#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000511/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000512 * Return the standard ls-like time string from a time_t
513 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000514 */
Eric Andersenf811e071999-10-09 00:25:00 +0000515const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000516{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 time_t now;
518 char *str;
519 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000520
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000522
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000524
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 strcpy(buf, &str[4]);
526 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000527
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
529 strcpy(&buf[7], &str[20]);
530 buf[11] = '\0';
531 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000532
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000534}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000535#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000536
Pavel Roskin21ddb382000-07-17 19:31:54 +0000537#if defined BB_TAR || defined BB_CP_MV || defined BB_AR
Eric Andersen17d49ef1999-10-06 20:25:32 +0000538/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000539 * Write all of the supplied buffer out to a file.
540 * This does multiple writes as necessary.
541 * Returns the amount written, or -1 on an error.
542 */
Eric Andersenf811e071999-10-09 00:25:00 +0000543int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000544{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 int cc;
546 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 while (len > 0) {
551 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 if (cc < 0)
554 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000555
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 buf += cc;
557 total += cc;
558 len -= cc;
559 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000560
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000562}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000563#endif /* BB_TAR || BB_CP_MV || BB_AR */
Eric Andersencc8ed391999-10-05 16:24:54 +0000564
565
Eric Andersenebee3012000-09-05 22:17:22 +0000566#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH || defined BB_CP_MV
Eric Andersencc8ed391999-10-05 16:24:54 +0000567/*
568 * Read all of the supplied buffer from a file.
569 * This does multiple reads as necessary.
570 * Returns the amount read, or -1 on an error.
571 * A short read is returned on an end of file.
572 */
Eric Andersenf811e071999-10-09 00:25:00 +0000573int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000574{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 int cc;
576 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000577
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000579
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 while (len > 0) {
581 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000582
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583 if (cc < 0)
584 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000585
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 if (cc == 0)
587 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000588
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 buf += cc;
590 total += cc;
591 len -= cc;
592 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000593
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000595}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000596#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000597
598
Erik Andersen23dea9b2000-05-13 00:28:25 +0000599#if defined (BB_CHMOD_CHOWN_CHGRP) \
600 || defined (BB_CP_MV) \
601 || defined (BB_FIND) \
602 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000603 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000604 || defined (BB_RM) \
605 || defined (BB_TAR)
606
Eric Andersencc8ed391999-10-05 16:24:54 +0000607/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000608 * Walk down all the directories under the specified
609 * location, and do something (something specified
610 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000611 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000612 * Unfortunatly, while nftw(3) could replace this and reduce
613 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000614 * and so isn't sufficiently portable to take over since glibc2.1
615 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000616 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000617int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 int recurse, int followLinks, int depthFirst,
619 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000620 struct stat * statbuf,
621 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000623 struct stat * statbuf,
624 void* userData),
625 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000626{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 int status;
628 struct stat statbuf;
629 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000630
Erik Andersene49d5ec2000-02-08 19:58:47 +0000631 if (followLinks == TRUE)
632 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000633 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000635
Eric Andersen2b69c401999-10-05 22:58:32 +0000636 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000637#ifdef BB_DEBUG_PRINT_SCAFFOLD
638 fprintf(stderr,
639 "status=%d followLinks=%d TRUE=%d\n",
640 status, followLinks, TRUE);
641#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000642 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000643 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000644 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645
646 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
647 if (fileAction == NULL)
648 return TRUE;
649 else
Erik Andersen3364d782000-03-28 00:58:14 +0000650 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651 }
652
653 if (recurse == FALSE) {
654 if (S_ISDIR(statbuf.st_mode)) {
655 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000656 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 else
658 return TRUE;
659 }
660 }
661
662 if (S_ISDIR(statbuf.st_mode)) {
663 DIR *dir;
664
665 dir = opendir(fileName);
666 if (!dir) {
667 perror(fileName);
668 return FALSE;
669 }
670 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000671 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 if (status == FALSE) {
673 perror(fileName);
674 return FALSE;
675 }
676 }
677 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000678 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000679
680 if ((strcmp(next->d_name, "..") == 0)
681 || (strcmp(next->d_name, ".") == 0)) {
682 continue;
683 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000684 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Matt Kraaid537a952000-07-14 01:51:25 +0000685 errorMsg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000686 return FALSE;
687 }
Erik Andersenc3660502000-04-18 21:18:57 +0000688 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 sprintf(nextFile, "%s/%s", fileName, next->d_name);
690 status =
691 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000692 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000693 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000694 closedir(dir);
695 return FALSE;
696 }
697 }
698 status = closedir(dir);
699 if (status < 0) {
700 perror(fileName);
701 return FALSE;
702 }
703 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000704 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 if (status == FALSE) {
706 perror(fileName);
707 return FALSE;
708 }
709 }
710 } else {
711 if (fileAction == NULL)
712 return TRUE;
713 else
Erik Andersen3364d782000-03-28 00:58:14 +0000714 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 }
716 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000717}
718
Erik Andersen23dea9b2000-05-13 00:28:25 +0000719#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000720
Eric Andersenf6be9441999-10-13 21:12:06 +0000721
722
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000723#if defined (BB_TAR) || defined (BB_MKDIR) || defined (BB_AR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000724/*
725 * Attempt to create the directories along the specified path, except for
726 * the final component. The mode is given for the final directory only,
727 * while all previous ones get default protections. Errors are not reported
728 * here, as failures to restore files can be reported later.
729 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000731{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 char *cp;
733 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000734 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000735 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000736
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 strcpy(buf, name);
738 for (cp = buf; *cp == '/'; cp++);
739 cp = strchr(cp, '/');
740 while (cp) {
741 cpOld = cp;
742 cp = strchr(cp + 1, '/');
743 *cpOld = '\0';
744 retVal = mkdir(buf, cp ? 0777 : mode);
745 if (retVal != 0 && errno != EEXIST) {
746 perror(buf);
747 return FALSE;
748 }
749 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000750 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000751 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000752}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000753#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000754
755
756
Pavel Roskin21ddb382000-07-17 19:31:54 +0000757#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
Glenn L McGrathac19b7e2000-09-11 05:06:13 +0000758 || defined (BB_MKFIFO) || defined (BB_MKNOD) || defined (BB_AR)
Eric Andersence8f3b91999-10-20 07:03:36 +0000759/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000760
761
762
Erik Andersene49d5ec2000-02-08 19:58:47 +0000763extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000764{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000765 mode_t andMode =
766
767 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000768 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 mode_t mode = 0;
770 mode_t groups = 0;
771 char type;
772 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000773
Eric Andersen4e61db22000-09-01 00:06:17 +0000774 if (s==NULL)
775 return (FALSE);
776
Eric Andersenf6be9441999-10-13 21:12:06 +0000777 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000778 for (;;) {
779 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000780 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000781 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000782 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000784 continue;
785 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000786 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000787 continue;
788 case 'o':
789 groups |= S_IRWXO;
790 continue;
791 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000792 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000793 continue;
794 case '+':
795 case '=':
796 case '-':
797 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 if (groups == 0) /* The default is "all" */
799 groups |=
800 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000801 break;
802 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000803 if (isdigit(c) && c >= '0' && c <= '7' &&
804 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000805 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000806 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000808 return (FALSE);
809 }
810 break;
811 }
812
Erik Andersene49d5ec2000-02-08 19:58:47 +0000813 while ((c = *s++) != '\0') {
814 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000815 case ',':
816 break;
817 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000819 continue;
820 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000822 continue;
823 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000825 continue;
826 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000827 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000828 continue;
829 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000830 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000831 continue;
832 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000833 *theMode &= andMode;
834 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000836 }
837 break;
838 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000840 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000841 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000842 /* fall through */
843 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000844 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000845 break;
846 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000847 andMode &= ~(mode & groups);
848 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000849 break;
850 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000852 *theMode &= andMode;
853 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000854 return (TRUE);
855}
Eric Andersence8f3b91999-10-20 07:03:36 +0000856
857
Pavel Roskin21ddb382000-07-17 19:31:54 +0000858#endif
859/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000860
Eric Andersene674eb71999-10-19 20:52:57 +0000861
862
Erik Andersen23dea9b2000-05-13 00:28:25 +0000863
864
Pavel Roskin21ddb382000-07-17 19:31:54 +0000865#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
866 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
867 || defined BB_LOGNAME || defined BB_WHOAMI
Erik Andersen23dea9b2000-05-13 00:28:25 +0000868
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000869/* This parses entries in /etc/passwd and /etc/group. This is desirable
870 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
871 * increases target size and is often not needed or wanted for embedded
872 * systems.
873 *
874 * /etc/passwd entries look like this:
875 * root:x:0:0:root:/root:/bin/bash
876 * and /etc/group entries look like this:
877 * root:x:0:
878 *
879 * This uses buf as storage to hold things.
880 *
881 */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000882unsigned long my_getid(const char *filename, char *name, long id, long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000883{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000884 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000885 char *rname, *start, *end, buf[128];
Eric Andersenfad04fd2000-07-14 06:49:52 +0000886 long rid;
887 long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000888
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000890 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000891 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000892 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000894 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000895
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000897 if (buf[0] == '#')
898 continue;
899
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000900 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000901 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000902 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000903 if (end == NULL)
904 continue;
905 *end = '\0';
906 rname = start;
907
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000908 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000909 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000911 if (end == NULL)
912 continue;
913
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000914 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000915 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000916 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000917 if (end == start)
918 continue;
919
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000920 /* gid in passwd */
921 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000922 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000923
Eric Andersend23f9ba1999-10-20 19:18:15 +0000924 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000926 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927 fclose(file);
928 return (rid);
929 }
930 }
931 if (id != -1 && id == rid) {
932 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000933 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 fclose(file);
935 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000936 }
937 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000938 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000939 return (-1);
940}
941
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000942/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000943long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000944{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000945 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000946}
947
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000948/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000949long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000950{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000951 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000952}
953
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000954/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000955void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000956{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000957 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000958}
959
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000960/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000961void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000962{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000963 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000964}
965
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000966/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000967long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000968{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000969 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000970 my_getid("/etc/passwd", name, -1, &gid);
971 return gid;
972}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000973
Pavel Roskin21ddb382000-07-17 19:31:54 +0000974#endif
975 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
976 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000977
978
Eric Andersenf86aee72000-07-06 17:20:46 +0000979#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000980
Eric Andersenbd22ed82000-07-08 18:55:24 +0000981/* From <linux/kd.h> */
982#define KDGKBTYPE 0x4B33 /* get keyboard type */
983#define KB_84 0x01
984#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000985
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000987{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 char arg;
989
990 arg = 0;
991 return (ioctl(fd, KDGKBTYPE, &arg) == 0
992 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000993}
994
Erik Andersene49d5ec2000-02-08 19:58:47 +0000995static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000996{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997 int fd;
998
999 /* try read-only */
1000 fd = open(fnam, O_RDWR);
1001
1002 /* if failed, try read-only */
1003 if (fd < 0 && errno == EACCES)
1004 fd = open(fnam, O_RDONLY);
1005
1006 /* if failed, try write-only */
1007 if (fd < 0 && errno == EACCES)
1008 fd = open(fnam, O_WRONLY);
1009
1010 /* if failed, fail */
1011 if (fd < 0)
1012 return -1;
1013
1014 /* if not a console, fail */
1015 if (!is_a_console(fd)) {
1016 close(fd);
1017 return -1;
1018 }
1019
1020 /* success */
1021 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001022}
1023
1024/*
1025 * Get an fd for use with kbd/console ioctls.
1026 * We try several things because opening /dev/console will fail
1027 * if someone else used X (which does a chown on /dev/console).
1028 *
1029 * if tty_name is non-NULL, try this one instead.
1030 */
1031
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001033{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001034 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001035
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 if (tty_name) {
1037 if (-1 == (fd = open_a_console(tty_name)))
1038 return -1;
1039 else
1040 return fd;
1041 }
1042
1043 fd = open_a_console("/dev/tty");
1044 if (fd >= 0)
1045 return fd;
1046
1047 fd = open_a_console("/dev/tty0");
1048 if (fd >= 0)
1049 return fd;
1050
1051 fd = open_a_console("/dev/console");
1052 if (fd >= 0)
1053 return fd;
1054
1055 for (fd = 0; fd < 3; fd++)
1056 if (is_a_console(fd))
1057 return fd;
1058
Matt Kraaid537a952000-07-14 01:51:25 +00001059 errorMsg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001061}
1062
1063
Eric Andersenf86aee72000-07-06 17:20:46 +00001064#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001065
1066
Eric Andersen9f16d612000-06-12 23:11:16 +00001067#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001068/*
1069 * Routine to see if a text string is matched by a wildcard pattern.
1070 * Returns TRUE if the text is matched, or FALSE if it is not matched
1071 * or if the pattern is invalid.
1072 * * matches zero or more characters
1073 * ? matches a single character
1074 * [abc] matches 'a', 'b' or 'c'
1075 * \c quotes character c
1076 * Adapted from code written by Ingo Wilken, and
1077 * then taken from sash, Copyright (c) 1999 by David I. Bell
1078 * Permission is granted to use, distribute, or modify this source,
1079 * provided that this copyright notice remains intact.
1080 * Permission to distribute this code under the GPL has been granted.
1081 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001083{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001084 const char *retryPat;
1085 const char *retryText;
1086 int ch;
1087 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001088 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001089
Erik Andersene49d5ec2000-02-08 19:58:47 +00001090 retryPat = NULL;
1091 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001092
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001094 ch = *pattern++;
1095
Erik Andersene49d5ec2000-02-08 19:58:47 +00001096 switch (ch) {
1097 case '*':
1098 retryPat = pattern;
1099 retryText = text;
1100 break;
1101
1102 case '[':
1103 found = FALSE;
1104
1105 while ((ch = *pattern++) != ']') {
1106 if (ch == '\\')
1107 ch = *pattern++;
1108
1109 if (ch == '\0')
1110 return FALSE;
1111
1112 if (*text == ch)
1113 found = TRUE;
1114 }
Erik Andersen632bb572000-04-18 22:09:06 +00001115 len=strlen(text);
1116 if (found == FALSE && len!=0) {
1117 return FALSE;
1118 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001120 if (strlen(pattern)==0 && len==1) {
1121 return TRUE;
1122 }
1123 if (len!=0) {
1124 text++;
1125 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001126 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 }
1128
1129 /* fall into next case */
1130
1131 case '?':
1132 if (*text++ == '\0')
1133 return FALSE;
1134
1135 break;
1136
1137 case '\\':
1138 ch = *pattern++;
1139
1140 if (ch == '\0')
1141 return FALSE;
1142
1143 /* fall into next case */
1144
1145 default:
1146 if (*text == ch) {
1147 if (*text)
1148 text++;
1149 break;
1150 }
1151
1152 if (*text) {
1153 pattern = retryPat;
1154 text = ++retryText;
1155 break;
1156 }
1157
Eric Andersenb186d981999-12-03 09:19:54 +00001158 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001159 }
1160
Erik Andersene49d5ec2000-02-08 19:58:47 +00001161 if (pattern == NULL)
1162 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001163 }
1164
Erik Andersene49d5ec2000-02-08 19:58:47 +00001165 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001166}
Eric Andersen9f16d612000-06-12 23:11:16 +00001167#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001168
1169
Eric Andersen29d2e361999-11-06 06:07:27 +00001170
1171
Erik Andersen23dea9b2000-05-13 00:28:25 +00001172#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001173/*
1174 * Given a block device, find the mount table entry if that block device
1175 * is mounted.
1176 *
1177 * Given any other file (or directory), find the mount table entry for its
1178 * filesystem.
1179 */
1180extern struct mntent *findMountPoint(const char *name, const char *table)
1181{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001182 struct stat s;
1183 dev_t mountDevice;
1184 FILE *mountTable;
1185 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001186
Erik Andersene49d5ec2000-02-08 19:58:47 +00001187 if (stat(name, &s) != 0)
1188 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001189
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 if ((s.st_mode & S_IFMT) == S_IFBLK)
1191 mountDevice = s.st_rdev;
1192 else
1193 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001194
1195
Erik Andersene49d5ec2000-02-08 19:58:47 +00001196 if ((mountTable = setmntent(table, "r")) == 0)
1197 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001198
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 while ((mountEntry = getmntent(mountTable)) != 0) {
1200 if (strcmp(name, mountEntry->mnt_dir) == 0
1201 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1202 break;
1203 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1204 break;
1205 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1206 break;
1207 }
1208 endmntent(mountTable);
1209 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001210}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001211#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001212
1213
1214
Erik Andersen23dea9b2000-05-13 00:28:25 +00001215#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001216/*
1217 * Read a number with a possible multiplier.
1218 * Returns -1 if the number format is illegal.
1219 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001221{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001223
Erik Andersene49d5ec2000-02-08 19:58:47 +00001224 if (!isDecimal(*cp))
1225 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001226
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001228
Erik Andersene49d5ec2000-02-08 19:58:47 +00001229 while (isDecimal(*cp))
1230 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 switch (*cp++) {
1233 case 'M':
1234 case 'm': /* `tail' uses it traditionally */
1235 value *= 1048576;
1236 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001237
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238 case 'k':
1239 value *= 1024;
1240 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001241
Erik Andersene49d5ec2000-02-08 19:58:47 +00001242 case 'b':
1243 value *= 512;
1244 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001245
Erik Andersene49d5ec2000-02-08 19:58:47 +00001246 case 'w':
1247 value *= 2;
1248 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001249
Erik Andersene49d5ec2000-02-08 19:58:47 +00001250 case '\0':
1251 return value;
1252
1253 default:
1254 return -1;
1255 }
1256
1257 if (*cp)
1258 return -1;
1259
Eric Andersen1792f8c1999-12-09 06:11:36 +00001260 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001261}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001262#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001263
Eric Andersen6805d5d1999-12-09 22:39:55 +00001264
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001265#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001266/* try to open up the specified device */
1267extern int device_open(char *device, int mode)
1268{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001269 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001270
Erik Andersene49d5ec2000-02-08 19:58:47 +00001271 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001272
Erik Andersene49d5ec2000-02-08 19:58:47 +00001273 /* Retry up to 5 times */
1274 for (f = 0; f < 5; f++)
1275 if ((fd = open(device, m, 0600)) >= 0)
1276 break;
1277 if (fd < 0)
1278 return fd;
1279 /* Reset original flags. */
1280 if (m != mode)
1281 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001282 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001283}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001284#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001285
1286
Erik Andersen23dea9b2000-05-13 00:28:25 +00001287#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001288#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001289#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001290#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001291
Erik Andersen23dea9b2000-05-13 00:28:25 +00001292#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001293/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001294 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001295 * This finds the pid of the specified process,
1296 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001297 *
Erik Andersen825aead2000-04-07 06:00:07 +00001298 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001299 */
Erik Andersen825aead2000-04-07 06:00:07 +00001300extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001301{
Erik Andersen825aead2000-04-07 06:00:07 +00001302 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001303 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001304 pid_t num_pids;
1305 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001306 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001307
1308 /* open device */
1309 fd = open(device, O_RDONLY);
1310 if (fd < 0)
1311 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1312
1313 /* Find out how many processes there are */
1314 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1315 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1316
1317 /* Allocate some memory -- grab a few extras just in case
1318 * some new processes start up while we wait. The kernel will
1319 * just ignore any extras if we give it too many, and will trunc.
1320 * the list if we give it too few. */
Matt Kraai322ae932000-09-13 02:46:14 +00001321 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
Erik Andersen246cc6d2000-03-07 07:41:42 +00001322 pid_array[0] = num_pids+10;
1323
1324 /* Now grab the pid list */
1325 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1326 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1327
1328 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001329 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001330 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001331 struct pid_info info;
1332
1333 info.pid = pid_array[i];
1334 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1335 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1336
Erik Andersen0a64de92000-04-18 20:00:03 +00001337 /* Make sure we only match on the process name */
1338 p=info.command_line+1;
1339 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1340 (p)++;
1341 }
1342 if (isspace(*(p)))
1343 *p='\0';
1344
Erik Andersenb9167cb2000-04-18 22:41:30 +00001345 if ((strstr(info.command_line, pidName) != NULL)
1346 && (strlen(pidName) == strlen(info.command_line))) {
Matt Kraai322ae932000-09-13 02:46:14 +00001347 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001348 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001349 }
1350 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001351 if (pidList)
1352 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001353
1354 /* Free memory */
1355 free( pid_array);
1356
1357 /* close device */
1358 if (close (fd) != 0)
1359 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1360
Erik Andersen825aead2000-04-07 06:00:07 +00001361 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001362}
Erik Andersen269bba22000-03-08 14:50:47 +00001363#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001364#if ! defined BB_FEATURE_USE_PROCFS
1365#error Sorry, I depend on the /proc filesystem right now.
1366#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001367
Erik Andersen246cc6d2000-03-07 07:41:42 +00001368/* findPidByName()
1369 *
1370 * This finds the pid of the specified process.
1371 * Currently, it's implemented by rummaging through
1372 * the proc filesystem.
1373 *
Erik Andersen825aead2000-04-07 06:00:07 +00001374 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001375 */
Erik Andersen825aead2000-04-07 06:00:07 +00001376extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001377{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001378 DIR *dir;
1379 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001380 pid_t* pidList=NULL;
1381 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001382
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001383 dir = opendir("/proc");
1384 if (!dir)
1385 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1386
1387 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001388 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001389 char filename[256];
1390 char buffer[256];
John Beppuf95ca971999-12-09 22:10:18 +00001391
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001392 /* If it isn't a number, we don't want it */
1393 if (!isdigit(*next->d_name))
1394 continue;
1395
Eric Andersen31e68292000-09-14 21:12:20 +00001396 sprintf(filename, "/proc/%s/cmdline", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001397 status = fopen(filename, "r");
1398 if (!status) {
1399 continue;
1400 }
1401 fgets(buffer, 256, status);
1402 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001403
Eric Andersen31e68292000-09-14 21:12:20 +00001404 if (strstr(get_last_path_component(buffer), pidName) != NULL) {
Matt Kraai322ae932000-09-13 02:46:14 +00001405 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
Erik Andersen825aead2000-04-07 06:00:07 +00001406 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001407 }
John Beppuf95ca971999-12-09 22:10:18 +00001408 }
Eric Andersen31e68292000-09-14 21:12:20 +00001409
Erik Andersen0a64de92000-04-18 20:00:03 +00001410 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001411 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001412 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001413}
Erik Andersen269bba22000-03-08 14:50:47 +00001414#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001415#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001416
Eric Andersenae6eae02000-07-19 17:35:54 +00001417#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001418/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001419extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001420{
Eric Andersenae6eae02000-07-19 17:35:54 +00001421 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001422
Eric Andersenae6eae02000-07-19 17:35:54 +00001423 if (!ptr)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001424 fatalError(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001425 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001426}
1427
Eric Andersen501c88b2000-07-28 15:14:45 +00001428extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001429{
1430 void *ptr = realloc(old, size);
1431 if (!ptr)
1432 fatalError(memory_exhausted);
1433 return ptr;
1434}
Eric Andersen501c88b2000-07-28 15:14:45 +00001435
1436extern void *xcalloc(size_t nmemb, size_t size)
1437{
1438 void *ptr = calloc(nmemb, size);
1439 if (!ptr)
1440 fatalError(memory_exhausted);
1441 return ptr;
1442}
Eric Andersenae6eae02000-07-19 17:35:54 +00001443#endif
1444
Eric Andersen11c65522000-09-07 17:24:47 +00001445#if defined BB_FEATURE_NFSMOUNT || defined BB_SH || defined BB_LS
Eric Andersenae6eae02000-07-19 17:35:54 +00001446# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001447extern char * xstrdup (const char *s) {
1448 char *t;
1449
1450 if (s == NULL)
1451 return NULL;
1452
1453 t = strdup (s);
1454
1455 if (t == NULL)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001456 fatalError(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001457
1458 return t;
1459}
Eric Andersenae6eae02000-07-19 17:35:54 +00001460# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001461#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001462
Matt Kraai88cc3052000-08-26 16:52:28 +00001463#if defined BB_FEATURE_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001464extern char * xstrndup (const char *s, int n) {
1465 char *t;
1466
1467 if (s == NULL)
1468 fatalError("xstrndup bug");
1469
1470 t = xmalloc(n+1);
1471 strncpy(t,s,n);
1472 t[n] = 0;
1473
1474 return t;
1475}
1476#endif
1477
1478
Erik Andersen23dea9b2000-05-13 00:28:25 +00001479#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001480extern int vdprintf(int d, const char *format, va_list ap)
1481{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001482 char buf[BUF_SIZE];
1483 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001484
Erik Andersene49d5ec2000-02-08 19:58:47 +00001485 len = vsprintf(buf, format, ap);
1486 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001487}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001488#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001489
Eric Andersen624cc772000-09-21 02:04:51 +00001490
Erik Andersen23dea9b2000-05-13 00:28:25 +00001491#if defined BB_FEATURE_MOUNT_LOOP
Eric Andersen624cc772000-09-21 02:04:51 +00001492#include <fcntl.h>
1493#include "loop.h" /* Pull in loop device support */
1494
Erik Andersen5cbdd712000-01-26 20:06:48 +00001495extern int del_loop(const char *device)
1496{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001497 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001498
Erik Andersene49d5ec2000-02-08 19:58:47 +00001499 if ((fd = open(device, O_RDONLY)) < 0) {
1500 perror(device);
1501 return (FALSE);
1502 }
1503 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1504 perror("ioctl: LOOP_CLR_FD");
1505 return (FALSE);
1506 }
1507 close(fd);
1508 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001509}
Erik Andersende7965c2000-01-26 23:49:21 +00001510
Erik Andersene49d5ec2000-02-08 19:58:47 +00001511extern int set_loop(const char *device, const char *file, int offset,
1512 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001513{
1514 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001515 int fd, ffd, mode;
1516
Erik Andersende7965c2000-01-26 23:49:21 +00001517 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001518 if ((ffd = open(file, mode)) < 0 && !*loopro
1519 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1520 perror(file);
1521 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001522 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001523 if ((fd = open(device, mode)) < 0) {
1524 close(ffd);
1525 perror(device);
1526 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001527 }
1528 *loopro = (mode == O_RDONLY);
1529
1530 memset(&loopinfo, 0, sizeof(loopinfo));
1531 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001532 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001533
1534 loopinfo.lo_offset = offset;
1535
1536 loopinfo.lo_encrypt_key_size = 0;
1537 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1538 perror("ioctl: LOOP_SET_FD");
1539 close(fd);
1540 close(ffd);
1541 return 1;
1542 }
1543 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1544 (void) ioctl(fd, LOOP_CLR_FD, 0);
1545 perror("ioctl: LOOP_SET_STATUS");
1546 close(fd);
1547 close(ffd);
1548 return 1;
1549 }
1550 close(fd);
1551 close(ffd);
1552 return 0;
1553}
1554
Erik Andersene49d5ec2000-02-08 19:58:47 +00001555extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001556{
1557 char dev[20];
1558 int i, fd;
1559 struct stat statbuf;
1560 struct loop_info loopinfo;
1561
Erik Andersene49d5ec2000-02-08 19:58:47 +00001562 for (i = 0; i <= 7; i++) {
1563 sprintf(dev, "/dev/loop%d", i);
1564 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1565 if ((fd = open(dev, O_RDONLY)) >= 0) {
1566 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1567 if (errno == ENXIO) { /* probably free */
1568 close(fd);
1569 return strdup(dev);
1570 }
1571 }
1572 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001573 }
Erik Andersende7965c2000-01-26 23:49:21 +00001574 }
Erik Andersende7965c2000-01-26 23:49:21 +00001575 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001576 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001577}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001578#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001579
Erik Andersen23dea9b2000-05-13 00:28:25 +00001580#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001581extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001582{
Erik Andersen016ffe92000-03-22 00:58:54 +00001583 DIR *dir;
1584 struct dirent *entry;
1585 struct stat statBuf, rootStat;
1586 char fileName[BUFSIZ];
1587
Erik Andersenec5bd902000-03-22 07:12:05 +00001588 if (stat("/", &rootStat) != 0) {
1589 errorMsg("could not stat '/'\n");
1590 return( FALSE);
1591 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001592
Erik Andersenec5bd902000-03-22 07:12:05 +00001593 dir = opendir("/dev");
1594 if (!dir) {
1595 errorMsg("could not open '/dev'\n");
1596 return( FALSE);
1597 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001598
1599 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001600
Erik Andersen016ffe92000-03-22 00:58:54 +00001601 /* Must skip ".." since that is "/", and so we
1602 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001603 if (strcmp(entry->d_name, "..") == 0)
1604 continue;
1605
Eric Andersenf9ca6532000-08-11 17:45:02 +00001606 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001607
1608 if (stat(fileName, &statBuf) != 0)
1609 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001610 /* Some char devices have the same dev_t as block
1611 * devices, so make sure this is a block device */
1612 if (! S_ISBLK(statBuf.st_mode))
1613 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001614 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001615 strcpy(name, fileName);
1616 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001617 }
1618 }
1619
Erik Andersenec5bd902000-03-22 07:12:05 +00001620 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001621}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001622#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001623
Mark Whitley1ca41772000-06-28 22:15:26 +00001624
1625/* get_line_from_file() - This function reads an entire line from a text file
1626 * up to a newline. It returns a malloc'ed char * which must be stored and
1627 * free'ed by the caller. */
1628extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001629{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001630 static const int GROWBY = 80; /* how large we will grow strings by */
1631
Mark Whitley1ca41772000-06-28 22:15:26 +00001632 int ch;
1633 int idx = 0;
1634 char *linebuf = NULL;
1635 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001636
Mark Whitley1ca41772000-06-28 22:15:26 +00001637 while (1) {
1638 ch = fgetc(file);
1639 if (ch == EOF)
1640 break;
1641 /* grow the line buffer as necessary */
Eric Andersen285b4462000-09-23 19:56:46 +00001642 while (idx > linebufsz-2)
Matt Kraai322ae932000-09-13 02:46:14 +00001643 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
Mark Whitley1ca41772000-06-28 22:15:26 +00001644 linebuf[idx++] = (char)ch;
1645 if ((char)ch == '\n')
1646 break;
1647 }
John Beppu5a728cf2000-04-17 04:22:09 +00001648
Mark Whitley1ca41772000-06-28 22:15:26 +00001649 if (idx == 0)
1650 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001651
Mark Whitley1ca41772000-06-28 22:15:26 +00001652 linebuf[idx] = 0;
1653 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001654}
Erik Andersen016ffe92000-03-22 00:58:54 +00001655
Eric Andersen944be902000-09-25 16:58:39 +00001656#if defined BB_CAT
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001657extern void print_file(FILE *file)
1658{
1659 int c;
1660
1661 while ((c = getc(file)) != EOF)
1662 putc(c, stdout);
1663 fclose(file);
1664 fflush(stdout);
1665}
1666
1667extern int print_file_by_name(char *filename)
1668{
1669 FILE *file;
Matt Kraaic0321f92000-09-27 04:09:22 +00001670 if ((file = wfopen(filename, "r")) == NULL)
1671 return FALSE;
1672 print_file(file);
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001673 return TRUE;
1674}
Matt Kraaie7c1af12000-09-27 03:01:40 +00001675#endif /* BB_CAT */
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001676
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001677#if defined BB_ECHO || defined BB_TR
1678char process_escape_sequence(char **ptr)
1679{
1680 char c;
1681
1682 switch (c = *(*ptr)++) {
1683 case 'a':
1684 c = '\a';
1685 break;
1686 case 'b':
1687 c = '\b';
1688 break;
1689 case 'f':
1690 c = '\f';
1691 break;
1692 case 'n':
1693 c = '\n';
1694 break;
1695 case 't':
1696 c = '\t';
1697 break;
1698 case 'v':
1699 c = '\v';
1700 break;
1701 case '\\':
1702 c = '\\';
1703 break;
1704 case '0': case '1': case '2': case '3':
1705 case '4': case '5': case '6': case '7':
1706 c -= '0';
1707 if ('0' <= **ptr && **ptr <= '7') {
1708 c = c * 8 + (*(*ptr)++ - '0');
1709 if ('0' <= **ptr && **ptr <= '7')
1710 c = c * 8 + (*(*ptr)++ - '0');
1711 }
1712 break;
1713 default:
1714 (*ptr)--;
1715 c = '\\';
1716 break;
1717 }
1718 return c;
1719}
1720#endif
1721
Eric Andersenba1ff9b2000-10-01 17:01:36 +00001722#if defined BB_BASENAME || defined BB_LN || defined BB_SH || defined BB_INIT || defined BB_FEATURE_USE_PROCFS
Eric Andersen0b4551f2000-07-10 16:44:03 +00001723char *get_last_path_component(char *path)
1724{
1725 char *s=path+strlen(path)-1;
1726
1727 /* strip trailing slashes */
1728 while (s && *s == '/') {
1729 *s-- = '\0';
1730 }
1731
1732 /* find last component */
1733 s = strrchr(path, '/');
1734 if (s==NULL) return path;
1735 else return s+1;
1736}
1737#endif
1738
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001739#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001740void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001741{
1742 int ret;
1743 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1744 int errmsgsz = regerror(ret, preg, NULL, 0);
1745 char *errmsg = xmalloc(errmsgsz);
1746 regerror(ret, preg, errmsg, errmsgsz);
Matt Kraaie0bcce02000-09-27 02:29:39 +00001747 fatalError("xregcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001748 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001749}
1750#endif
1751
Eric Andersen7a86e612000-10-09 18:21:44 +00001752#if defined BB_CAT || defined BB_HEAD || defined BB_WC
Matt Kraaic0321f92000-09-27 04:09:22 +00001753FILE *wfopen(const char *path, const char *mode)
1754{
1755 FILE *fp;
1756 if ((fp = fopen(path, mode)) == NULL) {
1757 errorMsg("%s: %s\n", path, strerror(errno));
1758 errno = 0;
1759 }
1760 return fp;
1761}
1762#endif
1763
Matt Kraaibbaef662000-09-27 02:43:35 +00001764#if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE || defined BB_SED || defined BB_SH || defined BB_UNIQ || defined BB_WC
Matt Kraaie0bcce02000-09-27 02:29:39 +00001765FILE *xfopen(const char *path, const char *mode)
1766{
1767 FILE *fp;
1768 if ((fp = fopen(path, mode)) == NULL)
1769 fatalError("%s: %s\n", path, strerror(errno));
1770 return fp;
1771}
1772#endif
1773
Eric Andersencc8ed391999-10-05 16:24:54 +00001774/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001775/*
1776Local Variables:
1777c-file-style: "linux"
1778c-basic-offset: 4
1779tab-width: 4
1780End:
1781*/