summary refs log tree commit diff
path: root/manual/examples
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
committerRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
commit28f540f45bbacd939bfd07f213bcad2bf730b1bf (patch)
tree15f07c4c43d635959c6afee96bde71fb1b3614ee /manual/examples
downloadglibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.gz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.xz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.zip
initial import
Diffstat (limited to 'manual/examples')
-rw-r--r--manual/examples/add.c30
-rw-r--r--manual/examples/atexit.c15
-rw-r--r--manual/examples/db.c52
-rw-r--r--manual/examples/dir.c25
-rw-r--r--manual/examples/filecli.c54
-rw-r--r--manual/examples/filesrv.c46
-rw-r--r--manual/examples/inetcli.c59
-rw-r--r--manual/examples/inetsrv.c103
-rw-r--r--manual/examples/isockad.c23
-rw-r--r--manual/examples/longopt.c92
-rw-r--r--manual/examples/memopen.c17
-rw-r--r--manual/examples/memstrm.c19
-rw-r--r--manual/examples/mkfsock.c43
-rw-r--r--manual/examples/mkisock.c31
-rw-r--r--manual/examples/pipe.c66
-rw-r--r--manual/examples/popen.c33
-rw-r--r--manual/examples/rprintf.c52
-rwxr-xr-xmanual/examples/searchbin0 -> 24576 bytes
-rw-r--r--manual/examples/search.c93
-rw-r--r--manual/examples/select.c40
-rw-r--r--manual/examples/setjmp.c32
-rw-r--r--manual/examples/sigh1.c36
-rw-r--r--manual/examples/sigusr.c61
-rw-r--r--manual/examples/stpcpy.c13
-rw-r--r--manual/examples/strftim.c31
-rw-r--r--manual/examples/strncat.c14
-rw-r--r--manual/examples/termios.c60
-rw-r--r--manual/examples/testopt.c50
28 files changed, 1190 insertions, 0 deletions
diff --git a/manual/examples/add.c b/manual/examples/add.c
new file mode 100644
index 0000000000..e4b1bba365
--- /dev/null
+++ b/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/manual/examples/atexit.c b/manual/examples/atexit.c
new file mode 100644
index 0000000000..42bba71126
--- /dev/null
+++ b/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/manual/examples/db.c b/manual/examples/db.c
new file mode 100644
index 0000000000..1a1cb0c0d7
--- /dev/null
+++ b/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/manual/examples/dir.c b/manual/examples/dir.c
new file mode 100644
index 0000000000..b90f72da03
--- /dev/null
+++ b/manual/examples/dir.c
@@ -0,0 +1,25 @@
+/*@group*/
+#include <stddef.h>
+#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
+    puts ("Couldn't open the directory.");
+
+  return 0;
+}
diff --git a/manual/examples/filecli.c b/manual/examples/filecli.c
new file mode 100644
index 0000000000..b77ae6763e
--- /dev/null
+++ b/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_UNIX;
+  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/manual/examples/filesrv.c b/manual/examples/filesrv.c
new file mode 100644
index 0000000000..3596b99982
--- /dev/null
+++ b/manual/examples/filesrv.c
@@ -0,0 +1,46 @@
+#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;
+
+  /* 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/manual/examples/inetcli.c b/manual/examples/inetcli.c
new file mode 100644
index 0000000000..258c6892aa
--- /dev/null
+++ b/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 	"churchy.gnu.ai.mit.edu"
+
+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,
+			     unsigned short int 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/manual/examples/inetsrv.c b/manual/examples/inetsrv.c
new file mode 100644
index 0000000000..bd86e80f36
--- /dev/null
+++ b/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 (unsigned short int 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/manual/examples/isockad.c b/manual/examples/isockad.c
new file mode 100644
index 0000000000..54ec1cca4c
--- /dev/null
+++ b/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,
+	       unsigned short int 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/manual/examples/longopt.c b/manual/examples/longopt.c
new file mode 100644
index 0000000000..d5c841f24a
--- /dev/null
+++ b/manual/examples/longopt.c
@@ -0,0 +1,92 @@
+#include <stdio.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", 0, &verbose_flag, 1},
+	  {"brief", 0, &verbose_flag, 0},
+	  /* These options don't set a flag.
+	     We distinguish them by their indices.  */
+	  {"add", 1, 0, 0},
+	  {"append", 0, 0, 0},
+	  {"delete", 1, 0, 0},
+	  {"create", 0, 0, 0},
+	  {"file", 1, 0, 0},
+	  {0, 0, 0, 0}
+	};
+      /* @code{getopt_long} stores the option index here.  */
+      int option_index = 0;
+
+      c = getopt_long (argc, argv, "abc:d:",
+		       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 '?':
+	  /* @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/manual/examples/memopen.c b/manual/examples/memopen.c
new file mode 100644
index 0000000000..682830fe5f
--- /dev/null
+++ b/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/manual/examples/memstrm.c b/manual/examples/memstrm.c
new file mode 100644
index 0000000000..1674c36e0b
--- /dev/null
+++ b/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/manual/examples/mkfsock.c b/manual/examples/mkfsock.c
new file mode 100644
index 0000000000..d3750ec150
--- /dev/null
+++ b/manual/examples/mkfsock.c
@@ -0,0 +1,43 @@
+#include <stddef.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.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_UNIX, SOCK_DGRAM, 0);
+  if (sock < 0)
+    {
+      perror ("socket");
+      exit (EXIT_FAILURE);
+    }
+
+  /* Bind a name to the socket.  */
+
+  name.sun_family = AF_FILE;
+  strcpy (name.sun_path, filename);
+
+  /* The size of the address is
+     the offset of the start of the filename,
+     plus its length,
+     plus one for the terminating null byte.  */
+  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/manual/examples/mkisock.c b/manual/examples/mkisock.c
new file mode 100644
index 0000000000..07411bb263
--- /dev/null
+++ b/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 (unsigned short int 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/manual/examples/pipe.c b/manual/examples/pipe.c
new file mode 100644
index 0000000000..054550fec6
--- /dev/null
+++ b/manual/examples/pipe.c
@@ -0,0 +1,66 @@
+#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. */
+      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. */
+      write_to_pipe (mypipe[1]);
+      return EXIT_SUCCESS;
+    }
+}
diff --git a/manual/examples/popen.c b/manual/examples/popen.c
new file mode 100644
index 0000000000..16ae32fa16
--- /dev/null
+++ b/manual/examples/popen.c
@@ -0,0 +1,33 @@
+#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, "Could not run more.\n");
+      return EXIT_FAILURE;
+    }
+  write_data (output);
+  pclose (output);
+  return EXIT_SUCCESS;
+}
+/*@end group*/
diff --git a/manual/examples/rprintf.c b/manual/examples/rprintf.c
new file mode 100644
index 0000000000..eff1d8e7cf
--- /dev/null
+++ b/manual/examples/rprintf.c
@@ -0,0 +1,52 @@
+#include <stdio.h>
+#include <printf.h>
+#include <stdarg.h>
+
+/*@group*/
+typedef struct
+  {
+    char *name;
+  } Widget;
+/*@end group*/
+
+int 
+print_widget (FILE *stream, const struct printf_info *info, va_list *app)
+{
+  Widget *w;
+  char *buffer;
+  int len;
+
+  /* Format the output into a string. */
+  w = va_arg (*app, Widget *);
+  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
+main (void)
+{
+  /* Make a widget to print. */
+  Widget mywidget;
+  mywidget.name = "mywidget";
+
+  /* Register the print function for widgets. */
+  register_printf_function ('W', print_widget, NULL); /* No arginfo.  */
+
+  /* Now print the widget. */
+  printf ("|%W|\n", &mywidget);
+  printf ("|%35W|\n", &mywidget);
+  printf ("|%-35W|\n", &mywidget);
+
+  return 0;
+}
diff --git a/manual/examples/search b/manual/examples/search
new file mode 100755
index 0000000000..4916a2c52f
--- /dev/null
+++ b/manual/examples/search
Binary files differdiff --git a/manual/examples/search.c b/manual/examples/search.c
new file mode 100644
index 0000000000..182e6e4a3f
--- /dev/null
+++ b/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/manual/examples/select.c b/manual/examples/select.c
new file mode 100644
index 0000000000..def2cd6f9f
--- /dev/null
+++ b/manual/examples/select.c
@@ -0,0 +1,40 @@
+/*@group*/
+#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/manual/examples/setjmp.c b/manual/examples/setjmp.c
new file mode 100644
index 0000000000..023339c602
--- /dev/null
+++ b/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/manual/examples/sigh1.c b/manual/examples/sigh1.c
new file mode 100644
index 0000000000..2c6e95b9c9
--- /dev/null
+++ b/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/manual/examples/sigusr.c b/manual/examples/sigusr.c
new file mode 100644
index 0000000000..11e3ceee8f
--- /dev/null
+++ b/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/manual/examples/stpcpy.c b/manual/examples/stpcpy.c
new file mode 100644
index 0000000000..b83226354b
--- /dev/null
+++ b/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/manual/examples/strftim.c b/manual/examples/strftim.c
new file mode 100644
index 0000000000..7f95ef02ad
--- /dev/null
+++ b/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/manual/examples/strncat.c b/manual/examples/strncat.c
new file mode 100644
index 0000000000..f865167f4a
--- /dev/null
+++ b/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/manual/examples/termios.c b/manual/examples/termios.c
new file mode 100644
index 0000000000..6db5990a0c
--- /dev/null
+++ b/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/manual/examples/testopt.c b/manual/examples/testopt.c
new file mode 100644
index 0000000000..8ebc9b6f7a
--- /dev/null
+++ b/manual/examples/testopt.c
@@ -0,0 +1,50 @@
+/*@group*/
+#include <unistd.h>
+#include <stdio.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 (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*/