From: Michael D. Lowis Date: Tue, 27 Mar 2018 01:11:19 +0000 (-0400) Subject: removed overly-complex logic for changing dir to project root X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=edd8f5ff44138eb8d000850d21138ab5d9a9cff8;p=projs%2Ftide.git removed overly-complex logic for changing dir to project root --- diff --git a/lib/utils.c b/lib/utils.c index f5eb902..deafb56 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -51,49 +51,6 @@ uint64_t modtime(char* path) { 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 */ @@ -102,7 +59,6 @@ char* strmcat(char* first, ...) { 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++); @@ -114,17 +70,3 @@ char* strmcat(char* 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; -} diff --git a/tide.c b/tide.c index 3291eab..5d46f59 100644 --- a/tide.c +++ b/tide.c @@ -635,56 +635,6 @@ static void oninput(Rune rune) { 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 */ @@ -700,7 +650,7 @@ int main(int argc, char** argv) { 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);