Request a password and echo it to stdout
authorWincent Colaiuta <win@wincent.com>
Sun, 15 May 2011 07:15:04 +0000 (00:15 -0700)
committerWincent Colaiuta <win@wincent.com>
Sun, 15 May 2011 07:15:04 +0000 (00:15 -0700)
Signed-off-by: Wincent Colaiuta <win@wincent.com>
main.c

diff --git a/main.c b/main.c
index 732daa8a6cc6ef46755e3515559fb11d03fa206d..9470ffbdeba5ad81a39cb41761daaa5ba075e3fc 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,7 +1,38 @@
 #include <CoreFoundation/CoreFoundation.h>
 
+#define PASSWORD_MAX_LEN 1024
+
 int main (int argc, const char * argv[]) {
-    // insert code here...
-    CFShow(CFSTR("Hello, World!\n"));
-    return 0;
+    SInt32 error;
+    CFOptionFlags flags = CFUserNotificationSecureTextField(0) | kCFUserNotificationPlainAlertLevel;
+    const void *keys[] = {
+        kCFUserNotificationAlertHeaderKey,
+        kCFUserNotificationTextFieldTitlesKey,
+        kCFUserNotificationDefaultButtonTitleKey,
+        kCFUserNotificationAlternateButtonTitleKey
+    };
+    const void *values[] = {
+        CFSTR("Enter your password"),
+        CFSTR("Password:"),
+        CFSTR("Continue"),
+        CFSTR("Cancel"),
+    };
+    CFDictionaryRef dictionary = CFDictionaryCreate(NULL, keys, values, sizeof(keys)/sizeof(*keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+    CFUserNotificationRef notification = CFUserNotificationCreate
+        (NULL, 0, flags, &error, dictionary);
+    CFOptionFlags responseFlags;
+    SInt32 response = CFUserNotificationReceiveResponse(notification, 0, &responseFlags);
+    if (response == 0 && (responseFlags & 0x3) != kCFUserNotificationAlternateResponse)
+    {
+        CFStringRef password = CFUserNotificationGetResponseValue(notification, kCFUserNotificationTextFieldValuesKey, 0);
+        char *buffer = malloc(PASSWORD_MAX_LEN);
+        if (buffer) {
+            if (CFStringGetCString(password, buffer, PASSWORD_MAX_LEN, kCFStringEncodingUTF8))
+                fprintf(stdout, "%s\n", buffer);
+            free(buffer);
+        }
+    }
+    CFRelease(notification);
+    CFRelease(dictionary);
+    return EXIT_SUCCESS;
 }