1 /* nproc - print the number of processors.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Giuseppe Scrivano. */
22 #include <sys/types.h>
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "nproc"
33 #define AUTHORS proper_name ("Giuseppe Scrivano")
37 ALL_OPTION
= CHAR_MAX
+ 1,
41 static struct option
const longopts
[] =
43 {"all", no_argument
, NULL
, ALL_OPTION
},
44 {"ignore", required_argument
, NULL
, IGNORE_OPTION
},
45 {GETOPT_HELP_OPTION_DECL
},
46 {GETOPT_VERSION_OPTION_DECL
},
53 if (status
!= EXIT_SUCCESS
)
57 printf (_("Usage: %s [OPTION]...\n"), program_name
);
59 Print the number of processing units available to the current process,\n\
60 which may be less than the number of online processors\n\
64 --all print the number of installed processors\n\
65 --ignore=N if possible, exclude N processing units\n\
68 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
69 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
70 emit_ancillary_info ();
76 main (int argc
, char **argv
)
78 unsigned long nproc
, ignore
= 0;
79 initialize_main (&argc
, &argv
);
80 set_program_name (argv
[0]);
81 setlocale (LC_ALL
, "");
82 bindtextdomain (PACKAGE
, LOCALEDIR
);
85 atexit (close_stdout
);
87 enum nproc_query mode
= NPROC_CURRENT_OVERRIDABLE
;
91 int c
= getopt_long (argc
, argv
, "", longopts
, NULL
);
96 case_GETOPT_HELP_CHAR
;
98 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
105 if (xstrtoul (optarg
, NULL
, 10, &ignore
, "") != LONGINT_OK
)
107 error (0, 0, _("%s: invalid number to ignore"), optarg
);
108 usage (EXIT_FAILURE
);
113 usage (EXIT_FAILURE
);
119 error (0, 0, _("extra operand %s"), quote (argv
[optind
]));
120 usage (EXIT_FAILURE
);
123 nproc
= num_processors (mode
);
130 printf ("%lu\n", nproc
);