Skip to content

Instantly share code, notes, and snippets.

@charmander
Created March 13, 2015 20:59
Show Gist options
  • Save charmander/b372925000387c31f75e to your computer and use it in GitHub Desktop.
Save charmander/b372925000387c31f75e to your computer and use it in GitHub Desktop.
static char *
resolve_editor(const char *ed, size_t edlen, int nfiles, char **files, int *argc_out, char ***argv_out)
{
char *cp, **nargv, *editor, *editor_path = NULL;
int ac, i, nargc;
bool wasblank;
debug_decl(resolve_editor, SUDOERS_DEBUG_PLUGIN)
/* Note: editor becomes part of argv_out and is not freed. */
editor = sudo_emalloc(edlen + 1);
memcpy(editor, ed, edlen);
editor[edlen] = '\0';
/*
* Split editor into an argument vector; editor is reused (do not free).
* The EDITOR and VISUAL environment variables may contain command
* line args so look for those and alloc space for them too.
*/
nargc = 1;
for (wasblank = false, cp = editor; *cp != '\0'; cp++) {
if (isblank((unsigned char) *cp))
wasblank = true;
else if (wasblank) {
wasblank = false;
nargc++;
}
}
/* If we can't find the editor in the user's PATH, give up. */
cp = strtok(editor, " \t");
if (cp == NULL ||
find_path(cp, &editor_path, NULL, getenv("PATH"), 0) != FOUND) {
sudo_efree(editor);
debug_return_str(NULL);
}
nargv = (char **) sudo_emallocarray(nargc + 1 + nfiles + 1, sizeof(char *));
for (ac = 0; cp != NULL && ac < nargc; ac++) {
nargv[ac] = cp;
cp = strtok(NULL, " \t");
}
nargv[ac++] = "--";
for (i = 0; i < nfiles; )
nargv[ac++] = files[i++];
nargv[ac] = NULL;
*argc_out = nargc;
*argv_out = nargv;
debug_return_str(editor_path);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment