dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libshare / common / issubdir.c
blobd24a4b89e538f806a3b024f5852d0a7dd42f4bdf
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
40 #pragma ident "%Z%%M% %I% %E% SMI"
43 * Subdirectory detection: needed by exportfs and rpc.mountd.
44 * The above programs call issubdir() frequently, so we make
45 * it fast by caching the results of stat().
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <string.h>
52 #define MAXSTATS MAXPATHLEN/2 /* maximum number of stat()'s to save */
54 #define inoeq(ino1, ino2) ((ino1) == (ino2))
55 #define deveq(dev1, dev2) ((dev1) == (dev2))
58 * dir1 is a subdirectory of dir2 within the same filesystem if
59 * (a) dir1 is identical to dir2
60 * (b) dir1's parent is dir2
62 int
63 issubdir(dir1, dir2)
64 char *dir1;
65 char *dir2;
67 struct stat st;
68 struct stat parent_st;
69 char *p;
70 int index;
72 static dev_t child_dev;
73 static dev_t child_rdev;
74 static ino_t child_ino[MAXSTATS];
75 static int valid;
76 static char childdir[MAXPATHLEN];
77 static char child_fstype[_ST_FSTYPSZ];
80 * Get parent directory info
82 if (stat(dir2, &parent_st) < 0) {
83 return (0);
86 if (strcmp(childdir, dir1) != 0) {
88 * Not in cache: get child directory info
90 p = strcpy(childdir, dir1) + strlen(dir1);
91 index = 0;
92 valid = 0;
93 for (;;) {
94 if (stat(childdir, &st) < 0) {
95 childdir[0] = 0; /* invalidate cache */
96 return (0);
98 if (index == 0) {
99 child_dev = st.st_dev;
100 child_rdev = st.st_rdev;
101 (void) strncpy(child_fstype, st.st_fstype,
102 sizeof (child_fstype));
104 if (index > 0 &&
105 (child_dev != st.st_dev ||
106 inoeq(child_ino[index - 1], st.st_ino))) {
108 * Hit root: done
110 break;
112 child_ino[index++] = st.st_ino;
113 if (S_ISDIR(st.st_mode)) {
114 p = strcpy(p, "/..") + 3;
115 } else {
116 p = strrchr(childdir, '/');
117 if (p == NULL) {
118 p = strcpy(childdir, ".") + 1;
119 } else {
120 while (((p - 1) > childdir) &&
121 *(p - 1) == '/') {
122 p--;
124 *p = '\0';
128 valid = index;
129 (void) strcpy(childdir, dir1);
133 * Perform the test
135 if (!deveq(parent_st.st_dev, child_dev)) {
136 return (0);
140 * Check rdev also in case of lofs
142 if (((strcmp(parent_st.st_fstype, "lofs") == 0)) &&
143 (strcmp(child_fstype, "lofs") == 0)) {
144 if (!deveq(parent_st.st_rdev, child_rdev)) {
145 return (0);
149 for (index = 0; index < valid; index++) {
150 if (inoeq(child_ino[index], parent_st.st_ino)) {
151 return (1);
154 return (0);