-->
Page 939
RETURN VALUE
The getopt()function returns the option character if the option was found successfully, : if there was a missing parameter for one of the options, ? for an unknown option character, or EOF for the end of the option list.
getopt_long() and getopt_long_only() also return the option character when a short option is recognized. For a long option, they return val if flag is NULL, and 0 otherwise. Error and EOF returns are the same as for getopt(), plus ? for an ambiguous match or an extraneous parameter.
ENVIROMENT VARIABLES
POSIXLY_CORRECT | If this is set, option processing stops as soon as a non-option argument is encountered. |
EXAMPLE
The following example program, from the source code, illustrates the use of getopt_long() with most of its features:
#include <stdio.h> int main (argc, argv) int argc; char **argv; { int c; int digit_optind = 0; while (1) { int this_option_optind = optind ? optind : 1; int option_index = 0; static struct option long_options[] = { {"add", 1, 0, 0}, {"append", 0, 0, 0}, {"delete", 1, 0, 0}, {"verbose", 0, 0, 0}, {"create", 1, 0, `c'}, {"file", 1, 0, 0g, f0, 0, 0, 0} }; c = getopt_long (argc, argv, "abc:d:012", long_options, &option_index); if (c == -1) break; switch(c) { case 0: printf ("option %s", long_options[option_index].name); if (optarg) printf (" with arg %s", optarg); printf ("\n"); break; case `0': case `1': case `2': if (digit_optind != 0 && digit_optind != this_option_optind) printf ("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf ("option %c\n", c); break; case `a': printf ("option a\n"); break; case `b':
Page 940
printf ("option b\n"); break; case `c': printf ("option c with value `%s'\n", optarg); break; case `d': printf ("option d with value `%s' \n", optarg); break; case `?': break; default: printf ("?? getopt returned character code 0%o ??\n", c); } } if (optind < argc) { printf ("non-option ARGV-elements: "); while (optind < argc) printf ("%s ", argv[optind++]); printf ("\n"); } exit (0); }
BUGS
This man page is confusing.
CONFORMS TO
getopt(): | POSIX.1, provided the environment variable POSIXLY_CORRECT is set. Otherwise, the elements of argv aren't really const, because they get permuted. They're set const in the prototype to be compatible with other systems. |
GNU, 30 August 1995
getpassGets a password
SYNOPSIS
#include <pwd.h> #include <unistd.h> char *getpass( const char * prompt );
DESCRIPTION
The getpass function displays a prompt to, and reads in, a password from, /dev/tty. If this file is not accessible, getpass displays the prompt on the standard error output and reads from the standard input.
The password may be up to _PASSWORD_LEN (currently 128) characters in length. Any additional characters and the terminating newline character are discarded. (This might be different in Linux.)
getpass turns off character echoing while reading the password.
RETURN VALUE
getpass returns a pointer to the null-terminated password.
Page 941
FILES
/dev/tty
SEE ALSO
crypt(3)
HISTORY
A getpass function appeared in Version 7 AT&T UNIX.
BUGS
The getpass function leaves its result in an internal static object and returns a pointer to that object. Subsequent calls to getpass will modify the same object.
The calling process should zero the password as soon as possible to avoid leaving the cleartext password visible in the process's address space.
BSD Man Page 29 November 1993
getprotoent, getprotobyname, getprotobynumber, setprotoent, endprotoentGet protocol entry
SYNOPSIS
#include <netdb.h> struct protoent *getprotoent(void); struct protoent *getprotobyname(const char *name); struct protoent *getprotobynumber(int proto); void setprotoent(int stayopen); void endprotoent(void);
DESCRIPTION
The getprotoent() function reads the next line from the file /etc/protocols and returns a structure protoent containing the broken-out fields from the line. The /etc/protocols file is opened if necessary.
The getprotobyname() function returns a protoent structure for the line from /etc/protocols that matches the protocol name name.
The getprotobynumber() function returns a protoent structure for the line that matches the protocol number number.
The setprotoent() function opens and rewinds the /etc/protocols file. If stayopen is true (1), the file will not be closed between calls to getprotobyname() or getprotobynumber().
The endprotoent() function closes /etc/protocols.
The protoent structure is defined in <netdb.h> as follows:
struct protoent { char *p_name; /* official protocol name */ char **p_aliases; /* alias list */ int p_proto; /* protocol number */ }
Page 942
The members of the protoent structure are
p_name | The official name of the protocol. |
p_aliases | A zero-terminated list of alternative names for the protocol. |
p_proto | The protocol number. |
RETURN VALUE
The getprotoent(), getprotobyname(), and getprotobynumber() functions return the protoent structure, or a NULL pointer if an error occurs or the end of the file is reached.
FILES
/etc/protocols protocol database file
CONFORMS TO
BSD 4.3
SEE ALSO
getservent(3), getnetent(3), protocols(5)
BSD, 24 April 1993
getpwReconstructs password line entry
SYNOPSIS
#include <pwd.h> #include <sys/types.h> int getpw(uid_t uid, char *buf);
DESCRIPTION
The getpw() function reconstructs the password line entry for the given user UID uid in the buffer buf. The returned buffer contains a line of format
name:passwd:uid:gid:gecos:dir:shell
The passwd structure is defined in <pwd.h> as follows:
struct passwd { char *pw_name; /*username*/ char *pw_passwd; /* user password */ uid_t pw_uid; /* user id */ gid_t pw_gid; /* group id */ char *pw_gecos; /* real name */ char *pw_dir; /* home directory */ char *pw_shell; /* shell program */ };
RETURN VALUE
The getpw()function returns 0 on success, or _1 if an error occurs.
ERRORS
ENOMEM | Insufficient memory to allocate passwd structure. |