Installer import into contrib (real import this time)
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / lib / libaura / fspred.c
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Chris Pressey <cpressey@catseye.mine.nu>.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35
36 /*
37  * fspred.c
38  * Filesystem predicates.
39  * $Id: fspred.c,v 1.3 2005/02/10 03:33:49 cpressey Exp $
40  */
41
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/ucred.h>
45 #include <sys/mount.h>
46
47 #include <stdarg.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include "fspred.h"
53
54 #if (__NetBSD_Version__ >= 200040000)
55 #define STATFS statvfs
56 #else
57 #define STATFS statfs
58 #endif
59
60 /** PREDICATES **/
61
62 int
63 is_dir(const char *fmt, ...)
64 {
65         va_list args;
66         char *filename;
67         int result;
68         struct stat sb;
69
70         va_start(args, fmt);
71         vasprintf(&filename, fmt, args);
72         va_end(args);
73
74         result = stat(filename, &sb);
75         free(filename);
76
77         if (result == 0)
78                 return(sb.st_mode & S_IFDIR);
79         else
80                 return(0);
81 }
82
83 int
84 is_file(const char *fmt, ...)
85 {
86         va_list args;
87         char *filename;
88         int result;
89         struct stat sb;
90
91         va_start(args, fmt);
92         vasprintf(&filename, fmt, args);
93         va_end(args);
94
95         result = stat(filename, &sb);
96         free(filename);
97
98         if (result == 0)
99                 return(sb.st_mode & S_IFREG);
100         else
101                 return(0);
102 }
103
104 int
105 is_program(const char *fmt, ...)
106 {
107         va_list args;
108         char *filename;
109         int result;
110         struct stat sb;
111
112         va_start(args, fmt);
113         vasprintf(&filename, fmt, args);
114         va_end(args);
115
116         result = stat(filename, &sb);
117         free(filename);
118
119         if (result == 0)
120                 return((sb.st_mode & S_IFREG) && (sb.st_mode & S_IXOTH));
121         else
122                 return(0);
123 }
124
125 int
126 is_device(const char *fmt, ...)
127 {
128         va_list args;
129         char *filename;
130         int result;
131         struct stat sb;
132
133         va_start(args, fmt);
134         vasprintf(&filename, fmt, args);
135         va_end(args);
136
137         result = stat(filename, &sb);
138         free(filename);
139
140         if (result == 0)
141                 return((sb.st_mode & S_IFCHR) | (sb.st_mode & S_IFBLK));
142         else
143                 return(0);
144 }
145
146 int
147 is_named_pipe(const char *fmt, ...)
148 {
149         va_list args;
150         char *filename;
151         int result;
152         struct stat sb;
153
154         va_start(args, fmt);
155         vasprintf(&filename, fmt, args);
156         va_end(args);
157
158         result = stat(filename, &sb);
159
160         free(filename);
161
162         if (result == 0)
163                 return(sb.st_mode & S_IFIFO);
164         else
165                 return(0);
166 }
167
168 int
169 is_mountpoint_mounted(const char *mtpt)
170 {
171         struct STATFS *mt_array, *mt_ptr;
172         int count;
173
174         count = getmntinfo(&mt_array, MNT_WAIT);
175         for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
176                 if (strcmp(mt_ptr->f_mntonname, mtpt) == 0)
177                         return(1);
178         }
179         return(0);
180 }
181
182 int
183 is_device_mounted(const char *device)
184 {
185         struct STATFS *mt_array, *mt_ptr;
186         int count;
187
188         count = getmntinfo(&mt_array, MNT_WAIT);
189         for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
190                 if (strcmp(mt_ptr->f_mntfromname, device) == 0)
191                         return(1);
192         }
193         return(0);
194 }
195
196 int
197 is_any_slice_mounted(const char *diskdev)
198 {
199         struct STATFS *mt_array, *mt_ptr;
200         int count;
201
202         count = getmntinfo(&mt_array, MNT_WAIT);
203         for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) {
204                 if (strstr(mt_ptr->f_mntfromname, diskdev) ==
205                     mt_ptr->f_mntfromname)
206                         return(1);
207         }
208         return(0);
209 }