summaryrefslogtreecommitdiffstats
path: root/examples/c4ls_buffer.c
blob: df63cd6d9a281d4e81d3e5aa7faa38a63fc7817f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <locale.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "cc4group.h"
#include "platform/platform.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, "%4u", 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;
	}

	SET_BINARY(STDIN_FILENO);

	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();
	cc4group.setLazy(group, false);
	bool success = cc4group.openMemory(group, buffer, pos, cc4group.MemoryManagement.Take);
	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, false);

		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;
}