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 /** PREDICATES **/ 55 56 int 57 is_dir(const char *fmt, ...) 58 { 59 va_list args; 60 char *filename; 61 int result; 62 struct stat sb; 63 64 va_start(args, fmt); 65 vasprintf(&filename, fmt, args); 66 va_end(args); 67 68 result = stat(filename, &sb); 69 free(filename); 70 71 if (result == 0) 72 return(sb.st_mode & S_IFDIR); 73 else 74 return(0); 75 } 76 77 int 78 is_file(const char *fmt, ...) 79 { 80 va_list args; 81 char *filename; 82 int result; 83 struct stat sb; 84 85 va_start(args, fmt); 86 vasprintf(&filename, fmt, args); 87 va_end(args); 88 89 result = stat(filename, &sb); 90 free(filename); 91 92 if (result == 0) 93 return(sb.st_mode & S_IFREG); 94 else 95 return(0); 96 } 97 98 int 99 is_program(const char *fmt, ...) 100 { 101 va_list args; 102 char *filename; 103 int result; 104 struct stat sb; 105 106 va_start(args, fmt); 107 vasprintf(&filename, fmt, args); 108 va_end(args); 109 110 result = stat(filename, &sb); 111 free(filename); 112 113 if (result == 0) 114 return((sb.st_mode & S_IFREG) && (sb.st_mode & S_IXOTH)); 115 else 116 return(0); 117 } 118 119 int 120 is_device(const char *fmt, ...) 121 { 122 va_list args; 123 char *filename; 124 int result; 125 struct stat sb; 126 127 va_start(args, fmt); 128 vasprintf(&filename, fmt, args); 129 va_end(args); 130 131 result = stat(filename, &sb); 132 free(filename); 133 134 if (result == 0) 135 return((sb.st_mode & S_IFCHR) | (sb.st_mode & S_IFBLK)); 136 else 137 return(0); 138 } 139 140 int 141 is_named_pipe(const char *fmt, ...) 142 { 143 va_list args; 144 char *filename; 145 int result; 146 struct stat sb; 147 148 va_start(args, fmt); 149 vasprintf(&filename, fmt, args); 150 va_end(args); 151 152 result = stat(filename, &sb); 153 154 free(filename); 155 156 if (result == 0) 157 return(sb.st_mode & S_IFIFO); 158 else 159 return(0); 160 } 161 162 int 163 is_mountpoint_mounted(const char *mtpt) 164 { 165 struct statfs *mt_array, *mt_ptr; 166 int count; 167 168 count = getmntinfo(&mt_array, MNT_WAIT); 169 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) { 170 if (strcmp(mt_ptr->f_mntonname, mtpt) == 0) 171 return(1); 172 } 173 return(0); 174 } 175 176 int 177 is_device_mounted(const char *device) 178 { 179 struct statfs *mt_array, *mt_ptr; 180 int count; 181 182 count = getmntinfo(&mt_array, MNT_WAIT); 183 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) { 184 if (strcmp(mt_ptr->f_mntfromname, device) == 0) 185 return(1); 186 } 187 return(0); 188 } 189 190 int 191 is_any_slice_mounted(const char *diskdev) 192 { 193 struct statfs *mt_array, *mt_ptr; 194 int count; 195 196 count = getmntinfo(&mt_array, MNT_WAIT); 197 for (mt_ptr = mt_array; count > 0; mt_ptr++, count--) { 198 if (strstr(mt_ptr->f_mntfromname, diskdev) == 199 mt_ptr->f_mntfromname) 200 return(1); 201 } 202 return(0); 203 } 204