Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/nproc.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /* nproc - print the number of processors. | - | ||||||
2 | Copyright (C) 2009-2018 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 "error.h" | - | ||||||
26 | #include "nproc.h" | - | ||||||
27 | #include "quote.h" | - | ||||||
28 | #include "xdectoint.h" | - | ||||||
29 | - | |||||||
30 | /* The official name of this program (e.g., no 'g' prefix). */ | - | ||||||
31 | #define PROGRAM_NAME "nproc" | - | ||||||
32 | - | |||||||
33 | #define AUTHORS proper_name ("Giuseppe Scrivano") | - | ||||||
34 | - | |||||||
35 | enum | - | ||||||
36 | { | - | ||||||
37 | ALL_OPTION = CHAR_MAX + 1, | - | ||||||
38 | IGNORE_OPTION | - | ||||||
39 | }; | - | ||||||
40 | - | |||||||
41 | static struct option const longopts[] = | - | ||||||
42 | { | - | ||||||
43 | {"all", no_argument, NULL, ALL_OPTION}, | - | ||||||
44 | {"ignore", required_argument, NULL, IGNORE_OPTION}, | - | ||||||
45 | {GETOPT_HELP_OPTION_DECL}, | - | ||||||
46 | {GETOPT_VERSION_OPTION_DECL}, | - | ||||||
47 | {NULL, 0, NULL, 0} | - | ||||||
48 | }; | - | ||||||
49 | - | |||||||
50 | void | - | ||||||
51 | usage (int status) | - | ||||||
52 | { | - | ||||||
53 | if (status != EXIT_SUCCESS)
| 3-4 | ||||||
54 | emit_try_help (); executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||
55 | else | - | ||||||
56 | { | - | ||||||
57 | printf (_("Usage: %s [OPTION]...\n"), program_name); | - | ||||||
58 | fputs (_("\ | - | ||||||
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\ | - | ||||||
61 | \n\ | - | ||||||
62 | "), stdout); | - | ||||||
63 | fputs (_("\ | - | ||||||
64 | --all print the number of installed processors\n\ | - | ||||||
65 | --ignore=N if possible, exclude N processing units\n\ | - | ||||||
66 | "), stdout); | - | ||||||
67 | - | |||||||
68 | fputs (HELP_OPTION_DESCRIPTION, stdout); | - | ||||||
69 | fputs (VERSION_OPTION_DESCRIPTION, stdout); | - | ||||||
70 | emit_ancillary_info (PROGRAM_NAME); | - | ||||||
71 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||
72 | exit (status); executed 7 times by 1 test: exit (status); Executed by:
| 7 | ||||||
73 | } | - | ||||||
74 | - | |||||||
75 | int | - | ||||||
76 | main (int argc, char **argv) | - | ||||||
77 | { | - | ||||||
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); | - | ||||||
83 | textdomain (PACKAGE); | - | ||||||
84 | - | |||||||
85 | atexit (close_stdout); | - | ||||||
86 | - | |||||||
87 | enum nproc_query mode = NPROC_CURRENT_OVERRIDABLE; | - | ||||||
88 | - | |||||||
89 | while (1) | - | ||||||
90 | { | - | ||||||
91 | int c = getopt_long (argc, argv, "", longopts, NULL); | - | ||||||
92 | if (c == -1)
| 23-30 | ||||||
93 | break; executed 30 times by 1 test: break; Executed by:
| 30 | ||||||
94 | switch (c) | - | ||||||
95 | { | - | ||||||
96 | case_GETOPT_HELP_CHAR; never executed: break; executed 4 times by 1 test: case GETOPT_HELP_CHAR: Executed by:
| 0-4 | ||||||
97 | - | |||||||
98 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); executed 6 times by 1 test: exit ( 0 ); Executed by:
never executed: break; executed 6 times by 1 test: case GETOPT_VERSION_CHAR: Executed by:
| 0-6 | ||||||
99 | - | |||||||
100 | case ALL_OPTION: executed 3 times by 1 test: case ALL_OPTION: Executed by:
| 3 | ||||||
101 | mode = NPROC_ALL; | - | ||||||
102 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||
103 | - | |||||||
104 | case IGNORE_OPTION: executed 7 times by 1 test: case IGNORE_OPTION: Executed by:
| 7 | ||||||
105 | ignore = xdectoumax (optarg, 0, ULONG_MAX, "", _("invalid number"),0); | - | ||||||
106 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||
107 | - | |||||||
108 | default: executed 3 times by 1 test: default: Executed by:
| 3 | ||||||
109 | usage (EXIT_FAILURE); | - | ||||||
110 | } never executed: end of block | 0 | ||||||
111 | } | - | ||||||
112 | - | |||||||
113 | if (argc != optind)
| 0-30 | ||||||
114 | { | - | ||||||
115 | error (0, 0, _("extra operand %s"), quote (argv[optind])); | - | ||||||
116 | usage (EXIT_FAILURE); | - | ||||||
117 | } never executed: end of block | 0 | ||||||
118 | - | |||||||
119 | nproc = num_processors (mode); | - | ||||||
120 | - | |||||||
121 | if (ignore < nproc)
| 1-29 | ||||||
122 | nproc -= ignore; executed 29 times by 1 test: nproc -= ignore; Executed by:
| 29 | ||||||
123 | else | - | ||||||
124 | nproc = 1; executed 1 time by 1 test: nproc = 1; Executed by:
| 1 | ||||||
125 | - | |||||||
126 | printf ("%lu\n", nproc); | - | ||||||
127 | - | |||||||
128 | return EXIT_SUCCESS; executed 30 times by 1 test: return 0 ; Executed by:
| 30 | ||||||
129 | } | - | ||||||
Source code | Switch to Preprocessed file |