File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / example / dircmp.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Tue Nov 13 14:19:39 2012 UTC (11 years, 7 months ago) by misho
Branches: MAIN
CVS tags: sync2_3, sync2_2, sync2_1, sync2_0, sync1_3, SYNC2_2, SYNC2_1, SYNC2_0, SYNC1_3, SYNC1_2, HEAD
version 1.2

    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.2 2012/11/13 14:19:39 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 <stdio.h>
   47: #include <stdlib.h>
   48: #include <unistd.h>
   49: #include <errno.h>
   50: #include <sys/types.h>
   51: #include <sys/param.h>
   52: #include <sys/stat.h>
   53: #include <aitsync.h>
   54: 
   55: 
   56: static void
   57: Usage()
   58: {
   59: 	printf(	"-= DirCmp =- Tool for compare directories and show differences\n"
   60: 		"====================\n\n"
   61: 		"  Syntax: dircmp [options] <dir> [<cmp_dir>]\n\n"
   62: 		"\t-l\t\tLong directory output ...\n"
   63: 		"\t-o <filename>\tOutput diff to filename\n"
   64: 		"\n");
   65: }
   66: 
   67: int
   68: main(int argc, char **argv)
   69: {
   70: 	char ch, szFName[MAXPATHLEN];
   71: 	int lm = 0;
   72: 	struct tagDirName *list;
   73: 	register int i;
   74: 	FILE *f = stdout;
   75: 	struct stat sb;
   76: 
   77: 	while ((ch = getopt(argc, argv, "hlo:")) != -1)
   78: 		switch (ch) {
   79: 			case 'o':
   80: 				lm |= 2;
   81: 				strlcpy(szFName, optarg, MAXPATHLEN);
   82: 				break;
   83: 			case 'l':
   84: 				lm |= 1;
   85: 				break;
   86: 			case 'h':
   87: 			default:
   88: 				Usage();
   89: 				return 127;
   90: 		}
   91: 	argc -= optind;
   92: 	argv += optind;
   93: 
   94: 	if (!argc || (!(lm & 2) && argc < 2)) {
   95: 		Usage();
   96: 		return 127;
   97: 	}
   98: 	/* check for general differences */
   99: 	if (argc > 1) {
  100: 		if (lstat(argv[1], &sb) == -1) {
  101: 			printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
  102: 			return 127;
  103: 		}
  104: 
  105: 		if (S_ISDIR(sb.st_mode))
  106: 			switch (sync_dircmp(argv[0], argv[1])) {
  107: 				case -1:
  108: 					printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
  109: 					return 127;
  110: 				case 0:
  111: 					printf("Directory %s == %s\n\n", argv[0], argv[1]);
  112: 					return 0;
  113: 				case 1:
  114: 					printf("Directory %s != %s ::\n\n", argv[0], argv[1]);
  115: 			}
  116: 	}
  117: 
  118: 	if (sync_dircmpList(argv[0], argc > 1 ? argv[1] : NULL, lm, &list) == -1) {
  119: 		printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, sync_GetErrno(), sync_GetError());
  120: 		return 127;
  121: 	}
  122: 
  123: 	if (lm & 2 && strcmp(szFName, "-")) {
  124: 		f = fopen(szFName, "w");
  125: 		if (!f) {
  126: 			printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
  127: 			if (list)
  128: 				free(list);
  129: 			return 0;
  130: 		}
  131: 	}
  132: 	for (i = 0; list[i].ch; i++)
  133: 		fprintf(f, "%c  %s %s\n", list[i].ch, list[i].name, list[i].extra);
  134: 	if (lm & 2)
  135: 		fclose(f);
  136: 
  137: 	printf("\nTotal count of elements = %d\n", i);
  138: 	free(list);
  139: 	return 1;
  140: }

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