summaryrefslogtreecommitdiffstats
path: root/main.c
blob: d9c3c53757992325c9837624f9e80dfe6281efd8 (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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>

#define REPO   ".cvn"
#define INDEX  "index"

static inline void init(int argc, char *argv[]);

struct command {
    char *name;
    void (*func)(int argc, char *argv[]);
};

struct command cmd[] = {
    {"init", init},
    {NULL, NULL}
};

int main(int argc, char *argv[])
{
	uint8_t i;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <command> [<args>]\n", argv[0]);
		return 1;
    }
	
	for (i = 0; cmd[i].name != NULL; i++) {
		if (strcmp(argv[1], cmd[i].name) == 0) {
			cmd[i].func(argc - 1, argv + 1);
			return 0;
		}
	}

	return 0;
}

static inline void init(int argc, char *argv[])
{
	char *branch;
	int opt, repo_fd, idx_fd;

	optind = 1; 
	branch = "master";

	while ((opt = getopt(argc, argv, "b:")) != -1) {
		switch (opt) {
			case 'b': 
				branch = optarg;
				break;
			default: 
				break;
        }
    }

	if (mkdir(REPO, 0755) == -1) {
		if (errno != EEXIST) {
			perror("Failed to create repository");
			return;
		}
	}

	if ((repo_fd = open(REPO, O_RDONLY | O_DIRECTORY)) == -1) {
		perror("Failed to open repository");
		return;
	}

	if ((idx_fd = openat(repo_fd, INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
		if (errno != EEXIST) {
			close(repo_fd);
			perror("Failed to create index");
			return;
		}
	}

	close(idx_fd);
	close(repo_fd);
	printf("Initialized repository in %s\n", REPO);
}