Annotation of embedaddon/pimd/libite/fexist.c, revision 1.1

1.1     ! misho       1: /* Check if file 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 <errno.h>
        !            25: #include <unistd.h>
        !            26: 
        !            27: /**
        !            28:  * fexist - Check if a file exists in the file system.
        !            29:  * @file: File to look for, with full path.
        !            30:  *
        !            31:  * Returns:
        !            32:  * %TRUE(1) if the file exists, otherwise %FALSE(0).
        !            33:  */
        !            34: int fexist(char *file)
        !            35: {
        !            36:        if (!file) {
        !            37:                errno = EINVAL;
        !            38:                return 0;       /* Doesn't exist ... */
        !            39:        }
        !            40: 
        !            41:        if (-1 == access(file, F_OK))
        !            42:                return 0;
        !            43: 
        !            44:        return 1;
        !            45: }
        !            46: 
        !            47: #ifdef UNITTEST
        !            48: #include "lite.h"
        !            49: 
        !            50: int main(void)
        !            51: {
        !            52:        int i = 0;
        !            53:        struct { char *file; int exist; } arr[] = {
        !            54:                { "/etc/passwd", 1 },
        !            55:                { "/etc/kalle",  0 },
        !            56:                { "/sbin/init",  1 },
        !            57:                { "/dev/null",   1 },
        !            58:                { NULL,  0 },
        !            59:        };
        !            60:        FILE *src, *dst;
        !            61: 
        !            62:        for (i = 0; i < NELEMS(arr); i++) {
        !            63:                if (fexist(arr[i].file) != arr[i].exist)
        !            64:                        err(1, "Failed fexist(%s)", arr[i].file ?: "NULL");
        !            65:                else
        !            66:                        printf("File %-11s %-14s => OK!\n", arr[i].file ?: "NULL",
        !            67:                               arr[i].exist ? "does exist" : "does not exist");
        !            68:        }
        !            69: 
        !            70:        return 0;
        !            71: }
        !            72: #endif /* UNITTEST */
        !            73: 
        !            74: /**
        !            75:  * Local Variables:
        !            76:  *  compile-command: "make V=1 -f fexist.mk"
        !            77:  *  version-control: t
        !            78:  *  indent-tabs-mode: t
        !            79:  *  c-file-style: "linux"
        !            80:  * End:
        !            81:  */

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