aboutsummaryrefslogtreecommitdiff
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.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;