{"thread":{"id":"27406","subject":"[PATCHv2] compat: add a getpass() compatibility function","startedAt":"2011-05-19T17:01:36Z","lastAt":"2011-05-23T17:59:49Z","messageCount":2,"participants":["Rafael Gieschke","Jakub Narebski"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"168224","messageId":"0301451F-9998-4208-8CE0-82B095DD5D32@gieschke.de","threadId":"27406","inReplyTo":null,"subject":"[PATCHv2] compat: add a getpass() compatibility function","fromName":"Rafael Gieschke","fromEmail":"rafael@gieschke.de","sentAt":"2011-05-19T17:01:36Z","receivedAt":"2011-05-19T17:01:36Z","isPatch":false,"sender":{"key":"rafael@gieschke.de","avatar":"https://avatars.githubusercontent.com/u/6069565?v=4"},"body":"\nIf NO_GETPASS is set, getpass is provided in compat/getpass.c from\nhttp://git.uclibc.org/uClibc/plain/libc/unistd/getpass.c\n(getpass() was renamed to gitgetpass()).\n\nSigned-off-by: Rafael Gieschke <rafael@gieschke.de>\n---\n Makefile          |    6 +++\n compat/getpass.c  |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n git-compat-util.h |    5 +++\n 3 files changed, 109 insertions(+), 0 deletions(-)\n create mode 100644 compat/getpass.c\n\ndiff --git a/Makefile b/Makefile\nindex d7147cf..e171fcd 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -71,6 +71,8 @@ all::\n #\n # Define NO_STRTOK_R if you don't have strtok_r in the C library.\n #\n+# Define NO_GETPASS if you don't have getpass in the C library.\n+#\n # Define NO_FNMATCH if you don't have fnmatch in the C library.\n #\n # Define NO_FNMATCH_CASEFOLD if your fnmatch function doesn't have the\n@@ -1406,6 +1408,10 @@ ifdef NO_STRTOK_R\n \tCOMPAT_CFLAGS += -DNO_STRTOK_R\n \tCOMPAT_OBJS += compat/strtok_r.o\n endif\n+ifdef NO_GETPASS\n+\tCOMPAT_CFLAGS += -DNO_GETPASS\n+\tCOMPAT_OBJS += compat/getpass.o\n+endif\n ifdef NO_FNMATCH\n \tCOMPAT_CFLAGS += -Icompat/fnmatch\n \tCOMPAT_CFLAGS += -DNO_FNMATCH\ndiff --git a/compat/getpass.c b/compat/getpass.c\nnew file mode 100644\nindex 0000000..f5170e9\n--- /dev/null\n+++ b/compat/getpass.c\n@@ -0,0 +1,98 @@\n+/* Copyright (C) 1992-1999,2001,2003,2004,2005 Free Software Foundation, Inc.\n+   This file is part of the GNU C Library.\n+\n+   The GNU C Library is free software; you can redistribute it and/or\n+   modify it under the terms of the GNU Lesser General Public\n+   License as published by the Free Software Foundation; either\n+   version 2.1 of the License, or (at your option) any later version.\n+\n+   The GNU C Library is distributed in the hope that it will be useful,\n+   but WITHOUT ANY WARRANTY; without even the implied warranty of\n+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n+   Lesser General Public License for more details.\n+\n+   You should have received a copy of the GNU Lesser General Public\n+   License along with the GNU C Library; if not, write to the Free\n+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA\n+   02111-1307 USA.  */\n+\n+#include <stdio.h>\n+#include <string.h>\n+#include <termios.h>\n+#include <unistd.h>\n+\n+#ifndef TCSASOFT\n+#define TCSASOFT 0\n+#endif\n+#define PWD_BUFFER_SIZE 256\n+\n+char *gitgetpass(const char *prompt)\n+{\n+  FILE *in, *out;\n+  struct termios s, t;\n+  int tty_changed;\n+  static char buf[PWD_BUFFER_SIZE];\n+  int nread;\n+\n+  /* Try to write to and read from the terminal if we can.\n+     If we can't open the terminal, use stderr and stdin.  */\n+\n+  in = fopen(\"/dev/tty\", \"r+\");\n+  if (in == NULL)\n+    {\n+      in = stdin;\n+      out = stderr;\n+    }\n+  else\n+    out = in;\n+\n+  /* Turn echoing off if it is on now.  */\n+\n+  if (tcgetattr(fileno(in), &t) == 0)\n+    {\n+      /* Save the old one. */\n+      s = t;\n+      /* Tricky, tricky. */\n+      t.c_lflag &= ~(ECHO|ISIG);\n+      tty_changed = (tcsetattr(fileno(in), TCSAFLUSH|TCSASOFT, &t) == 0);\n+      if (in != stdin) {\n+\t/* Disable buffering for read/write FILE to prevent problems with\n+\t * fseek and buffering for read/write auto-transitioning. */\n+\tsetvbuf(in, NULL, _IONBF, 0);\n+      }\n+    }\n+  else\n+    tty_changed = 0;\n+\n+  /* Write the prompt.  */\n+  fputs(prompt, out);\n+  fflush(out);\n+\n+  /* Read the password.  */\n+  fgets(buf, PWD_BUFFER_SIZE-1, in);\n+  if (buf != NULL)\n+    {\n+      nread = strlen(buf);\n+      if (nread < 0)\n+\tbuf[0] = '\\0';\n+      else if (buf[nread - 1] == '\\n')\n+\t{\n+\t  /* Remove the newline.  */\n+\t  buf[nread - 1] = '\\0';\n+\t  if (tty_changed)\n+\t    /* Write the newline that was not echoed.  */\n+\t    putc('\\n', out);\n+\t}\n+    }\n+\n+  /* Restore the original setting.  */\n+  if (tty_changed) {\n+    (void) tcsetattr(fileno(in), TCSAFLUSH|TCSASOFT, &s);\n+  }\n+\n+  if (in != stdin)\n+    /* We opened the terminal; now close it.  */\n+    fclose(in);\n+\n+  return buf;\n+}\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex e0bb81e..2ea5ac9 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -352,6 +352,11 @@ extern uintmax_t gitstrtoumax(const char *, char **, int);\n extern char *gitstrtok_r(char *s, const char *delim, char **save_ptr);\n #endif\n \n+#ifdef NO_GETPASS\n+#define getpass gitgetpass\n+extern char *gitgetpass(const char *prompt);\n+#endif\n+\n #ifdef NO_HSTRERROR\n #define hstrerror githstrerror\n extern const char *githstrerror(int herror);\n-- \n1.7.4\n"},{"id":"168514","messageId":"m3fwo5z4bi.fsf@localhost.localdomain","threadId":"27406","inReplyTo":"0301451F-9998-4208-8CE0-82B095DD5D32@gieschke.de","subject":"Re: [PATCHv2] compat: add a getpass() compatibility function","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2011-05-23T17:59:49Z","receivedAt":"2011-05-23T17:59:49Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Rafael Gieschke <rafael@gieschke.de> writes:\n\n> If NO_GETPASS is set, getpass is provided in compat/getpass.c from\n> http://git.uclibc.org/uClibc/plain/libc/unistd/getpass.c\n> (getpass() was renamed to gitgetpass()).\n> \n> Signed-off-by: Rafael Gieschke <rafael@gieschke.de>\n> ---\n>  Makefile          |    6 +++\n>  compat/getpass.c  |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  git-compat-util.h |    5 +++\n>  3 files changed, 109 insertions(+), 0 deletions(-)\n>  create mode 100644 compat/getpass.c\n\nThanks.  What is missing is optional automatic detection of this case,\nwhich means changes to configure.ac and config.mak.in; see e.g. how it\nis done for NO_STRLCPY.\n\n-- \nJakub Narebski\nPoland\nShadeHawk on #git\n"}]}