* my_getid was leaking file descriptors, causing 'ls -l' on long
[oweals/busybox.git] / utility.c
1 /*
2  * Utility routines.
3  *
4  * Copyright (C) tons of folks.  Tracking down who wrote what
5  * isn't something I'm going to worry about...  If you wrote something
6  * here, please feel free to acknowledge your work.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 
23  * Permission has been granted to redistribute this code under the GPL.
24  *
25  */
26
27 #include "internal.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <time.h>
34 #include <utime.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <ctype.h>
38
39 #if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
40 #  if defined BB_FEATURE_USE_PROCFS
41 const char mtab_file[] = "/proc/mounts";
42 #  else
43 #    if defined BB_MTAB
44 const char mtab_file[] = "/etc/mtab";
45 #    else
46 #      error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS
47 #    endif
48 #  endif
49 #endif
50
51
52 extern void usage(const char *usage)
53 {
54     fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
55     fprintf(stderr, "Usage: %s\n", usage);
56     exit(FALSE);
57 }
58
59
60 #if defined (BB_INIT) || defined (BB_PS)
61
62 #if ! defined BB_FEATURE_USE_PROCFS
63 #error Sorry, I depend on the /proc filesystem right now.
64 #endif
65 /* Returns kernel version encoded as major*65536 + minor*256 + patch,
66  * so, for example,  to check if the kernel is greater than 2.2.11:
67  *      if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
68  */
69 int
70 get_kernel_revision()
71 {
72   FILE *file;
73   int major=0, minor=0, patch=0;
74   char* filename="/proc/sys/kernel/osrelease";
75
76   file = fopen(filename,"r");
77   if (file == NULL) {
78     /* bummer, /proc must not be mounted... */
79     return( 0);
80   }
81   fscanf(file,"%d.%d.%d",&major,&minor,&patch);
82   fclose(file);
83   return major*65536 + minor*256 + patch;
84 }
85
86 #endif
87
88
89
90 #if defined (BB_CP) || defined (BB_MV)
91 /*
92  * Return TRUE if a fileName is a directory.
93  * Nonexistant files return FALSE.
94  */
95 int isDirectory(const char *name)
96 {
97     struct stat statBuf;
98
99     if (stat(name, &statBuf) < 0)
100         return FALSE;
101     if (S_ISDIR(statBuf.st_mode))
102         return TRUE;
103     return(FALSE);
104 }
105
106
107 /*
108  * Copy one file to another, while possibly preserving its modes, times,
109  * and modes.  Returns TRUE if successful, or FALSE on a failure with an
110  * error message output.  (Failure is not indicted if the attributes cannot
111  * be set.)
112  *  -Erik Andersen
113  */
114 int
115 copyFile( const char *srcName, const char *destName, 
116          int setModes, int followLinks)
117 {
118     int rfd;
119     int wfd;
120     int rcc;
121     int result;
122     char buf[BUF_SIZE];
123     struct stat srcStatBuf;
124     struct stat dstStatBuf;
125     struct utimbuf times;
126
127     if (followLinks == FALSE)
128         result = stat(srcName, &srcStatBuf);
129     else 
130         result = lstat(srcName, &srcStatBuf);
131     if (result < 0) {
132         perror(srcName);
133         return FALSE;
134     }
135
136     if (followLinks == FALSE)
137         result = stat(destName, &dstStatBuf);
138     else 
139         result = lstat(destName, &dstStatBuf);
140     if (result < 0) {
141         dstStatBuf.st_ino = -1;
142         dstStatBuf.st_dev = -1;
143     }
144
145     if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
146         (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
147         fprintf(stderr, "Copying file \"%s\" to itself\n", srcName);
148         return FALSE;
149     }
150
151     if (S_ISDIR(srcStatBuf.st_mode)) {
152         //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
153         /* Make sure the directory is writable */
154         if (mkdir(destName, 0777777 ^ umask(0))) {
155             perror(destName);
156             return (FALSE);
157         }
158     } else if (S_ISLNK(srcStatBuf.st_mode)) {
159         char *link_val;
160         int link_size;
161
162         //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
163         link_val = (char *) alloca(PATH_MAX + 2);
164         link_size = readlink(srcName, link_val, PATH_MAX + 1);
165         if (link_size < 0) {
166             perror(srcName);
167             return (FALSE);
168         }
169         link_val[link_size] = '\0';
170         link_size = symlink(link_val, destName);
171         if (link_size != 0) {
172             perror(destName);
173             return (FALSE);
174         }
175     } else if (S_ISFIFO(srcStatBuf.st_mode)) {
176         //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
177         if (mkfifo(destName, 644)) {
178             perror(destName);
179             return (FALSE);
180         }
181     } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) 
182             || S_ISSOCK (srcStatBuf.st_mode)) {
183         //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
184         if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) {
185             perror(destName);
186             return (FALSE);
187         }
188     } else if (S_ISREG(srcStatBuf.st_mode)) {
189         //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
190         rfd = open(srcName, O_RDONLY);
191         if (rfd < 0) {
192             perror(srcName);
193             return FALSE;
194         }
195
196         wfd = creat(destName, srcStatBuf.st_mode);
197         if (wfd < 0) {
198             perror(destName);
199             close(rfd);
200             return FALSE;
201         }
202
203         while ((rcc = read(rfd, buf, sizeof(buf))) > 0) {
204             if (fullWrite(wfd, buf, rcc) < 0)
205                 goto error_exit;
206         }
207         if (rcc < 0) {
208             goto error_exit;
209         }
210
211         close(rfd);
212         if (close(wfd) < 0) {
213             return FALSE;
214         }
215     }
216
217     if (setModes == TRUE) {
218         //fprintf(stderr, "Setting permissions for %s\n", destName);
219         chmod(destName, srcStatBuf.st_mode);
220         if (followLinks == TRUE)
221             chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
222         else
223             lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid);
224
225         times.actime = srcStatBuf.st_atime;
226         times.modtime = srcStatBuf.st_mtime;
227
228         utime(destName, &times);
229     }
230
231     return TRUE;
232
233
234   error_exit:
235     perror(destName);
236     close(rfd);
237     close(wfd);
238
239     return FALSE;
240 }
241 #endif
242
243
244
245 #if defined BB_TAR || defined BB_LS
246
247 #define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
248 #define TYPECHAR(mode)  ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
249
250 /* The special bits. If set, display SMODE0/1 instead of MODE0/1 */
251 static const mode_t SBIT[] = {
252     0, 0, S_ISUID,
253     0, 0, S_ISGID,
254     0, 0, S_ISVTX
255 };
256
257 /* The 9 mode bits to test */
258 static const mode_t MBIT[] = {
259     S_IRUSR, S_IWUSR, S_IXUSR,
260     S_IRGRP, S_IWGRP, S_IXGRP,
261     S_IROTH, S_IWOTH, S_IXOTH
262 };
263
264 #define MODE1  "rwxrwxrwx"
265 #define MODE0  "---------"
266 #define SMODE1 "..s..s..t"
267 #define SMODE0 "..S..S..T"
268
269 /*
270  * Return the standard ls-like mode string from a file mode.
271  * This is static and so is overwritten on each call.
272  */
273 const char *modeString(int mode)
274 {
275     static char buf[12];
276
277     int i;
278     buf[0] = TYPECHAR(mode);
279     for (i=0; i<9; i++) {
280         if (mode & SBIT[i])
281             buf[i+1] = (mode & MBIT[i])? 
282                 SMODE1[i] : SMODE0[i];
283         else
284             buf[i+1] = (mode & MBIT[i])? 
285                 MODE1[i] : MODE0[i];
286     }
287     return buf;
288 }
289 #endif
290
291
292 #ifdef BB_TAR
293 /*
294  * Return the standard ls-like time string from a time_t
295  * This is static and so is overwritten on each call.
296  */
297 const char *timeString(time_t timeVal)
298 {
299     time_t now;
300     char *str;
301     static char buf[26];
302
303     time(&now);
304
305     str = ctime(&timeVal);
306
307     strcpy(buf, &str[4]);
308     buf[12] = '\0';
309
310     if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
311         strcpy(&buf[7], &str[20]);
312         buf[11] = '\0';
313     }
314
315     return buf;
316 }
317
318 /*
319  * Write all of the supplied buffer out to a file.
320  * This does multiple writes as necessary.
321  * Returns the amount written, or -1 on an error.
322  */
323 int fullWrite(int fd, const char *buf, int len)
324 {
325     int cc;
326     int total;
327
328     total = 0;
329
330     while (len > 0) {
331         cc = write(fd, buf, len);
332
333         if (cc < 0)
334             return -1;
335
336         buf += cc;
337         total += cc;
338         len -= cc;
339     }
340
341     return total;
342 }
343
344
345 /*
346  * Read all of the supplied buffer from a file.
347  * This does multiple reads as necessary.
348  * Returns the amount read, or -1 on an error.
349  * A short read is returned on an end of file.
350  */
351 int fullRead(int fd, char *buf, int len)
352 {
353     int cc;
354     int total;
355
356     total = 0;
357
358     while (len > 0) {
359         cc = read(fd, buf, len);
360
361         if (cc < 0)
362             return -1;
363
364         if (cc == 0)
365             break;
366
367         buf += cc;
368         total += cc;
369         len -= cc;
370     }
371
372     return total;
373 }
374 #endif
375
376
377 #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS)
378 /*
379  * Walk down all the directories under the specified 
380  * location, and do something (something specified
381  * by the fileAction and dirAction function pointers).
382  *
383  * Unfortunatly, while nftw(3) could replace this and reduce 
384  * code size a bit, nftw() wasn't supported before GNU libc 2.1, 
385  * and so isn't sufficiently portable to take over since glibc2.1
386  * is so stinking huge.
387  */
388 int
389 recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst,
390                 int (*fileAction) (const char *fileName, struct stat* statbuf),
391                 int (*dirAction) (const char *fileName, struct stat* statbuf))
392 {
393     int status;
394     struct stat statbuf;
395     struct dirent *next;
396
397     if (followLinks == TRUE)
398         status = stat(fileName, &statbuf);
399     else
400         status = lstat(fileName, &statbuf);
401
402     if (status < 0) {
403         perror(fileName);
404         return (FALSE);
405     }
406
407     if ( (followLinks == FALSE) && (S_ISLNK(statbuf.st_mode)) ) {
408         if (fileAction == NULL)
409             return (TRUE);
410         else
411             return (fileAction(fileName, &statbuf));
412     }
413
414     if (recurse == FALSE) {
415         if (S_ISDIR(statbuf.st_mode)) {
416             if (dirAction != NULL)
417                 return (dirAction(fileName, &statbuf));
418             else
419                 return (TRUE);
420         } 
421     }
422
423     if (S_ISDIR(statbuf.st_mode)) {
424         DIR *dir;
425         dir = opendir(fileName);
426         if (!dir) {
427             perror(fileName);
428             return (FALSE);
429         }
430         if (dirAction != NULL && depthFirst == FALSE) {
431             status = dirAction(fileName, &statbuf);
432             if (status == FALSE) {
433                 perror(fileName);
434                 return (FALSE);
435             }
436         }
437         while ((next = readdir(dir)) != NULL) {
438             char nextFile[NAME_MAX];
439             if ((strcmp(next->d_name, "..") == 0)
440                 || (strcmp(next->d_name, ".") == 0)) {
441                 continue;
442             }
443             sprintf(nextFile, "%s/%s", fileName, next->d_name);
444             status =
445                 recursiveAction(nextFile, TRUE, followLinks, depthFirst, 
446                         fileAction, dirAction);
447             if (status < 0) {
448                 closedir(dir);
449                 return (FALSE);
450             }
451         }
452         status = closedir(dir);
453         if (status < 0) {
454             perror(fileName);
455             return (FALSE);
456         }
457         if (dirAction != NULL && depthFirst == TRUE) {
458             status = dirAction(fileName, &statbuf);
459             if (status == FALSE) {
460                 perror(fileName);
461                 return (FALSE);
462             }
463         }
464     } else {
465         if (fileAction == NULL)
466             return (TRUE);
467         else
468             return (fileAction(fileName, &statbuf));
469     }
470     return (TRUE);
471 }
472
473 #endif
474
475
476
477 #if defined (BB_TAR) || defined (BB_MKDIR)
478 /*
479  * Attempt to create the directories along the specified path, except for
480  * the final component.  The mode is given for the final directory only,
481  * while all previous ones get default protections.  Errors are not reported
482  * here, as failures to restore files can be reported later.
483  */
484 extern void createPath (const char *name, int mode)
485 {
486     char *cp;
487     char *cpOld;
488     char buf[NAME_MAX];
489
490     strcpy( buf, name);
491     cp = strchr (buf, '/');
492     while (cp) {
493         cpOld = cp;
494         cp = strchr (cp + 1, '/');
495         *cpOld = '\0';
496         mkdir (buf, cp ? 0777 : mode);
497         *cpOld = '/';
498     }
499 }
500 #endif
501
502
503
504 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
505 /* [ugoa]{+|-|=}[rwxst] */
506
507
508
509 extern int 
510 parse_mode( const char* s, mode_t* theMode)
511 {
512         mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
513         mode_t orMode = 0;
514         mode_t  mode = 0;
515         mode_t  groups = 0;
516         char    type;
517         char    c;
518
519         do {
520                 for ( ; ; ) {
521                         switch ( c = *s++ ) {
522                         case '\0':
523                                 return -1;
524                         case 'u':
525                                 groups |= S_ISUID|S_IRWXU;
526                                 continue;
527                         case 'g':
528                                 groups |= S_ISGID|S_IRWXG;
529                                 continue;
530                         case 'o':
531                                 groups |= S_IRWXO;
532                                 continue;
533                         case 'a':
534                                 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
535                                 continue;
536                         case '+':
537                         case '=':
538                         case '-':
539                                 type = c;
540                                 if ( groups == 0 ) /* The default is "all" */
541                                         groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
542                                 break;
543                         default:
544                                 if ( isdigit(c) && c >= '0' && c <= '7' && 
545                                                 mode == 0 && groups == 0 ) {
546                                         *theMode = strtol(--s, NULL, 8);
547                                         return (TRUE);
548                                 }
549                                 else
550                                         return (FALSE);
551                         }
552                         break;
553                 }
554
555                 while ( (c = *s++) != '\0' ) {
556                         switch ( c ) {
557                         case ',':
558                                 break;
559                         case 'r':
560                                 mode |= S_IRUSR|S_IRGRP|S_IROTH;
561                                 continue;
562                         case 'w':
563                                 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
564                                 continue;
565                         case 'x':
566                                 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
567                                 continue;
568                         case 's':
569                                 mode |= S_IXGRP|S_ISUID|S_ISGID;
570                                 continue;
571                         case 't':
572                                 mode |= 0;
573                                 continue;
574                         default:
575                                 *theMode &= andMode;
576                                 *theMode |= orMode;
577                                 return( TRUE);
578                         }
579                         break;
580                 }
581                 switch ( type ) {
582                 case '=':
583                         andMode &= ~(groups);
584                         /* fall through */
585                 case '+':
586                         orMode |= mode & groups;
587                         break;
588                 case '-':
589                         andMode &= ~(mode & groups);
590                         orMode &= andMode;
591                         break;
592                 }
593         } while ( c == ',' );
594         *theMode &= andMode;
595         *theMode |= orMode;
596         return (TRUE);
597 }
598
599
600 #endif
601
602
603
604
605
606
607
608 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
609
610 /* Use this to avoid needing the glibc NSS stuff 
611  * This uses storage buf to hold things.
612  * */
613 uid_t 
614 my_getid(const char *filename, char *name, uid_t id) 
615 {
616         FILE *file;
617         char *rname, *start, *end, buf[128];
618         uid_t rid;
619
620         file=fopen(filename,"r");
621         if (file == NULL) {
622             perror(filename);
623             return (-1);
624         }
625
626         while (fgets (buf, 128, file) != NULL) {
627                 if (buf[0] == '#')
628                         continue;
629
630                 start = buf;
631                 end = strchr (start, ':');
632                 if (end == NULL)
633                         continue;
634                 *end = '\0';
635                 rname = start;
636
637                 start = end + 1;
638                 end = strchr (start, ':');
639                 if (end == NULL)
640                         continue;
641
642                 start = end + 1;
643                 rid = (uid_t) strtol (start, &end, 10);
644                 if (end == start)
645                         continue;
646
647                 if (name) {
648                     if (0 == strcmp(rname, name)) {
649                         fclose( file);
650                         return( rid);
651                     }
652                 }
653                 if ( id != -1 && id == rid ) {
654                     strncpy(name, rname, 8);
655                     fclose( file);
656                     return( TRUE);
657                 }
658         }
659         fclose(file);
660         return (-1);
661 }
662
663 uid_t 
664 my_getpwnam(char *name) 
665 {
666     return my_getid("/etc/passwd", name, -1);
667 }
668
669 gid_t 
670 my_getgrnam(char *name) 
671 {
672     return my_getid("/etc/group", name, -1);
673 }
674
675 void
676 my_getpwuid(char* name, uid_t uid) 
677 {
678     my_getid("/etc/passwd", name, uid);
679 }
680
681 void
682 my_getgrgid(char* group, gid_t gid) 
683 {
684     my_getid("/etc/group", group, gid);
685 }
686
687
688 #endif
689
690
691
692
693 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
694
695
696 #include <linux/kd.h>
697 #include <sys/ioctl.h>
698
699 int is_a_console(int fd) 
700 {
701   char arg;
702   
703   arg = 0;
704   return (ioctl(fd, KDGKBTYPE, &arg) == 0
705           && ((arg == KB_101) || (arg == KB_84)));
706 }
707
708 static int open_a_console(char *fnam) 
709 {
710   int fd;
711   
712   /* try read-only */
713   fd = open(fnam, O_RDWR);
714   
715   /* if failed, try read-only */
716   if (fd < 0 && errno == EACCES)
717       fd = open(fnam, O_RDONLY);
718   
719   /* if failed, try write-only */
720   if (fd < 0 && errno == EACCES)
721       fd = open(fnam, O_WRONLY);
722   
723   /* if failed, fail */
724   if (fd < 0)
725       return -1;
726   
727   /* if not a console, fail */
728   if (! is_a_console(fd))
729     {
730       close(fd);
731       return -1;
732     }
733   
734   /* success */
735   return fd;
736 }
737
738 /*
739  * Get an fd for use with kbd/console ioctls.
740  * We try several things because opening /dev/console will fail
741  * if someone else used X (which does a chown on /dev/console).
742  *
743  * if tty_name is non-NULL, try this one instead.
744  */
745
746 int get_console_fd(char* tty_name) 
747 {
748   int fd;
749
750   if (tty_name)
751     {
752       if (-1 == (fd = open_a_console(tty_name)))
753         return -1;
754       else
755         return fd;
756     }
757   
758   fd = open_a_console("/dev/tty");
759   if (fd >= 0)
760     return fd;
761   
762   fd = open_a_console("/dev/tty0");
763   if (fd >= 0)
764     return fd;
765   
766   fd = open_a_console("/dev/console");
767   if (fd >= 0)
768     return fd;
769   
770   for (fd = 0; fd < 3; fd++)
771     if (is_a_console(fd))
772       return fd;
773   
774   fprintf(stderr,
775           "Couldnt get a file descriptor referring to the console\n");
776   return -1;            /* total failure */
777 }
778
779
780 #endif
781
782
783 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_SED)  
784
785 /* Do a case insensitive strstr() */
786 char* stristr(char *haystack, const char *needle)
787 {
788     int len = strlen( needle );
789     while( *haystack ) {
790         if( !strncasecmp( haystack, needle, len ) )
791             break;
792         haystack++;
793     }
794
795     if( !(*haystack) )
796             haystack = NULL;
797
798     return haystack;
799 }
800
801 /* This tries to find a needle in a haystack, but does so by
802  * only trying to match literal strings (look 'ma, no regexps!)
803  * This is short, sweet, and carries _very_ little baggage,
804  * unlike its beefier cousin in regexp.c
805  *  -Erik Andersen
806  */
807 extern int find_match(char *haystack, char *needle, int ignoreCase)
808 {
809
810     if (ignoreCase == FALSE)
811         haystack = strstr (haystack, needle);
812     else
813         haystack = stristr (haystack, needle);
814     if (haystack == NULL)
815         return FALSE;
816     return TRUE;
817 }
818
819
820 /* This performs substitutions after a string match has been found.  */
821 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
822 {
823     int foundOne=0;
824     char *where, *slider, *slider1, *oldhayStack;
825
826     if (ignoreCase == FALSE)
827         where = strstr (haystack, needle);
828     else
829         where = stristr (haystack, needle);
830
831     if (strcmp(needle, newNeedle)==0)
832         return FALSE;
833
834     oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
835     while(where!=NULL) {
836         foundOne++;
837         strcpy(oldhayStack, haystack);
838 #if 0
839         if ( strlen(newNeedle) > strlen(needle)) {
840             haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - 
841                 strlen(needle) + strlen(newNeedle)));
842         }
843 #endif
844         for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
845         *slider=0;
846         haystack=strcat(haystack, newNeedle);
847         slider1+=strlen(needle);
848         haystack = strcat(haystack, slider1);
849         where = strstr (slider, needle);
850     }
851     free( oldhayStack);
852
853     if (foundOne > 0)
854         return TRUE;
855     else
856         return FALSE;
857 }
858
859
860 #endif
861
862
863 #if defined BB_FIND
864 /*
865  * Routine to see if a text string is matched by a wildcard pattern.
866  * Returns TRUE if the text is matched, or FALSE if it is not matched
867  * or if the pattern is invalid.
868  *  *           matches zero or more characters
869  *  ?           matches a single character
870  *  [abc]       matches 'a', 'b' or 'c'
871  *  \c          quotes character c
872  * Adapted from code written by Ingo Wilken, and
873  * then taken from sash, Copyright (c) 1999 by David I. Bell
874  * Permission is granted to use, distribute, or modify this source,
875  * provided that this copyright notice remains intact.
876  * Permission to distribute this code under the GPL has been granted.
877  */
878 extern int
879 check_wildcard_match(const char* text, const char* pattern)
880 {
881     const char* retryPat;
882     const char* retryText;
883     int         ch;
884     int         found;
885
886     retryPat = NULL;
887     retryText = NULL;
888
889     while (*text || *pattern)
890     {
891         ch = *pattern++;
892
893         switch (ch)
894         {
895             case '*':  
896                 retryPat = pattern;
897                 retryText = text;
898                 break;
899
900             case '[':  
901                 found = FALSE;
902
903                 while ((ch = *pattern++) != ']')
904                 {
905                     if (ch == '\\')
906                         ch = *pattern++;
907
908                     if (ch == '\0')
909                         return FALSE;
910
911                     if (*text == ch)
912                         found = TRUE;
913                 }
914
915                 //if (!found)
916                 if (found==TRUE)
917                 {
918                     pattern = retryPat;
919                     text = ++retryText;
920                 }
921
922                 /* fall into next case */
923
924             case '?':  
925                 if (*text++ == '\0')
926                     return FALSE;
927
928                 break;
929
930             case '\\':  
931                 ch = *pattern++;
932
933                 if (ch == '\0')
934                         return FALSE;
935
936                 /* fall into next case */
937
938             default:        
939                 if (*text == ch)
940                 {
941                     if (*text)
942                         text++;
943                     break;
944                 }
945
946                 if (*text)
947                 {
948                     pattern = retryPat;
949                     text = ++retryText;
950                     break;
951                 }
952
953                 return FALSE;
954         }
955
956         if (pattern == NULL)
957                 return FALSE;
958     }
959
960     return TRUE;
961 }
962 #endif
963
964
965
966
967 #if defined BB_DF | defined BB_MTAB
968 /*
969  * Given a block device, find the mount table entry if that block device
970  * is mounted.
971  *
972  * Given any other file (or directory), find the mount table entry for its
973  * filesystem.
974  */
975 extern struct mntent *findMountPoint(const char *name, const char *table)
976 {
977     struct stat s;
978     dev_t mountDevice;
979     FILE *mountTable;
980     struct mntent *mountEntry;
981
982     if (stat(name, &s) != 0)
983         return 0;
984
985     if ((s.st_mode & S_IFMT) == S_IFBLK)
986         mountDevice = s.st_rdev;
987     else
988         mountDevice = s.st_dev;
989
990
991     if ((mountTable = setmntent(table, "r")) == 0)
992         return 0;
993
994     while ((mountEntry = getmntent(mountTable)) != 0) {
995         if (strcmp(name, mountEntry->mnt_dir) == 0
996             || strcmp(name, mountEntry->mnt_fsname) == 0)       /* String match. */
997             break;
998         if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice)  /* Match the device. */
999             break;
1000         if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice)      /* Match the directory's mount point. */
1001             break;
1002     }
1003     endmntent(mountTable);
1004     return mountEntry;
1005 }
1006
1007 #endif
1008
1009
1010
1011 #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
1012 extern void whine_if_fstab_is_missing()
1013 {
1014     struct stat statBuf;
1015     if (stat("/etc/fstab", &statBuf) < 0) 
1016         fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
1017 }
1018 #endif
1019
1020
1021 /* END CODE */
1022
1023
1024
1025
1026
1027
1028
1029
1030