|
|
| version 1.1.2.1, 2010/09/28 15:02:55 | version 1.1.2.3, 2010/09/28 15:50:21 |
|---|---|
| Line 18 Usage() | Line 18 Usage() |
| "\n", compiled, compiledby, compilehost); | "\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 | int |
| main(int argc, char **argv) | main(int argc, char **argv) |
| { | { |
| char ch; | char ch, m = 0, szLog[MAXPATHLEN]; |
| size_t siz = 0; | |
| while ((ch = getopt(argc, argv, "hfis:")) != -1) | while ((ch = getopt(argc, argv, "hfis:")) != -1) |
| switch (ch) { | switch (ch) { |
| case 'i': | |
| m |= 1; | |
| break; | |
| case 'f': | |
| m |= 2; | |
| case 's': | |
| siz = strtol(optarg, NULL, 0); | |
| if (siz < 1) { | |
| printf("Error:: size is invalid %u!\n", siz); | |
| Usage(); | |
| return 1; | |
| } | |
| break; | |
| case 'h': | case 'h': |
| default: | default: |
| Usage(); | Usage(); |
| Line 33 main(int argc, char **argv) | Line 90 main(int argc, char **argv) |
| } | } |
| argc -= optind; | argc -= optind; |
| argv += optind; | argv += optind; |
| if (m & 1 && !siz) { | |
| printf("Error:: not specified size when use with init log!\n"); | |
| 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; | return 0; |
| } | } |