7 static const char *guess_repo_name(const char *path);
8 static int parse_args(int argc, char **argv);
10 static const char *repo_path, *repo_name;
12 int main(int argc, char **argv)
14 if(parse_args(argc, argv) == -1) {
18 if(repo_init(repo_path) == -1) {
24 if(!(repo_name = guess_repo_name(repo_path))) {
29 printf("Serving %s from %s\n", repo_name, repo_path);
33 int i, count = repo_num_files();
34 for(i=0; i<count; i++) {
35 printf("%s\n", repo_file(i));
42 static const char *guess_repo_name(const char *path)
44 static char pathbuf[PATH_MAX];
48 if(!realpath(path, pathbuf)) {
51 len = strlen(pathbuf);
53 if(len > 0 && pathbuf[len - 1] == '/') {
56 if(len <= 0) goto fail;
58 if((last_slash = strrchr(pathbuf, '/'))) {
59 return last_slash + 1;
64 fprintf(stderr, "failed to resolve path %s while determining the repo name. Please specify an explicit name with -id\n", repo_path);
68 static void print_usage(const char *argv0)
70 printf("Usage: %s [options] [repo path]\n", argv0);
72 printf(" -id <name>: repo identifier (defaults to directory name)\n");
73 printf(" -h,-help: print help and exit\n");
76 static int parse_args(int argc, char **argv)
80 for(i=1; i<argc; i++) {
81 if(argv[i][0] == '-') {
82 if(strcmp(argv[i], "-id") == 0) {
83 repo_name = argv[++i];
84 } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
88 fprintf(stderr, "invalid option: %s\n", argv[i]);
93 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
100 if(!repo_path) repo_path = ".";