summaryrefslogtreecommitdiffstats
path: root/examples/c4touch.c
blob: 3410e559e7189daedf48c4a1d3a52a0f53f4932c (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
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

#include "cc4group.h"

int main(int argc, char* argv[])
{
	if(argc != 2 && argc != 3)
	{
		fprintf(stderr, "USAGE: %s <group> [author]\n", argv[0]);
		return EXIT_FAILURE;
	}

	CC4Group* group = cc4group.new();
	bool success, exists;
	struct stat st;
	if(stat(argv[1], &st) == 0)
	{
		success = cc4group.openExisting(group, argv[1]);
		exists = true;
	}
	else
	{
		success = cc4group.create(group);
		exists = false;
	}
	if(!success)
	{
		const char* error = cc4group.getErrorMessage(group);
		if(exists)
		{
			fprintf(stderr, "ERROR: Can not open group file \"%s\": %s\n", argv[1], error);
		}
		else
		{
			fprintf(stderr, "ERROR: Can not initialize empty group file: %s\n", error);
		}
	}

	if(success)
	{
		cc4group.setCreation(group, time(NULL), NULL, false);

		if(argc == 3)
		{
			cc4group.setMaker(group, argv[2], NULL, true);
		}

		success = (exists ? cc4group.saveOverwrite : cc4group.save)(group, argv[1]);

		if(!success)
		{
			fprintf(stderr, "ERROR: Can not save group file to \"%s\": %s\n", argv[1], cc4group.getErrorMessage(group));
		}
	}
	cc4group.delete(group);

	return success ? EXIT_SUCCESS : EXIT_FAILURE;
}