Provide Win32 implementation so that kinit and such work under Win32
authorDanilo Almeida <dalmeida@mit.edu>
Mon, 9 Aug 1999 21:46:49 +0000 (21:46 +0000)
committerDanilo Almeida <dalmeida@mit.edu>
Mon, 9 Aug 1999 21:46:49 +0000 (21:46 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11635 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/os/ChangeLog
src/lib/krb5/os/prompter.c

index 147d54f0ceb13500f781ffb407109ddfd5b5ad5a..8f2afbe960a92eb6fd2b8ed421156e8085aa44cf 100644 (file)
@@ -1,3 +1,8 @@
+1999-08-09  Danilo Almeida  <dalmeida@mit.edu>
+
+       * prompter.c (krb5_prompter_posix): Provide Win32 implementation
+       so that kinit and such work under Win32.
+
 1999-08-06  Danilo Almeida  <dalmeida@mit.edu>
 
        * def_realm.c (krb5_get_default_realm): 
index 684c5b5099848b8870ef768c5ea598963df8f318..b43e0ae0e16d838c611e63de3911e4b34deb4e2e 100644 (file)
@@ -121,6 +121,102 @@ cleanup:
 }
 #else /* MSDOS */
 
+#if defined(_WIN32)
+
+#include <io.h>
+
+KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
+krb5_prompter_posix(krb5_context context,
+                   void *data,
+                   const char *name,
+                   const char *banner,
+                   int num_prompts,
+                   krb5_prompt prompts[])
+{
+    HANDLE             handle;
+    DWORD              old_mode, new_mode;
+    char               *ptr;
+    int                        scratchchar;
+    krb5_error_code    errcode = 0;
+    int                        i;
+
+    handle = GetStdHandle(STD_INPUT_HANDLE);
+    if (handle == INVALID_HANDLE_VALUE)
+       return ENOTTY;
+    if (!GetConsoleMode(handle, &old_mode))
+       return ENOTTY;
+
+    new_mode = old_mode;
+    new_mode |=  ( ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT );
+    new_mode &= ~( ENABLE_ECHO_INPUT );
+
+    if (!SetConsoleMode(handle, new_mode))
+       return ENOTTY;
+
+    if (!SetConsoleMode(handle, old_mode))
+       return ENOTTY;
+
+    if (name) {
+       fputs(name, stdout);
+       fputs("\n", stdout);
+    }
+
+    if (banner) {
+       fputs(banner, stdout);
+       fputs("\n", stdout);
+    }
+
+    for (i = 0; i < num_prompts; i++) {
+       if (prompts[i].hidden) {
+           if (!SetConsoleMode(handle, new_mode)) {
+               errcode = ENOTTY;
+               goto cleanup;
+           }
+       }
+
+       fputs(prompts[i].prompt,stdout);
+       fputs(": ", stdout);
+       fflush(stdout);
+       memset(prompts[i].reply->data, 0, prompts[i].reply->length);
+
+       if (fgets(prompts[i].reply->data, prompts[i].reply->length, stdin)
+           == NULL) {
+           if (prompts[i].hidden)
+               putchar('\n');
+           errcode = KRB5_LIBOS_CANTREADPWD;
+           goto cleanup;
+       }
+       if (prompts[i].hidden)
+           putchar('\n');
+       /* fgets always null-terminates the returned string */
+
+       /* replace newline with null */
+       if ((ptr = strchr(prompts[i].reply->data, '\n')))
+           *ptr = '\0';
+       else /* flush rest of input line */
+           do {
+               scratchchar = getchar();
+           } while (scratchchar != EOF && scratchchar != '\n');
+    
+       prompts[i].reply->length = strlen(prompts[i].reply->data);
+
+       if (!SetConsoleMode(handle, old_mode)) {
+           errcode = ENOTTY;
+           goto cleanup;
+       }
+    }
+
+ cleanup:
+    if (errcode) {
+       for (i = 0; i < num_prompts; i++) {
+           memset(prompts[i].reply->data, 0, prompts[i].reply->length);
+       }
+    }
+    return errcode;
+}
+
+#else /* !_WIN32 */
+
 KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
 krb5_prompter_posix(krb5_context context,
                    void *data,
@@ -129,7 +225,7 @@ krb5_prompter_posix(krb5_context context,
                    int num_prompts,
                    krb5_prompt prompts[])
 {
-   return(EINVAL);
+    return(EINVAL);
 }
-#endif   /* !MSDOS */
-   
+#endif /* !_WIN32 */
+#endif /* !MSDOS */