-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.c
More file actions
87 lines (77 loc) · 2 KB
/
path.c
File metadata and controls
87 lines (77 loc) · 2 KB
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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glob.h>
#include "path.h"
/* Returns full path of generated .troll directory */
char *init_troll_dir(const char *base) {
char *dir = getcwd(NULL, 0);
if (base) {
dir = (char *) realloc(dir, strlen(dir) + strlen(base) + 9);
}
else {
dir = (char *) realloc(dir, strlen(dir) + 9);
}
strcat(dir, "/");
if (base) {
strncat(dir, base, strlen(dir));
strcat(dir, "/");
mkdir(base, 0755);
}
strcat(dir, TROLL_DIR);
strcat(dir, "/");
mkdir(dir, 0755);
char *trolldir = (char *) malloc(strlen(dir) + 1);
memcpy(trolldir, dir, strlen(dir) + 1);
dir = (char *) realloc(dir, strlen(dir) + 7);
strcat(dir, "objects");
mkdir(dir, 0755);
char *indexpath = (char *) malloc(strlen(dir) + 6);
memcpy(indexpath, trolldir, strlen(trolldir) + 1);
strcat(indexpath, "index");
int fd = open(indexpath, O_CREAT, 0644);
close(fd);
char *headpath = (char *) malloc(strlen(dir) + 5);
memcpy(headpath, trolldir, strlen(trolldir) + 1);
strcat(headpath, "HEAD");
int headfd = open(headpath, O_CREAT, 0644);
close(headfd);
free(dir);
return trolldir;
}
char *recursive_get_repo_troll_dir() {
char *cwd = getcwd(NULL, 0);
int ret;
ret = chdir(".troll");
if (ret == 0 ) {
cwd = (char *) realloc(cwd, strlen(cwd) + 9);
strcat(cwd, "/.troll/");
return cwd;
}
if (!strcmp(cwd, "/")) {
return NULL;
}
ret = chdir("..");
return recursive_get_repo_troll_dir();
}
char *get_repo_troll_dir() {
char *cwd = getcwd(NULL, 0);
char *trolldir = recursive_get_repo_troll_dir();
chdir(cwd);
free(cwd);
return trolldir;
}
void die_if_not_troll_repo() {
char *trolldir = get_repo_troll_dir();
if (trolldir) {
free(trolldir);
}
else {
free(trolldir);
printf("Not a troll repository.\n");
exit(128);
}
}