Skip to content

Commit

Permalink
add joinpath, pass '-' to ssh-add
Browse files Browse the repository at this point in the history
  • Loading branch information
vodik committed Dec 11, 2013
1 parent 5e2acda commit fc6cb67
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
5 changes: 2 additions & 3 deletions envoy-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ static _noreturn_ void exec_wrapper(const char *cmd, int argc, char *argv[])

char *saveptr, *segment = strtok_r(buf, ":", &saveptr);
for (; segment; segment = strtok_r(NULL, ":", &saveptr)) {
_cleanup_free_ char *full_path;

safe_asprintf(&full_path, "%s/%s", segment, cmd);
char *full_path = joinpath(segment, cmd, NULL);
safe_execv(full_path, args);
free(full_path);
}
}

Expand Down
9 changes: 3 additions & 6 deletions envoy.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,12 @@ static int get_agent(struct agent_data_t *data, enum agent id, bool start)

static char *get_key_path(const char *home, const char *fragment)
{
char *out;

/* path exists, add it */
if (access(fragment, F_OK) == 0)
if (fragment[0] == '-' || access(fragment, F_OK) == 0)
return strdup(fragment);

/* assume it's a key in $HOME/.ssh */
safe_asprintf(&out, "%s/.ssh/%s", home, fragment);
return out;
return joinpath(home, ".ssh", fragment, NULL);
}

static _noreturn_ void add_keys(char **keys, int count)
Expand Down Expand Up @@ -307,7 +304,7 @@ int main(int argc, char *argv[])
print_env(&data);
/* fall through */
case ACTION_NONE:
if (data.status == ENVOY_RUNNING || data.type == AGENT_GPG_AGENT)
if (data.status != ENVOY_STARTED || data.type == AGENT_GPG_AGENT)
break;
/* fall through */
case ACTION_FORCE_ADD:
Expand Down
46 changes: 46 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,52 @@
#include <stdarg.h>
#include <err.h>

static char *joinpath_ap(const char *root, va_list ap)
{
size_t len;
char *ret, *p;
const char *temp;

va_list aq;
va_copy(aq, ap);

if (!root)
return NULL;

len = strlen(root);
while ((temp = va_arg(ap, const char *))) {
size_t temp_len = strlen(temp) + 1;
if (temp_len > ((size_t) -1) - len) {
return NULL;
}

len += temp_len;
}

ret = malloc(len + 1);
if (ret) {
p = stpcpy(ret, root);
while ((temp = va_arg(aq, const char *))) {
p++[0] = '/';
p = stpcpy(p, temp);
}
}

return ret;
}

char *joinpath(const char *root, ...)
{
va_list ap;
char *ret;

va_start(ap, root);
ret = joinpath_ap(root, ap);
va_end(ap);

return ret;
}

void safe_asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static inline void freep(void *p) { free(*(void **)p); }
static inline bool streq(const char *s1, const char *s2) { return strcmp(s1, s2) == 0; }
static inline bool strneq(const char *s1, const char *s2, size_t n) { return strncmp(s1, s2, n) == 0; }

char *joinpath(const char *root, ...);
void safe_asprintf(char **strp, const char *fmt, ...) _printf_(2, 3);

// vim: et:sts=4:sw=4:cino=(0

0 comments on commit fc6cb67

Please sign in to comment.