Annotation of libaitsync/example/dircmp.c, revision 1.1.2.1

1.1.2.1 ! misho       1: /*************************************************************************
        !             2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
        !             3:  *  by Michael Pounov <misho@aitbg.com>
        !             4:  *
        !             5:  * $Author: misho $
        !             6:  * $Id: dircmp.c,v 1.3 2012/07/22 22:46:47 misho Exp $
        !             7:  *
        !             8:  *************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
        !            46: #include "global.h"
        !            47: 
        !            48: 
        !            49: extern char compiled[], compiledby[], compilehost[];
        !            50: 
        !            51: 
        !            52: static void
        !            53: Usage()
        !            54: {
        !            55:        printf( "-= DirCmp =- Tool for compare directories and show differences\n"
        !            56:                "=== %s === %s@%s ===\n\n"
        !            57:                "  Syntax: dircmp [options] <dir> [<cmp_dir>]\n\n"
        !            58:                "\t-l\t\tLong directory output ...\n"
        !            59:                "\t-o <filename>\tOutput diff to filename\n"
        !            60:                "\n", compiled, compiledby, compilehost);
        !            61: }
        !            62: 
        !            63: int
        !            64: main(int argc, char **argv)
        !            65: {
        !            66:        char ch, szFName[MAXPATHLEN];
        !            67:        int lm = 0;
        !            68:        struct tagDirName *list;
        !            69:        register int i;
        !            70:        FILE *f = stdout;
        !            71:        struct stat sb;
        !            72: 
        !            73:        while ((ch = getopt(argc, argv, "hlo:")) != -1)
        !            74:                switch (ch) {
        !            75:                        case 'o':
        !            76:                                lm |= 2;
        !            77:                                strlcpy(szFName, optarg, MAXPATHLEN);
        !            78:                                break;
        !            79:                        case 'l':
        !            80:                                lm |= 1;
        !            81:                                break;
        !            82:                        case 'h':
        !            83:                        default:
        !            84:                                Usage();
        !            85:                                return 127;
        !            86:                }
        !            87:        argc -= optind;
        !            88:        argv += optind;
        !            89: 
        !            90:        if (!argc || (!(lm & 2) && argc < 2)) {
        !            91:                Usage();
        !            92:                return 127;
        !            93:        }
        !            94:        // check for general differences
        !            95:        if (argc > 1) {
        !            96:                if (lstat(argv[1], &sb) == -1) {
        !            97:                        printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
        !            98:                        return 127;
        !            99:                }
        !           100: 
        !           101:                if (S_ISDIR(sb.st_mode))
        !           102:                        switch (sync_dircmp(argv[0], argv[1])) {
        !           103:                                case -1:
        !           104:                                        printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
        !           105:                                        return 127;
        !           106:                                case 0:
        !           107:                                        printf("Directory %s == %s\n\n", argv[0], argv[1]);
        !           108:                                        return 0;
        !           109:                                case 1:
        !           110:                                        printf("Directory %s != %s ::\n\n", argv[0], argv[1]);
        !           111:                        }
        !           112:        }
        !           113: 
        !           114:        if (sync_dircmpList(argv[0], argc > 1 ? argv[1] : NULL, lm, &list) == -1) {
        !           115:                printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, sync_GetErrno(), sync_GetError());
        !           116:                return 127;
        !           117:        }
        !           118: 
        !           119:        if (lm & 2 && strcmp(szFName, "-")) {
        !           120:                f = fopen(szFName, "w");
        !           121:                if (!f) {
        !           122:                        printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
        !           123:                        if (list)
        !           124:                                free(list);
        !           125:                        return 0;
        !           126:                }
        !           127:        }
        !           128:        for (i = 0; list[i].ch; i++)
        !           129:                fprintf(f, "%c  %s %s\n", list[i].ch, list[i].name, list[i].extra);
        !           130:        if (lm & 2)
        !           131:                fclose(f);
        !           132: 
        !           133:        printf("\nTotal count of elements = %d\n", i);
        !           134:        free(list);
        !           135:        return 1;
        !           136: }

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