diff options
author | Roland McGrath <roland@gnu.org> | 1992-05-07 18:34:40 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 1992-05-07 18:34:40 +0000 |
commit | fcc87cede508b7fcc09dc4eac7eb041c64b0ea18 (patch) | |
tree | 69988e9f1d3d29219d6e949de872bc1d47f133b9 /manual/examples | |
parent | fe8c20466391dfafe8232c235c3015a2592c3021 (diff) |
Initial revision
Diffstat (limited to 'manual/examples')
-rw-r--r-- | manual/examples/add.c | 30 | ||||
-rw-r--r-- | manual/examples/db.c | 50 | ||||
-rw-r--r-- | manual/examples/dir.c | 23 | ||||
-rw-r--r-- | manual/examples/memopen.c | 17 | ||||
-rw-r--r-- | manual/examples/memstrm.c | 19 | ||||
-rw-r--r-- | manual/examples/pipe.c | 64 | ||||
-rw-r--r-- | manual/examples/popen.c | 31 | ||||
-rw-r--r-- | manual/examples/rprintf.c | 56 | ||||
-rw-r--r-- | manual/examples/search.c | 89 | ||||
-rw-r--r-- | manual/examples/setjmp.c | 32 | ||||
-rw-r--r-- | manual/examples/sigh1.c | 38 | ||||
-rw-r--r-- | manual/examples/sigusr.c | 60 | ||||
-rw-r--r-- | manual/examples/strftim.c | 29 | ||||
-rw-r--r-- | manual/examples/strncat.c | 14 | ||||
-rw-r--r-- | manual/examples/termios.c | 59 | ||||
-rw-r--r-- | manual/examples/testopt.c | 38 |
16 files changed, 649 insertions, 0 deletions
diff --git a/manual/examples/add.c b/manual/examples/add.c new file mode 100644 index 0000000000..cb37b2b435 --- /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 = 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/db.c b/manual/examples/db.c new file mode 100644 index 0000000000..a8403ddd59 --- /dev/null +++ b/manual/examples/db.c @@ -0,0 +1,50 @@ +#include <grp.h> +#include <pwd.h> +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> + +void +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 ("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++; + } + + exit (EXIT_SUCCESS); +} diff --git a/manual/examples/dir.c b/manual/examples/dir.c new file mode 100644 index 0000000000..05aa5422cb --- /dev/null +++ b/manual/examples/dir.c @@ -0,0 +1,23 @@ +#include <stddef.h> +#include <stdio.h> +#include <sys/types.h> +#include <dirent.h> + +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/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..7119f70d2d --- /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/pipe.c b/manual/examples/pipe.c new file mode 100644 index 0000000000..c0e242ee20 --- /dev/null +++ b/manual/examples/pipe.c @@ -0,0 +1,64 @@ +#include <sys/types.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> + +/* Read characters from the pipe and echo them to 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]; + + /* Create the pipe. */ + if (pipe (mypipe)) + { + fprintf (stderr, "Pipe failed.\n"); + return EXIT_FAILURE; + } + + /* 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..15a085e2cc --- /dev/null +++ b/manual/examples/popen.c @@ -0,0 +1,31 @@ +#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); + } +} + +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; +} diff --git a/manual/examples/rprintf.c b/manual/examples/rprintf.c new file mode 100644 index 0000000000..c62ff4956e --- /dev/null +++ b/manual/examples/rprintf.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <printf.h> +#include <stdarg.h> + +typedef struct widget + { + char *name; + } Widget; + +int +print_widget (FILE *stream, const struct printf_info *info, va_list *app) +{ + Widget *w; + char *buffer; + int len, fill, i; + + /* 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; + fill = info->width - len; + if (fill < 0) + fill = 0; + + /* Pad to the minimum field width and print to the stream. */ + if (!info->left) + for (i = 0; i < fill; i++) + putc (' ', stream); + fputs (buffer, stream); + if (info->left) + for (i = 0; i < fill; i++) + putc (' ', stream); + + /* Clean up and return. */ + free (buffer); + return (len + fill); +} + + +void +main (void) +{ + + /* Make a widget to print. */ + Widget mywidget; + mywidget.name = "mywidget"; + + /* Register the print function for widgets. */ + register_printf_function ('W', print_widget); + + /* Now print the widget. */ + printf ("|%W|\n", &mywidget); + printf ("|%35W|\n", &mywidget); + printf ("|%-35W|\n", &mywidget); +} diff --git a/manual/examples/search.c b/manual/examples/search.c new file mode 100644 index 0000000000..892290b942 --- /dev/null +++ b/manual/examples/search.c @@ -0,0 +1,89 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +/* Define an array of critters to sort. */ + +struct critter +{ + char *name; + 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); +} + + +/* Do the lookup into the sorted array. */ + +void +find_critter (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); +} + + +/* Main program. */ + +void +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"); +} diff --git a/manual/examples/setjmp.c b/manual/examples/setjmp.c new file mode 100644 index 0000000000..5b5e7d8eda --- /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)) + printf ("Back at main loop....\n"); + else + do_command (); +} + + +void +do_command (void) +{ + char buffer[128]; + if (fgets (buffer, 128, stdin)) + 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..909c55b995 --- /dev/null +++ b/manual/examples/sigh1.c @@ -0,0 +1,38 @@ +#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) +{ + printf ("Doing stuff while waiting for alarm....\n"); +} + +main () +{ + + /* 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 (); + exit (EXIT_SUCCESS); +} diff --git a/manual/examples/sigusr.c b/manual/examples/sigusr.c new file mode 100644 index 0000000000..59e7320235 --- /dev/null +++ b/manual/examples/sigusr.c @@ -0,0 +1,60 @@ +#include <signal.h> +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +/* 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. */ + printf ("Bye, now....\n"); + 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 */ + + /* Busy wait for child to send a signal. */ + while (!usr_interrupt) + ; + + /* Now continue execution. */ + printf ("That's all, folks!\n"); + return 0; +} diff --git a/manual/examples/strftim.c b/manual/examples/strftim.c new file mode 100644 index 0000000000..3b27d555c4 --- /dev/null +++ b/manual/examples/strftim.c @@ -0,0 +1,29 @@ +#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); + + /* 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; +} diff --git a/manual/examples/strncat.c b/manual/examples/strncat.c new file mode 100644 index 0000000000..c959d40b51 --- /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); + printf ("%s\n", buffer); + strncat (buffer, ", world", SIZE - strlen (buffer) - 1); + printf ("%s\n", buffer); +} diff --git a/manual/examples/termios.c b/manual/examples/termios.c new file mode 100644 index 0000000000..ab50cfc586 --- /dev/null +++ b/manual/examples/termios.c @@ -0,0 +1,59 @@ +#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); + + /* Set the funny terminal modes. */ + tcgetattr (STDIN_FILENO, &tattr); + tattr.c_lflag = tattr.c_lflag & (~ICANON); /* Clear ICANON. */ + tattr.c_lflag = tattr.c_lflag & (~ECHO); /* Clear ECHO. */ + tattr.c_cc[VMIN] = 1; + tattr.c_cc[VTIME] = 0; + tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr); +} + +int +main (void) +{ + char c; + + set_input_mode (); + + while (1) + { + read (STDIN_FILENO, &c, 1); + if (c == '\004') + 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..e0745429a3 --- /dev/null +++ b/manual/examples/testopt.c @@ -0,0 +1,38 @@ +#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; + + while ((c = getopt (argc, argv, "abc:")) >= 0) + switch (c) + { + case 'a': + aflag = 1; + break; + case 'b': + bflag = 1; + break; + case 'c': + cvalue = optarg; + break; + case '?': + fprintf (stderr, "Unknown option %c.\n", optopt); + return -1; + default: + fprintf (stderr, "This should never happen!\n"); + return -1; + } + + 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; +} |