Annotation of embedaddon/pimd/libite/fisdir.c, revision 1.1.1.1

1.1       misho       1: /* Check if directory exists
                      2:  *
                      3:  * Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
                      4:  *
                      5:  * Permission is hereby granted, free of charge, to any person obtaining a copy
                      6:  * of this software and associated documentation files (the "Software"), to deal
                      7:  * in the Software without restriction, including without limitation the rights
                      8:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                      9:  * copies of the Software, and to permit persons to whom the Software is
                     10:  * furnished to do so, subject to the following conditions:
                     11:  *
                     12:  * The above copyright notice and this permission notice shall be included in
                     13:  * all copies or substantial portions of the Software.
                     14:  *
                     15:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                     16:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     17:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
                     18:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     19:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
                     20:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
                     21:  * THE SOFTWARE.
                     22:  */
                     23: 
                     24: #include <sys/types.h>
                     25: #include <sys/stat.h>
                     26: #include <unistd.h>
                     27: 
                     28: /**
                     29:  * fexist - Check if a file exists and is a directory.
                     30:  * @file: File to look for, with full path.
                     31:  *
                     32:  * Returns:
                     33:  * %TRUE(1) if the file exists and is a directory, otherwise %FALSE(0).
                     34:  */
                     35: int fisdir(char *file)
                     36: {
                     37:        struct stat sb;
                     38: 
                     39:        if (!stat(file, &sb) && S_ISDIR(sb.st_mode))
                     40:                return 1;
                     41: 
                     42:        return 0;
                     43: }
                     44: 
                     45: #ifdef UNITTEST
                     46: #include "lite.h"
                     47: 
                     48: int main(void)
                     49: {
                     50:        int i = 0;
                     51:        struct { char *file; int exist; } arr[] = {
                     52:                { "/etc/passwd", 0 },
                     53:                { "/etc/",       1 },
                     54:                { "/sbin/init",  0 },
                     55:                { "/dev/null",   0 },
                     56:                { "/dev/",       1 },
                     57:                { NULL,  0 },
                     58:        };
                     59:        FILE *src, *dst;
                     60: 
                     61:        for (i = 0; i < NELEMS(arr); i++) {
                     62:                if (fisdir(arr[i].file) != arr[i].exist)
                     63:                        err(1, "Failed fisdir(%s)", arr[i].file ?: "NULL");
                     64:                else
                     65:                        printf("fisdir() %-11s %-18s => OK!\n", arr[i].file ?: "NULL",
                     66:                               arr[i].exist ? "is a directory" : "is NOT a directory");
                     67:        }
                     68: 
                     69:        return 0;
                     70: }
                     71: #endif /* UNITTEST */
                     72: 
                     73: /**
                     74:  * Local Variables:
                     75:  *  compile-command: "make V=1 -f fisdir.mk"
                     76:  *  version-control: t
                     77:  *  indent-tabs-mode: t
                     78:  *  c-file-style: "linux"
                     79:  * End:
                     80:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>