77eb5f2dd5fbcadcd466351215bc316129abad87
[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 enod 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
486     cp = strchr (buf, '/');
487
488     while (cp) {
489         cpOld = cp;
490         cp = strchr (cp + 1, '/');
491
492         *cpOld = '\0';
493
494         if (mkdir (buf, cp ? 0777 : mode) == 0)
495             printf ("Directory \"%s\" created\n", buf);
496
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                         return( rid);
650                 }
651                 if ( id != -1 && id == rid ) {
652                     strncpy(name, rname, 8);
653                     return( TRUE);
654                 }
655         }
656         fclose(file);
657         return (-1);
658 }
659
660 uid_t 
661 my_getpwnam(char *name) 
662 {
663     return my_getid("/etc/passwd", name, -1);
664 }
665
666 gid_t 
667 my_getgrnam(char *name) 
668 {
669     return my_getid("/etc/group", name, -1);
670 }
671
672 void
673 my_getpwuid(char* name, uid_t uid) 
674 {
675     my_getid("/etc/passwd", name, uid);
676 }
677
678 void
679 my_getgrgid(char* group, gid_t gid) 
680 {
681     my_getid("/etc/group", group, gid);
682 }
683
684
685 #endif
686
687
688
689
690 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
691
692
693 #include <linux/kd.h>
694 #include <sys/ioctl.h>
695
696 int is_a_console(int fd) 
697 {
698   char arg;
699   
700   arg = 0;
701   return (ioctl(fd, KDGKBTYPE, &arg) == 0
702           && ((arg == KB_101) || (arg == KB_84)));
703 }
704
705 static int open_a_console(char *fnam) 
706 {
707   int fd;
708   
709   /* try read-only */
710   fd = open(fnam, O_RDWR);
711   
712   /* if failed, try read-only */
713   if (fd < 0 && errno == EACCES)
714       fd = open(fnam, O_RDONLY);
715   
716   /* if failed, try write-only */
717   if (fd < 0 && errno == EACCES)
718       fd = open(fnam, O_WRONLY);
719   
720   /* if failed, fail */
721   if (fd < 0)
722       return -1;
723   
724   /* if not a console, fail */
725   if (! is_a_console(fd))
726     {
727       close(fd);
728       return -1;
729     }
730   
731   /* success */
732   return fd;
733 }
734
735 /*
736  * Get an fd for use with kbd/console ioctls.
737  * We try several things because opening /dev/console will fail
738  * if someone else used X (which does a chown on /dev/console).
739  *
740  * if tty_name is non-NULL, try this one instead.
741  */
742
743 int get_console_fd(char* tty_name) 
744 {
745   int fd;
746
747   if (tty_name)
748     {
749       if (-1 == (fd = open_a_console(tty_name)))
750         return -1;
751       else
752         return fd;
753     }
754   
755   fd = open_a_console("/dev/tty");
756   if (fd >= 0)
757     return fd;
758   
759   fd = open_a_console("/dev/tty0");
760   if (fd >= 0)
761     return fd;
762   
763   fd = open_a_console("/dev/console");
764   if (fd >= 0)
765     return fd;
766   
767   for (fd = 0; fd < 3; fd++)
768     if (is_a_console(fd))
769       return fd;
770   
771   fprintf(stderr,
772           "Couldnt get a file descriptor referring to the console\n");
773   return -1;            /* total failure */
774 }
775
776
777 #endif
778
779
780 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)  
781
782 /* Do a case insensitive strstr() */
783 char* stristr(char *haystack, const char *needle)
784 {
785     int len = strlen( needle );
786     while( *haystack ) {
787         if( !strncasecmp( haystack, needle, len ) )
788             break;
789         haystack++;
790     }
791
792     if( !(*haystack) )
793             haystack = NULL;
794
795     return haystack;
796 }
797
798 /* This tries to find a needle in a haystack, but does so by
799  * only trying to match literal strings (look 'ma, no regexps!)
800  * This is short, sweet, and carries _very_ little baggage,
801  * unlike its beefier cousin in regexp.c
802  *  -Erik Andersen
803  */
804 extern int find_match(char *haystack, char *needle, int ignoreCase)
805 {
806
807     if (ignoreCase == FALSE)
808         haystack = strstr (haystack, needle);
809     else
810         haystack = stristr (haystack, needle);
811     if (haystack == NULL)
812         return FALSE;
813     return TRUE;
814 }
815
816
817 /* This performs substitutions after a string match has been found.  */
818 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
819 {
820     int foundOne=0;
821     char *where, *slider, *slider1, *oldhayStack;
822
823     if (ignoreCase == FALSE)
824         where = strstr (haystack, needle);
825     else
826         where = stristr (haystack, needle);
827
828     if (strcmp(needle, newNeedle)==0)
829         return FALSE;
830
831     oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
832     while(where!=NULL) {
833         foundOne++;
834         strcpy(oldhayStack, haystack);
835 #if 0
836         if ( strlen(newNeedle) > strlen(needle)) {
837             haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - 
838                 strlen(needle) + strlen(newNeedle)));
839         }
840 #endif
841         for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
842         *slider=0;
843         haystack=strcat(haystack, newNeedle);
844         slider1+=strlen(needle);
845         haystack = strcat(haystack, slider1);
846         where = strstr (slider, needle);
847     }
848     free( oldhayStack);
849
850     if (foundOne > 0)
851         return TRUE;
852     else
853         return FALSE;
854 }
855
856
857 #endif
858
859
860
861
862 #if defined BB_DF | defined BB_MTAB
863 /*
864  * Given a block device, find the mount table entry if that block device
865  * is mounted.
866  *
867  * Given any other file (or directory), find the mount table entry for its
868  * filesystem.
869  */
870 extern struct mntent *findMountPoint(const char *name, const char *table)
871 {
872     struct stat s;
873     dev_t mountDevice;
874     FILE *mountTable;
875     struct mntent *mountEntry;
876
877     if (stat(name, &s) != 0)
878         return 0;
879
880     if ((s.st_mode & S_IFMT) == S_IFBLK)
881         mountDevice = s.st_rdev;
882     else
883         mountDevice = s.st_dev;
884
885
886     if ((mountTable = setmntent(table, "r")) == 0)
887         return 0;
888
889     while ((mountEntry = getmntent(mountTable)) != 0) {
890         if (strcmp(name, mountEntry->mnt_dir) == 0
891             || strcmp(name, mountEntry->mnt_fsname) == 0)       /* String match. */
892             break;
893         if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice)  /* Match the device. */
894             break;
895         if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice)      /* Match the directory's mount point. */
896             break;
897     }
898     endmntent(mountTable);
899     return mountEntry;
900 }
901
902 #endif
903
904
905
906 #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
907 extern void whine_if_fstab_is_missing()
908 {
909     struct stat statBuf;
910     if (stat("/etc/fstab", &statBuf) < 0) 
911         fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
912 }
913 #endif
914
915
916 /* END CODE */
917
918