1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 1990-2002 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* David MacKenzie <djm@gnu.ai.mit.edu> */
26 #include <sys/types.h>
28 # include <sys/time.h>
29 # include <sys/resource.h>
35 #include "long-options.h"
39 /* The official name of this program (e.g., no `g' prefix). */
40 #define PROGRAM_NAME "nice"
42 #define AUTHORS "David MacKenzie"
45 # define GET_PRIORITY() nice (0)
47 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
50 /* The name this program was run with. */
53 static struct option
const longopts
[] =
55 {"adjustment", required_argument
, NULL
, 'n'},
63 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
67 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name
);
69 Run COMMAND with an adjusted scheduling priority.\n\
70 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
71 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
73 -n, --adjustment=ADJUST increment priority by ADJUST first\n\
75 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
76 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
77 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
83 main (int argc
, char **argv
)
86 long int adjustment
= 0;
88 int adjustment_given
= 0;
91 program_name
= argv
[0];
92 setlocale (LC_ALL
, "");
93 bindtextdomain (PACKAGE
, LOCALEDIR
);
96 atexit (close_stdout
);
98 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
101 for (i
= 1; i
< argc
; /* empty */)
105 if (s
[0] == '-' && s
[1] == '-' && ISDIGIT (s
[2])
106 && posix2_version () < 200112)
108 if (xstrtol (&s
[2], NULL
, 10, &adjustment
, "") != LONGINT_OK
)
109 error (EXIT_FAILURE
, 0, _("invalid option `%s'"), s
);
112 adjustment_given
= 1;
116 && (ISDIGIT (s
[1]) || (s
[1] == '+' && ISDIGIT (s
[2])))
117 && posix2_version () < 200112)
121 if (xstrtol (&s
[1], NULL
, 10, &adjustment
, "") != LONGINT_OK
)
122 error (EXIT_FAILURE
, 0, _("invalid option `%s'"), s
);
125 adjustment_given
= 1;
131 char **fake_argv
= argv
+ i
- 1;
133 /* Initialize getopt_long's internal state. */
136 if ((optc
= getopt_long (argc
- (i
- 1), fake_argv
, "+n:",
137 longopts
, NULL
)) != -1)
142 usage (EXIT_FAILURE
);
145 if (xstrtol (optarg
, NULL
, 10, &adjustment
, "")
147 error (EXIT_FAILURE
, 0, _("invalid priority `%s'"), optarg
);
150 adjustment_given
= 1;
163 adjustment
= -adjustment
;
164 if (!adjustment_given
)
169 if (adjustment_given
)
171 error (0, 0, _("a command must be given with an adjustment"));
172 usage (EXIT_FAILURE
);
174 /* No command given; print the priority. */
176 current_priority
= GET_PRIORITY ();
177 if (current_priority
== -1 && errno
!= 0)
178 error (EXIT_FAILURE
, errno
, _("cannot get priority"));
179 printf ("%d\n", current_priority
);
183 #ifndef NICE_PRIORITY
185 current_priority
= GET_PRIORITY ();
186 if (current_priority
== -1 && errno
!= 0)
187 error (EXIT_FAILURE
, errno
, _("cannot get priority"));
188 if (setpriority (PRIO_PROCESS
, 0, current_priority
+ adjustment
))
190 if (nice (adjustment
) == -1)
192 error (EXIT_FAILURE
, errno
, _("cannot set priority"));
194 execvp (argv
[i
], &argv
[i
]);
197 int exit_status
= (errno
== ENOENT
? 127 : 126);
198 error (0, errno
, "%s", argv
[i
]);