summaryrefslogtreecommitdiff
path: root/examples/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/common.c')
-rw-r--r--examples/common.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/examples/common.c b/examples/common.c
index e20767a9e..f1ee27e6a 100644
--- a/examples/common.c
+++ b/examples/common.c
@@ -13,6 +13,9 @@
*/
#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
#include "common.h"
@@ -289,3 +292,76 @@ int resolve_refish(git_annotated_commit **commit, git_repository *repo, const ch
return err;
}
+
+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 *url,
+ const char *username_from_url,
+ unsigned int allowed_types,
+ void *payload)
+{
+ char *username = NULL, *password = NULL;
+ int error;
+
+ UNUSED(url);
+ UNUSED(username_from_url);
+ UNUSED(allowed_types);
+ UNUSED(payload);
+
+ printf("Username: ");
+ 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 (readline(&password) < 0) {
+ fprintf(stderr, "Unable to read password: %s", strerror(errno));
+ free(username);
+ return -1;
+ }
+
+ error = git_cred_userpass_plaintext_new(out, username, password);
+
+ free(username);
+ free(password);
+
+ return error;
+}