summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 65ec01dbf13ede4e46f410509b1138203b24da9c (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
#include <err.h>
#include <errno.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#define PATCH_DIR      ".cvn"
#define BASE           PATCH_DIR "/base"
#define EXCLUDE_PATHS  PATCH_DIR "ignore" 

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

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

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

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

	if (argc < 2)
		errx(1, "Usage: %s <command> [<args>]", argv[0]);

	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[])
{
	int opt;
	char *branch;

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

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

	if (mkdir(BASE, 0755) == -1) {
		if (errno != EEXIST)
			err(1, "Failed to create base directory.");
	}

	printf("Ready\n");
}

static inline void status(int argc, char *argv[])
{
	FILE *fp;
	pid_t pid;
	int status;
	int pipefd[2];
	char line[1024];

	if (pipe(pipefd) == -1)
		err(1, "pipe()");

	if ((pid = fork()) == -1)
		err(1, "fork()");

	if (pid == 0) {
		close(pipefd[0]);

		dup2(pipefd[1], STDOUT_FILENO);
		close(pipefd[1]);

		if (access(EXCLUDE_PATHS, F_OK) == 0) {
			char *args[] = {"diff", "-x", PATCH_DIR, "-X", 
				EXCLUDE_PATHS, "-rq", BASE, ".", NULL};
			execvp("diff", args);
		} else {
			char *args[] = {"diff", "-x", PATCH_DIR, "-rq", BASE, ".", NULL};
			execvp("diff", args);
		}
		err(1, "execvp");
	} else {
		waitpid(pid, &status, 0);
		close(pipefd[1]);

		if (!(fp = fdopen(pipefd[0], "r")))
			err(1, "fdopen()");

		const char *src = "Only in .: ";
		const char *rep = "(N) ";

		while (fgets(line, sizeof(line), fp) != NULL) {
			if (strncmp(line, src, strlen(src)) == 0)
				printf("%s%s", rep, line + strlen(src));
			else
				printf("%s", line);
		}

		fclose(fp);
		waitpid(pid, &status, 0);
	}
}