blob: aa8bef6b39507a2fd08572bf5dea780f4202da50 [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 Andersen2b69c401999-10-05 22:58:32 +000028#include "internal.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 Andersen5cbdd712000-01-26 20:06:48 +000056#if defined BB_FEATURE_MOUNT_LOOP
57#include <fcntl.h>
Eric Andersenc674d702000-07-10 22:57:14 +000058#include <linux/loop.h> /* Pull in loop device support */
Erik Andersen5cbdd712000-01-26 20:06:48 +000059#endif
60
Erik Andersen246cc6d2000-03-07 07:41:42 +000061/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
62 * list of available filesystems used for the -t auto option */
Erik Andersen269bba22000-03-08 14:50:47 +000063#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000064//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
65#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
66#endif
67
Erik Andersen5cbdd712000-01-26 20:06:48 +000068
Eric Andersend0246fb1999-11-04 21:18:07 +000069#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
Eric Andersenfdd51032000-08-02 18:48:26 +000070# if defined BB_MTAB
Eric Andersen0ecb54a1999-12-05 23:24:55 +000071const char mtab_file[] = "/etc/mtab";
Eric Andersenfdd51032000-08-02 18:48:26 +000072# else
73# if defined BB_FEATURE_USE_PROCFS
74const char mtab_file[] = "/proc/mounts";
Eric Andersen0ecb54a1999-12-05 23:24:55 +000075# else
Erik Andersen269bba22000-03-08 14:50:47 +000076# if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +000077const char mtab_file[] = "/dev/mtab";
78# else
Erik Andersen269bba22000-03-08 14:50:47 +000079# 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 +000080# endif
81# endif
Erik Andersen246cc6d2000-03-07 07:41:42 +000082# endif
Eric Andersend0246fb1999-11-04 21:18:07 +000083#endif
84
Eric Andersend73dc5b1999-11-10 23:13:02 +000085extern void usage(const char *usage)
Eric Andersenb0e9a701999-10-18 22:28:26 +000086{
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000087 fprintf(stderr, "%s\n\n", full_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 fprintf(stderr, "Usage: %s\n", usage);
89 exit FALSE;
Eric Andersenb0e9a701999-10-18 22:28:26 +000090}
91
Erik Andersen1ad302a2000-03-24 00:54:46 +000092extern void errorMsg(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +000093{
94 va_list p;
95
96 va_start(p, s);
97 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +000098 fprintf(stderr, "%s: ", applet_name);
Erik Andersen9ffdaa62000-02-11 21:55:04 +000099 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000100 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000101 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000102}
103
Erik Andersen1ad302a2000-03-24 00:54:46 +0000104extern void fatalError(const char *s, ...)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000105{
106 va_list p;
107
108 va_start(p, s);
109 fflush(stdout);
Matt Kraaibe84cd42000-07-12 17:02:35 +0000110 fprintf(stderr, "%s: ", applet_name);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000111 vfprintf(stderr, s, p);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000112 va_end(p);
Erik Andersen3522eb12000-03-12 23:49:18 +0000113 fflush(stderr);
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000114 exit( FALSE);
115}
Eric Andersenb0e9a701999-10-18 22:28:26 +0000116
Erik Andersen23dea9b2000-05-13 00:28:25 +0000117#if defined BB_INIT
Eric Andersend23f9ba1999-10-20 19:18:15 +0000118/* Returns kernel version encoded as major*65536 + minor*256 + patch,
119 * so, for example, to check if the kernel is greater than 2.2.11:
Erik Andersen298854f2000-03-23 01:09:18 +0000120 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000121 */
Erik Andersen1ad302a2000-03-24 00:54:46 +0000122extern int get_kernel_revision(void)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000123{
Erik Andersend07ee462000-02-21 21:26:32 +0000124 struct utsname name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 int major = 0, minor = 0, patch = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000126
Erik Andersend07ee462000-02-21 21:26:32 +0000127 if (uname(&name) == -1) {
128 perror("cannot get system information");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 return (0);
130 }
Erik Andersend07ee462000-02-21 21:26:32 +0000131 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 return major * 65536 + minor * 256 + patch;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000133}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000134#endif /* BB_INIT */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000135
Eric Andersen10dc9d42000-06-26 10:45:52 +0000136
137
138#if defined BB_FREE || defined BB_INIT || defined BB_UNAME || defined BB_UPTIME
Eric Andersen10dc9d42000-06-26 10:45:52 +0000139_syscall1(int, sysinfo, struct sysinfo *, info);
140#endif /* BB_INIT */
141
Eric Andersena57ba4d2000-07-08 19:20:49 +0000142#if defined BB_MOUNT || defined BB_UMOUNT
Eric Andersena57ba4d2000-07-08 19:20:49 +0000143
144#ifndef __NR_umount2
145#define __NR_umount2 52
146#endif
147
148/* Include our own version of <sys/mount.h>, since libc5 doesn't
149 * know about umount2 */
150extern _syscall1(int, umount, const char *, special_file);
151extern _syscall2(int, umount2, const char *, special_file, int, flags);
152extern _syscall5(int, mount, const char *, special_file, const char *, dir,
153 const char *, fstype, unsigned long int, rwflag, const void *, data);
154#endif
155
Eric Andersenc86e5e62000-08-21 19:38:49 +0000156#if defined BB_INSMOD || defined BB_LSMOD
157#ifndef __NR_query_module
158#define __NR_query_module 167
159#endif
160_syscall5(int, query_module, const char *, name, int, which,
161 void *, buf, size_t, bufsize, size_t*, ret);
162#endif
Eric Andersen10dc9d42000-06-26 10:45:52 +0000163
164
Erik Andersen23dea9b2000-05-13 00:28:25 +0000165#if defined (BB_CP_MV) || defined (BB_DU)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000166
Erik Andersen029011b2000-03-04 21:19:32 +0000167#define HASH_SIZE 311 /* Should be prime */
168#define hash_inode(i) ((i) % HASH_SIZE)
169
170static ino_dev_hashtable_bucket_t *ino_dev_hashtable[HASH_SIZE];
171
172/*
173 * Return 1 if statbuf->st_ino && statbuf->st_dev are recorded in
174 * `ino_dev_hashtable', else return 0
175 *
176 * If NAME is a non-NULL pointer to a character pointer, and there is
177 * a match, then set *NAME to the value of the name slot in that
178 * bucket.
179 */
180int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name)
181{
182 ino_dev_hashtable_bucket_t *bucket;
183
184 bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)];
185 while (bucket != NULL) {
186 if ((bucket->ino == statbuf->st_ino) &&
187 (bucket->dev == statbuf->st_dev))
188 {
189 if (name) *name = bucket->name;
190 return 1;
191 }
192 bucket = bucket->next;
193 }
194 return 0;
195}
196
197/* Add statbuf to statbuf hash table */
198void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name)
199{
200 int i;
201 size_t s;
202 ino_dev_hashtable_bucket_t *bucket;
203
204 i = hash_inode(statbuf->st_ino);
205 s = name ? strlen(name) : 0;
Erik Andersen0d068a22000-03-21 22:32:57 +0000206 bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + s);
Erik Andersen029011b2000-03-04 21:19:32 +0000207 bucket->ino = statbuf->st_ino;
208 bucket->dev = statbuf->st_dev;
209 if (name)
210 strcpy(bucket->name, name);
211 else
212 bucket->name[0] = '\0';
213 bucket->next = ino_dev_hashtable[i];
214 ino_dev_hashtable[i] = bucket;
215}
216
217/* Clear statbuf hash table */
218void reset_ino_dev_hashtable(void)
219{
220 int i;
221 ino_dev_hashtable_bucket_t *bucket;
222
223 for (i = 0; i < HASH_SIZE; i++) {
224 while (ino_dev_hashtable[i] != NULL) {
225 bucket = ino_dev_hashtable[i]->next;
226 free(ino_dev_hashtable[i]);
227 ino_dev_hashtable[i] = bucket;
228 }
229 }
230}
231
Erik Andersen23dea9b2000-05-13 00:28:25 +0000232#endif /* BB_CP_MV || BB_DU */
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000233
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000234#if defined (BB_CP_MV) || defined (BB_DU) || defined (BB_LN)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000235/*
236 * Return TRUE if a fileName is a directory.
237 * Nonexistant files return FALSE.
238 */
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000239int isDirectory(const char *fileName, const int followLinks, struct stat *statBuf)
Eric Andersencc8ed391999-10-05 16:24:54 +0000240{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 int status;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000242 int didMalloc = 0;
243
244 if (statBuf == NULL) {
245 statBuf = (struct stat *)xmalloc(sizeof(struct stat));
246 ++didMalloc;
247 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000248
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 if (followLinks == TRUE)
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000250 status = stat(fileName, statBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000252 status = lstat(fileName, statBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000253
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000254 if (status < 0 || !(S_ISDIR(statBuf->st_mode))) {
255 status = FALSE;
256 }
257 else status = TRUE;
258
259 if (didMalloc) {
260 free(statBuf);
261 statBuf = NULL;
262 }
263 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000264}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000265#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000266
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000267#if defined (BB_AR)
268/*
269 * Copy readSize bytes between two file descriptors
270 */
271int copySubFile(int srcFd, int dstFd, size_t remaining)
272{
273 size_t size;
274 char buffer[BUFSIZ];
275
276 while (remaining > 0) {
277 if (remaining > BUFSIZ)
278 size = BUFSIZ;
279 else
280 size = remaining;
281 if (fullWrite(dstFd, buffer, fullRead(srcFd, buffer, size)) < size)
282 return(FALSE);
283 remaining -= size;
284 }
285 return (TRUE);
286}
287#endif
288
289
Erik Andersen23dea9b2000-05-13 00:28:25 +0000290#if defined (BB_CP_MV)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000291/*
Erik Andersen59b9e872000-05-10 05:05:45 +0000292 * Copy one file to another, while possibly preserving its modes, times, and
293 * modes. Returns TRUE if successful, or FALSE on a failure with an error
294 * message output. (Failure is not indicated if attributes cannot be set.)
295 * -Erik Andersen
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000296 */
297int
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298copyFile(const char *srcName, const char *destName,
Erik Andersen59b9e872000-05-10 05:05:45 +0000299 int setModes, int followLinks, int forceFlag)
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000300{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 int rfd;
302 int wfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 struct stat srcStatBuf;
305 struct stat dstStatBuf;
306 struct utimbuf times;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000307
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 if (followLinks == TRUE)
309 status = stat(srcName, &srcStatBuf);
310 else
311 status = lstat(srcName, &srcStatBuf);
Erik Andersenfac10d72000-02-07 05:29:42 +0000312
Erik Andersenfac10d72000-02-07 05:29:42 +0000313 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 perror(srcName);
315 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000316 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317
318 if (followLinks == TRUE)
319 status = stat(destName, &dstStatBuf);
320 else
321 status = lstat(destName, &dstStatBuf);
322
Erik Andersen59b9e872000-05-10 05:05:45 +0000323 if (status < 0 || forceFlag==TRUE) {
324 unlink(destName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 dstStatBuf.st_ino = -1;
326 dstStatBuf.st_dev = -1;
327 }
328
329 if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
330 (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
Matt Kraaid537a952000-07-14 01:51:25 +0000331 errorMsg("Copying file \"%s\" to itself\n", srcName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 return FALSE;
333 }
334
335 if (S_ISDIR(srcStatBuf.st_mode)) {
336 //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
337 /* Make sure the directory is writable */
338 status = mkdir(destName, 0777777 ^ umask(0));
339 if (status < 0 && errno != EEXIST) {
340 perror(destName);
341 return FALSE;
342 }
343 } else if (S_ISLNK(srcStatBuf.st_mode)) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000344 char link_val[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 int link_size;
346
347 //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000348 /* Warning: This could possibly truncate silently, to BUFSIZ chars */
349 link_size = readlink(srcName, &link_val[0], BUFSIZ);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350 if (link_size < 0) {
351 perror(srcName);
352 return FALSE;
353 }
354 link_val[link_size] = '\0';
355 status = symlink(link_val, destName);
356 if (status < 0) {
357 perror(destName);
358 return FALSE;
359 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000360#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 if (setModes == TRUE) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000362 /* Try to set owner, but fail silently like GNU cp */
363 lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 }
Erik Andersen8e759aa2000-01-29 05:52:40 +0000365#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 return TRUE;
367 } else if (S_ISFIFO(srcStatBuf.st_mode)) {
368 //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
369 if (mkfifo(destName, 0644) < 0) {
370 perror(destName);
371 return FALSE;
372 }
373 } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
374 || S_ISSOCK(srcStatBuf.st_mode)) {
375 //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
376 if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
377 perror(destName);
378 return FALSE;
379 }
380 } else if (S_ISREG(srcStatBuf.st_mode)) {
381 //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
382 rfd = open(srcName, O_RDONLY);
383 if (rfd < 0) {
384 perror(srcName);
385 return FALSE;
386 }
387
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000388 wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 srcStatBuf.st_mode);
390 if (wfd < 0) {
391 perror(destName);
392 close(rfd);
393 return FALSE;
394 }
395
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000396 if (copySubFile(rfd, wfd, srcStatBuf.st_size)==FALSE)
397 goto error_exit;
398
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 close(rfd);
400 if (close(wfd) < 0) {
401 return FALSE;
402 }
403 }
404
405 if (setModes == TRUE) {
406 /* This is fine, since symlinks never get here */
407 if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) {
408 perror(destName);
409 exit FALSE;
410 }
411 if (chmod(destName, srcStatBuf.st_mode) < 0) {
412 perror(destName);
413 exit FALSE;
414 }
415 times.actime = srcStatBuf.st_atime;
416 times.modtime = srcStatBuf.st_mtime;
417 if (utime(destName, &times) < 0) {
418 perror(destName);
419 exit FALSE;
420 }
421 }
422
Erik Andersenfac10d72000-02-07 05:29:42 +0000423 return TRUE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000424
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 error_exit:
426 perror(destName);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000427 close(rfd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428 close(wfd);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000429
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 return FALSE;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000431}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000432#endif /* BB_CP_MV */
Eric Andersencc8ed391999-10-05 16:24:54 +0000433
434
435
Erik Andersen23dea9b2000-05-13 00:28:25 +0000436#if defined BB_TAR || defined BB_LS
437
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000438#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
439#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
440
441/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
442static const mode_t SBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 0, 0, S_ISUID,
444 0, 0, S_ISGID,
445 0, 0, S_ISVTX
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000446};
447
448/* The 9 mode bits to test */
449static const mode_t MBIT[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 S_IRUSR, S_IWUSR, S_IXUSR,
451 S_IRGRP, S_IWGRP, S_IXGRP,
452 S_IROTH, S_IWOTH, S_IXOTH
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000453};
454
455#define MODE1 "rwxrwxrwx"
456#define MODE0 "---------"
457#define SMODE1 "..s..s..t"
458#define SMODE0 "..S..S..T"
459
Eric Andersencc8ed391999-10-05 16:24:54 +0000460/*
461 * Return the standard ls-like mode string from a file mode.
462 * This is static and so is overwritten on each call.
463 */
Eric Andersenf811e071999-10-09 00:25:00 +0000464const char *modeString(int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000465{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466 static char buf[12];
Eric Andersencc8ed391999-10-05 16:24:54 +0000467
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 int i;
469
470 buf[0] = TYPECHAR(mode);
471 for (i = 0; i < 9; i++) {
472 if (mode & SBIT[i])
473 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
474 else
475 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
476 }
477 return buf;
Eric Andersencc8ed391999-10-05 16:24:54 +0000478}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000479#endif /* BB_TAR || BB_LS */
Eric Andersencc8ed391999-10-05 16:24:54 +0000480
481
Pavel Roskin21ddb382000-07-17 19:31:54 +0000482#if defined BB_TAR || defined BB_AR
Eric Andersencc8ed391999-10-05 16:24:54 +0000483/*
Eric Andersen3d8dbe11999-11-09 01:51:02 +0000484 * Return the standard ls-like time string from a time_t
485 * This is static and so is overwritten on each call.
Eric Andersen17d49ef1999-10-06 20:25:32 +0000486 */
Eric Andersenf811e071999-10-09 00:25:00 +0000487const char *timeString(time_t timeVal)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000488{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 time_t now;
490 char *str;
491 static char buf[26];
Eric Andersen17d49ef1999-10-06 20:25:32 +0000492
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 time(&now);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000494
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 str = ctime(&timeVal);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000496
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 strcpy(buf, &str[4]);
498 buf[12] = '\0';
Eric Andersen17d49ef1999-10-06 20:25:32 +0000499
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
501 strcpy(&buf[7], &str[20]);
502 buf[11] = '\0';
503 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000504
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 return buf;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000506}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000507#endif /* BB_TAR || BB_AR */
Eric Andersen17d49ef1999-10-06 20:25:32 +0000508
Pavel Roskin21ddb382000-07-17 19:31:54 +0000509#if defined BB_TAR || defined BB_CP_MV || defined BB_AR
Eric Andersen17d49ef1999-10-06 20:25:32 +0000510/*
Eric Andersencc8ed391999-10-05 16:24:54 +0000511 * Write all of the supplied buffer out to a file.
512 * This does multiple writes as necessary.
513 * Returns the amount written, or -1 on an error.
514 */
Eric Andersenf811e071999-10-09 00:25:00 +0000515int fullWrite(int fd, const char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000516{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 int cc;
518 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000519
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000521
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 while (len > 0) {
523 cc = write(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000524
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 if (cc < 0)
526 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000527
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 buf += cc;
529 total += cc;
530 len -= cc;
531 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000532
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000534}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000535#endif /* BB_TAR || BB_CP_MV || BB_AR */
Eric Andersencc8ed391999-10-05 16:24:54 +0000536
537
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000538#if defined BB_TAR || defined BB_TAIL || defined BB_AR || defined BB_SH
Eric Andersencc8ed391999-10-05 16:24:54 +0000539/*
540 * Read all of the supplied buffer from a file.
541 * This does multiple reads as necessary.
542 * Returns the amount read, or -1 on an error.
543 * A short read is returned on an end of file.
544 */
Eric Andersenf811e071999-10-09 00:25:00 +0000545int fullRead(int fd, char *buf, int len)
Eric Andersencc8ed391999-10-05 16:24:54 +0000546{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 int cc;
548 int total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 total = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000551
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 while (len > 0) {
553 cc = read(fd, buf, len);
Eric Andersencc8ed391999-10-05 16:24:54 +0000554
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 if (cc < 0)
556 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000557
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 if (cc == 0)
559 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000560
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 buf += cc;
562 total += cc;
563 len -= cc;
564 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000565
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566 return total;
Eric Andersencc8ed391999-10-05 16:24:54 +0000567}
Pavel Roskincc8a66b2000-07-17 20:18:42 +0000568#endif /* BB_TAR || BB_TAIL || BB_AR || BB_SH */
Eric Andersencc8ed391999-10-05 16:24:54 +0000569
570
Erik Andersen23dea9b2000-05-13 00:28:25 +0000571#if defined (BB_CHMOD_CHOWN_CHGRP) \
572 || defined (BB_CP_MV) \
573 || defined (BB_FIND) \
574 || defined (BB_INSMOD) \
Pavel Roskin21ddb382000-07-17 19:31:54 +0000575 || defined (BB_LS) \
Erik Andersen23dea9b2000-05-13 00:28:25 +0000576 || defined (BB_RM) \
577 || defined (BB_TAR)
578
Eric Andersencc8ed391999-10-05 16:24:54 +0000579/*
Eric Andersen2b69c401999-10-05 22:58:32 +0000580 * Walk down all the directories under the specified
581 * location, and do something (something specified
582 * by the fileAction and dirAction function pointers).
Eric Andersenb7a1a751999-10-19 23:37:14 +0000583 *
Eric Andersen63a0e531999-10-22 05:12:14 +0000584 * Unfortunatly, while nftw(3) could replace this and reduce
585 * code size a bit, nftw() wasn't supported before GNU libc 2.1,
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000586 * and so isn't sufficiently portable to take over since glibc2.1
587 * is so stinking huge.
Eric Andersencc8ed391999-10-05 16:24:54 +0000588 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000589int recursiveAction(const char *fileName,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 int recurse, int followLinks, int depthFirst,
591 int (*fileAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000592 struct stat * statbuf,
593 void* userData),
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 int (*dirAction) (const char *fileName,
Erik Andersen3364d782000-03-28 00:58:14 +0000595 struct stat * statbuf,
596 void* userData),
597 void* userData)
Eric Andersencc8ed391999-10-05 16:24:54 +0000598{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 int status;
600 struct stat statbuf;
601 struct dirent *next;
Eric Andersen9d3aba71999-10-06 09:04:55 +0000602
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 if (followLinks == TRUE)
604 status = stat(fileName, &statbuf);
Eric Andersen50d63601999-11-09 01:47:36 +0000605 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606 status = lstat(fileName, &statbuf);
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000607
Eric Andersen2b69c401999-10-05 22:58:32 +0000608 if (status < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609#ifdef BB_DEBUG_PRINT_SCAFFOLD
610 fprintf(stderr,
611 "status=%d followLinks=%d TRUE=%d\n",
612 status, followLinks, TRUE);
613#endif
Eric Andersenbed30e91999-10-18 19:02:32 +0000614 perror(fileName);
Erik Andersenfac10d72000-02-07 05:29:42 +0000615 return FALSE;
Eric Andersenbed30e91999-10-18 19:02:32 +0000616 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617
618 if ((followLinks == FALSE) && (S_ISLNK(statbuf.st_mode))) {
619 if (fileAction == NULL)
620 return TRUE;
621 else
Erik Andersen3364d782000-03-28 00:58:14 +0000622 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000623 }
624
625 if (recurse == FALSE) {
626 if (S_ISDIR(statbuf.st_mode)) {
627 if (dirAction != NULL)
Erik Andersen3364d782000-03-28 00:58:14 +0000628 return (dirAction(fileName, &statbuf, userData));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 else
630 return TRUE;
631 }
632 }
633
634 if (S_ISDIR(statbuf.st_mode)) {
635 DIR *dir;
636
637 dir = opendir(fileName);
638 if (!dir) {
639 perror(fileName);
640 return FALSE;
641 }
642 if (dirAction != NULL && depthFirst == FALSE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000643 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 if (status == FALSE) {
645 perror(fileName);
646 return FALSE;
647 }
648 }
649 while ((next = readdir(dir)) != NULL) {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000650 char nextFile[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651
652 if ((strcmp(next->d_name, "..") == 0)
653 || (strcmp(next->d_name, ".") == 0)) {
654 continue;
655 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000656 if (strlen(fileName) + strlen(next->d_name) + 1 > BUFSIZ) {
Matt Kraaid537a952000-07-14 01:51:25 +0000657 errorMsg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 return FALSE;
659 }
Erik Andersenc3660502000-04-18 21:18:57 +0000660 memset(nextFile, 0, sizeof(nextFile));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000661 sprintf(nextFile, "%s/%s", fileName, next->d_name);
662 status =
663 recursiveAction(nextFile, TRUE, followLinks, depthFirst,
Erik Andersen3364d782000-03-28 00:58:14 +0000664 fileAction, dirAction, userData);
Matt Kraai0f8f7b82000-08-01 18:16:56 +0000665 if (status == FALSE) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 closedir(dir);
667 return FALSE;
668 }
669 }
670 status = closedir(dir);
671 if (status < 0) {
672 perror(fileName);
673 return FALSE;
674 }
675 if (dirAction != NULL && depthFirst == TRUE) {
Erik Andersen3364d782000-03-28 00:58:14 +0000676 status = dirAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 if (status == FALSE) {
678 perror(fileName);
679 return FALSE;
680 }
681 }
682 } else {
683 if (fileAction == NULL)
684 return TRUE;
685 else
Erik Andersen3364d782000-03-28 00:58:14 +0000686 return fileAction(fileName, &statbuf, userData);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 }
688 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000689}
690
Erik Andersen23dea9b2000-05-13 00:28:25 +0000691#endif /* BB_CHMOD_CHOWN_CHGRP || BB_CP_MV || BB_FIND || BB_LS || BB_INSMOD */
Eric Andersencc8ed391999-10-05 16:24:54 +0000692
Eric Andersenf6be9441999-10-13 21:12:06 +0000693
694
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000695#if defined (BB_TAR) || defined (BB_MKDIR)
Eric Andersenf6be9441999-10-13 21:12:06 +0000696/*
697 * Attempt to create the directories along the specified path, except for
698 * the final component. The mode is given for the final directory only,
699 * while all previous ones get default protections. Errors are not reported
700 * here, as failures to restore files can be reported later.
701 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702extern int createPath(const char *name, int mode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000703{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000704 char *cp;
705 char *cpOld;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000706 char buf[BUFSIZ + 1];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 int retVal = 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000708
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 strcpy(buf, name);
710 for (cp = buf; *cp == '/'; cp++);
711 cp = strchr(cp, '/');
712 while (cp) {
713 cpOld = cp;
714 cp = strchr(cp + 1, '/');
715 *cpOld = '\0';
716 retVal = mkdir(buf, cp ? 0777 : mode);
717 if (retVal != 0 && errno != EEXIST) {
718 perror(buf);
719 return FALSE;
720 }
721 *cpOld = '/';
Erik Andersen06936df2000-01-23 02:14:20 +0000722 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 return TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +0000724}
Erik Andersen23dea9b2000-05-13 00:28:25 +0000725#endif /* BB_TAR || BB_MKDIR */
Eric Andersenf6be9441999-10-13 21:12:06 +0000726
727
728
Pavel Roskin21ddb382000-07-17 19:31:54 +0000729#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) \
730 || defined (BB_MKFIFO) || defined (BB_MKNOD)
Eric Andersence8f3b91999-10-20 07:03:36 +0000731/* [ugoa]{+|-|=}[rwxst] */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000732
733
734
Erik Andersene49d5ec2000-02-08 19:58:47 +0000735extern int parse_mode(const char *s, mode_t * theMode)
Eric Andersenf6be9441999-10-13 21:12:06 +0000736{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 mode_t andMode =
738
739 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersence8f3b91999-10-20 07:03:36 +0000740 mode_t orMode = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 mode_t mode = 0;
742 mode_t groups = 0;
743 char type;
744 char c;
Eric Andersenf6be9441999-10-13 21:12:06 +0000745
746 do {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 for (;;) {
748 switch (c = *s++) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000749 case '\0':
Eric Andersence8f3b91999-10-20 07:03:36 +0000750 return -1;
Eric Andersenf6be9441999-10-13 21:12:06 +0000751 case 'u':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 groups |= S_ISUID | S_IRWXU;
Eric Andersenf6be9441999-10-13 21:12:06 +0000753 continue;
754 case 'g':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755 groups |= S_ISGID | S_IRWXG;
Eric Andersenf6be9441999-10-13 21:12:06 +0000756 continue;
757 case 'o':
758 groups |= S_IRWXO;
759 continue;
760 case 'a':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000761 groups |= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000762 continue;
763 case '+':
764 case '=':
765 case '-':
766 type = c;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 if (groups == 0) /* The default is "all" */
768 groups |=
769 S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
Eric Andersenf6be9441999-10-13 21:12:06 +0000770 break;
771 default:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000772 if (isdigit(c) && c >= '0' && c <= '7' &&
773 mode == 0 && groups == 0) {
Eric Andersenfa0540f1999-10-22 18:18:31 +0000774 *theMode = strtol(--s, NULL, 8);
Eric Andersenf6be9441999-10-13 21:12:06 +0000775 return (TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000776 } else
Eric Andersenf6be9441999-10-13 21:12:06 +0000777 return (FALSE);
778 }
779 break;
780 }
781
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 while ((c = *s++) != '\0') {
783 switch (c) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000784 case ',':
785 break;
786 case 'r':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787 mode |= S_IRUSR | S_IRGRP | S_IROTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000788 continue;
789 case 'w':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000790 mode |= S_IWUSR | S_IWGRP | S_IWOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000791 continue;
792 case 'x':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
Eric Andersenf6be9441999-10-13 21:12:06 +0000794 continue;
795 case 's':
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 mode |= S_IXGRP | S_ISUID | S_ISGID;
Eric Andersenf6be9441999-10-13 21:12:06 +0000797 continue;
798 case 't':
Eric Andersence8f3b91999-10-20 07:03:36 +0000799 mode |= 0;
Eric Andersenf6be9441999-10-13 21:12:06 +0000800 continue;
801 default:
Eric Andersence8f3b91999-10-20 07:03:36 +0000802 *theMode &= andMode;
803 *theMode |= orMode;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 return (TRUE);
Eric Andersenf6be9441999-10-13 21:12:06 +0000805 }
806 break;
807 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 switch (type) {
Eric Andersenf6be9441999-10-13 21:12:06 +0000809 case '=':
Eric Andersence8f3b91999-10-20 07:03:36 +0000810 andMode &= ~(groups);
Eric Andersenf6be9441999-10-13 21:12:06 +0000811 /* fall through */
812 case '+':
Eric Andersence8f3b91999-10-20 07:03:36 +0000813 orMode |= mode & groups;
Eric Andersenf6be9441999-10-13 21:12:06 +0000814 break;
815 case '-':
Eric Andersence8f3b91999-10-20 07:03:36 +0000816 andMode &= ~(mode & groups);
817 orMode &= andMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000818 break;
819 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 } while (c == ',');
Eric Andersence8f3b91999-10-20 07:03:36 +0000821 *theMode &= andMode;
822 *theMode |= orMode;
Eric Andersenf6be9441999-10-13 21:12:06 +0000823 return (TRUE);
824}
Eric Andersence8f3b91999-10-20 07:03:36 +0000825
826
Pavel Roskin21ddb382000-07-17 19:31:54 +0000827#endif
828/* BB_CHMOD_CHOWN_CHGRP || BB_MKDIR || BB_MKFIFO || BB_MKNOD */
Eric Andersenf6be9441999-10-13 21:12:06 +0000829
Eric Andersene674eb71999-10-19 20:52:57 +0000830
831
Erik Andersen23dea9b2000-05-13 00:28:25 +0000832
833
Pavel Roskin21ddb382000-07-17 19:31:54 +0000834#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
835 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
836 || defined BB_LOGNAME || defined BB_WHOAMI
Erik Andersen23dea9b2000-05-13 00:28:25 +0000837
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000838/* This parses entries in /etc/passwd and /etc/group. This is desirable
839 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
840 * increases target size and is often not needed or wanted for embedded
841 * systems.
842 *
843 * /etc/passwd entries look like this:
844 * root:x:0:0:root:/root:/bin/bash
845 * and /etc/group entries look like this:
846 * root:x:0:
847 *
848 * This uses buf as storage to hold things.
849 *
850 */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000851unsigned long my_getid(const char *filename, char *name, long id, long *gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000852{
Eric Andersenaa0765e1999-10-22 04:30:20 +0000853 FILE *file;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000854 char *rname, *start, *end, buf[128];
Eric Andersenfad04fd2000-07-14 06:49:52 +0000855 long rid;
856 long rgid = 0;
Eric Andersend23f9ba1999-10-20 19:18:15 +0000857
Erik Andersene49d5ec2000-02-08 19:58:47 +0000858 file = fopen(filename, "r");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000859 if (file == NULL) {
Eric Andersen3199bda2000-06-02 17:38:18 +0000860 /* Do not complain. It is ok for /etc/passwd and
Erik Andersen330fd2b2000-05-19 05:35:19 +0000861 * friends to be missing... */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 return (-1);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000863 }
Eric Andersend23f9ba1999-10-20 19:18:15 +0000864
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865 while (fgets(buf, 128, file) != NULL) {
Eric Andersend23f9ba1999-10-20 19:18:15 +0000866 if (buf[0] == '#')
867 continue;
868
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000869 /* username/group name */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000870 start = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000872 if (end == NULL)
873 continue;
874 *end = '\0';
875 rname = start;
876
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000877 /* password */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000878 start = end + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000879 end = strchr(start, ':');
Eric Andersend23f9ba1999-10-20 19:18:15 +0000880 if (end == NULL)
881 continue;
882
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000883 /* uid in passwd, gid in group */
Eric Andersend23f9ba1999-10-20 19:18:15 +0000884 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000885 rid = (unsigned long) strtol(start, &end, 10);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000886 if (end == start)
887 continue;
888
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000889 /* gid in passwd */
890 start = end + 1;
Erik Andersen5b46d792000-05-17 05:24:24 +0000891 rgid = (unsigned long) strtol(start, &end, 10);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000892
Eric Andersend23f9ba1999-10-20 19:18:15 +0000893 if (name) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 if (0 == strcmp(rname, name)) {
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000895 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 fclose(file);
897 return (rid);
898 }
899 }
900 if (id != -1 && id == rid) {
901 strncpy(name, rname, 8);
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000902 if (gid) *gid = rgid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 fclose(file);
904 return (TRUE);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000905 }
906 }
Eric Andersenaa0765e1999-10-22 04:30:20 +0000907 fclose(file);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000908 return (-1);
909}
910
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000911/* returns a uid given a username */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000912long my_getpwnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000913{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000914 return my_getid("/etc/passwd", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000915}
916
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000917/* returns a gid given a group name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000918long my_getgrnam(char *name)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000919{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000920 return my_getid("/etc/group", name, -1, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000921}
922
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000923/* gets a username given a uid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000924void my_getpwuid(char *name, long uid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000925{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000926 my_getid("/etc/passwd", name, uid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000927}
928
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000929/* gets a groupname given a gid */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000930void my_getgrgid(char *group, long gid)
Eric Andersend23f9ba1999-10-20 19:18:15 +0000931{
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000932 my_getid("/etc/group", group, gid, NULL);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000933}
934
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000935/* gets a gid given a user name */
Eric Andersenfad04fd2000-07-14 06:49:52 +0000936long my_getpwnamegid(char *name)
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000937{
Eric Andersenfad04fd2000-07-14 06:49:52 +0000938 long gid;
Erik Andersen94f5e0b2000-05-01 19:10:52 +0000939 my_getid("/etc/passwd", name, -1, &gid);
940 return gid;
941}
Eric Andersend23f9ba1999-10-20 19:18:15 +0000942
Pavel Roskin21ddb382000-07-17 19:31:54 +0000943#endif
944 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
945 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
Erik Andersen23dea9b2000-05-13 00:28:25 +0000946
947
Eric Andersenf86aee72000-07-06 17:20:46 +0000948#if (defined BB_CHVT) || (defined BB_DEALLOCVT) || (defined BB_SETKEYCODES)
Eric Andersen0460ff21999-10-25 23:32:44 +0000949
Eric Andersenbd22ed82000-07-08 18:55:24 +0000950/* From <linux/kd.h> */
951#define KDGKBTYPE 0x4B33 /* get keyboard type */
952#define KB_84 0x01
953#define KB_101 0x02 /* this is what we always answer */
Eric Andersen0460ff21999-10-25 23:32:44 +0000954
Erik Andersene49d5ec2000-02-08 19:58:47 +0000955int is_a_console(int fd)
Eric Andersen0460ff21999-10-25 23:32:44 +0000956{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000957 char arg;
958
959 arg = 0;
960 return (ioctl(fd, KDGKBTYPE, &arg) == 0
961 && ((arg == KB_101) || (arg == KB_84)));
Eric Andersen0460ff21999-10-25 23:32:44 +0000962}
963
Erik Andersene49d5ec2000-02-08 19:58:47 +0000964static int open_a_console(char *fnam)
Eric Andersen0460ff21999-10-25 23:32:44 +0000965{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000966 int fd;
967
968 /* try read-only */
969 fd = open(fnam, O_RDWR);
970
971 /* if failed, try read-only */
972 if (fd < 0 && errno == EACCES)
973 fd = open(fnam, O_RDONLY);
974
975 /* if failed, try write-only */
976 if (fd < 0 && errno == EACCES)
977 fd = open(fnam, O_WRONLY);
978
979 /* if failed, fail */
980 if (fd < 0)
981 return -1;
982
983 /* if not a console, fail */
984 if (!is_a_console(fd)) {
985 close(fd);
986 return -1;
987 }
988
989 /* success */
990 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +0000991}
992
993/*
994 * Get an fd for use with kbd/console ioctls.
995 * We try several things because opening /dev/console will fail
996 * if someone else used X (which does a chown on /dev/console).
997 *
998 * if tty_name is non-NULL, try this one instead.
999 */
1000
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001int get_console_fd(char *tty_name)
Eric Andersen0460ff21999-10-25 23:32:44 +00001002{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001003 int fd;
Eric Andersen0460ff21999-10-25 23:32:44 +00001004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005 if (tty_name) {
1006 if (-1 == (fd = open_a_console(tty_name)))
1007 return -1;
1008 else
1009 return fd;
1010 }
1011
1012 fd = open_a_console("/dev/tty");
1013 if (fd >= 0)
1014 return fd;
1015
1016 fd = open_a_console("/dev/tty0");
1017 if (fd >= 0)
1018 return fd;
1019
1020 fd = open_a_console("/dev/console");
1021 if (fd >= 0)
1022 return fd;
1023
1024 for (fd = 0; fd < 3; fd++)
1025 if (is_a_console(fd))
1026 return fd;
1027
Matt Kraaid537a952000-07-14 01:51:25 +00001028 errorMsg("Couldnt get a file descriptor referring to the console\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 return -1; /* total failure */
Eric Andersen0460ff21999-10-25 23:32:44 +00001030}
1031
1032
Eric Andersenf86aee72000-07-06 17:20:46 +00001033#endif /* BB_CHVT || BB_DEALLOCVT || BB_SETKEYCODES */
Eric Andersen0460ff21999-10-25 23:32:44 +00001034
1035
Erik Andersen23dea9b2000-05-13 00:28:25 +00001036#if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)
1037
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001038/* Do a case insensitive strstr() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001039char *stristr(char *haystack, const char *needle)
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001040{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001041 int len = strlen(needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001042
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043 while (*haystack) {
1044 if (!strncasecmp(haystack, needle, len))
1045 break;
1046 haystack++;
1047 }
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001048
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049 if (!(*haystack))
1050 haystack = NULL;
1051
1052 return haystack;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001053}
1054
Eric Andersenc1525e81999-10-29 00:07:31 +00001055/* This tries to find a needle in a haystack, but does so by
1056 * only trying to match literal strings (look 'ma, no regexps!)
1057 * This is short, sweet, and carries _very_ little baggage,
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001058 * unlike its beefier cousin in regexp.c
Eric Andersenc1525e81999-10-29 00:07:31 +00001059 * -Erik Andersen
1060 */
1061extern int find_match(char *haystack, char *needle, int ignoreCase)
1062{
1063
Erik Andersene49d5ec2000-02-08 19:58:47 +00001064 if (ignoreCase == FALSE)
1065 haystack = strstr(haystack, needle);
1066 else
1067 haystack = stristr(haystack, needle);
1068 if (haystack == NULL)
1069 return FALSE;
1070 return TRUE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001071}
1072
1073
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001074/* This performs substitutions after a string match has been found. */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001075extern int replace_match(char *haystack, char *needle, char *newNeedle,
1076 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +00001077{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 int foundOne = 0;
1079 char *where, *slider, *slider1, *oldhayStack;
Eric Andersenc1525e81999-10-29 00:07:31 +00001080
Erik Andersene49d5ec2000-02-08 19:58:47 +00001081 if (ignoreCase == FALSE)
1082 where = strstr(haystack, needle);
1083 else
1084 where = stristr(haystack, needle);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001085
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 if (strcmp(needle, newNeedle) == 0)
1087 return FALSE;
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001088
Erik Andersen0d068a22000-03-21 22:32:57 +00001089 oldhayStack = (char *) xmalloc((unsigned) (strlen(haystack)));
Erik Andersene49d5ec2000-02-08 19:58:47 +00001090 while (where != NULL) {
1091 foundOne++;
1092 strcpy(oldhayStack, haystack);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 for (slider = haystack, slider1 = oldhayStack; slider != where;
1094 slider++, slider1++);
1095 *slider = 0;
1096 haystack = strcat(haystack, newNeedle);
1097 slider1 += strlen(needle);
1098 haystack = strcat(haystack, slider1);
1099 where = strstr(slider, needle);
1100 }
1101 free(oldhayStack);
Eric Andersen24d8e7d1999-10-29 06:50:17 +00001102
Erik Andersene49d5ec2000-02-08 19:58:47 +00001103 if (foundOne > 0)
1104 return TRUE;
1105 else
1106 return FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +00001107}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001108
1109#endif /* ! BB_REGEXP && (BB_GREP || BB_SED) */
Eric Andersen29d2e361999-11-06 06:07:27 +00001110
1111
Eric Andersen9f16d612000-06-12 23:11:16 +00001112#if defined BB_FIND || defined BB_INSMOD
Eric Andersenb186d981999-12-03 09:19:54 +00001113/*
1114 * Routine to see if a text string is matched by a wildcard pattern.
1115 * Returns TRUE if the text is matched, or FALSE if it is not matched
1116 * or if the pattern is invalid.
1117 * * matches zero or more characters
1118 * ? matches a single character
1119 * [abc] matches 'a', 'b' or 'c'
1120 * \c quotes character c
1121 * Adapted from code written by Ingo Wilken, and
1122 * then taken from sash, Copyright (c) 1999 by David I. Bell
1123 * Permission is granted to use, distribute, or modify this source,
1124 * provided that this copyright notice remains intact.
1125 * Permission to distribute this code under the GPL has been granted.
1126 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127extern int check_wildcard_match(const char *text, const char *pattern)
Eric Andersenb186d981999-12-03 09:19:54 +00001128{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 const char *retryPat;
1130 const char *retryText;
1131 int ch;
1132 int found;
Erik Andersen632bb572000-04-18 22:09:06 +00001133 int len;
Eric Andersenb186d981999-12-03 09:19:54 +00001134
Erik Andersene49d5ec2000-02-08 19:58:47 +00001135 retryPat = NULL;
1136 retryText = NULL;
Eric Andersenb186d981999-12-03 09:19:54 +00001137
Erik Andersene49d5ec2000-02-08 19:58:47 +00001138 while (*text || *pattern) {
Eric Andersenb186d981999-12-03 09:19:54 +00001139 ch = *pattern++;
1140
Erik Andersene49d5ec2000-02-08 19:58:47 +00001141 switch (ch) {
1142 case '*':
1143 retryPat = pattern;
1144 retryText = text;
1145 break;
1146
1147 case '[':
1148 found = FALSE;
1149
1150 while ((ch = *pattern++) != ']') {
1151 if (ch == '\\')
1152 ch = *pattern++;
1153
1154 if (ch == '\0')
1155 return FALSE;
1156
1157 if (*text == ch)
1158 found = TRUE;
1159 }
Erik Andersen632bb572000-04-18 22:09:06 +00001160 len=strlen(text);
1161 if (found == FALSE && len!=0) {
1162 return FALSE;
1163 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001164 if (found == TRUE) {
Erik Andersen632bb572000-04-18 22:09:06 +00001165 if (strlen(pattern)==0 && len==1) {
1166 return TRUE;
1167 }
1168 if (len!=0) {
1169 text++;
1170 continue;
Erik Andersenc3660502000-04-18 21:18:57 +00001171 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 }
1173
1174 /* fall into next case */
1175
1176 case '?':
1177 if (*text++ == '\0')
1178 return FALSE;
1179
1180 break;
1181
1182 case '\\':
1183 ch = *pattern++;
1184
1185 if (ch == '\0')
1186 return FALSE;
1187
1188 /* fall into next case */
1189
1190 default:
1191 if (*text == ch) {
1192 if (*text)
1193 text++;
1194 break;
1195 }
1196
1197 if (*text) {
1198 pattern = retryPat;
1199 text = ++retryText;
1200 break;
1201 }
1202
Eric Andersenb186d981999-12-03 09:19:54 +00001203 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001204 }
1205
Erik Andersene49d5ec2000-02-08 19:58:47 +00001206 if (pattern == NULL)
1207 return FALSE;
Eric Andersenb186d981999-12-03 09:19:54 +00001208 }
1209
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210 return TRUE;
Eric Andersenb186d981999-12-03 09:19:54 +00001211}
Eric Andersen9f16d612000-06-12 23:11:16 +00001212#endif /* BB_FIND || BB_INSMOD */
Eric Andersenb186d981999-12-03 09:19:54 +00001213
1214
Eric Andersen29d2e361999-11-06 06:07:27 +00001215
1216
Erik Andersen23dea9b2000-05-13 00:28:25 +00001217#if defined BB_DF || defined BB_MTAB
Eric Andersen29d2e361999-11-06 06:07:27 +00001218/*
1219 * Given a block device, find the mount table entry if that block device
1220 * is mounted.
1221 *
1222 * Given any other file (or directory), find the mount table entry for its
1223 * filesystem.
1224 */
1225extern struct mntent *findMountPoint(const char *name, const char *table)
1226{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 struct stat s;
1228 dev_t mountDevice;
1229 FILE *mountTable;
1230 struct mntent *mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 if (stat(name, &s) != 0)
1233 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001234
Erik Andersene49d5ec2000-02-08 19:58:47 +00001235 if ((s.st_mode & S_IFMT) == S_IFBLK)
1236 mountDevice = s.st_rdev;
1237 else
1238 mountDevice = s.st_dev;
Eric Andersen29d2e361999-11-06 06:07:27 +00001239
1240
Erik Andersene49d5ec2000-02-08 19:58:47 +00001241 if ((mountTable = setmntent(table, "r")) == 0)
1242 return 0;
Eric Andersen29d2e361999-11-06 06:07:27 +00001243
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 while ((mountEntry = getmntent(mountTable)) != 0) {
1245 if (strcmp(name, mountEntry->mnt_dir) == 0
1246 || strcmp(name, mountEntry->mnt_fsname) == 0) /* String match. */
1247 break;
1248 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) /* Match the device. */
1249 break;
1250 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) /* Match the directory's mount point. */
1251 break;
1252 }
1253 endmntent(mountTable);
1254 return mountEntry;
Eric Andersen29d2e361999-11-06 06:07:27 +00001255}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001256#endif /* BB_DF || BB_MTAB */
Eric Andersen29d2e361999-11-06 06:07:27 +00001257
1258
1259
Erik Andersen23dea9b2000-05-13 00:28:25 +00001260#if defined BB_DD || defined BB_TAIL
Eric Andersen1792f8c1999-12-09 06:11:36 +00001261/*
1262 * Read a number with a possible multiplier.
1263 * Returns -1 if the number format is illegal.
1264 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001265extern long getNum(const char *cp)
Eric Andersen1792f8c1999-12-09 06:11:36 +00001266{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001267 long value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001268
Erik Andersene49d5ec2000-02-08 19:58:47 +00001269 if (!isDecimal(*cp))
1270 return -1;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001271
Erik Andersene49d5ec2000-02-08 19:58:47 +00001272 value = 0;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001273
Erik Andersene49d5ec2000-02-08 19:58:47 +00001274 while (isDecimal(*cp))
1275 value = value * 10 + *cp++ - '0';
Eric Andersen1792f8c1999-12-09 06:11:36 +00001276
Erik Andersene49d5ec2000-02-08 19:58:47 +00001277 switch (*cp++) {
1278 case 'M':
1279 case 'm': /* `tail' uses it traditionally */
1280 value *= 1048576;
1281 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001282
Erik Andersene49d5ec2000-02-08 19:58:47 +00001283 case 'k':
1284 value *= 1024;
1285 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001286
Erik Andersene49d5ec2000-02-08 19:58:47 +00001287 case 'b':
1288 value *= 512;
1289 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001290
Erik Andersene49d5ec2000-02-08 19:58:47 +00001291 case 'w':
1292 value *= 2;
1293 break;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001294
Erik Andersene49d5ec2000-02-08 19:58:47 +00001295 case '\0':
1296 return value;
1297
1298 default:
1299 return -1;
1300 }
1301
1302 if (*cp)
1303 return -1;
1304
Eric Andersen1792f8c1999-12-09 06:11:36 +00001305 return value;
Eric Andersen1792f8c1999-12-09 06:11:36 +00001306}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001307#endif /* BB_DD || BB_TAIL */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001308
Eric Andersen6805d5d1999-12-09 22:39:55 +00001309
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00001310#if defined BB_INIT || defined BB_SYSLOGD
Erik Andersen31cf8e02000-02-07 20:31:19 +00001311/* try to open up the specified device */
1312extern int device_open(char *device, int mode)
1313{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001314 int m, f, fd = -1;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001315
Erik Andersene49d5ec2000-02-08 19:58:47 +00001316 m = mode | O_NONBLOCK;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001317
Erik Andersene49d5ec2000-02-08 19:58:47 +00001318 /* Retry up to 5 times */
1319 for (f = 0; f < 5; f++)
1320 if ((fd = open(device, m, 0600)) >= 0)
1321 break;
1322 if (fd < 0)
1323 return fd;
1324 /* Reset original flags. */
1325 if (m != mode)
1326 fcntl(fd, F_SETFL, mode);
Erik Andersen31cf8e02000-02-07 20:31:19 +00001327 return fd;
Erik Andersen31cf8e02000-02-07 20:31:19 +00001328}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001329#endif /* BB_INIT BB_SYSLOGD */
Erik Andersen31cf8e02000-02-07 20:31:19 +00001330
1331
Erik Andersen23dea9b2000-05-13 00:28:25 +00001332#if defined BB_KILLALL || ( defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF ))
Erik Andersen269bba22000-03-08 14:50:47 +00001333#ifdef BB_FEATURE_USE_DEVPS_PATCH
Eric Andersenc674d702000-07-10 22:57:14 +00001334#include <linux/devps.h> /* For Erik's nifty devps device driver */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001335#endif
Erik Andersen246cc6d2000-03-07 07:41:42 +00001336
Erik Andersen23dea9b2000-05-13 00:28:25 +00001337#if defined BB_FEATURE_USE_DEVPS_PATCH
Erik Andersen246cc6d2000-03-07 07:41:42 +00001338/* findPidByName()
John Beppuf95ca971999-12-09 22:10:18 +00001339 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00001340 * This finds the pid of the specified process,
1341 * by using the /dev/ps device driver.
John Beppuf95ca971999-12-09 22:10:18 +00001342 *
Erik Andersen825aead2000-04-07 06:00:07 +00001343 * Returns a list of all matching PIDs
John Beppuf95ca971999-12-09 22:10:18 +00001344 */
Erik Andersen825aead2000-04-07 06:00:07 +00001345extern pid_t* findPidByName( char* pidName)
John Beppuf95ca971999-12-09 22:10:18 +00001346{
Erik Andersen825aead2000-04-07 06:00:07 +00001347 int fd, i, j;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001348 char device[] = "/dev/ps";
Erik Andersen246cc6d2000-03-07 07:41:42 +00001349 pid_t num_pids;
1350 pid_t* pid_array = NULL;
Erik Andersen825aead2000-04-07 06:00:07 +00001351 pid_t* pidList=NULL;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001352
1353 /* open device */
1354 fd = open(device, O_RDONLY);
1355 if (fd < 0)
1356 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1357
1358 /* Find out how many processes there are */
1359 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1360 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1361
1362 /* Allocate some memory -- grab a few extras just in case
1363 * some new processes start up while we wait. The kernel will
1364 * just ignore any extras if we give it too many, and will trunc.
1365 * the list if we give it too few. */
1366 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1367 pid_array[0] = num_pids+10;
1368
1369 /* Now grab the pid list */
1370 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1371 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1372
1373 /* Now search for a match */
Eric Andersenf86aee72000-07-06 17:20:46 +00001374 for (i=1, j=0; i<pid_array[0] ; i++) {
Erik Andersen0a64de92000-04-18 20:00:03 +00001375 char* p;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001376 struct pid_info info;
1377
1378 info.pid = pid_array[i];
1379 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1380 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1381
Erik Andersen0a64de92000-04-18 20:00:03 +00001382 /* Make sure we only match on the process name */
1383 p=info.command_line+1;
1384 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
1385 (p)++;
1386 }
1387 if (isspace(*(p)))
1388 *p='\0';
1389
Erik Andersenb9167cb2000-04-18 22:41:30 +00001390 if ((strstr(info.command_line, pidName) != NULL)
1391 && (strlen(pidName) == strlen(info.command_line))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001392 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1393 if (pidList==NULL)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001394 fatalError(memory_exhausted);
Erik Andersen825aead2000-04-07 06:00:07 +00001395 pidList[j++]=info.pid;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001396 }
1397 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001398 if (pidList)
1399 pidList[j]=0;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001400
1401 /* Free memory */
1402 free( pid_array);
1403
1404 /* close device */
1405 if (close (fd) != 0)
1406 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1407
Erik Andersen825aead2000-04-07 06:00:07 +00001408 return pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +00001409}
Erik Andersen269bba22000-03-08 14:50:47 +00001410#else /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen246cc6d2000-03-07 07:41:42 +00001411#if ! defined BB_FEATURE_USE_PROCFS
1412#error Sorry, I depend on the /proc filesystem right now.
1413#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001414
Erik Andersen246cc6d2000-03-07 07:41:42 +00001415/* findPidByName()
1416 *
1417 * This finds the pid of the specified process.
1418 * Currently, it's implemented by rummaging through
1419 * the proc filesystem.
1420 *
Erik Andersen825aead2000-04-07 06:00:07 +00001421 * Returns a list of all matching PIDs
Erik Andersen246cc6d2000-03-07 07:41:42 +00001422 */
Erik Andersen825aead2000-04-07 06:00:07 +00001423extern pid_t* findPidByName( char* pidName)
Erik Andersen246cc6d2000-03-07 07:41:42 +00001424{
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001425 DIR *dir;
1426 struct dirent *next;
Erik Andersen825aead2000-04-07 06:00:07 +00001427 pid_t* pidList=NULL;
1428 int i=0;
John Beppuf95ca971999-12-09 22:10:18 +00001429
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001430 dir = opendir("/proc");
1431 if (!dir)
1432 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1433
1434 while ((next = readdir(dir)) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001435 FILE *status;
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001436 char filename[256];
1437 char buffer[256];
Erik Andersenb9167cb2000-04-18 22:41:30 +00001438 char* p;
John Beppuf95ca971999-12-09 22:10:18 +00001439
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001440 /* If it isn't a number, we don't want it */
1441 if (!isdigit(*next->d_name))
1442 continue;
1443
Erik Andersen825aead2000-04-07 06:00:07 +00001444 /* Now open the status file */
Erik Andersen2ac2fae2000-03-07 23:32:17 +00001445 sprintf(filename, "/proc/%s/status", next->d_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001446 status = fopen(filename, "r");
1447 if (!status) {
1448 continue;
1449 }
1450 fgets(buffer, 256, status);
1451 fclose(status);
John Beppuf95ca971999-12-09 22:10:18 +00001452
Erik Andersen0a64de92000-04-18 20:00:03 +00001453 /* Make sure we only match on the process name */
1454 p=buffer+5; /* Skip the name */
1455 while ((p)++) {
1456 if (*p==0 || *p=='\n') {
1457 *p='\0';
1458 break;
1459 }
1460 }
1461 p=buffer+6; /* Skip the "Name:\t" */
1462
Erik Andersenb9167cb2000-04-18 22:41:30 +00001463 if ((strstr(p, pidName) != NULL)
1464 && (strlen(pidName) == strlen(p))) {
Erik Andersen825aead2000-04-07 06:00:07 +00001465 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1466 if (pidList==NULL)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001467 fatalError(memory_exhausted);
Erik Andersen825aead2000-04-07 06:00:07 +00001468 pidList[i++]=strtol(next->d_name, NULL, 0);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001469 }
John Beppuf95ca971999-12-09 22:10:18 +00001470 }
Erik Andersen0a64de92000-04-18 20:00:03 +00001471 if (pidList)
Erik Andersena6c75222000-04-18 00:00:52 +00001472 pidList[i]=0;
Erik Andersen825aead2000-04-07 06:00:07 +00001473 return pidList;
John Beppuf95ca971999-12-09 22:10:18 +00001474}
Erik Andersen269bba22000-03-08 14:50:47 +00001475#endif /* BB_FEATURE_USE_DEVPS_PATCH */
Erik Andersen23dea9b2000-05-13 00:28:25 +00001476#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
Eric Andersen1792f8c1999-12-09 06:11:36 +00001477
Eric Andersenae6eae02000-07-19 17:35:54 +00001478#ifndef DMALLOC
Erik Andersen0d068a22000-03-21 22:32:57 +00001479/* this should really be farmed out to libbusybox.a */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001480extern void *xmalloc(size_t size)
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001481{
Eric Andersenae6eae02000-07-19 17:35:54 +00001482 void *ptr = malloc(size);
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001483
Eric Andersenae6eae02000-07-19 17:35:54 +00001484 if (!ptr)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001485 fatalError(memory_exhausted);
Eric Andersenae6eae02000-07-19 17:35:54 +00001486 return ptr;
Erik Andersen3fe39dc2000-01-25 18:13:53 +00001487}
1488
Eric Andersen501c88b2000-07-28 15:14:45 +00001489extern void *xrealloc(void *old, size_t size)
Eric Andersenae6eae02000-07-19 17:35:54 +00001490{
1491 void *ptr = realloc(old, size);
1492 if (!ptr)
1493 fatalError(memory_exhausted);
1494 return ptr;
1495}
Eric Andersen501c88b2000-07-28 15:14:45 +00001496
1497extern void *xcalloc(size_t nmemb, size_t size)
1498{
1499 void *ptr = calloc(nmemb, size);
1500 if (!ptr)
1501 fatalError(memory_exhausted);
1502 return ptr;
1503}
Eric Andersenae6eae02000-07-19 17:35:54 +00001504#endif
1505
Matt Kraai88cc3052000-08-26 16:52:28 +00001506#if defined BB_FEATURE_NFSMOUNT || defined BB_SH
Eric Andersenae6eae02000-07-19 17:35:54 +00001507# ifndef DMALLOC
Eric Andersenda1d1e72000-07-10 23:39:44 +00001508extern char * xstrdup (const char *s) {
1509 char *t;
1510
1511 if (s == NULL)
1512 return NULL;
1513
1514 t = strdup (s);
1515
1516 if (t == NULL)
Matt Kraaibe84cd42000-07-12 17:02:35 +00001517 fatalError(memory_exhausted);
Eric Andersenda1d1e72000-07-10 23:39:44 +00001518
1519 return t;
1520}
Eric Andersenae6eae02000-07-19 17:35:54 +00001521# endif
Matt Kraai88cc3052000-08-26 16:52:28 +00001522#endif
Eric Andersenda1d1e72000-07-10 23:39:44 +00001523
Matt Kraai88cc3052000-08-26 16:52:28 +00001524#if defined BB_FEATURE_NFSMOUNT
Eric Andersenda1d1e72000-07-10 23:39:44 +00001525extern char * xstrndup (const char *s, int n) {
1526 char *t;
1527
1528 if (s == NULL)
1529 fatalError("xstrndup bug");
1530
1531 t = xmalloc(n+1);
1532 strncpy(t,s,n);
1533 t[n] = 0;
1534
1535 return t;
1536}
1537#endif
1538
1539
Erik Andersen23dea9b2000-05-13 00:28:25 +00001540#if (__GLIBC__ < 2) && (defined BB_SYSLOGD || defined BB_INIT)
Erik Andersen7dc16072000-01-04 01:10:25 +00001541extern int vdprintf(int d, const char *format, va_list ap)
1542{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001543 char buf[BUF_SIZE];
1544 int len;
Erik Andersen7dc16072000-01-04 01:10:25 +00001545
Erik Andersene49d5ec2000-02-08 19:58:47 +00001546 len = vsprintf(buf, format, ap);
1547 return write(d, buf, len);
Erik Andersen7dc16072000-01-04 01:10:25 +00001548}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001549#endif /* BB_SYSLOGD */
Erik Andersen7dc16072000-01-04 01:10:25 +00001550
Erik Andersen23dea9b2000-05-13 00:28:25 +00001551#if defined BB_FEATURE_MOUNT_LOOP
Erik Andersen5cbdd712000-01-26 20:06:48 +00001552extern int del_loop(const char *device)
1553{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001554 int fd;
Erik Andersen5cbdd712000-01-26 20:06:48 +00001555
Erik Andersene49d5ec2000-02-08 19:58:47 +00001556 if ((fd = open(device, O_RDONLY)) < 0) {
1557 perror(device);
1558 return (FALSE);
1559 }
1560 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
1561 perror("ioctl: LOOP_CLR_FD");
1562 return (FALSE);
1563 }
1564 close(fd);
1565 return (TRUE);
Erik Andersen5cbdd712000-01-26 20:06:48 +00001566}
Erik Andersende7965c2000-01-26 23:49:21 +00001567
Erik Andersene49d5ec2000-02-08 19:58:47 +00001568extern int set_loop(const char *device, const char *file, int offset,
1569 int *loopro)
Erik Andersende7965c2000-01-26 23:49:21 +00001570{
1571 struct loop_info loopinfo;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001572 int fd, ffd, mode;
1573
Erik Andersende7965c2000-01-26 23:49:21 +00001574 mode = *loopro ? O_RDONLY : O_RDWR;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001575 if ((ffd = open(file, mode)) < 0 && !*loopro
1576 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
1577 perror(file);
1578 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001579 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001580 if ((fd = open(device, mode)) < 0) {
1581 close(ffd);
1582 perror(device);
1583 return 1;
Erik Andersende7965c2000-01-26 23:49:21 +00001584 }
1585 *loopro = (mode == O_RDONLY);
1586
1587 memset(&loopinfo, 0, sizeof(loopinfo));
1588 strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001589 loopinfo.lo_name[LO_NAME_SIZE - 1] = 0;
Erik Andersende7965c2000-01-26 23:49:21 +00001590
1591 loopinfo.lo_offset = offset;
1592
1593 loopinfo.lo_encrypt_key_size = 0;
1594 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
1595 perror("ioctl: LOOP_SET_FD");
1596 close(fd);
1597 close(ffd);
1598 return 1;
1599 }
1600 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
1601 (void) ioctl(fd, LOOP_CLR_FD, 0);
1602 perror("ioctl: LOOP_SET_STATUS");
1603 close(fd);
1604 close(ffd);
1605 return 1;
1606 }
1607 close(fd);
1608 close(ffd);
1609 return 0;
1610}
1611
Erik Andersene49d5ec2000-02-08 19:58:47 +00001612extern char *find_unused_loop_device(void)
Erik Andersende7965c2000-01-26 23:49:21 +00001613{
1614 char dev[20];
1615 int i, fd;
1616 struct stat statbuf;
1617 struct loop_info loopinfo;
1618
Erik Andersene49d5ec2000-02-08 19:58:47 +00001619 for (i = 0; i <= 7; i++) {
1620 sprintf(dev, "/dev/loop%d", i);
1621 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
1622 if ((fd = open(dev, O_RDONLY)) >= 0) {
1623 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) == -1) {
1624 if (errno == ENXIO) { /* probably free */
1625 close(fd);
1626 return strdup(dev);
1627 }
1628 }
1629 close(fd);
Erik Andersende7965c2000-01-26 23:49:21 +00001630 }
Erik Andersende7965c2000-01-26 23:49:21 +00001631 }
Erik Andersende7965c2000-01-26 23:49:21 +00001632 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001633 return NULL;
Erik Andersende7965c2000-01-26 23:49:21 +00001634}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001635#endif /* BB_FEATURE_MOUNT_LOOP */
Erik Andersende7965c2000-01-26 23:49:21 +00001636
Erik Andersen23dea9b2000-05-13 00:28:25 +00001637#if defined BB_MOUNT || defined BB_DF || ( defined BB_UMOUNT && ! defined BB_MTAB)
Erik Andersenec5bd902000-03-22 07:12:05 +00001638extern int find_real_root_device_name(char* name)
Erik Andersen016ffe92000-03-22 00:58:54 +00001639{
Erik Andersen016ffe92000-03-22 00:58:54 +00001640 DIR *dir;
1641 struct dirent *entry;
1642 struct stat statBuf, rootStat;
1643 char fileName[BUFSIZ];
1644
Erik Andersenec5bd902000-03-22 07:12:05 +00001645 if (stat("/", &rootStat) != 0) {
1646 errorMsg("could not stat '/'\n");
1647 return( FALSE);
1648 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001649
Erik Andersenec5bd902000-03-22 07:12:05 +00001650 dir = opendir("/dev");
1651 if (!dir) {
1652 errorMsg("could not open '/dev'\n");
1653 return( FALSE);
1654 }
Erik Andersen016ffe92000-03-22 00:58:54 +00001655
1656 while((entry = readdir(dir)) != NULL) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001657
Erik Andersen016ffe92000-03-22 00:58:54 +00001658 /* Must skip ".." since that is "/", and so we
1659 * would get a false positive on ".." */
Erik Andersen016ffe92000-03-22 00:58:54 +00001660 if (strcmp(entry->d_name, "..") == 0)
1661 continue;
1662
Eric Andersenf9ca6532000-08-11 17:45:02 +00001663 snprintf( fileName, strlen(name)+1, "/dev/%s", entry->d_name);
Erik Andersen016ffe92000-03-22 00:58:54 +00001664
1665 if (stat(fileName, &statBuf) != 0)
1666 continue;
Erik Andersenec5bd902000-03-22 07:12:05 +00001667 /* Some char devices have the same dev_t as block
1668 * devices, so make sure this is a block device */
1669 if (! S_ISBLK(statBuf.st_mode))
1670 continue;
Erik Andersen016ffe92000-03-22 00:58:54 +00001671 if (statBuf.st_rdev == rootStat.st_rdev) {
Erik Andersenec5bd902000-03-22 07:12:05 +00001672 strcpy(name, fileName);
1673 return ( TRUE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001674 }
1675 }
1676
Erik Andersenec5bd902000-03-22 07:12:05 +00001677 return( FALSE);
Erik Andersen016ffe92000-03-22 00:58:54 +00001678}
Erik Andersen23dea9b2000-05-13 00:28:25 +00001679#endif
Erik Andersen016ffe92000-03-22 00:58:54 +00001680
Mark Whitley1ca41772000-06-28 22:15:26 +00001681
1682/* get_line_from_file() - This function reads an entire line from a text file
1683 * up to a newline. It returns a malloc'ed char * which must be stored and
1684 * free'ed by the caller. */
1685extern char *get_line_from_file(FILE *file)
John Beppu5a728cf2000-04-17 04:22:09 +00001686{
Mark Whitley9a8243a2000-07-10 19:31:31 +00001687 static const int GROWBY = 80; /* how large we will grow strings by */
1688
Mark Whitley1ca41772000-06-28 22:15:26 +00001689 int ch;
1690 int idx = 0;
1691 char *linebuf = NULL;
1692 int linebufsz = 0;
John Beppu5a728cf2000-04-17 04:22:09 +00001693
Mark Whitley1ca41772000-06-28 22:15:26 +00001694 while (1) {
1695 ch = fgetc(file);
1696 if (ch == EOF)
1697 break;
1698 /* grow the line buffer as necessary */
Eric Andersen161cf932000-07-09 02:38:01 +00001699 if (idx > linebufsz-2)
Mark Whitley1ca41772000-06-28 22:15:26 +00001700 linebuf = realloc(linebuf, linebufsz += GROWBY);
1701 linebuf[idx++] = (char)ch;
1702 if ((char)ch == '\n')
1703 break;
1704 }
John Beppu5a728cf2000-04-17 04:22:09 +00001705
Mark Whitley1ca41772000-06-28 22:15:26 +00001706 if (idx == 0)
1707 return NULL;
John Beppu5a728cf2000-04-17 04:22:09 +00001708
Mark Whitley1ca41772000-06-28 22:15:26 +00001709 linebuf[idx] = 0;
1710 return linebuf;
John Beppu5a728cf2000-04-17 04:22:09 +00001711}
Erik Andersen016ffe92000-03-22 00:58:54 +00001712
Pavel Roskincc8a66b2000-07-17 20:18:42 +00001713#if defined BB_CAT || defined BB_LSMOD
1714extern void print_file(FILE *file)
1715{
1716 int c;
1717
1718 while ((c = getc(file)) != EOF)
1719 putc(c, stdout);
1720 fclose(file);
1721 fflush(stdout);
1722}
1723
1724extern int print_file_by_name(char *filename)
1725{
1726 FILE *file;
1727 file = fopen(filename, "r");
1728 if (file == NULL) {
1729 return FALSE;
1730 }
1731 print_file(file);
1732 return TRUE;
1733}
1734#endif /* BB_CAT || BB_LSMOD */
1735
Eric Andersenf7cf2f72000-07-05 17:26:35 +00001736#if defined BB_ECHO || defined BB_TR
1737char process_escape_sequence(char **ptr)
1738{
1739 char c;
1740
1741 switch (c = *(*ptr)++) {
1742 case 'a':
1743 c = '\a';
1744 break;
1745 case 'b':
1746 c = '\b';
1747 break;
1748 case 'f':
1749 c = '\f';
1750 break;
1751 case 'n':
1752 c = '\n';
1753 break;
1754 case 't':
1755 c = '\t';
1756 break;
1757 case 'v':
1758 c = '\v';
1759 break;
1760 case '\\':
1761 c = '\\';
1762 break;
1763 case '0': case '1': case '2': case '3':
1764 case '4': case '5': case '6': case '7':
1765 c -= '0';
1766 if ('0' <= **ptr && **ptr <= '7') {
1767 c = c * 8 + (*(*ptr)++ - '0');
1768 if ('0' <= **ptr && **ptr <= '7')
1769 c = c * 8 + (*(*ptr)++ - '0');
1770 }
1771 break;
1772 default:
1773 (*ptr)--;
1774 c = '\\';
1775 break;
1776 }
1777 return c;
1778}
1779#endif
1780
Eric Andersen0b4551f2000-07-10 16:44:03 +00001781#if defined BB_BASENAME || defined BB_LN
1782char *get_last_path_component(char *path)
1783{
1784 char *s=path+strlen(path)-1;
1785
1786 /* strip trailing slashes */
1787 while (s && *s == '/') {
1788 *s-- = '\0';
1789 }
1790
1791 /* find last component */
1792 s = strrchr(path, '/');
1793 if (s==NULL) return path;
1794 else return s+1;
1795}
1796#endif
1797
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001798#if defined BB_GREP || defined BB_SED
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001799void xregcomp(regex_t *preg, const char *regex, int cflags)
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001800{
1801 int ret;
1802 if ((ret = regcomp(preg, regex, cflags)) != 0) {
1803 int errmsgsz = regerror(ret, preg, NULL, 0);
1804 char *errmsg = xmalloc(errmsgsz);
1805 regerror(ret, preg, errmsg, errmsgsz);
Mark Whitleyc41e8c82000-07-12 23:35:21 +00001806 fatalError("bb_regcomp: %s\n", errmsg);
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001807 }
Mark Whitleydf5f6ba2000-07-11 16:53:56 +00001808}
1809#endif
1810
Eric Andersencc8ed391999-10-05 16:24:54 +00001811/* END CODE */
Erik Andersen029011b2000-03-04 21:19:32 +00001812/*
1813Local Variables:
1814c-file-style: "linux"
1815c-basic-offset: 4
1816tab-width: 4
1817End:
1818*/