summaryrefslogtreecommitdiffstats
path: root/examples/c4ls.c
blob: 20b1acaa6d5ea79d5a7b33e9d770006ec12ff1bd (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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <locale.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, "%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 && argc != 3)
	{
		fprintf(stderr, "USAGE: %s <group> [subgroup]\n", argv[0]);
		return EXIT_FAILURE;
	}

	CC4Group* group = cc4group.new();
	cc4group.setLazy(group, false);
	bool success = cc4group.openExisting(group, argv[1]);
	if(!success)
	{
		fprintf(stderr, "ERROR: Can not open group file \"%s\": %s\n", argv[1], cc4group.getErrorMessage(group));
	}
	else
	{
		CC4Group_EntryInfo* infos;
		size_t entries;

		success = cc4group.getEntryInfos(group, argc == 3 ? argv[2] : 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;
}