]> PatchyGit - patchyircd.git/commitdiff
setpass: replace getpass with my own impl
authorEgor <egor@patchysicp.xyz>
Thu, 12 Jun 2025 05:53:46 +0000 (08:53 +0300)
committerEgor <egor@patchysicp.xyz>
Thu, 12 Jun 2025 05:53:46 +0000 (08:53 +0300)
src/setpass.cpp

index d33053c..0bab3ac 100644 (file)
@@ -1,12 +1,40 @@
 // Copyright (c) 2022-2023, 2025 Egor
 // SPDX-License-Identifier: GPL-2.0-or-later
+#include <string.h>
+#include <termios.h>
 #include <unistd.h>
 #include "passwd.h"
+
+char *read_password(const char *prompt)
+{
+       static char buffer[256];
+       struct termios oldattr, newattr;
+
+       if (tcgetattr(0, &oldattr) == -1)
+               return 0;
+       newattr = oldattr;
+       newattr.c_lflag &= ~(ECHO|ISIG);
+       tcsetattr(0, TCSAFLUSH, &newattr);
+
+       write(0, prompt, strlen(prompt));
+       ssize_t n = read(0, buffer, 256);
+       if (n < 0)
+               n = 0;
+       else if (n > 0 && buffer[n-1] == '\n')
+               n--;
+       buffer[n] = '\0';
+
+       tcsetattr(0, TCSAFLUSH, &oldattr);
+       write(0, "\n", 1);
+
+       return n ? buffer : 0;
+}
+
 int main(int argc, char **argv)
 {
        if (argc < 3)
                return 1;
-       char *pass = getpass("password: ");
+       char *pass = read_password("password: ");
        if (!pass)
                return 1;
        return -password_set(argv[1], pass, argv[2], "setpass.tmp");