Annotation of embedtools/src/direr.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: cfexec.c,v 1.1.1.1.2.3 2009/11/12 11:51:50 misho Exp $
! 7: *
! 8: *************************************************************************/
! 9: #include "global.h"
! 10:
! 11:
! 12: extern char compiled[], compiledby[], compilehost[];
! 13:
! 14: static void
! 15: Usage()
! 16: {
! 17: printf( "-= Direr =- Tool for compare directories by checksum\n"
! 18: "=== %s === %s@%s ===\n\n"
! 19: " Syntax: direr [options] <dir> [cmp_dir]\n\n"
! 20: "\t-s <cksum>\t\tCompare dir with this checksum ...\n"
! 21: "\n", compiled, compiledby, compilehost);
! 22: }
! 23:
! 24:
! 25: int
! 26: calcDir(const char *csDir, u_char **md)
! 27: {
! 28: DIR *dir;
! 29: struct dirent d, *pd;
! 30: MD5_CTX ctx;
! 31:
! 32: *md = malloc(MD5_DIGEST_LENGTH);
! 33: if (!*md) {
! 34: printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
! 35: return -1;
! 36: } else
! 37: memset(*md, 0, MD5_DIGEST_LENGTH);
! 38:
! 39: dir = opendir(csDir);
! 40: if (!dir) {
! 41: printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
! 42: free(*md);
! 43: return -1;
! 44: }
! 45:
! 46: MD5_Init(&ctx);
! 47: while (!readdir_r(dir, &d, &pd) && pd) {
! 48: if (d.d_type == DT_DIR && (!strcmp(d.d_name, ".") || !strcmp(d.d_name, "..")))
! 49: continue;
! 50: MD5_Update(&ctx, d.d_name, d.d_namlen);
! 51: }
! 52: MD5_Final(*md, &ctx);
! 53:
! 54: closedir(dir);
! 55: return 0;
! 56: }
! 57:
! 58: inline int
! 59: convMD5(u_char *md, char **str)
! 60: {
! 61: register int i;
! 62:
! 63: *str = malloc(MD5_DIGEST_LENGTH * 2 + 1);
! 64: if (!*str) {
! 65: printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
! 66: return -1;
! 67: } else
! 68: (*str)[MD5_DIGEST_LENGTH * 2] = 0;
! 69:
! 70: for (i = 0; i < MD5_DIGEST_LENGTH; i++)
! 71: snprintf(&(*str)[i * 2], 3, "%02x", md[i]);
! 72:
! 73: return 0;
! 74: }
! 75:
! 76: // ----------------------------------------------------
! 77:
! 78: int
! 79: main(int argc, char **argv)
! 80: {
! 81: u_char *md[2], mode = 0;
! 82: char szCmp[MAXPATHLEN], *str[2], ch;
! 83: int ret = 0;
! 84:
! 85: memset(md, 0, sizeof md);
! 86: memset(str, 0, sizeof str);
! 87: memset(szCmp, 0, MAXPATHLEN);
! 88: while ((ch = getopt(argc, argv, "hs:")) != -1)
! 89: switch (ch) {
! 90: case 's':
! 91: str[1] = strdup(optarg);
! 92: mode = 1;
! 93: break;
! 94: case 'h':
! 95: default:
! 96: Usage();
! 97: return 1;
! 98: }
! 99: argc -= optind;
! 100: argv += optind;
! 101:
! 102: if (argc < 1) {
! 103: Usage();
! 104: return 1;
! 105: }
! 106: if (argc > 1) {
! 107: strlcpy(szCmp, argv[1], MAXPATHLEN);
! 108: mode |= 2;
! 109: }
! 110:
! 111: if (calcDir(argv[0], &md[0]) == -1)
! 112: return 1;
! 113: if (convMD5(md[0], &str[0]) == -1) {
! 114: free(md[0]);
! 115: return 1;
! 116: }
! 117: printf("1.Directory '%s': %s\n", argv[0], str[0]);
! 118:
! 119: if (mode & 1 && *str[1]) {
! 120: ret |= !strcmp((char*) str[0], (char*) str[1]) << 1;
! 121: printf("Result checksum:: %s %s %s\n\n", str[0], ret & 1 ? "==" : "!=", str[1]);
! 122: free(str[1]);
! 123: }
! 124:
! 125: if (mode & 2 && *szCmp) {
! 126: if (calcDir(szCmp, &md[1]) == -1)
! 127: return 1;
! 128: if (convMD5(md[1], &str[1]) == -1) {
! 129: free(md[0]);
! 130: free(str[0]);
! 131: free(md[1]);
! 132: return 1;
! 133: }
! 134: printf("2.Directory '%s': %s\n", szCmp, str[1]);
! 135:
! 136: ret |= (!strcmp((char*) str[0], (char*) str[1]) << 2);
! 137: printf("Result directory:: %s %s %s\n\n", argv[0], ret & 2 ? "==" : "!=", szCmp);
! 138:
! 139: if (str[1])
! 140: free(str[1]);
! 141: if (md[1])
! 142: free(md[1]);
! 143: }
! 144:
! 145: if (str[0])
! 146: free(str[0]);
! 147: if (md[0])
! 148: free(md[0]);
! 149: return ret;
! 150: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>