diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/c4ls_buffer.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/examples/c4ls_buffer.c b/examples/c4ls_buffer.c new file mode 100644 index 0000000..a021e61 --- /dev/null +++ b/examples/c4ls_buffer.c @@ -0,0 +1,130 @@ +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include <locale.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#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; +} |
