diff options
author | Patrick Steinhardt <ps@pks.im> | 2018-10-26 14:54:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-26 14:54:25 +0200 |
commit | 32dc763c116999240440b5054798208d97b4c562 (patch) | |
tree | 8348a99862938d5d4ced1b8540c7674522489c1b /examples/network/common.c | |
parent | 2bd9b6b67706c8cb84d367f699cc9c48c2719dff (diff) | |
parent | 3b6e006e38ab0c41968f4135104162861fa3f984 (diff) | |
download | libgit2-maint/v0.26.tar.gz |
Merge pull request #4865 from pks-t/pks/v0.26.8v0.26.8maint/v0.26
Release v0.26.8
Diffstat (limited to 'examples/network/common.c')
-rw-r--r-- | examples/network/common.c | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/examples/network/common.c b/examples/network/common.c index 1a81a10f8..b0afb0238 100644 --- a/examples/network/common.c +++ b/examples/network/common.c @@ -16,6 +16,43 @@ # define UNUSED(x) x #endif +static int readline(char **out) +{ + int c, error = 0, length = 0, allocated = 0; + char *line = NULL; + + errno = 0; + + while ((c = getchar()) != EOF) { + if (length == allocated) { + allocated += 16; + + if ((line = realloc(line, allocated)) == NULL) { + error = -1; + goto error; + } + } + + if (c == '\n') + break; + + line[length++] = c; + } + + if (errno != 0) { + error = -1; + goto error; + } + + line[length] = '\0'; + *out = line; + line = NULL; + error = length; +error: + free(line); + return error; +} + int cred_acquire_cb(git_cred **out, const char * UNUSED(url), const char * UNUSED(username_from_url), @@ -26,14 +63,14 @@ int cred_acquire_cb(git_cred **out, int error; printf("Username: "); - if (getline(&username, NULL, stdin) < 0) { + if (readline(&username) < 0) { fprintf(stderr, "Unable to read username: %s", strerror(errno)); return -1; } /* Yup. Right there on your terminal. Careful where you copy/paste output. */ printf("Password: "); - if (getline(&password, NULL, stdin) < 0) { + if (readline(&password) < 0) { fprintf(stderr, "Unable to read password: %s", strerror(errno)); free(username); return -1; |