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