OpenCoverage

nice.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/nice.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* nice -- run a program with modified niceness-
2 Copyright (C) 1990-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/* David MacKenzie <djm@gnu.ai.mit.edu> */-
18-
19#include <config.h>-
20#include <stdio.h>-
21#include <getopt.h>-
22#include <sys/types.h>-
23-
24#include "system.h"-
25-
26#if ! HAVE_NICE-
27/* Include this after "system.h" so we're sure to have definitions-
28 (from time.h or sys/time.h) required for e.g. the ru_utime member. */-
29# include <sys/resource.h>-
30#endif-
31-
32#include "die.h"-
33#include "error.h"-
34#include "quote.h"-
35#include "xstrtol.h"-
36-
37/* The official name of this program (e.g., no 'g' prefix). */-
38#define PROGRAM_NAME "nice"-
39-
40#define AUTHORS proper_name ("David MacKenzie")-
41-
42#if HAVE_NICE-
43# define GET_NICENESS() nice (0)-
44#else-
45# define GET_NICENESS() getpriority (PRIO_PROCESS, 0)-
46#endif-
47-
48#ifndef NZERO-
49# define NZERO 20-
50#endif-
51-
52/* This is required for Darwin Kernel Version 7.7.0. */-
53#if NZERO == 0-
54# undef NZERO-
55# define NZERO 20-
56#endif-
57-
58static struct option const longopts[] =-
59{-
60 {"adjustment", required_argument, NULL, 'n'},-
61 {GETOPT_HELP_OPTION_DECL},-
62 {GETOPT_VERSION_OPTION_DECL},-
63 {NULL, 0, NULL, 0}-
64};-
65-
66void-
67usage (int status)-
68{-
69 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • nice
FALSEevaluated 6 times by 1 test
Evaluated by:
  • nice
5-6
70 emit_try_help ();
executed 5 times by 1 test: end of block
Executed by:
  • nice
5
71 else-
72 {-
73 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);-
74 printf (_("\-
75Run COMMAND with an adjusted niceness, which affects process scheduling.\n\-
76With no COMMAND, print the current niceness. Niceness values range from\n\-
77%d (most favorable to the process) to %d (least favorable to the process).\n\-
78"),-
79 - NZERO, NZERO - 1);-
80-
81 emit_mandatory_arg_note ();-
82-
83 fputs (_("\-
84 -n, --adjustment=N add integer N to the niceness (default 10)\n\-
85"), stdout);-
86 fputs (HELP_OPTION_DESCRIPTION, stdout);-
87 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
88 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);-
89 emit_ancillary_info (PROGRAM_NAME);-
90 }
executed 6 times by 1 test: end of block
Executed by:
  • nice
6
91 exit (status);
executed 11 times by 1 test: exit (status);
Executed by:
  • nice
11
92}-
93-
94static bool-
95perm_related_errno (int err)-
96{-
97 return err == EACCES || err == EPERM;
never executed: return err == 13 || err == 1 ;
0
98}-
99-
100int-
101main (int argc, char **argv)-
102{-
103 int current_niceness;-
104 int adjustment = 10;-
105 char const *adjustment_given = NULL;-
106 bool ok;-
107 int i;-
108-
109 initialize_main (&argc, &argv);-
110 set_program_name (argv[0]);-
111 setlocale (LC_ALL, "");-
112 bindtextdomain (PACKAGE, LOCALEDIR);-
113 textdomain (PACKAGE);-
114-
115 initialize_exit_failure (EXIT_CANCELED);-
116 atexit (close_stdout);-
117-
118 for (i = 1; i < argc; /* empty */)
i < argcDescription
TRUEevaluated 22 times by 1 test
Evaluated by:
  • nice
FALSEevaluated 25 times by 1 test
Evaluated by:
  • nice
22-25
119 {-
120 char const *s = argv[i];-
121-
122 if (s[0] == '-' && ISDIGIT (s[1 + (s[1] == '-' || s[1] == '+')]))
s[0] == '-'Description
TRUEevaluated 20 times by 1 test
Evaluated by:
  • nice
FALSEevaluated 2 times by 1 test
Evaluated by:
  • nice
((unsigned int...]) - '0' <= 9)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • nice
FALSEevaluated 19 times by 1 test
Evaluated by:
  • nice
1-20
123 {-
124 adjustment_given = s + 1;-
125 ++i;-
126 }
executed 1 time by 1 test: end of block
Executed by:
  • nice
1
127 else-
128 {-
129 int c;-
130 int fake_argc = argc - (i - 1);-
131 char **fake_argv = argv + (i - 1);-
132-
133 /* Ensure that any getopt diagnostics use the right name. */-
134 fake_argv[0] = argv[0];-
135-
136 /* Initialize getopt_long's internal state. */-
137 optind = 0;-
138-
139 c = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);-
140 i += optind - 1;-
141-
142 switch (c)-
143 {-
144 case 'n':
executed 4 times by 1 test: case 'n':
Executed by:
  • nice
4
145 adjustment_given = optarg;-
146 break;
executed 4 times by 1 test: break;
Executed by:
  • nice
4
147-
148 case -1:
executed 2 times by 1 test: case -1:
Executed by:
  • nice
2
149 break;
executed 2 times by 1 test: break;
Executed by:
  • nice
2
150-
151 case_GETOPT_HELP_CHAR;
never executed: break;
executed 6 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • nice
0-6
152-
153 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 5 times by 1 test: exit ( 0 );
Executed by:
  • nice
never executed: break;
executed 5 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • nice
0-5
154-
155 default:
executed 4 times by 1 test: default:
Executed by:
  • nice
4
156 usage (EXIT_CANCELED);-
157 break;
never executed: break;
0
158 }-
159-
160 if (c == -1)
c == -1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • nice
FALSEevaluated 4 times by 1 test
Evaluated by:
  • nice
2-4
161 break;
executed 2 times by 1 test: break;
Executed by:
  • nice
2
162 }
executed 4 times by 1 test: end of block
Executed by:
  • nice
4
163 }-
164-
165 if (adjustment_given)
adjustment_givenDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • nice
FALSEevaluated 25 times by 1 test
Evaluated by:
  • nice
2-25
166 {-
167 /* If the requested adjustment is outside the valid range,-
168 silently bring it to just within range; this mimics what-
169 "setpriority" and "nice" do. */-
170 enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };-
171 long int tmp;-
172 if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))
LONGINT_OVERFL... 10, &tmp, "")Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • nice
FALSEevaluated 1 time by 1 test
Evaluated by:
  • nice
1
173 die (EXIT_CANCELED, 0, _("invalid adjustment %s"),
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, 0, dcgettext (((void *)0), \"invalid adjustment %s\", 5), quote (adjustment_given)), assume (false))" ")"); int _gl_dummy; })) ? ((error (EXIT...ustment %s" , 5) , quote (adjustment_given)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error (EXIT_CANCELED, 0, dcgettext (((void *)0), "invalid adjustment %s" , 5) , quote (adjustment_given)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
Executed by:
  • nice
1
174 quote (adjustment_given));
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, 0, dcgettext (((void *)0), \"invalid adjustment %s\", 5), quote (adjustment_given)), assume (false))" ")"); int _gl_dummy; })) ? ((error (EXIT...ustment %s" , 5) , quote (adjustment_given)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error (EXIT_CANCELED, 0, dcgettext (((void *)0), "invalid adjustment %s" , 5) , quote (adjustment_given)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
Executed by:
  • nice
1
175 adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));
(( MIN_ADJUSTM...DJUSTMENT ))))Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • nice
(( tmp )<( MAX_ADJUSTMENT ))Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • nice
FALSEnever evaluated
0-1
176 }
executed 1 time by 1 test: end of block
Executed by:
  • nice
1
177-
178 if (i == argc)
i == argcDescription
TRUEevaluated 24 times by 1 test
Evaluated by:
  • nice
FALSEevaluated 2 times by 1 test
Evaluated by:
  • nice
2-24
179 {-
180 if (adjustment_given)
adjustment_givenDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • nice
FALSEevaluated 23 times by 1 test
Evaluated by:
  • nice
1-23
181 {-
182 error (0, 0, _("a command must be given with an adjustment"));-
183 usage (EXIT_CANCELED);-
184 }
never executed: end of block
0
185 /* No command given; print the niceness. */-
186 errno = 0;-
187 current_niceness = GET_NICENESS ();-
188 if (current_niceness == -1 && errno != 0)
current_niceness == -1Description
TRUEnever evaluated
FALSEevaluated 23 times by 1 test
Evaluated by:
  • nice
(*__errno_location ()) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0-23
189 die (EXIT_CANCELED, errno, _("cannot get niceness"));
never executed: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, (*__errno_location ()), dcgettext (((void *)0), \"cannot get niceness\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error (EXIT_CANCEL...cgettext (((void *)0), "cannot get niceness" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error (EXIT_CANCELED, (*__errno_location ()) , dcgettext (((void *)0), "cannot get niceness" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
190 printf ("%d\n", current_niceness);-
191 return EXIT_SUCCESS;
executed 23 times by 1 test: return 0 ;
Executed by:
  • nice
23
192 }-
193-
194 errno = 0;-
195#if HAVE_NICE-
196 ok = (nice (adjustment) != -1 || errno == 0);-
197#else-
198 current_niceness = GET_NICENESS ();-
199 if (current_niceness == -1 && errno != 0)
current_niceness == -1Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • nice
(*__errno_location ()) != 0Description
TRUEnever evaluated
FALSEnever evaluated
0-2
200 die (EXIT_CANCELED, errno, _("cannot get niceness"));
never executed: ((!!sizeof (struct { _Static_assert (EXIT_CANCELED, "verify_expr (" "EXIT_CANCELED" ", " "(error (EXIT_CANCELED, (*__errno_location ()), dcgettext (((void *)0), \"cannot get niceness\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error (EXIT_CANCEL...cgettext (((void *)0), "cannot get niceness" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error (EXIT_CANCELED, (*__errno_location ()) , dcgettext (((void *)0), "cannot get niceness" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
201 ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);-
202#endif-
203 if (!ok)
!okDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • nice
0-2
204 {-
205 error (perm_related_errno (errno) ? 0-
206 : EXIT_CANCELED, errno, _("cannot set niceness"));-
207 /* error() flushes stderr, but does not check for write failure.-
208 Normally, we would catch this via our atexit() hook of-
209 close_stdout, but execvp() gets in the way. If stderr-
210 encountered a write failure, there is no need to try calling-
211 error() again. */-
212 if (ferror (stderr))
ferror_unlocked ( stderr )Description
TRUEnever evaluated
FALSEnever evaluated
0
213 return EXIT_CANCELED;
never executed: return EXIT_CANCELED;
0
214 }
never executed: end of block
0
215-
216 execvp (argv[i], &argv[i]);-
217-
218 int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
(*__errno_location ()) == 2Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • nice
FALSEevaluated 1 time by 1 test
Evaluated by:
  • nice
1
219 error (0, errno, "%s", quote (argv[i]));-
220 return exit_status;
executed 2 times by 1 test: return exit_status;
Executed by:
  • nice
2
221}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2