ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
password.c
Go to the documentation of this file.
1
6
9#include <libplatform/question.h>
12
13#include <string.h>
14
15int prompt_password(const char *prompt, char *password, size_t max_len) {
16 if (!prompt || !password || max_len < 2) {
17 return -1;
18 }
19
20 // The prompt renderer owns its header and masked input as one snapshot.
21 int result = platform_prompt_question(prompt, password, max_len, PROMPT_OPTS_PASSWORD);
22
23 // Validate password is valid UTF-8 (should always succeed since platform_prompt_question handles it)
24 if (result == 0 && !utf8_is_valid(password)) {
25 log_warn("Password contains invalid UTF-8 sequence, input may be corrupted");
26 }
27
28 return result;
29}
30
31int prompt_password_simple(const char *prompt, char *password, size_t max_len) {
32 return prompt_password_simple_kind(prompt, password, max_len, UI_PROMPT_PASSWORD);
33}
34
35int prompt_password_simple_kind(const char *prompt, char *password, size_t max_len, ui_prompt_kind_t kind) {
36 if (!prompt || !password || max_len < 2) {
37 return -1;
38 }
39
40 // Build prompt with colon suffix, using byte-length for memcpy
41 char full_prompt[BUFFER_SIZE_SMALL];
42 size_t prompt_byte_len = strlen(prompt);
43 if (prompt_byte_len >= sizeof(full_prompt) - 2) {
44 prompt_byte_len = sizeof(full_prompt) - 3;
45 }
46 memcpy(full_prompt, prompt, prompt_byte_len);
47 full_prompt[prompt_byte_len] = ':';
48 full_prompt[prompt_byte_len + 1] = '\0';
49
50 // Prompt for password with asterisk masking, same line
51 prompt_opts_t opts = PROMPT_OPTS_PASSWORD;
52 opts.kind = kind;
53 int result = platform_prompt_question(full_prompt, password, max_len, opts);
54
55 // Validate password is valid UTF-8 (should always succeed since platform_prompt_question handles it)
56 if (result == 0 && !utf8_is_valid(password)) {
57 log_warn("Password contains invalid UTF-8 sequence, input may be corrupted");
58 }
59
60 return result;
61}
Common buffer size definitions.
#define BUFFER_SIZE_SMALL
Small buffer size (256 bytes).
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
bool utf8_is_valid(const char *str)
Check if a string is valid UTF-8.
Definition utf8.c:128
int prompt_password(const char *prompt, char *password, size_t max_len)
Prompt the user for a password with secure input.
Definition password.c:15
int prompt_password_simple_kind(const char *prompt, char *password, size_t max_len, ui_prompt_kind_t kind)
Definition password.c:35
int prompt_password_simple(const char *prompt, char *password, size_t max_len)
Prompt the user for a password with simple formatting.
Definition password.c:31
📝 Logging API with multiple log levels and terminal output control
Password prompting utilities with secure input and formatting.
🔤 String Manipulation and Shell Escaping Utilities
🔤 UTF-8 Encoding and Decoding Utilities