/
projectroot.c
51 lines (40 loc) · 1.08 KB
/
projectroot.c
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
#include <dirent.h>
#include <glob.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "commander.h"
#include "libprojectroot.h"
static void weight_mode(command_t* self) {
weight_mode_on = 1;
}
static void verbose(command_t* self) {
verbose_on = 1;
}
int main(int argc, char** argv) {
command_t cmd;
command_init(&cmd, argv[0], "1.1.2");
command_option(&cmd, "-t", "--top", "always prefer the topmost artifact", weight_mode);
command_option(&cmd, "-v", "--verbose", "be chatty", verbose);
command_parse(&cmd, argc, argv);
char cwd[1024];
if(!getcwd(cwd, sizeof(cwd))) {
fprintf(stderr, "Error: Couldn't get the current working directory\n");
return 1;
}
if(verbose_on) fprintf(stderr, "Finding: looking for root file in %s\n", cwd);
char* root;
if(weight_mode_on) {
root = find_project_root_weighted(cwd);
} else {
root = find_project_root(cwd);
}
if(root) {
printf("%s\n", root);
return 0;
}
fprintf(stderr, "Error: Couldn't find the project's root directory\n");
return 2;
}