James Leddy wrote:
I'm writing a program that includes a call
which returns a string, namely getpass() in the standard library.
A UNIX standard library -- not an ANSI/ISO C standard library.
I was not surprised to learn that
I had improperly allocated memory to hold the return value.
I don't really know how a function returning a string works,
Type
man getpass
at your UNIX prompt.
and I have no knowledge at compile time
as to how long the string is going to be.
Should I just allocate enough memory for anything possible?
ex.
// char *pass;
// pass = malloc(100) maybe 10000?
const char* pass = getpass("password: ");
GETPASS(3) Linux Programmer’s Manual GETPASS(3)
NAME
getpass - get a password
SYNOPSIS
#include <unistd.h>
char *getpass( const char * prompt );
DESCRIPTION
This function is obsolete. Do not use it.
The getpass() function opens /dev/tty
(the controlling terminal of the process),
outputs the string prompt, turns off echoing,
reads one line (the "password"),
restores the terminal state and closes /dev/tty again.
RETURN VALUE
The function getpass returns a pointer
to a static buffer containing the (first PASS_MAX bytes of)
the password without the trailing newline, terminated by a NUL.
This buffer may be overwritten by a following call.
On error, the terminal state is restored,
errno is set appropriately, and NULL is returned.