b4dd0ccc3e79e9166b322fe3f6678f785a0c22f1
[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         *cpOld = '\0';
492         mkdir (buf, cp ? 0777 : mode);
493         *cpOld = '/';
494     }
495 }
496 #endif
497
498
499
500 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR)
501 /* [ugoa]{+|-|=}[rwxst] */
502
503
504
505 extern int 
506 parse_mode( const char* s, mode_t* theMode)
507 {
508         mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
509         mode_t orMode = 0;
510         mode_t  mode = 0;
511         mode_t  groups = 0;
512         char    type;
513         char    c;
514
515         do {
516                 for ( ; ; ) {
517                         switch ( c = *s++ ) {
518                         case '\0':
519                                 return -1;
520                         case 'u':
521                                 groups |= S_ISUID|S_IRWXU;
522                                 continue;
523                         case 'g':
524                                 groups |= S_ISGID|S_IRWXG;
525                                 continue;
526                         case 'o':
527                                 groups |= S_IRWXO;
528                                 continue;
529                         case 'a':
530                                 groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
531                                 continue;
532                         case '+':
533                         case '=':
534                         case '-':
535                                 type = c;
536                                 if ( groups == 0 ) /* The default is "all" */
537                                         groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO;
538                                 break;
539                         default:
540                                 if ( isdigit(c) && c >= '0' && c <= '7' && 
541                                                 mode == 0 && groups == 0 ) {
542                                         *theMode = strtol(--s, NULL, 8);
543                                         return (TRUE);
544                                 }
545                                 else
546                                         return (FALSE);
547                         }
548                         break;
549                 }
550
551                 while ( (c = *s++) != '\0' ) {
552                         switch ( c ) {
553                         case ',':
554                                 break;
555                         case 'r':
556                                 mode |= S_IRUSR|S_IRGRP|S_IROTH;
557                                 continue;
558                         case 'w':
559                                 mode |= S_IWUSR|S_IWGRP|S_IWOTH;
560                                 continue;
561                         case 'x':
562                                 mode |= S_IXUSR|S_IXGRP|S_IXOTH;
563                                 continue;
564                         case 's':
565                                 mode |= S_IXGRP|S_ISUID|S_ISGID;
566                                 continue;
567                         case 't':
568                                 mode |= 0;
569                                 continue;
570                         default:
571                                 *theMode &= andMode;
572                                 *theMode |= orMode;
573                                 return( TRUE);
574                         }
575                         break;
576                 }
577                 switch ( type ) {
578                 case '=':
579                         andMode &= ~(groups);
580                         /* fall through */
581                 case '+':
582                         orMode |= mode & groups;
583                         break;
584                 case '-':
585                         andMode &= ~(mode & groups);
586                         orMode &= andMode;
587                         break;
588                 }
589         } while ( c == ',' );
590         *theMode &= andMode;
591         *theMode |= orMode;
592         return (TRUE);
593 }
594
595
596 #endif
597
598
599
600
601
602
603
604 #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
605
606 /* Use this to avoid needing the glibc NSS stuff 
607  * This uses storage buf to hold things.
608  * */
609 uid_t 
610 my_getid(const char *filename, char *name, uid_t id) 
611 {
612         FILE *file;
613         char *rname, *start, *end, buf[128];
614         uid_t rid;
615
616         file=fopen(filename,"r");
617         if (file == NULL) {
618             perror(filename);
619             return (-1);
620         }
621
622         while (fgets (buf, 128, file) != NULL) {
623                 if (buf[0] == '#')
624                         continue;
625
626                 start = buf;
627                 end = strchr (start, ':');
628                 if (end == NULL)
629                         continue;
630                 *end = '\0';
631                 rname = start;
632
633                 start = end + 1;
634                 end = strchr (start, ':');
635                 if (end == NULL)
636                         continue;
637
638                 start = end + 1;
639                 rid = (uid_t) strtol (start, &end, 10);
640                 if (end == start)
641                         continue;
642
643                 if (name) {
644                     if (0 == strcmp(rname, name))
645                         return( rid);
646                 }
647                 if ( id != -1 && id == rid ) {
648                     strncpy(name, rname, 8);
649                     return( TRUE);
650                 }
651         }
652         fclose(file);
653         return (-1);
654 }
655
656 uid_t 
657 my_getpwnam(char *name) 
658 {
659     return my_getid("/etc/passwd", name, -1);
660 }
661
662 gid_t 
663 my_getgrnam(char *name) 
664 {
665     return my_getid("/etc/group", name, -1);
666 }
667
668 void
669 my_getpwuid(char* name, uid_t uid) 
670 {
671     my_getid("/etc/passwd", name, uid);
672 }
673
674 void
675 my_getgrgid(char* group, gid_t gid) 
676 {
677     my_getid("/etc/group", group, gid);
678 }
679
680
681 #endif
682
683
684
685
686 #if (defined BB_CHVT) || (defined BB_DEALLOCVT)
687
688
689 #include <linux/kd.h>
690 #include <sys/ioctl.h>
691
692 int is_a_console(int fd) 
693 {
694   char arg;
695   
696   arg = 0;
697   return (ioctl(fd, KDGKBTYPE, &arg) == 0
698           && ((arg == KB_101) || (arg == KB_84)));
699 }
700
701 static int open_a_console(char *fnam) 
702 {
703   int fd;
704   
705   /* try read-only */
706   fd = open(fnam, O_RDWR);
707   
708   /* if failed, try read-only */
709   if (fd < 0 && errno == EACCES)
710       fd = open(fnam, O_RDONLY);
711   
712   /* if failed, try write-only */
713   if (fd < 0 && errno == EACCES)
714       fd = open(fnam, O_WRONLY);
715   
716   /* if failed, fail */
717   if (fd < 0)
718       return -1;
719   
720   /* if not a console, fail */
721   if (! is_a_console(fd))
722     {
723       close(fd);
724       return -1;
725     }
726   
727   /* success */
728   return fd;
729 }
730
731 /*
732  * Get an fd for use with kbd/console ioctls.
733  * We try several things because opening /dev/console will fail
734  * if someone else used X (which does a chown on /dev/console).
735  *
736  * if tty_name is non-NULL, try this one instead.
737  */
738
739 int get_console_fd(char* tty_name) 
740 {
741   int fd;
742
743   if (tty_name)
744     {
745       if (-1 == (fd = open_a_console(tty_name)))
746         return -1;
747       else
748         return fd;
749     }
750   
751   fd = open_a_console("/dev/tty");
752   if (fd >= 0)
753     return fd;
754   
755   fd = open_a_console("/dev/tty0");
756   if (fd >= 0)
757     return fd;
758   
759   fd = open_a_console("/dev/console");
760   if (fd >= 0)
761     return fd;
762   
763   for (fd = 0; fd < 3; fd++)
764     if (is_a_console(fd))
765       return fd;
766   
767   fprintf(stderr,
768           "Couldnt get a file descriptor referring to the console\n");
769   return -1;            /* total failure */
770 }
771
772
773 #endif
774
775
776 #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED)  
777
778 /* Do a case insensitive strstr() */
779 char* stristr(char *haystack, const char *needle)
780 {
781     int len = strlen( needle );
782     while( *haystack ) {
783         if( !strncasecmp( haystack, needle, len ) )
784             break;
785         haystack++;
786     }
787
788     if( !(*haystack) )
789             haystack = NULL;
790
791     return haystack;
792 }
793
794 /* This tries to find a needle in a haystack, but does so by
795  * only trying to match literal strings (look 'ma, no regexps!)
796  * This is short, sweet, and carries _very_ little baggage,
797  * unlike its beefier cousin in regexp.c
798  *  -Erik Andersen
799  */
800 extern int find_match(char *haystack, char *needle, int ignoreCase)
801 {
802
803     if (ignoreCase == FALSE)
804         haystack = strstr (haystack, needle);
805     else
806         haystack = stristr (haystack, needle);
807     if (haystack == NULL)
808         return FALSE;
809     return TRUE;
810 }
811
812
813 /* This performs substitutions after a string match has been found.  */
814 extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
815 {
816     int foundOne=0;
817     char *where, *slider, *slider1, *oldhayStack;
818
819     if (ignoreCase == FALSE)
820         where = strstr (haystack, needle);
821     else
822         where = stristr (haystack, needle);
823
824     if (strcmp(needle, newNeedle)==0)
825         return FALSE;
826
827     oldhayStack = (char*)malloc((unsigned)(strlen(haystack)));
828     while(where!=NULL) {
829         foundOne++;
830         strcpy(oldhayStack, haystack);
831 #if 0
832         if ( strlen(newNeedle) > strlen(needle)) {
833             haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - 
834                 strlen(needle) + strlen(newNeedle)));
835         }
836 #endif
837         for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++);
838         *slider=0;
839         haystack=strcat(haystack, newNeedle);
840         slider1+=strlen(needle);
841         haystack = strcat(haystack, slider1);
842         where = strstr (slider, needle);
843     }
844     free( oldhayStack);
845
846     if (foundOne > 0)
847         return TRUE;
848     else
849         return FALSE;
850 }
851
852
853 #endif
854
855
856
857
858 #if defined BB_DF | defined BB_MTAB
859 /*
860  * Given a block device, find the mount table entry if that block device
861  * is mounted.
862  *
863  * Given any other file (or directory), find the mount table entry for its
864  * filesystem.
865  */
866 extern struct mntent *findMountPoint(const char *name, const char *table)
867 {
868     struct stat s;
869     dev_t mountDevice;
870     FILE *mountTable;
871     struct mntent *mountEntry;
872
873     if (stat(name, &s) != 0)
874         return 0;
875
876     if ((s.st_mode & S_IFMT) == S_IFBLK)
877         mountDevice = s.st_rdev;
878     else
879         mountDevice = s.st_dev;
880
881
882     if ((mountTable = setmntent(table, "r")) == 0)
883         return 0;
884
885     while ((mountEntry = getmntent(mountTable)) != 0) {
886         if (strcmp(name, mountEntry->mnt_dir) == 0
887             || strcmp(name, mountEntry->mnt_fsname) == 0)       /* String match. */
888             break;
889         if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice)  /* Match the device. */
890             break;
891         if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice)      /* Match the directory's mount point. */
892             break;
893     }
894     endmntent(mountTable);
895     return mountEntry;
896 }
897
898 #endif
899
900
901
902 #if !defined BB_MTAB && (defined BB_MOUNT || defined BB_DF )
903 extern void whine_if_fstab_is_missing()
904 {
905     struct stat statBuf;
906     if (stat("/etc/fstab", &statBuf) < 0) 
907         fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
908 }
909 #endif
910
911
912 /* END CODE */
913
914