Browse Source

libc: getpass,getutent: allocate buffer dynamically

 Saves 0.6k bss with default buffer size(256).

   text    data     bss     dec     hex filename
-  1172       8     408    1588     634 libc/misc/utmp/utent.os
-   429       0     256     685     2ad libc/unistd/getpass.os
+  1208       8      28    1244     4dc libc/misc/utmp/utent.os
+   471       0       4     475     1db libc/unistd/getpass.os
================================================================
    +78            -632

Signed-off-by: Leonid Lisovskiy <lly.dev@gmail.com>
Waldemar Brodkorb 8 years ago
parent
commit
a6975db114
2 changed files with 14 additions and 6 deletions
  1. 8 4
      libc/misc/utmp/utent.c
  2. 6 2
      libc/unistd/getpass.c

+ 8 - 4
libc/misc/utmp/utent.c

@@ -18,6 +18,7 @@
 #include <fcntl.h>
 #include <paths.h>
 #include <errno.h>
+#include <malloc.h>
 #include <string.h>
 #include "internal/utmp.h"
 #include <not-cancel.h>
@@ -27,7 +28,7 @@ __UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
 
 /* Some global crap */
 static int static_fd = -1;
-static struct UT static_utmp;
+static struct UT *static_utmp = NULL;
 static const char default_file[] = __DEFAULT_PATH_UTMP;
 static const char *current_file = default_file;
 
@@ -72,9 +73,12 @@ static struct UT *__get_unlocked(void)
 			return NULL;
 	}
 
-	if (read_not_cancel(static_fd, &static_utmp,
-				sizeof(static_utmp)) == sizeof(static_utmp)) {
-		return &static_utmp;
+	if (static_utmp == NULL)
+		static_utmp = (struct UT *)__uc_malloc(sizeof(struct UT));
+
+	if (read_not_cancel(static_fd, static_utmp,
+			sizeof(struct UT)) == sizeof(struct UT)) {
+	return static_utmp;
 	}
 
 	return NULL;

+ 6 - 2
libc/unistd/getpass.c

@@ -19,6 +19,7 @@
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
+#include <malloc.h>
 
 #if defined __USE_BSD || (defined __USE_XOPEN && !defined __USE_XOPEN2K)
 
@@ -38,9 +39,12 @@ char * getpass (const char *prompt)
   FILE *in, *out;
   struct termios s, t;
   int tty_changed;
-  static char buf[PWD_BUFFER_SIZE];
+  static char *buf = NULL;
   int nread;
 
+  if (buf == NULL)
+    buf = (char *)__uc_malloc(PWD_BUFFER_SIZE);
+
   /* Try to write to and read from the terminal if we can.
      If we can't open the terminal, use stderr and stdin.  */
 
@@ -74,7 +78,7 @@ char * getpass (const char *prompt)
   fflush(out);
 
   /* Read the password.  */
-  if (!fgets (buf, sizeof(buf), in))
+  if (!fgets (buf, PWD_BUFFER_SIZE, in))
     buf[0] = '\0';
   nread = strlen(buf);
   if (nread > 0 && buf[nread - 1] == '\n')