--- embedtools/src/Attic/clog.c 2010/09/28 15:12:21 1.1.2.2 +++ embedtools/src/Attic/clog.c 2010/09/28 15:50:21 1.1.2.3 @@ -18,12 +18,55 @@ Usage() "\n", compiled, compiledby, compilehost); } +static int +initlog(const char *csLog, size_t size) +{ + int f, fill = size; + struct clogFooter cf = { 0 }; + char buffer[BUFLEN] = { 0 }; + memcpy(&cf.cf_magic, MAGIC, sizeof cf.cf_magic); + cf.cf_max = size - sizeof cf; + + f = open(csLog, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if (f == -1) { + printf("Error:: in open log %s #%d - %s\n", csLog, errno, strerror(errno)); + return -1; + } + while (fill > BUFLEN) + if (write(f, buffer, BUFLEN) == -1) { + printf("Error:: in fill log %s #%d - %s\n", csLog, errno, strerror(errno)); + goto end; + } else + fill -= BUFLEN; + if (fill > BUFLEN) { + printf("Error:: in fill log %s uninspected result!!!\n", csLog); + goto end; + } else if (fill && write(f, buffer, fill) == -1) { + printf("Error:: in last fill log %s #%d - %s\n", csLog, errno, strerror(errno)); + goto end; + } + // return to write cfooter + if (lseek(f, -(off_t)(sizeof cf), SEEK_END) == -1) { + printf("Error:: can`t set position for write footer #%d - %s\n", errno, strerror(errno)); + goto end; + } + if (write(f, &cf, sizeof cf) == -1) { + printf("Error:: in footer log %s #%d - %s\n", csLog, errno, strerror(errno)); + goto end; + } + +end: + close(f); + return 0; +} + + int main(int argc, char **argv) { char ch, m = 0, szLog[MAXPATHLEN]; - off_t siz = 0; + size_t siz = 0; while ((ch = getopt(argc, argv, "hfis:")) != -1) switch (ch) { @@ -33,9 +76,9 @@ main(int argc, char **argv) case 'f': m |= 2; case 's': - siz = strtoll(optarg, NULL, 0); + siz = strtol(optarg, NULL, 0); if (siz < 1) { - printf("Error:: size is invalid %llu!\n", siz); + printf("Error:: size is invalid %u!\n", siz); Usage(); return 1; } @@ -52,12 +95,20 @@ main(int argc, char **argv) Usage(); return 1; } + if (m == 3) { + printf("Error:: can`t in same time init and force!\n"); + Usage(); + return 1; + } if (!argc) { printf("Error:: not specified log file!\n"); Usage(); return 1; } else strlcpy(szLog, *argv, MAXPATHLEN); + + if (m & 1 && initlog(szLog, siz) == -1) + return 2; return 0; }