summaryrefslogtreecommitdiff
path: root/libc/manual/examples
diff options
context:
space:
mode:
authorgcc <gcc@7b3dc134-2b1b-0410-93df-9e9f96275f8d>2006-08-17 01:18:26 +0000
committergcc <gcc@7b3dc134-2b1b-0410-93df-9e9f96275f8d>2006-08-17 01:18:26 +0000
commit15f34685e7a9b5caf761af2ebf6afa20438d440b (patch)
treedc04ce3cdf040f198743c15b64557824de174680 /libc/manual/examples
parent1e848e0e775a36f6359161f5deb890942ef42ff3 (diff)
Import glibc-mainline for 2006-08-16
git-svn-id: svn://svn.eglibc.org/fsf/trunk@4 7b3dc134-2b1b-0410-93df-9e9f96275f8d
Diffstat (limited to 'libc/manual/examples')
-rw-r--r--libc/manual/examples/add.c30
-rw-r--r--libc/manual/examples/argp-ex1.c14
-rw-r--r--libc/manual/examples/argp-ex2.c44
-rw-r--r--libc/manual/examples/argp-ex3.c152
-rw-r--r--libc/manual/examples/argp-ex4.c167
-rw-r--r--libc/manual/examples/atexit.c15
-rw-r--r--libc/manual/examples/db.c52
-rw-r--r--libc/manual/examples/dir.c24
-rw-r--r--libc/manual/examples/dir2.c29
-rw-r--r--libc/manual/examples/execinfo.c37
-rw-r--r--libc/manual/examples/filecli.c54
-rw-r--r--libc/manual/examples/filesrv.c48
-rw-r--r--libc/manual/examples/fmtmsgexpl.c12
-rw-r--r--libc/manual/examples/genpass.c32
-rw-r--r--libc/manual/examples/inetcli.c59
-rw-r--r--libc/manual/examples/inetsrv.c103
-rw-r--r--libc/manual/examples/isockad.c23
-rw-r--r--libc/manual/examples/longopt.c98
-rw-r--r--libc/manual/examples/memopen.c17
-rw-r--r--libc/manual/examples/memstrm.c19
-rw-r--r--libc/manual/examples/mkfsock.c46
-rw-r--r--libc/manual/examples/mkisock.c31
-rw-r--r--libc/manual/examples/mygetpass.c25
-rw-r--r--libc/manual/examples/pipe.c70
-rw-r--r--libc/manual/examples/popen.c38
-rw-r--r--libc/manual/examples/rprintf.c67
-rw-r--r--libc/manual/examples/search.c93
-rw-r--r--libc/manual/examples/select.c41
-rw-r--r--libc/manual/examples/setjmp.c32
-rw-r--r--libc/manual/examples/sigh1.c36
-rw-r--r--libc/manual/examples/sigusr.c61
-rw-r--r--libc/manual/examples/stpcpy.c13
-rw-r--r--libc/manual/examples/strdupa.c19
-rw-r--r--libc/manual/examples/strftim.c31
-rw-r--r--libc/manual/examples/strncat.c14
-rw-r--r--libc/manual/examples/subopt.c78
-rw-r--r--libc/manual/examples/swapcontext.c99
-rw-r--r--libc/manual/examples/termios.c60
-rw-r--r--libc/manual/examples/testopt.c55
-rw-r--r--libc/manual/examples/testpass.c26
40 files changed, 1964 insertions, 0 deletions
diff --git a/libc/manual/examples/add.c b/libc/manual/examples/add.c
new file mode 100644
index 000000000..e4b1bba36
--- /dev/null
+++ b/libc/manual/examples/add.c
@@ -0,0 +1,30 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+int
+add_em_up (int count,...)
+{
+ va_list ap;
+ int i, sum;
+
+ va_start (ap, count); /* Initialize the argument list. */
+
+ sum = 0;
+ for (i = 0; i < count; i++)
+ sum += va_arg (ap, int); /* Get the next argument value. */
+
+ va_end (ap); /* Clean up. */
+ return sum;
+}
+
+int
+main (void)
+{
+ /* This call prints 16. */
+ printf ("%d\n", add_em_up (3, 5, 5, 6));
+
+ /* This call prints 55. */
+ printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
+
+ return 0;
+}
diff --git a/libc/manual/examples/argp-ex1.c b/libc/manual/examples/argp-ex1.c
new file mode 100644
index 000000000..50ac0ed97
--- /dev/null
+++ b/libc/manual/examples/argp-ex1.c
@@ -0,0 +1,14 @@
+/* Argp example #1 -- a minimal program using argp */
+
+/* This is (probably) the smallest possible program that
+ uses argp. It won't do much except give an error
+ messages and exit when there are any arguments, and print
+ a (rather pointless) messages for --help. */
+
+#include <argp.h>
+
+int main (int argc, char **argv)
+{
+ argp_parse (0, argc, argv, 0, 0, 0);
+ exit (0);
+}
diff --git a/libc/manual/examples/argp-ex2.c b/libc/manual/examples/argp-ex2.c
new file mode 100644
index 000000000..313c6819a
--- /dev/null
+++ b/libc/manual/examples/argp-ex2.c
@@ -0,0 +1,44 @@
+/* Argp example #2 -- a pretty minimal program using argp */
+
+/* This program doesn't use any options or arguments, but uses
+ argp to be compliant with the GNU standard command line
+ format.
+
+ In addition to making sure no arguments are given, and
+ implementing a --help option, this example will have a
+ --version option, and will put the given documentation string
+ and bug address in the --help output, as per GNU standards.
+
+ The variable ARGP contains the argument parser specification;
+ adding fields to this structure is the way most parameters are
+ passed to argp_parse (the first three fields are usually used,
+ but not in this small program). There are also two global
+ variables that argp knows about defined here,
+ ARGP_PROGRAM_VERSION and ARGP_PROGRAM_BUG_ADDRESS (they are
+ global variables because they will almost always be constant
+ for a given program, even if it uses different argument
+ parsers for various tasks). */
+
+#include <argp.h>
+
+const char *argp_program_version =
+ "argp-ex2 1.0";
+const char *argp_program_bug_address =
+ "<bug-gnu-utils@@gnu.org>";
+
+/* Program documentation. */
+static char doc[] =
+ "Argp example #2 -- a pretty minimal program using argp";
+
+/* Our argument parser. The @code{options}, @code{parser}, and
+ @code{args_doc} fields are zero because we have neither options or
+ arguments; @code{doc} and @code{argp_program_bug_address} will be
+ used in the output for @samp{--help}, and the @samp{--version}
+ option will print out @code{argp_program_version}. */
+static struct argp argp = { 0, 0, 0, doc };
+
+int main (int argc, char **argv)
+{
+ argp_parse (&argp, argc, argv, 0, 0, 0);
+ exit (0);
+}
diff --git a/libc/manual/examples/argp-ex3.c b/libc/manual/examples/argp-ex3.c
new file mode 100644
index 000000000..0ea9ce075
--- /dev/null
+++ b/libc/manual/examples/argp-ex3.c
@@ -0,0 +1,152 @@
+/* Argp example #3 -- a program with options and arguments using argp */
+
+/* This program uses the same features as example 2, and uses options and
+ arguments.
+
+ We now use the first four fields in ARGP, so here's a description of them:
+ OPTIONS -- A pointer to a vector of struct argp_option (see below)
+ PARSER -- A function to parse a single option, called by argp
+ ARGS_DOC -- A string describing how the non-option arguments should look
+ DOC -- A descriptive string about this program; if it contains a
+ vertical tab character (\v), the part after it will be
+ printed *following* the options
+
+ The function PARSER takes the following arguments:
+ KEY -- An integer specifying which option this is (taken
+ from the KEY field in each struct argp_option), or
+ a special key specifying something else; the only
+ special keys we use here are ARGP_KEY_ARG, meaning
+ a non-option argument, and ARGP_KEY_END, meaning
+ that all arguments have been parsed
+ ARG -- For an option KEY, the string value of its
+ argument, or NULL if it has none
+ STATE-- A pointer to a struct argp_state, containing
+ various useful information about the parsing state; used here
+ are the INPUT field, which reflects the INPUT argument to
+ argp_parse, and the ARG_NUM field, which is the number of the
+ current non-option argument being parsed
+ It should return either 0, meaning success, ARGP_ERR_UNKNOWN, meaning the
+ given KEY wasn't recognized, or an errno value indicating some other
+ error.
+
+ Note that in this example, main uses a structure to communicate with the
+ parse_opt function, a pointer to which it passes in the INPUT argument to
+ argp_parse. Of course, it's also possible to use global variables
+ instead, but this is somewhat more flexible.
+
+ The OPTIONS field contains a pointer to a vector of struct argp_option's;
+ that structure has the following fields (if you assign your option
+ structures using array initialization like this example, unspecified
+ fields will be defaulted to 0, and need not be specified):
+ NAME -- The name of this option's long option (may be zero)
+ KEY -- The KEY to pass to the PARSER function when parsing this option,
+ *and* the name of this option's short option, if it is a
+ printable ascii character
+ ARG -- The name of this option's argument, if any
+ FLAGS -- Flags describing this option; some of them are:
+ OPTION_ARG_OPTIONAL -- The argument to this option is optional
+ OPTION_ALIAS -- This option is an alias for the
+ previous option
+ OPTION_HIDDEN -- Don't show this option in --help output
+ DOC -- A documentation string for this option, shown in --help output
+
+ An options vector should be terminated by an option with all fields zero. */
+
+#include <argp.h>
+
+const char *argp_program_version =
+ "argp-ex3 1.0";
+const char *argp_program_bug_address =
+ "<bug-gnu-utils@@gnu.org>";
+
+/* Program documentation. */
+static char doc[] =
+ "Argp example #3 -- a program with options and arguments using argp";
+
+/* A description of the arguments we accept. */
+static char args_doc[] = "ARG1 ARG2";
+
+/* The options we understand. */
+static struct argp_option options[] = {
+ {"verbose", 'v', 0, 0, "Produce verbose output" },
+ {"quiet", 'q', 0, 0, "Don't produce any output" },
+ {"silent", 's', 0, OPTION_ALIAS },
+ {"output", 'o', "FILE", 0,
+ "Output to FILE instead of standard output" },
+ { 0 }
+};
+
+/* Used by @code{main} to communicate with @code{parse_opt}. */
+struct arguments
+{
+ char *args[2]; /* @var{arg1} & @var{arg2} */
+ int silent, verbose;
+ char *output_file;
+};
+
+/* Parse a single option. */
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ /* Get the @var{input} argument from @code{argp_parse}, which we
+ know is a pointer to our arguments structure. */
+ struct arguments *arguments = state->input;
+
+ switch (key)
+ {
+ case 'q': case 's':
+ arguments->silent = 1;
+ break;
+ case 'v':
+ arguments->verbose = 1;
+ break;
+ case 'o':
+ arguments->output_file = arg;
+ break;
+
+ case ARGP_KEY_ARG:
+ if (state->arg_num >= 2)
+ /* Too many arguments. */
+ argp_usage (state);
+
+ arguments->args[state->arg_num] = arg;
+
+ break;
+
+ case ARGP_KEY_END:
+ if (state->arg_num < 2)
+ /* Not enough arguments. */
+ argp_usage (state);
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+/* Our argp parser. */
+static struct argp argp = { options, parse_opt, args_doc, doc };
+
+int main (int argc, char **argv)
+{
+ struct arguments arguments;
+
+ /* Default values. */
+ arguments.silent = 0;
+ arguments.verbose = 0;
+ arguments.output_file = "-";
+
+ /* Parse our arguments; every option seen by @code{parse_opt} will
+ be reflected in @code{arguments}. */
+ argp_parse (&argp, argc, argv, 0, 0, &arguments);
+
+ printf ("ARG1 = %s\nARG2 = %s\nOUTPUT_FILE = %s\n"
+ "VERBOSE = %s\nSILENT = %s\n",
+ arguments.args[0], arguments.args[1],
+ arguments.output_file,
+ arguments.verbose ? "yes" : "no",
+ arguments.silent ? "yes" : "no");
+
+ exit (0);
+}
diff --git a/libc/manual/examples/argp-ex4.c b/libc/manual/examples/argp-ex4.c
new file mode 100644
index 000000000..8628a235d
--- /dev/null
+++ b/libc/manual/examples/argp-ex4.c
@@ -0,0 +1,167 @@
+/* Argp example #4 -- a program with somewhat more complicated options */
+
+/* This program uses the same features as example 3, but has more
+ options, and somewhat more structure in the -help output. It
+ also shows how you can `steal' the remainder of the input
+ arguments past a certain point, for programs that accept a
+ list of items. It also shows the special argp KEY value
+ ARGP_KEY_NO_ARGS, which is only given if no non-option
+ arguments were supplied to the program.
+
+ For structuring the help output, two features are used,
+ *headers* which are entries in the options vector with the
+ first four fields being zero, and a two part documentation
+ string (in the variable DOC), which allows documentation both
+ before and after the options; the two parts of DOC are
+ separated by a vertical-tab character ('\v', or '\013'). By
+ convention, the documentation before the options is just a
+ short string saying what the program does, and that afterwards
+ is longer, describing the behavior in more detail. All
+ documentation strings are automatically filled for output,
+ although newlines may be included to force a line break at a
+ particular point. All documentation strings are also passed to
+ the `gettext' function, for possible translation into the
+ current locale. */
+
+#include <stdlib.h>
+#include <error.h>
+#include <argp.h>
+
+const char *argp_program_version =
+ "argp-ex4 1.0";
+const char *argp_program_bug_address =
+ "<bug-gnu-utils@@prep.ai.mit.edu>";
+
+/* Program documentation. */
+static char doc[] =
+ "Argp example #4 -- a program with somewhat more complicated\
+options\
+\vThis part of the documentation comes *after* the options;\
+ note that the text is automatically filled, but it's possible\
+ to force a line-break, e.g.\n<-- here.";
+
+/* A description of the arguments we accept. */
+static char args_doc[] = "ARG1 [STRING...]";
+
+/* Keys for options without short-options. */
+#define OPT_ABORT 1 /* --abort */
+
+/* The options we understand. */
+static struct argp_option options[] = {
+ {"verbose", 'v', 0, 0, "Produce verbose output" },
+ {"quiet", 'q', 0, 0, "Don't produce any output" },
+ {"silent", 's', 0, OPTION_ALIAS },
+ {"output", 'o', "FILE", 0,
+ "Output to FILE instead of standard output" },
+
+ {0,0,0,0, "The following options should be grouped together:" },
+ {"repeat", 'r', "COUNT", OPTION_ARG_OPTIONAL,
+ "Repeat the output COUNT (default 10) times"},
+ {"abort", OPT_ABORT, 0, 0, "Abort before showing any output"},
+
+ { 0 }
+};
+
+/* Used by @code{main} to communicate with @code{parse_opt}. */
+struct arguments
+{
+ char *arg1; /* @var{arg1} */
+ char **strings; /* [@var{string}@dots{}] */
+ int silent, verbose, abort; /* @samp{-s}, @samp{-v}, @samp{--abort} */
+ char *output_file; /* @var{file} arg to @samp{--output} */
+ int repeat_count; /* @var{count} arg to @samp{--repeat} */
+};
+
+/* Parse a single option. */
+static error_t
+parse_opt (int key, char *arg, struct argp_state *state)
+{
+ /* Get the @code{input} argument from @code{argp_parse}, which we
+ know is a pointer to our arguments structure. */
+ struct arguments *arguments = state->input;
+
+ switch (key)
+ {
+ case 'q': case 's':
+ arguments->silent = 1;
+ break;
+ case 'v':
+ arguments->verbose = 1;
+ break;
+ case 'o':
+ arguments->output_file = arg;
+ break;
+ case 'r':
+ arguments->repeat_count = arg ? atoi (arg) : 10;
+ break;
+ case OPT_ABORT:
+ arguments->abort = 1;
+ break;
+
+ case ARGP_KEY_NO_ARGS:
+ argp_usage (state);
+
+ case ARGP_KEY_ARG:
+ /* Here we know that @code{state->arg_num == 0}, since we
+ force argument parsing to end before any more arguments can
+ get here. */
+ arguments->arg1 = arg;
+
+ /* Now we consume all the rest of the arguments.
+ @code{state->next} is the index in @code{state->argv} of the
+ next argument to be parsed, which is the first @var{string}
+ we're interested in, so we can just use
+ @code{&state->argv[state->next]} as the value for
+ arguments->strings.
+
+ @emph{In addition}, by setting @code{state->next} to the end
+ of the arguments, we can force argp to stop parsing here and
+ return. */
+ arguments->strings = &state->argv[state->next];
+ state->next = state->argc;
+
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
+
+/* Our argp parser. */
+static struct argp argp = { options, parse_opt, args_doc, doc };
+
+int main (int argc, char **argv)
+{
+ int i, j;
+ struct arguments arguments;
+
+ /* Default values. */
+ arguments.silent = 0;
+ arguments.verbose = 0;
+ arguments.output_file = "-";
+ arguments.repeat_count = 1;
+ arguments.abort = 0;
+
+ /* Parse our arguments; every option seen by @code{parse_opt} will be
+ reflected in @code{arguments}. */
+ argp_parse (&argp, argc, argv, 0, 0, &arguments);
+
+ if (arguments.abort)
+ error (10, 0, "ABORTED");
+
+ for (i = 0; i < arguments.repeat_count; i++)
+ {
+ printf ("ARG1 = %s\n", arguments.arg1);
+ printf ("STRINGS = ");
+ for (j = 0; arguments.strings[j]; j++)
+ printf (j == 0 ? "%s" : ", %s", arguments.strings[j]);
+ printf ("\n");
+ printf ("OUTPUT_FILE = %s\nVERBOSE = %s\nSILENT = %s\n",
+ arguments.output_file,
+ arguments.verbose ? "yes" : "no",
+ arguments.silent ? "yes" : "no");
+ }
+
+ exit (0);
+}
diff --git a/libc/manual/examples/atexit.c b/libc/manual/examples/atexit.c
new file mode 100644
index 000000000..42bba7112
--- /dev/null
+++ b/libc/manual/examples/atexit.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+void
+bye (void)
+{
+ puts ("Goodbye, cruel world....");
+}
+
+int
+main (void)
+{
+ atexit (bye);
+ exit (EXIT_SUCCESS);
+}
diff --git a/libc/manual/examples/db.c b/libc/manual/examples/db.c
new file mode 100644
index 000000000..1a1cb0c0d
--- /dev/null
+++ b/libc/manual/examples/db.c
@@ -0,0 +1,52 @@
+#include <grp.h>
+#include <pwd.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+int
+main (void)
+{
+ uid_t me;
+ struct passwd *my_passwd;
+ struct group *my_group;
+ char **members;
+
+ /* Get information about the user ID. */
+ me = getuid ();
+ my_passwd = getpwuid (me);
+ if (!my_passwd)
+ {
+ printf ("Couldn't find out about user %d.\n", (int) me);
+ exit (EXIT_FAILURE);
+ }
+
+ /* Print the information. */
+ printf ("I am %s.\n", my_passwd->pw_gecos);
+ printf ("My login name is %s.\n", my_passwd->pw_name);
+ printf ("My uid is %d.\n", (int) (my_passwd->pw_uid));
+ printf ("My home directory is %s.\n", my_passwd->pw_dir);
+ printf ("My default shell is %s.\n", my_passwd->pw_shell);
+
+ /* Get information about the default group ID. */
+ my_group = getgrgid (my_passwd->pw_gid);
+ if (!my_group)
+ {
+ printf ("Couldn't find out about group %d.\n",
+ (int) my_passwd->pw_gid);
+ exit (EXIT_FAILURE);
+ }
+
+ /* Print the information. */
+ printf ("My default group is %s (%d).\n",
+ my_group->gr_name, (int) (my_passwd->pw_gid));
+ printf ("The members of this group are:\n");
+ members = my_group->gr_mem;
+ while (*members)
+ {
+ printf (" %s\n", *(members));
+ members++;
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/libc/manual/examples/dir.c b/libc/manual/examples/dir.c
new file mode 100644
index 000000000..8ab77dc34
--- /dev/null
+++ b/libc/manual/examples/dir.c
@@ -0,0 +1,24 @@
+/*@group*/
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+/*@end group*/
+
+int
+main (void)
+{
+ DIR *dp;
+ struct dirent *ep;
+
+ dp = opendir ("./");
+ if (dp != NULL)
+ {
+ while (ep = readdir (dp))
+ puts (ep->d_name);
+ (void) closedir (dp);
+ }
+ else
+ perror ("Couldn't open the directory");
+
+ return 0;
+}
diff --git a/libc/manual/examples/dir2.c b/libc/manual/examples/dir2.c
new file mode 100644
index 000000000..a75c88574
--- /dev/null
+++ b/libc/manual/examples/dir2.c
@@ -0,0 +1,29 @@
+/*@group*/
+#include <stdio.h>
+#include <dirent.h>
+/*@end group*/
+
+static int
+one (const struct dirent *unused)
+{
+ return 1;
+}
+
+int
+main (void)
+{
+ struct dirent **eps;
+ int n;
+
+ n = scandir ("./", &eps, one, alphasort);
+ if (n >= 0)
+ {
+ int cnt;
+ for (cnt = 0; cnt < n; ++cnt)
+ puts (eps[cnt]->d_name);
+ }
+ else
+ perror ("Couldn't open the directory");
+
+ return 0;
+}
diff --git a/libc/manual/examples/execinfo.c b/libc/manual/examples/execinfo.c
new file mode 100644
index 000000000..5a0490195
--- /dev/null
+++ b/libc/manual/examples/execinfo.c
@@ -0,0 +1,37 @@
+#include <execinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Obtain a backtrace and print it to @code{stdout}. */
+void
+print_trace (void)
+{
+ void *array[10];
+ size_t size;
+ char **strings;
+ size_t i;
+
+ size = backtrace (array, 10);
+ strings = backtrace_symbols (array, size);
+
+ printf ("Obtained %zd stack frames.\n", size);
+
+ for (i = 0; i < size; i++)
+ printf ("%s\n", strings[i]);
+
+ free (strings);
+}
+
+/* A dummy function to make the backtrace more interesting. */
+void
+dummy_function (void)
+{
+ print_trace ();
+}
+
+int
+main (void)
+{
+ dummy_function ();
+ return 0;
+}
diff --git a/libc/manual/examples/filecli.c b/libc/manual/examples/filecli.c
new file mode 100644
index 000000000..9f64445fa
--- /dev/null
+++ b/libc/manual/examples/filecli.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#define SERVER "/tmp/serversocket"
+#define CLIENT "/tmp/mysocket"
+#define MAXMSG 512
+#define MESSAGE "Yow!!! Are we having fun yet?!?"
+
+int
+main (void)
+{
+ extern int make_named_socket (const char *name);
+ int sock;
+ char message[MAXMSG];
+ struct sockaddr_un name;
+ size_t size;
+ int nbytes;
+
+ /* Make the socket. */
+ sock = make_named_socket (CLIENT);
+
+ /* Initialize the server socket address. */
+ name.sun_family = AF_LOCAL;
+ strcpy (name.sun_path, SERVER);
+ size = strlen (name.sun_path) + sizeof (name.sun_family);
+
+ /* Send the datagram. */
+ nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
+ (struct sockaddr *) & name, size);
+ if (nbytes < 0)
+ {
+ perror ("sendto (client)");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Wait for a reply. */
+ nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
+ if (nbytes < 0)
+ {
+ perror ("recfrom (client)");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Print a diagnostic message. */
+ fprintf (stderr, "Client: got message: %s\n", message);
+
+ /* Clean up. */
+ remove (CLIENT);
+ close (sock);
+}
diff --git a/libc/manual/examples/filesrv.c b/libc/manual/examples/filesrv.c
new file mode 100644
index 000000000..32507c655
--- /dev/null
+++ b/libc/manual/examples/filesrv.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#define SERVER "/tmp/serversocket"
+#define MAXMSG 512
+
+int
+main (void)
+{
+ int sock;
+ char message[MAXMSG];
+ struct sockaddr_un name;
+ size_t size;
+ int nbytes;
+
+ /* Remove the filename first, it's ok if the call fails */
+ unlink (SERVER);
+
+ /* Make the socket, then loop endlessly. */
+ sock = make_named_socket (SERVER);
+ while (1)
+ {
+ /* Wait for a datagram. */
+ size = sizeof (name);
+ nbytes = recvfrom (sock, message, MAXMSG, 0,
+ (struct sockaddr *) & name, &size);
+ if (nbytes < 0)
+ {
+ perror ("recfrom (server)");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Give a diagnostic message. */
+ fprintf (stderr, "Server: got message: %s\n", message);
+
+ /* Bounce the message back to the sender. */
+ nbytes = sendto (sock, message, nbytes, 0,
+ (struct sockaddr *) & name, size);
+ if (nbytes < 0)
+ {
+ perror ("sendto (server)");
+ exit (EXIT_FAILURE);
+ }
+ }
+}
diff --git a/libc/manual/examples/fmtmsgexpl.c b/libc/manual/examples/fmtmsgexpl.c
new file mode 100644
index 000000000..42b8bb517
--- /dev/null
+++ b/libc/manual/examples/fmtmsgexpl.c
@@ -0,0 +1,12 @@
+#include <fmtmsg.h>
+
+int
+main (void)
+{
+ addseverity (5, "NOTE2");
+ fmtmsg (MM_PRINT, "only1field", MM_INFO, "text2", "action2", "tag2");
+ fmtmsg (MM_PRINT, "UX:cat", 5, "invalid syntax", "refer to manual",
+ "UX:cat:001");
+ fmtmsg (MM_PRINT, "label:foo", 6, "text", "action", "tag");
+ return 0;
+}
diff --git a/libc/manual/examples/genpass.c b/libc/manual/examples/genpass.c
new file mode 100644
index 000000000..a7626dbed
--- /dev/null
+++ b/libc/manual/examples/genpass.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+#include <crypt.h>
+
+int
+main(void)
+{
+ unsigned long seed[2];
+ char salt[] = "$1$........";
+ const char *const seedchars =
+ "./0123456789ABCDEFGHIJKLMNOPQRST"
+ "UVWXYZabcdefghijklmnopqrstuvwxyz";
+ char *password;
+ int i;
+
+ /* Generate a (not very) random seed.
+ You should do it better than this... */
+ seed[0] = time(NULL);
+ seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
+
+ /* Turn it into printable characters from `seedchars'. */
+ for (i = 0; i < 8; i++)
+ salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
+
+ /* Read in the user's password and encrypt it. */
+ password = crypt(getpass("Password:"), salt);
+
+ /* Print the results. */
+ puts(password);
+ return 0;
+}
diff --git a/libc/manual/examples/inetcli.c b/libc/manual/examples/inetcli.c
new file mode 100644
index 000000000..35dfb379e
--- /dev/null
+++ b/libc/manual/examples/inetcli.c
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#define PORT 5555
+#define MESSAGE "Yow!!! Are we having fun yet?!?"
+#define SERVERHOST "mescaline.gnu.org"
+
+void
+write_to_server (int filedes)
+{
+ int nbytes;
+
+ nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
+ if (nbytes < 0)
+ {
+ perror ("write");
+ exit (EXIT_FAILURE);
+ }
+}
+
+
+int
+main (void)
+{
+ extern void init_sockaddr (struct sockaddr_in *name,
+ const char *hostname,
+ uint16_t port);
+ int sock;
+ struct sockaddr_in servername;
+
+ /* Create the socket. */
+ sock = socket (PF_INET, SOCK_STREAM, 0);
+ if (sock < 0)
+ {
+ perror ("socket (client)");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Connect to the server. */
+ init_sockaddr (&servername, SERVERHOST, PORT);
+ if (0 > connect (sock,
+ (struct sockaddr *) &servername,
+ sizeof (servername)))
+ {
+ perror ("connect (client)");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Send data to the server. */
+ write_to_server (sock);
+ close (sock);
+ exit (EXIT_SUCCESS);
+}
diff --git a/libc/manual/examples/inetsrv.c b/libc/manual/examples/inetsrv.c
new file mode 100644
index 000000000..3d544c005
--- /dev/null
+++ b/libc/manual/examples/inetsrv.c
@@ -0,0 +1,103 @@
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#define PORT 5555
+#define MAXMSG 512
+
+int
+read_from_client (int filedes)
+{
+ char buffer[MAXMSG];
+ int nbytes;
+
+ nbytes = read (filedes, buffer, MAXMSG);
+ if (nbytes < 0)
+ {
+ /* Read error. */
+ perror ("read");
+ exit (EXIT_FAILURE);
+ }
+ else if (nbytes == 0)
+ /* End-of-file. */
+ return -1;
+ else
+ {
+ /* Data read. */
+ fprintf (stderr, "Server: got message: `%s'\n", buffer);
+ return 0;
+ }
+}
+
+int
+main (void)
+{
+ extern int make_socket (uint16_t port);
+ int sock;
+ fd_set active_fd_set, read_fd_set;
+ int i;
+ struct sockaddr_in clientname;
+ size_t size;
+
+ /* Create the socket and set it up to accept connections. */
+ sock = make_socket (PORT);
+ if (listen (sock, 1) < 0)
+ {
+ perror ("listen");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Initialize the set of active sockets. */
+ FD_ZERO (&active_fd_set);
+ FD_SET (sock, &active_fd_set);
+
+ while (1)
+ {
+ /* Block until input arrives on one or more active sockets. */
+ read_fd_set = active_fd_set;
+ if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
+ {
+ perror ("select");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Service all the sockets with input pending. */
+ for (i = 0; i < FD_SETSIZE; ++i)
+ if (FD_ISSET (i, &read_fd_set))
+ {
+ if (i == sock)
+ {
+ /* Connection request on original socket. */
+ int new;
+ size = sizeof (clientname);
+ new = accept (sock,
+ (struct sockaddr *) &clientname,
+ &size);
+ if (new < 0)
+ {
+ perror ("accept");
+ exit (EXIT_FAILURE);
+ }
+ fprintf (stderr,
+ "Server: connect from host %s, port %hd.\n",
+ inet_ntoa (clientname.sin_addr),
+ ntohs (clientname.sin_port));
+ FD_SET (new, &active_fd_set);
+ }
+ else
+ {
+ /* Data arriving on an already-connected socket. */
+ if (read_from_client (i) < 0)
+ {
+ close (i);
+ FD_CLR (i, &active_fd_set);
+ }
+ }
+ }
+ }
+}
diff --git a/libc/manual/examples/isockad.c b/libc/manual/examples/isockad.c
new file mode 100644
index 000000000..9c21149a8
--- /dev/null
+++ b/libc/manual/examples/isockad.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+void
+init_sockaddr (struct sockaddr_in *name,
+ const char *hostname,
+ uint16_t port)
+{
+ struct hostent *hostinfo;
+
+ name->sin_family = AF_INET;
+ name->sin_port = htons (port);
+ hostinfo = gethostbyname (hostname);
+ if (hostinfo == NULL)
+ {
+ fprintf (stderr, "Unknown host %s.\n", hostname);
+ exit (EXIT_FAILURE);
+ }
+ name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
+}
diff --git a/libc/manual/examples/longopt.c b/libc/manual/examples/longopt.c
new file mode 100644
index 000000000..1661327f5
--- /dev/null
+++ b/libc/manual/examples/longopt.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+
+/* Flag set by @samp{--verbose}. */
+static int verbose_flag;
+
+int
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ int c;
+
+ while (1)
+ {
+ static struct option long_options[] =
+ {
+ /* These options set a flag. */
+ {"verbose", no_argument, &verbose_flag, 1},
+ {"brief", no_argument, &verbose_flag, 0},
+ /* These options don't set a flag.
+ We distinguish them by their indices. */
+ {"add", no_argument, 0, 'a'},
+ {"append", no_argument, 0, 'b'},
+ {"delete", required_argument, 0, 'd'},
+ {"create", required_argument, 0, 'c'},
+ {"file", required_argument, 0, 'f'},
+ {0, 0, 0, 0}
+ };
+ /* @code{getopt_long} stores the option index here. */
+ int option_index = 0;
+
+ c = getopt_long (argc, argv, "abc:d:f:",
+ long_options, &option_index);
+
+ /* Detect the end of the options. */
+ if (c == -1)
+ break;
+
+ switch (c)
+ {
+ case 0:
+ /* If this option set a flag, do nothing else now. */
+ if (long_options[option_index].flag != 0)
+ break;
+ printf ("option %s", long_options[option_index].name);
+ if (optarg)
+ printf (" with arg %s", optarg);
+ printf ("\n");
+ break;
+
+ case 'a':
+ puts ("option -a\n");
+ break;
+
+ case 'b':
+ puts ("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 'f':
+ printf ("option -f with value `%s'\n", optarg);
+ break;
+
+ case '?':
+ /* @code{getopt_long} already printed an error message. */
+ break;
+
+ default:
+ abort ();
+ }
+ }
+
+ /* Instead of reporting @samp{--verbose}
+ and @samp{--brief} as they are encountered,
+ we report the final status resulting from them. */
+ if (verbose_flag)
+ puts ("verbose flag is set");
+
+ /* Print any remaining command line arguments (not options). */
+ if (optind < argc)
+ {
+ printf ("non-option ARGV-elements: ");
+ while (optind < argc)
+ printf ("%s ", argv[optind++]);
+ putchar ('\n');
+ }
+
+ exit (0);
+}
diff --git a/libc/manual/examples/memopen.c b/libc/manual/examples/memopen.c
new file mode 100644
index 000000000..682830fe5
--- /dev/null
+++ b/libc/manual/examples/memopen.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+static char buffer[] = "foobar";
+
+int
+main (void)
+{
+ int ch;
+ FILE *stream;
+
+ stream = fmemopen (buffer, strlen (buffer), "r");
+ while ((ch = fgetc (stream)) != EOF)
+ printf ("Got %c\n", ch);
+ fclose (stream);
+
+ return 0;
+}
diff --git a/libc/manual/examples/memstrm.c b/libc/manual/examples/memstrm.c
new file mode 100644
index 000000000..1674c36e0
--- /dev/null
+++ b/libc/manual/examples/memstrm.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+
+int
+main (void)
+{
+ char *bp;
+ size_t size;
+ FILE *stream;
+
+ stream = open_memstream (&bp, &size);
+ fprintf (stream, "hello");
+ fflush (stream);
+ printf ("buf = `%s', size = %d\n", bp, size);
+ fprintf (stream, ", world");
+ fclose (stream);
+ printf ("buf = `%s', size = %d\n", bp, size);
+
+ return 0;
+}
diff --git a/libc/manual/examples/mkfsock.c b/libc/manual/examples/mkfsock.c
new file mode 100644
index 000000000..615ecd868
--- /dev/null
+++ b/libc/manual/examples/mkfsock.c
@@ -0,0 +1,46 @@
+#include <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+int
+make_named_socket (const char *filename)
+{
+ struct sockaddr_un name;
+ int sock;
+ size_t size;
+
+ /* Create the socket. */
+ sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
+ if (sock < 0)
+ {
+ perror ("socket");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Bind a name to the socket. */
+ name.sun_family = AF_LOCAL;
+ strncpy (name.sun_path, filename, sizeof (name.sun_path));
+ name.sun_path[sizeof (name.sun_path) - 1] = '\0';
+
+ /* The size of the address is
+ the offset of the start of the filename,
+ plus its length,
+ plus one for the terminating null byte.
+ Alternatively you can just do:
+ size = SUN_LEN (&name);
+ */
+ size = (offsetof (struct sockaddr_un, sun_path)
+ + strlen (name.sun_path) + 1);
+
+ if (bind (sock, (struct sockaddr *) &name, size) < 0)
+ {
+ perror ("bind");
+ exit (EXIT_FAILURE);
+ }
+
+ return sock;
+}
diff --git a/libc/manual/examples/mkisock.c b/libc/manual/examples/mkisock.c
new file mode 100644
index 000000000..2fd8b3d2f
--- /dev/null
+++ b/libc/manual/examples/mkisock.c
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+int
+make_socket (uint16_t port)
+{
+ int sock;
+ struct sockaddr_in name;
+
+ /* Create the socket. */
+ sock = socket (PF_INET, SOCK_STREAM, 0);
+ if (sock < 0)
+ {
+ perror ("socket");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Give the socket a name. */
+ name.sin_family = AF_INET;
+ name.sin_port = htons (port);
+ name.sin_addr.s_addr = htonl (INADDR_ANY);
+ if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0)
+ {
+ perror ("bind");
+ exit (EXIT_FAILURE);
+ }
+
+ return sock;
+}
diff --git a/libc/manual/examples/mygetpass.c b/libc/manual/examples/mygetpass.c
new file mode 100644
index 000000000..6fe06f463
--- /dev/null
+++ b/libc/manual/examples/mygetpass.c
@@ -0,0 +1,25 @@
+#include <termios.h>
+#include <stdio.h>
+
+ssize_t
+my_getpass (char **lineptr, size_t *n, FILE *stream)
+{
+ struct termios old, new;
+ int nread;
+
+ /* Turn echoing off and fail if we can't. */
+ if (tcgetattr (fileno (stream), &old) != 0)
+ return -1;
+ new = old;
+ new.c_lflag &= ~ECHO;
+ if (tcsetattr (fileno (stream), TCSAFLUSH, &new) != 0)
+ return -1;
+
+ /* Read the password. */
+ nread = getline (lineptr, n, stream);
+
+ /* Restore terminal. */
+ (void) tcsetattr (fileno (stream), TCSAFLUSH, &old);
+
+ return nread;
+}
diff --git a/libc/manual/examples/pipe.c b/libc/manual/examples/pipe.c
new file mode 100644
index 000000000..92d339a7b
--- /dev/null
+++ b/libc/manual/examples/pipe.c
@@ -0,0 +1,70 @@
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Read characters from the pipe and echo them to @code{stdout}. */
+
+void
+read_from_pipe (int file)
+{
+ FILE *stream;
+ int c;
+ stream = fdopen (file, "r");
+ while ((c = fgetc (stream)) != EOF)
+ putchar (c);
+ fclose (stream);
+}
+
+/* Write some random text to the pipe. */
+
+void
+write_to_pipe (int file)
+{
+ FILE *stream;
+ stream = fdopen (file, "w");
+ fprintf (stream, "hello, world!\n");
+ fprintf (stream, "goodbye, world!\n");
+ fclose (stream);
+}
+
+int
+main (void)
+{
+ pid_t pid;
+ int mypipe[2];
+
+/*@group*/
+ /* Create the pipe. */
+ if (pipe (mypipe))
+ {
+ fprintf (stderr, "Pipe failed.\n");
+ return EXIT_FAILURE;
+ }
+/*@end group*/
+
+ /* Create the child process. */
+ pid = fork ();
+ if (pid == (pid_t) 0)
+ {
+ /* This is the child process.
+ Close other end first. */
+ close (mypipe[1]);
+ read_from_pipe (mypipe[0]);
+ return EXIT_SUCCESS;
+ }
+ else if (pid < (pid_t) 0)
+ {
+ /* The fork failed. */
+ fprintf (stderr, "Fork failed.\n");
+ return EXIT_FAILURE;
+ }
+ else
+ {
+ /* This is the parent process.
+ Close other end first. */
+ close (mypipe[0]);
+ write_to_pipe (mypipe[1]);
+ return EXIT_SUCCESS;
+ }
+}
diff --git a/libc/manual/examples/popen.c b/libc/manual/examples/popen.c
new file mode 100644
index 000000000..1b9ee826b
--- /dev/null
+++ b/libc/manual/examples/popen.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+void
+write_data (FILE * stream)
+{
+ int i;
+ for (i = 0; i < 100; i++)
+ fprintf (stream, "%d\n", i);
+ if (ferror (stream))
+ {
+ fprintf (stderr, "Output to stream failed.\n");
+ exit (EXIT_FAILURE);
+ }
+}
+
+/*@group*/
+int
+main (void)
+{
+ FILE *output;
+
+ output = popen ("more", "w");
+ if (!output)
+ {
+ fprintf (stderr,
+ "incorrect parameters or too many files.\n");
+ return EXIT_FAILURE;
+ }
+ write_data (output);
+ if (pclose (output) != 0)
+ {
+ fprintf (stderr,
+ "Could not run more or other error.\n");
+ }
+ return EXIT_SUCCESS;
+}
+/*@end group*/
diff --git a/libc/manual/examples/rprintf.c b/libc/manual/examples/rprintf.c
new file mode 100644
index 000000000..2b8f6bfe7
--- /dev/null
+++ b/libc/manual/examples/rprintf.c
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <printf.h>
+
+/*@group*/
+typedef struct
+{
+ char *name;
+}
+Widget;
+/*@end group*/
+
+int
+print_widget (FILE *stream,
+ const struct printf_info *info,
+ const void *const *args)
+{
+ const Widget *w;
+ char *buffer;
+ int len;
+
+ /* Format the output into a string. */
+ w = *((const Widget **) (args[0]));
+ len = asprintf (&buffer, "<Widget %p: %s>", w, w->name);
+ if (len == -1)
+ return -1;
+
+ /* Pad to the minimum field width and print to the stream. */
+ len = fprintf (stream, "%*s",
+ (info->left ? -info->width : info->width),
+ buffer);
+
+ /* Clean up and return. */
+ free (buffer);
+ return len;
+}
+
+
+int
+print_widget_arginfo (const struct printf_info *info, size_t n,
+ int *argtypes)
+{
+ /* We always take exactly one argument and this is a pointer to the
+ structure.. */
+ if (n > 0)
+ argtypes[0] = PA_POINTER;
+ return 1;
+}
+
+
+int
+main (void)
+{
+ /* Make a widget to print. */
+ Widget mywidget;
+ mywidget.name = "mywidget";
+
+ /* Register the print function for widgets. */
+ register_printf_function ('W', print_widget, print_widget_arginfo);
+
+ /* Now print the widget. */
+ printf ("|%W|\n", &mywidget);
+ printf ("|%35W|\n", &mywidget);
+ printf ("|%-35W|\n", &mywidget);
+
+ return 0;
+}
diff --git a/libc/manual/examples/search.c b/libc/manual/examples/search.c
new file mode 100644
index 000000000..182e6e4a3
--- /dev/null
+++ b/libc/manual/examples/search.c
@@ -0,0 +1,93 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+/* Define an array of critters to sort. */
+
+struct critter
+ {
+ const char *name;
+ const char *species;
+ };
+
+struct critter muppets[] =
+ {
+ {"Kermit", "frog"},
+ {"Piggy", "pig"},
+ {"Gonzo", "whatever"},
+ {"Fozzie", "bear"},
+ {"Sam", "eagle"},
+ {"Robin", "frog"},
+ {"Animal", "animal"},
+ {"Camilla", "chicken"},
+ {"Sweetums", "monster"},
+ {"Dr. Strangepork", "pig"},
+ {"Link Hogthrob", "pig"},
+ {"Zoot", "human"},
+ {"Dr. Bunsen Honeydew", "human"},
+ {"Beaker", "human"},
+ {"Swedish Chef", "human"}
+ };
+
+int count = sizeof (muppets) / sizeof (struct critter);
+
+
+
+/* This is the comparison function used for sorting and searching. */
+
+int
+critter_cmp (const struct critter *c1, const struct critter *c2)
+{
+ return strcmp (c1->name, c2->name);
+}
+
+
+/* Print information about a critter. */
+
+void
+print_critter (const struct critter *c)
+{
+ printf ("%s, the %s\n", c->name, c->species);
+}
+
+
+/*@group*/
+/* Do the lookup into the sorted array. */
+
+void
+find_critter (const char *name)
+{
+ struct critter target, *result;
+ target.name = name;
+ result = bsearch (&target, muppets, count, sizeof (struct critter),
+ critter_cmp);
+ if (result)
+ print_critter (result);
+ else
+ printf ("Couldn't find %s.\n", name);
+}
+/*@end group*/
+
+/* Main program. */
+
+int
+main (void)
+{
+ int i;
+
+ for (i = 0; i < count; i++)
+ print_critter (&muppets[i]);
+ printf ("\n");
+
+ qsort (muppets, count, sizeof (struct critter), critter_cmp);
+
+ for (i = 0; i < count; i++)
+ print_critter (&muppets[i]);
+ printf ("\n");
+
+ find_critter ("Kermit");
+ find_critter ("Gonzo");
+ find_critter ("Janice");
+
+ return 0;
+}
diff --git a/libc/manual/examples/select.c b/libc/manual/examples/select.c
new file mode 100644
index 000000000..a65ed7750
--- /dev/null
+++ b/libc/manual/examples/select.c
@@ -0,0 +1,41 @@
+/*@group*/
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+/*@end group*/
+
+/*@group*/
+int
+input_timeout (int filedes, unsigned int seconds)
+{
+ fd_set set;
+ struct timeval timeout;
+/*@end group*/
+
+ /* Initialize the file descriptor set. */
+ FD_ZERO (&set);
+ FD_SET (filedes, &set);
+
+ /* Initialize the timeout data structure. */
+ timeout.tv_sec = seconds;
+ timeout.tv_usec = 0;
+
+/*@group*/
+ /* @code{select} returns 0 if timeout, 1 if input available, -1 if error. */
+ return TEMP_FAILURE_RETRY (select (FD_SETSIZE,
+ &set, NULL, NULL,
+ &timeout));
+}
+/*@end group*/
+
+/*@group*/
+int
+main (void)
+{
+ fprintf (stderr, "select returned %d.\n",
+ input_timeout (STDIN_FILENO, 5));
+ return 0;
+}
+/*@end group*/
diff --git a/libc/manual/examples/setjmp.c b/libc/manual/examples/setjmp.c
new file mode 100644
index 000000000..023339c60
--- /dev/null
+++ b/libc/manual/examples/setjmp.c
@@ -0,0 +1,32 @@
+#include <setjmp.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+jmp_buf main_loop;
+
+void
+abort_to_main_loop (int status)
+{
+ longjmp (main_loop, status);
+}
+
+int
+main (void)
+{
+ while (1)
+ if (setjmp (main_loop))
+ puts ("Back at main loop....");
+ else
+ do_command ();
+}
+
+
+void
+do_command (void)
+{
+ char buffer[128];
+ if (fgets (buffer, 128, stdin) == NULL)
+ abort_to_main_loop (-1);
+ else
+ exit (EXIT_SUCCESS);
+}
diff --git a/libc/manual/examples/sigh1.c b/libc/manual/examples/sigh1.c
new file mode 100644
index 000000000..2c6e95b9c
--- /dev/null
+++ b/libc/manual/examples/sigh1.c
@@ -0,0 +1,36 @@
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/* This flag controls termination of the main loop. */
+volatile sig_atomic_t keep_going = 1;
+
+/* The signal handler just clears the flag and re-enables itself. */
+void
+catch_alarm (int sig)
+{
+ keep_going = 0;
+ signal (sig, catch_alarm);
+}
+
+void
+do_stuff (void)
+{
+ puts ("Doing stuff while waiting for alarm....");
+}
+
+int
+main (void)
+{
+ /* Establish a handler for SIGALRM signals. */
+ signal (SIGALRM, catch_alarm);
+
+ /* Set an alarm to go off in a little while. */
+ alarm (2);
+
+ /* Check the flag once in a while to see when to quit. */
+ while (keep_going)
+ do_stuff ();
+
+ return EXIT_SUCCESS;
+}
diff --git a/libc/manual/examples/sigusr.c b/libc/manual/examples/sigusr.c
new file mode 100644
index 000000000..11e3ceee8
--- /dev/null
+++ b/libc/manual/examples/sigusr.c
@@ -0,0 +1,61 @@
+/*@group*/
+#include <signal.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+/*@end group*/
+
+/* When a @code{SIGUSR1} signal arrives, set this variable. */
+volatile sig_atomic_t usr_interrupt = 0;
+
+void
+synch_signal (int sig)
+{
+ usr_interrupt = 1;
+}
+
+/* The child process executes this function. */
+void
+child_function (void)
+{
+ /* Perform initialization. */
+ printf ("I'm here!!! My pid is %d.\n", (int) getpid ());
+
+ /* Let parent know you're done. */
+ kill (getppid (), SIGUSR1);
+
+ /* Continue with execution. */
+ puts ("Bye, now....");
+ exit (0);
+}
+
+int
+main (void)
+{
+ struct sigaction usr_action;
+ sigset_t block_mask;
+ pid_t child_id;
+
+ /* Establish the signal handler. */
+ sigfillset (&block_mask);
+ usr_action.sa_handler = synch_signal;
+ usr_action.sa_mask = block_mask;
+ usr_action.sa_flags = 0;
+ sigaction (SIGUSR1, &usr_action, NULL);
+
+ /* Create the child process. */
+ child_id = fork ();
+ if (child_id == 0)
+ child_function (); /* Does not return. */
+
+/*@group*/
+ /* Busy wait for the child to send a signal. */
+ while (!usr_interrupt)
+ ;
+/*@end group*/
+
+ /* Now continue execution. */
+ puts ("That's all, folks!");
+
+ return 0;
+}
diff --git a/libc/manual/examples/stpcpy.c b/libc/manual/examples/stpcpy.c
new file mode 100644
index 000000000..b83226354
--- /dev/null
+++ b/libc/manual/examples/stpcpy.c
@@ -0,0 +1,13 @@
+#include <string.h>
+#include <stdio.h>
+
+int
+main (void)
+{
+ char buffer[10];
+ char *to = buffer;
+ to = stpcpy (to, "foo");
+ to = stpcpy (to, "bar");
+ puts (buffer);
+ return 0;
+}
diff --git a/libc/manual/examples/strdupa.c b/libc/manual/examples/strdupa.c
new file mode 100644
index 000000000..7d6ef3994
--- /dev/null
+++ b/libc/manual/examples/strdupa.c
@@ -0,0 +1,19 @@
+#include <paths.h>
+#include <string.h>
+#include <stdio.h>
+
+const char path[] = _PATH_STDPATH;
+
+int
+main (void)
+{
+ char *wr_path = strdupa (path);
+ char *cp = strtok (wr_path, ":");
+
+ while (cp != NULL)
+ {
+ puts (cp);
+ cp = strtok (NULL, ":");
+ }
+ return 0;
+}
diff --git a/libc/manual/examples/strftim.c b/libc/manual/examples/strftim.c
new file mode 100644
index 000000000..7f95ef02a
--- /dev/null
+++ b/libc/manual/examples/strftim.c
@@ -0,0 +1,31 @@
+#include <time.h>
+#include <stdio.h>
+
+#define SIZE 256
+
+int
+main (void)
+{
+ char buffer[SIZE];
+ time_t curtime;
+ struct tm *loctime;
+
+ /* Get the current time. */
+ curtime = time (NULL);
+
+ /* Convert it to local time representation. */
+ loctime = localtime (&curtime);
+
+ /* Print out the date and time in the standard format. */
+ fputs (asctime (loctime), stdout);
+
+/*@group*/
+ /* Print it out in a nice format. */
+ strftime (buffer, SIZE, "Today is %A, %B %d.\n", loctime);
+ fputs (buffer, stdout);
+ strftime (buffer, SIZE, "The time is %I:%M %p.\n", loctime);
+ fputs (buffer, stdout);
+
+ return 0;
+}
+/*@end group*/
diff --git a/libc/manual/examples/strncat.c b/libc/manual/examples/strncat.c
new file mode 100644
index 000000000..f865167f4
--- /dev/null
+++ b/libc/manual/examples/strncat.c
@@ -0,0 +1,14 @@
+#include <string.h>
+#include <stdio.h>
+
+#define SIZE 10
+
+static char buffer[SIZE];
+
+main ()
+{
+ strncpy (buffer, "hello", SIZE);
+ puts (buffer);
+ strncat (buffer, ", world", SIZE - strlen (buffer) - 1);
+ puts (buffer);
+}
diff --git a/libc/manual/examples/subopt.c b/libc/manual/examples/subopt.c
new file mode 100644
index 000000000..287fe8c49
--- /dev/null
+++ b/libc/manual/examples/subopt.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int do_all;
+const char *type;
+int read_size;
+int write_size;
+int read_only;
+
+enum
+{
+ RO_OPTION = 0,
+ RW_OPTION,
+ READ_SIZE_OPTION,
+ WRITE_SIZE_OPTION,
+ THE_END
+};
+
+const char *mount_opts[] =
+{
+ [RO_OPTION] = "ro",
+ [RW_OPTION] = "rw",
+ [READ_SIZE_OPTION] = "rsize",
+ [WRITE_SIZE_OPTION] = "wsize",
+ [THE_END] = NULL
+};
+
+int
+main (int argc, char *argv[])
+{
+ char *subopts, *value;
+ int opt;
+
+ while ((opt = getopt (argc, argv, "at:o:")) != -1)
+ switch (opt)
+ {
+ case 'a':
+ do_all = 1;
+ break;
+ case 't':
+ type = optarg;
+ break;
+ case 'o':
+ subopts = optarg;
+ while (*subopts != '\0')
+ switch (getsubopt (&subopts, mount_opts, &value))
+ {
+ case RO_OPTION:
+ read_only = 1;
+ break;
+ case RW_OPTION:
+ read_only = 0;
+ break;
+ case READ_SIZE_OPTION:
+ if (value == NULL)
+ abort ();
+ read_size = atoi (value);
+ break;
+ case WRITE_SIZE_OPTION:
+ if (value == NULL)
+ abort ();
+ write_size = atoi (value);
+ break;
+ default:
+ /* Unknown suboption. */
+ printf ("Unknown suboption `%s'\n", value);
+ break;
+ }
+ break;
+ default:
+ abort ();
+ }
+
+ /* Do the real work. */
+
+ return 0;
+}
diff --git a/libc/manual/examples/swapcontext.c b/libc/manual/examples/swapcontext.c
new file mode 100644
index 000000000..f733510f8
--- /dev/null
+++ b/libc/manual/examples/swapcontext.c
@@ -0,0 +1,99 @@
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ucontext.h>
+#include <sys/time.h>
+
+/* Set by the signal handler. */
+static volatile int expired;
+
+/* The contexts. */
+static ucontext_t uc[3];
+
+/* We do only a certain number of switches. */
+static int switches;
+
+
+/* This is the function doing the work. It is just a
+ skeleton, real code has to be filled in. */
+static void
+f (int n)
+{
+ int m = 0;
+ while (1)
+ {
+ /* This is where the work would be done. */
+ if (++m % 100 == 0)
+ {
+ putchar ('.');
+ fflush (stdout);
+ }
+
+ /* Regularly the @var{expire} variable must be checked. */
+ if (expired)
+ {
+ /* We do not want the program to run forever. */
+ if (++switches == 20)
+ return;
+
+ printf ("\nswitching from %d to %d\n", n, 3 - n);
+ expired = 0;
+ /* Switch to the other context, saving the current one. */
+ swapcontext (&uc[n], &uc[3 - n]);
+ }
+ }
+}
+
+/* This is the signal handler which simply set the variable. */
+void
+handler (int signal)
+{
+ expired = 1;
+}
+
+
+int
+main (void)
+{
+ struct sigaction sa;
+ struct itimerval it;
+ char st1[8192];
+ char st2[8192];
+
+ /* Initialize the data structures for the interval timer. */
+ sa.sa_flags = SA_RESTART;
+ sigfillset (&sa.sa_mask);
+ sa.sa_handler = handler;
+ it.it_interval.tv_sec = 0;
+ it.it_interval.tv_usec = 1;
+ it.it_value = it.it_interval;
+
+ /* Install the timer and get the context we can manipulate. */
+ if (sigaction (SIGPROF, &sa, NULL) < 0
+ || setitimer (ITIMER_PROF, &it, NULL) < 0
+ || getcontext (&uc[1]) == -1
+ || getcontext (&uc[2]) == -1)
+ abort ();
+
+ /* Create a context with a separate stack which causes the
+ function @code{f} to be call with the parameter @code{1}.
+ Note that the @code{uc_link} points to the main context
+ which will cause the program to terminate once the function
+ return. */
+ uc[1].uc_link = &uc[0];
+ uc[1].uc_stack.ss_sp = st1;
+ uc[1].uc_stack.ss_size = sizeof st1;
+ makecontext (&uc[1], (void (*) (void)) f, 1, 1);
+
+ /* Similarly, but @code{2} is passed as the parameter to @code{f}. */
+ uc[2].uc_link = &uc[0];
+ uc[2].uc_stack.ss_sp = st2;
+ uc[2].uc_stack.ss_size = sizeof st2;
+ makecontext (&uc[2], (void (*) (void)) f, 1, 2);
+
+ /* Start running. */
+ swapcontext (&uc[0], &uc[1]);
+ putchar ('\n');
+
+ return 0;
+}
diff --git a/libc/manual/examples/termios.c b/libc/manual/examples/termios.c
new file mode 100644
index 000000000..6db5990a0
--- /dev/null
+++ b/libc/manual/examples/termios.c
@@ -0,0 +1,60 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+
+/* Use this variable to remember original terminal attributes. */
+
+struct termios saved_attributes;
+
+void
+reset_input_mode (void)
+{
+ tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
+}
+
+void
+set_input_mode (void)
+{
+ struct termios tattr;
+ char *name;
+
+ /* Make sure stdin is a terminal. */
+ if (!isatty (STDIN_FILENO))
+ {
+ fprintf (stderr, "Not a terminal.\n");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Save the terminal attributes so we can restore them later. */
+ tcgetattr (STDIN_FILENO, &saved_attributes);
+ atexit (reset_input_mode);
+
+/*@group*/
+ /* Set the funny terminal modes. */
+ tcgetattr (STDIN_FILENO, &tattr);
+ tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
+ tattr.c_cc[VMIN] = 1;
+ tattr.c_cc[VTIME] = 0;
+ tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
+}
+/*@end group*/
+
+int
+main (void)
+{
+ char c;
+
+ set_input_mode ();
+
+ while (1)
+ {
+ read (STDIN_FILENO, &c, 1);
+ if (c == '\004') /* @kbd{C-d} */
+ break;
+ else
+ putchar (c);
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/libc/manual/examples/testopt.c b/libc/manual/examples/testopt.c
new file mode 100644
index 000000000..44ca8e4ad
--- /dev/null
+++ b/libc/manual/examples/testopt.c
@@ -0,0 +1,55 @@
+/*@group*/
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main (int argc, char **argv)
+{
+ int aflag = 0;
+ int bflag = 0;
+ char *cvalue = NULL;
+ int index;
+ int c;
+
+ opterr = 0;
+/*@end group*/
+
+/*@group*/
+ while ((c = getopt (argc, argv, "abc:")) != -1)
+ switch (c)
+ {
+ case 'a':
+ aflag = 1;
+ break;
+ case 'b':
+ bflag = 1;
+ break;
+ case 'c':
+ cvalue = optarg;
+ break;
+ case '?':
+ if (optopt == 'c')
+ fprintf (stderr, "Option -%c requires an argument.\n", optopt);
+ else if (isprint (optopt))
+ fprintf (stderr, "Unknown option `-%c'.\n", optopt);
+ else
+ fprintf (stderr,
+ "Unknown option character `\\x%x'.\n",
+ optopt);
+ return 1;
+ default:
+ abort ();
+ }
+/*@end group*/
+
+/*@group*/
+ printf ("aflag = %d, bflag = %d, cvalue = %s\n",
+ aflag, bflag, cvalue);
+
+ for (index = optind; index < argc; index++)
+ printf ("Non-option argument %s\n", argv[index]);
+ return 0;
+}
+/*@end group*/
diff --git a/libc/manual/examples/testpass.c b/libc/manual/examples/testpass.c
new file mode 100644
index 000000000..5bd616d2a
--- /dev/null
+++ b/libc/manual/examples/testpass.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <crypt.h>
+
+int
+main(void)
+{
+ /* Hashed form of "GNU libc manual". */
+ const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/";
+
+ char *result;
+ int ok;
+
+/*@group*/
+ /* Read in the user's password and encrypt it,
+ passing the expected password in as the salt. */
+ result = crypt(getpass("Password:"), pass);
+/*@end group*/
+
+ /* Test the result. */
+ ok = strcmp (result, pass) == 0;
+
+ puts(ok ? "Access granted." : "Access denied.");
+ return ok ? 0 : 1;
+}