return (uint64_t)status.st_mtime;
}
-char* getcurrdir(void) {
- size_t size = 4096;
- char *buf = NULL, *ptr = NULL;
- for (; ptr == NULL; size *= 2) {
- buf = realloc(buf, size);
- ptr = getcwd(buf, size);
- if (ptr == NULL && errno != ERANGE)
- die("Failed to retrieve current directory");
- }
- return buf;
-}
-
-char* dirname(char* path) {
- path = stringdup(path);
- char* end = strrchr(path, '/');
- if (!end) return (free(path), NULL);
- *end = '\0';
- return path;
-}
-
-bool try_chdir(char* fpath) {
- char* dir = dirname(fpath);
- bool success = (dir && *dir && chdir(dir) >= 0);
- free(dir);
- return success;
-}
-
-char* strconcat(char* dest, ...) {
- va_list args;
- char* curr = dest;
- va_start(args, dest);
- for (char* s = NULL; (s = va_arg(args, char*));)
- while (s && *s) *(curr++) = *(s++);
- va_end(args);
- *curr = '\0';
- return dest;
-}
-
-bool file_exists(char* path) {
- struct stat st;
- return (stat(path, &st) < 0);
-}
-
char* strmcat(char* first, ...) {
va_list args;
/* calculate the length of the final string */
for (char* s = NULL; (s = va_arg(args, char*));)
len += strlen(s);
va_end(args);
-
/* allocate the final string and copy the args into it */
char *str = malloc(len+1), *curr = str;
while (first && *first) *(curr++) = *(first++);
*curr = '\0';
return str;
}
-
-int daemonize(void) {
- pid_t pid;
- if (chdir("/") < 0) return -1;
- close(0), close(1), close(2);
- pid = fork();
- if (pid < 0) return -1;
- if (pid > 0) _exit(0);
- if (setsid() < 0) return -1;
- pid = fork();
- if (pid < 0) return -1;
- if (pid > 0) _exit(0);
- return 0;
-}
view_insert(win_view(FOCUSED), true, rune);
}
-void edit_relative(char* path) {
- char *currdir = NULL, *currpath = NULL, *relpath = NULL;
- char* origdir = getcurrdir();
-
- /* search for a ctags index file indicating the project root */
- if (try_chdir(path)) {
- currdir = getcurrdir();
- size_t sz = strlen(currdir) + strlen(path) + strlen("/tags") + 1;
- currpath = calloc(sz, 1);
- relpath = calloc(sz, 1);
- while (true) {
- /* figure out the current path to check */
- strconcat(currpath, currdir, "/tags", 0);
- if (file_exists(currpath)) {
- /* move up a dir */
- char* end = strrchr(currdir,'/');
- if (!end) break;
- char* temp = stringdup(relpath);
- strconcat(relpath, end, temp, 0);
- free(temp);
- *end = '\0';
- } else {
- break;
- }
- }
- }
-
- /* cd to the project directory or the original working directory and open
- the file relative to the new working directory */
- if (currdir && *currdir) {
- char* fname = strrchr(path, '/')+1;
- if (*relpath)
- strconcat(currpath, (*relpath == '/' ? relpath+1 : relpath), "/", fname, 0);
- else
- strconcat(currpath, fname, 0);
- chdir(currdir);
- view_init(win_view(EDIT), currpath, ondiagmsg);
- } else {
- chdir(origdir);
- view_init(win_view(EDIT), path, ondiagmsg);
- }
-
- /* cleanup */
- free(currdir);
- free(currpath);
- free(relpath);
- free(origdir);
-}
-
-
#ifndef TEST
int main(int argc, char** argv) {
/* setup the shell */
cmd_execwitharg(CMD_TIDE, *argv);
/* if we still have args left we're going to open it in this instance */
- if (*argv) edit_relative(*argv);
+ if (*argv) view_init(win_view(EDIT), *argv, ondiagmsg);
/* now create the window and start the event loop */
win_settext(TAGS, TagString);