diff options
author | Zack Weinberg <zackw@panix.com> | 2017-06-08 15:39:03 -0400 |
---|---|---|
committer | Zack Weinberg <zackw@panix.com> | 2017-06-08 15:39:03 -0400 |
commit | 5046dbb4a7eba5eccfd258f92f4735c9ffc8d069 (patch) | |
tree | 4470480d904b65cf14ca524f96f79eca818c3eaf /manual/examples | |
parent | 199fc19d3aaaf57944ef036e15904febe877fc93 (diff) | |
download | glibc-zack/build-layout-experiment.tar glibc-zack/build-layout-experiment.tar.gz glibc-zack/build-layout-experiment.tar.bz2 glibc-zack/build-layout-experiment.zip |
Prepare for radical source tree reorganization.zack/build-layout-experiment
All top-level files and directories are moved into a temporary storage
directory, REORG.TODO, except for files that will certainly still
exist in their current form at top level when we're done (COPYING,
COPYING.LIB, LICENSES, NEWS, README), all old ChangeLog files (which
are moved to the new directory OldChangeLogs, instead), and the
generated file INSTALL (which is just deleted; in the new order, there
will be no generated files checked into version control).
Diffstat (limited to 'manual/examples')
43 files changed, 0 insertions, 2783 deletions
diff --git a/manual/examples/README b/manual/examples/README deleted file mode 100644 index 7d0070fdd5..0000000000 --- a/manual/examples/README +++ /dev/null @@ -1,8 +0,0 @@ -These are source files for example code that appears in The GNU C -Library Reference Manual. - -While the manual itself is licensed under the terms of the GNU Free -Documentation License, you can use these source files on their own -under the terms of the GNU General Public License as published by -the Free Software Foundation; version 2 of the License, or (at your -option) any later version. diff --git a/manual/examples/add.c b/manual/examples/add.c deleted file mode 100644 index 9261f105da..0000000000 --- a/manual/examples/add.c +++ /dev/null @@ -1,47 +0,0 @@ -/* Example of a Variadic Function - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/argp-ex1.c b/manual/examples/argp-ex1.c deleted file mode 100644 index 9058595e9b..0000000000 --- a/manual/examples/argp-ex1.c +++ /dev/null @@ -1,31 +0,0 @@ -/* Argp example #1 -- a minimal program using argp - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/* 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 <stdlib.h> -#include <argp.h> - -int -main (int argc, char **argv) -{ - argp_parse (0, argc, argv, 0, 0, 0); - exit (0); -} diff --git a/manual/examples/argp-ex2.c b/manual/examples/argp-ex2.c deleted file mode 100644 index 83bb85b107..0000000000 --- a/manual/examples/argp-ex2.c +++ /dev/null @@ -1,61 +0,0 @@ -/* Argp example #2 -- a pretty minimal program using argp - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/* 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 <stdlib.h> -#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/manual/examples/argp-ex3.c b/manual/examples/argp-ex3.c deleted file mode 100644 index e37ab73408..0000000000 --- a/manual/examples/argp-ex3.c +++ /dev/null @@ -1,169 +0,0 @@ -/* Argp example #3 -- a program with options and arguments using argp - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/* 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 <stdlib.h> -#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/manual/examples/argp-ex4.c b/manual/examples/argp-ex4.c deleted file mode 100644 index 37218af23e..0000000000 --- a/manual/examples/argp-ex4.c +++ /dev/null @@ -1,183 +0,0 @@ -/* Argp example #4 -- a program with somewhat more complicated options - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/* 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/manual/examples/atexit.c b/manual/examples/atexit.c deleted file mode 100644 index fe14930ef8..0000000000 --- a/manual/examples/atexit.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Cleanups on Exit - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 deleted file mode 100644 index 652662682d..0000000000 --- a/manual/examples/db.c +++ /dev/null @@ -1,69 +0,0 @@ -/* User and Group Database Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 deleted file mode 100644 index da6eef2a46..0000000000 --- a/manual/examples/dir.c +++ /dev/null @@ -1,41 +0,0 @@ -/* Simple Program to List a Directory - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/*@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/manual/examples/dir2.c b/manual/examples/dir2.c deleted file mode 100644 index a8390822b4..0000000000 --- a/manual/examples/dir2.c +++ /dev/null @@ -1,46 +0,0 @@ -/* Simple Program to List a Directory, Mark II - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/*@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/manual/examples/execinfo.c b/manual/examples/execinfo.c deleted file mode 100644 index c357fa48ed..0000000000 --- a/manual/examples/execinfo.c +++ /dev/null @@ -1,54 +0,0 @@ -/* Obtain a backtrace and print it. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/filecli.c b/manual/examples/filecli.c deleted file mode 100644 index 50f4ef43ac..0000000000 --- a/manual/examples/filecli.c +++ /dev/null @@ -1,71 +0,0 @@ -/* Example of Reading Datagrams - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/filesrv.c b/manual/examples/filesrv.c deleted file mode 100644 index 9cf3d0ee91..0000000000 --- a/manual/examples/filesrv.c +++ /dev/null @@ -1,65 +0,0 @@ -/* Datagram Socket Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/fmtmsgexpl.c b/manual/examples/fmtmsgexpl.c deleted file mode 100644 index 5cd965cd3f..0000000000 --- a/manual/examples/fmtmsgexpl.c +++ /dev/null @@ -1,29 +0,0 @@ -/* How to use fmtmsg and addseverity. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/genpass.c b/manual/examples/genpass.c deleted file mode 100644 index 7d76dedfbc..0000000000 --- a/manual/examples/genpass.c +++ /dev/null @@ -1,49 +0,0 @@ -/* Encrypting Passwords - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/inetcli.c b/manual/examples/inetcli.c deleted file mode 100644 index 1723e2fc56..0000000000 --- a/manual/examples/inetcli.c +++ /dev/null @@ -1,76 +0,0 @@ -/* Byte Stream Socket Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 "www.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/manual/examples/inetsrv.c b/manual/examples/inetsrv.c deleted file mode 100644 index 01ae631818..0000000000 --- a/manual/examples/inetsrv.c +++ /dev/null @@ -1,120 +0,0 @@ -/* Byte Stream Connection Server Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/isockad.c b/manual/examples/isockad.c deleted file mode 100644 index be2def3be8..0000000000 --- a/manual/examples/isockad.c +++ /dev/null @@ -1,40 +0,0 @@ -/* Internet Socket Example using sockaddr_in. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/longopt.c b/manual/examples/longopt.c deleted file mode 100644 index 40967174aa..0000000000 --- a/manual/examples/longopt.c +++ /dev/null @@ -1,113 +0,0 @@ -/* Example of Parsing Long Options with getopt_long. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <getopt.h> - -/* Flag set by @samp{--verbose}. */ -static int verbose_flag; - -int -main (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/manual/examples/memopen.c b/manual/examples/memopen.c deleted file mode 100644 index 407aeba878..0000000000 --- a/manual/examples/memopen.c +++ /dev/null @@ -1,34 +0,0 @@ -/* String Streams - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 deleted file mode 100644 index d339ffb95d..0000000000 --- a/manual/examples/memstrm.c +++ /dev/null @@ -1,36 +0,0 @@ -/* open_memstream example. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 = %zu\n", bp, size); - fprintf (stream, ", world"); - fclose (stream); - printf ("buf = `%s', size = %zu\n", bp, size); - - return 0; -} diff --git a/manual/examples/mkdirent.c b/manual/examples/mkdirent.c deleted file mode 100644 index 988e988460..0000000000 --- a/manual/examples/mkdirent.c +++ /dev/null @@ -1,42 +0,0 @@ -/* Example for creating a struct dirent object for use with glob. - Copyright (C) 2016-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#include <dirent.h> -#include <errno.h> -#include <stddef.h> -#include <stdlib.h> -#include <string.h> - -struct dirent * -mkdirent (const char *name) -{ - size_t dirent_size = offsetof (struct dirent, d_name) + 1; - size_t name_length = strlen (name); - size_t total_size = dirent_size + name_length; - if (total_size < dirent_size) - { - errno = ENOMEM; - return NULL; - } - struct dirent *result = malloc (total_size); - if (result == NULL) - return NULL; - result->d_type = DT_UNKNOWN; - result->d_ino = 1; /* Do not skip this entry. */ - memcpy (result->d_name, name, name_length + 1); - return result; -} diff --git a/manual/examples/mkfsock.c b/manual/examples/mkfsock.c deleted file mode 100644 index ffe1b46928..0000000000 --- a/manual/examples/mkfsock.c +++ /dev/null @@ -1,62 +0,0 @@ -/* Example of Local-Namespace Sockets - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 (not including the terminating null byte). - Alternatively you can just do: - size = SUN_LEN (&name); - */ - size = (offsetof (struct sockaddr_un, sun_path) - + strlen (name.sun_path)); - - 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 deleted file mode 100644 index 5657322631..0000000000 --- a/manual/examples/mkisock.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Internet Socket Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/mygetpass.c b/manual/examples/mygetpass.c deleted file mode 100644 index 152d7bee7f..0000000000 --- a/manual/examples/mygetpass.c +++ /dev/null @@ -1,42 +0,0 @@ -/* Reading Passwords - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/ofdlocks.c b/manual/examples/ofdlocks.c deleted file mode 100644 index 42074105a8..0000000000 --- a/manual/examples/ofdlocks.c +++ /dev/null @@ -1,77 +0,0 @@ -/* Open File Description Locks Usage Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, see <http://www.gnu.org/licenses/>. -*/ - -#define _GNU_SOURCE -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> -#include <fcntl.h> -#include <pthread.h> - -#define FILENAME "/tmp/foo" -#define NUM_THREADS 3 -#define ITERATIONS 5 - -void * -thread_start (void *arg) -{ - int i, fd, len; - long tid = (long) arg; - char buf[256]; - struct flock lck = { - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 1, - }; - - fd = open ("/tmp/foo", O_RDWR | O_CREAT, 0666); - - for (i = 0; i < ITERATIONS; i++) - { - lck.l_type = F_WRLCK; - fcntl (fd, F_OFD_SETLKW, &lck); - - len = sprintf (buf, "%d: tid=%ld fd=%d\n", i, tid, fd); - - lseek (fd, 0, SEEK_END); - write (fd, buf, len); - fsync (fd); - - lck.l_type = F_UNLCK; - fcntl (fd, F_OFD_SETLK, &lck); - - /* sleep to ensure lock is yielded to another thread */ - usleep (1); - } - pthread_exit (NULL); -} - -int -main (int argc, char **argv) -{ - long i; - pthread_t threads[NUM_THREADS]; - - truncate (FILENAME, 0); - - for (i = 0; i < NUM_THREADS; i++) - pthread_create (&threads[i], NULL, thread_start, (void *) i); - - pthread_exit (NULL); - return 0; -} diff --git a/manual/examples/pipe.c b/manual/examples/pipe.c deleted file mode 100644 index adfa4658dc..0000000000 --- a/manual/examples/pipe.c +++ /dev/null @@ -1,87 +0,0 @@ -/* Creating a Pipe - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/popen.c b/manual/examples/popen.c deleted file mode 100644 index cc7617c65c..0000000000 --- a/manual/examples/popen.c +++ /dev/null @@ -1,55 +0,0 @@ -/* Pipe to a Subprocess - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/rprintf.c b/manual/examples/rprintf.c deleted file mode 100644 index e49a2c4fbf..0000000000 --- a/manual/examples/rprintf.c +++ /dev/null @@ -1,84 +0,0 @@ -/* Printf Extension Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/search.c b/manual/examples/search.c deleted file mode 100644 index c434c11f8c..0000000000 --- a/manual/examples/search.c +++ /dev/null @@ -1,113 +0,0 @@ -/* Searching and Sorting Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 void *v1, const void *v2) -{ - const struct critter *c1 = v1; - const struct critter *c2 = v2; - - 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 deleted file mode 100644 index 16c54c0c63..0000000000 --- a/manual/examples/select.c +++ /dev/null @@ -1,58 +0,0 @@ -/* Waiting for Input or Output - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/*@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/manual/examples/setjmp.c b/manual/examples/setjmp.c deleted file mode 100644 index 20b5064914..0000000000 --- a/manual/examples/setjmp.c +++ /dev/null @@ -1,49 +0,0 @@ -/* Introduction to Non-Local Exits - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 deleted file mode 100644 index 6d4a0752f3..0000000000 --- a/manual/examples/sigh1.c +++ /dev/null @@ -1,53 +0,0 @@ -/* Signal Handlers that Return - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 deleted file mode 100644 index 51b44dcf03..0000000000 --- a/manual/examples/sigusr.c +++ /dev/null @@ -1,78 +0,0 @@ -/* Using kill for Communication - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/*@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 deleted file mode 100644 index a0dda1757a..0000000000 --- a/manual/examples/stpcpy.c +++ /dev/null @@ -1,30 +0,0 @@ -/* stpcpy example. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/strdupa.c b/manual/examples/strdupa.c deleted file mode 100644 index c8e61bac16..0000000000 --- a/manual/examples/strdupa.c +++ /dev/null @@ -1,36 +0,0 @@ -/* strdupa example. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/strftim.c b/manual/examples/strftim.c deleted file mode 100644 index 08eb22d539..0000000000 --- a/manual/examples/strftim.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Time Functions Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/subopt.c b/manual/examples/subopt.c deleted file mode 100644 index 278b6dbbc1..0000000000 --- a/manual/examples/subopt.c +++ /dev/null @@ -1,95 +0,0 @@ -/* Parsing of Suboptions Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/swapcontext.c b/manual/examples/swapcontext.c deleted file mode 100644 index 3747230c31..0000000000 --- a/manual/examples/swapcontext.c +++ /dev/null @@ -1,116 +0,0 @@ -/* Complete Context Control - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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/manual/examples/termios.c b/manual/examples/termios.c deleted file mode 100644 index 8ac2f626d5..0000000000 --- a/manual/examples/termios.c +++ /dev/null @@ -1,77 +0,0 @@ -/* Noncanonical Mode Example - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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 deleted file mode 100644 index 1cb53352eb..0000000000 --- a/manual/examples/testopt.c +++ /dev/null @@ -1,72 +0,0 @@ -/* Example of Parsing Arguments with getopt. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/*@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/manual/examples/testpass.c b/manual/examples/testpass.c deleted file mode 100644 index 12c44f3028..0000000000 --- a/manual/examples/testpass.c +++ /dev/null @@ -1,43 +0,0 @@ -/* Verify a password. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -#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; -} diff --git a/manual/examples/timeval_subtract.c b/manual/examples/timeval_subtract.c deleted file mode 100644 index 554dc77f40..0000000000 --- a/manual/examples/timeval_subtract.c +++ /dev/null @@ -1,44 +0,0 @@ -/* struct timeval subtraction. - Copyright (C) 1991-2017 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, if not, see <http://www.gnu.org/licenses/>. -*/ - -/* Subtract the `struct timeval' values X and Y, - storing the result in RESULT. - Return 1 if the difference is negative, otherwise 0. */ - -int -timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) -{ - /* Perform the carry for the later subtraction by updating @var{y}. */ - if (x->tv_usec < y->tv_usec) { - int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; - y->tv_usec -= 1000000 * nsec; - y->tv_sec += nsec; - } - if (x->tv_usec - y->tv_usec > 1000000) { - int nsec = (x->tv_usec - y->tv_usec) / 1000000; - y->tv_usec += 1000000 * nsec; - y->tv_sec -= nsec; - } - - /* Compute the time remaining to wait. - @code{tv_usec} is certainly positive. */ - result->tv_sec = x->tv_sec - y->tv_sec; - result->tv_usec = x->tv_usec - y->tv_usec; - - /* Return 1 if result is negative. */ - return x->tv_sec < y->tv_sec; -} |