#include #include #include #include #include #include #include #include "cc4group.h" const char* formatTime(int32_t time) { time_t tTime = time; static char ret[128]; strftime(ret, sizeof(ret), "%c", localtime(&tTime)); return ret; } const char* formatSize(uint32_t size) { static char ret[128]; if(size < 1000) { snprintf(ret, 128, "%4ud", size); return ret; } static const char prefixes[] = " KMGTPE"; const char* prefix = prefixes; uint32_t fract = 0; while(size >= 1000 && *(prefix + 1) != '\0') { ++prefix; fract = size % 1000; size /= 1000; } if(size < 10) { snprintf(ret, 128, "%1.1f%c", (float)size + (float)fract / 1000, *prefix); } else { snprintf(ret, 128, "%3u%c", size, *prefix); } return ret; } int main(int argc, char* argv[]) { if(argc > 2) { fprintf(stderr, "USAGE: %s [subgroup]\n", argv[0]); fprintf(stderr, "Reads the group from stdin into memory and then opens it to list its contents (or the contents of a subgroup).\n"); return EXIT_FAILURE; } size_t size = 1024; uint8_t* buffer = malloc(size); if(buffer == NULL) { fprintf(stderr, "ERROR: Can not allocate memory for the buffer: \"%s\"\n", strerror(errno)); return EXIT_FAILURE; } size_t pos = 0; for(;;) { ssize_t count = read(STDIN_FILENO, buffer + pos, size - pos); if(count == 0) { break; } if(count == -1) { fprintf(stderr, "ERROR: Reading stdin failed: \"%s\"\n", strerror(errno)); free(buffer); return EXIT_FAILURE; } pos += count; if(pos >= size) { buffer = realloc(buffer, size *= 2); if(buffer == NULL) { fprintf(stderr, "ERROR: Can not allocate more memory for the buffer: \"%s\"\n", strerror(errno)); return EXIT_FAILURE; } } } CC4Group* group = cc4group.new(); bool success = cc4group.openMemory(group, buffer, pos); free(buffer); if(!success) { fprintf(stderr, "ERROR: Can not open group file from stdin: %s\n", cc4group.getErrorMessage(group)); } else { CC4Group_EntryInfo* infos; size_t entries; success = cc4group.getEntryInfos(group, argc == 2 ? argv[1] : NULL, &infos, &entries); if(!success) { fprintf(stderr, "ERROR: Can not list group entries: %s\n", cc4group.getErrorMessage(group)); } else { for(size_t i = 0; i < entries; ++i) { printf("%c %s %s %s\t%s\n", infos[i].directory ? 'd' : infos[i].executable ? 'x' : ' ', formatTime(infos[i].modified), formatSize(infos[i].totalSize), infos[i].fileName, infos[i].author); } free(infos); } } cc4group.delete(group); return success ? EXIT_SUCCESS : EXIT_FAILURE; }