/* nproc - print the number of processors. This is the nproc utility
Copyright (C) 2009-2018 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 3 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 <https://www.gnu.org/licenses/>. */ The GNUv3 license
/* Written by Giuseppe Scrivano. */
#include <config.h> Provides system specific information
#include <getopt.h> ...!includes auto-comment...
#include <stdio.h> Provides standard I/O capability
#include <sys/types.h> Provides system data types
#include "system.h" ...!includes auto-comment...
#include "error.h" ...!includes auto-comment...
#include "nproc.h" ...!includes auto-comment...
#include "quote.h" ...!includes auto-comment...
#include "xdectoint.h" ...!includes auto-comment...
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "nproc" Line 31
#define AUTHORS proper_name ("Giuseppe Scrivano") Line 33
enum Line 35
{
ALL_OPTION = CHAR_MAX + 1, Line 37
IGNORE_OPTION Line 38
}; Block 1
static struct option const longopts[] = Line 41
{
{"all", no_argument, NULL, ALL_OPTION}, Line 43
{"ignore", required_argument, NULL, IGNORE_OPTION}, Line 44
{GETOPT_HELP_OPTION_DECL}, Line 45
{GETOPT_VERSION_OPTION_DECL}, Line 46
{NULL, 0, NULL, 0} Line 47
}; Block 2
void Line 50
usage (int status) Line 51
{
if (status != EXIT_SUCCESS) Line 53
emit_try_help (); ...!common auto-comment...
else Line 55
{
printf (_("Usage: %s [OPTION]...\n"), program_name); Line 57
fputs (_("\ Line 58
Print the number of processing units available to the current process,\n\ Line 59
which may be less than the number of online processors\n\ Line 60
\n\
"), stdout); Line 62
fputs (_("\ Line 63
--all print the number of installed processors\n\ Line 64
--ignore=N if possible, exclude N processing units\n\ Line 65
"), stdout); Line 66
fputs (HELP_OPTION_DESCRIPTION, stdout); Line 68
fputs (VERSION_OPTION_DESCRIPTION, stdout); Line 69
emit_ancillary_info (PROGRAM_NAME); Line 70
}
exit (status); Line 72
} Block 3
int
main (int argc, char **argv) Line 76
{
unsigned long nproc, ignore = 0; Line 78
initialize_main (&argc, &argv); VMS-specific entry point handling wildcard expansion
set_program_name (argv[0]); Retains program name and discards path
setlocale (LC_ALL, ""); Sets up internationalization (i18n)
bindtextdomain (PACKAGE, LOCALEDIR); Assigns i18n directorySets text domain for _() [gettext()] function
textdomain (PACKAGE); Sets text domain for _() [gettext()] function
atexit (close_stdout); Close stdout on exit (see gnulib)
enum nproc_query mode = NPROC_CURRENT_OVERRIDABLE; Line 87
while (1) Line 89
{
int c = getopt_long (argc, argv, "", longopts, NULL); Line 91
if (c == -1) Line 92
break; Line 93
switch (c) Line 94
{
case_GETOPT_HELP_CHAR; Line 96
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); Line 98
case ALL_OPTION: Line 100
mode = NPROC_ALL; Line 101
break; Line 102
case IGNORE_OPTION: Line 104
ignore = xdectoumax (optarg, 0, ULONG_MAX, "", _("invalid number"),0);Line 105
break; Line 106
default: Line 108
usage (EXIT_FAILURE); Line 109
}
}
if (argc != optind) Line 113
{
error (0, 0, _("extra operand %s"), quote (argv[optind])); Line 115
usage (EXIT_FAILURE); Line 116
}
nproc = num_processors (mode); Line 119
if (ignore < nproc) Line 121
nproc -= ignore; Line 122
else Line 123
nproc = 1; Line 124
printf ("%lu\n", nproc); Line 126
return EXIT_SUCCESS; Line 128
} Block 4