1 /* nproc - print the number of processors.
2    Copyright (C) 2009-2023 Free Software Foundation, Inc.
3 
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.
8 
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.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Giuseppe Scrivano.  */
18 
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 
24 #include "system.h"
25 #include "nproc.h"
26 #include "quote.h"
27 #include "xdectoint.h"
28 
29 /* The official name of this program (e.g., no 'g' prefix).  */
30 #define PROGRAM_NAME "nproc"
31 
32 #define AUTHORS proper_name ("Giuseppe Scrivano")
33 
34 enum
35 {
36   ALL_OPTION = CHAR_MAX + 1,
37   IGNORE_OPTION
38 };
39 
40 static struct option const longopts[] =
41 {
42   {"all", no_argument, nullptr, ALL_OPTION},
43   {"ignore", required_argument, nullptr, IGNORE_OPTION},
44   {GETOPT_HELP_OPTION_DECL},
45   {GETOPT_VERSION_OPTION_DECL},
46   {nullptr, 0, nullptr, 0}
47 };
48 
49 void
usage(int status)50 usage (int status)
51 {
52   if (status != EXIT_SUCCESS)
53     emit_try_help ();
54   else
55     {
56       printf (_("Usage: %s [OPTION]...\n"), program_name);
57       fputs (_("\
58 Print the number of processing units available to the current process,\n\
59 which may be less than the number of online processors\n\
60 \n\
61 "), stdout);
62       fputs (_("\
63       --all      print the number of installed processors\n\
64       --ignore=N  if possible, exclude N processing units\n\
65 "), stdout);
66 
67       fputs (HELP_OPTION_DESCRIPTION, stdout);
68       fputs (VERSION_OPTION_DESCRIPTION, stdout);
69       emit_ancillary_info (PROGRAM_NAME);
70     }
71   exit (status);
72 }
73 
74 int
main(int argc,char ** argv)75 main (int argc, char **argv)
76 {
77   unsigned long nproc, ignore = 0;
78   initialize_main (&argc, &argv);
79   set_program_name (argv[0]);
80   setlocale (LC_ALL, "");
81   bindtextdomain (PACKAGE, LOCALEDIR);
82   textdomain (PACKAGE);
83 
84   atexit (close_stdout);
85 
86   enum nproc_query mode = NPROC_CURRENT_OVERRIDABLE;
87 
88   while (true)
89     {
90       int c = getopt_long (argc, argv, "", longopts, nullptr);
91       if (c == -1)
92         break;
93       switch (c)
94         {
95         case_GETOPT_HELP_CHAR;
96 
97         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
98 
99         case ALL_OPTION:
100           mode = NPROC_ALL;
101           break;
102 
103         case IGNORE_OPTION:
104           ignore = xdectoumax (optarg, 0, ULONG_MAX, "", _("invalid number"),0);
105           break;
106 
107         default:
108           usage (EXIT_FAILURE);
109         }
110     }
111 
112   if (argc != optind)
113     {
114       error (0, 0, _("extra operand %s"), quote (argv[optind]));
115       usage (EXIT_FAILURE);
116     }
117 
118   nproc = num_processors (mode);
119 
120   if (ignore < nproc)
121     nproc -= ignore;
122   else
123     nproc = 1;
124 
125   printf ("%lu\n", nproc);
126 
127   return EXIT_SUCCESS;
128 }
129