*** empty log message ***
[coreutils.git] / src / nice.c
blob784b2cc864ee84a6816d0a79a1f39d9f55489124
1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 1990-2001 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)
7 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, 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> */
20 #include <config.h>
21 #include <stdio.h>
23 #include <assert.h>
25 #include <getopt.h>
26 #include <sys/types.h>
27 #ifndef NICE_PRIORITY
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 #endif
32 #include "system.h"
33 #include "error.h"
34 #include "long-options.h"
35 #include "xstrtol.h"
36 #include "closeout.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "nice"
41 #define AUTHORS "David MacKenzie"
43 #ifdef NICE_PRIORITY
44 # define GET_PRIORITY() nice (0)
45 #else
46 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
47 #endif
49 /* The name this program was run with. */
50 char *program_name;
52 static struct option const longopts[] =
54 {"adjustment", required_argument, NULL, 'n'},
55 {NULL, 0, NULL, 0}
58 void
59 usage (int status)
61 if (status != 0)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
66 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
67 fputs (_("\
68 Run COMMAND with an adjusted scheduling priority.\n\
69 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
70 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
71 \n\
72 -ADJUST increment priority by ADJUST first\n\
73 -n, --adjustment=ADJUST same as -ADJUST\n\
74 "), stdout);
75 fputs (HELP_OPTION_DESCRIPTION, stdout);
76 fputs (VERSION_OPTION_DESCRIPTION, stdout);
77 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
79 exit (status);
82 int
83 main (int argc, char **argv)
85 int current_priority;
86 long int adjustment = 0;
87 int minusflag = 0;
88 int adjustment_given = 0;
89 int i;
91 program_name = argv[0];
92 setlocale (LC_ALL, "");
93 bindtextdomain (PACKAGE, LOCALEDIR);
94 textdomain (PACKAGE);
96 atexit (close_stdout);
98 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
99 AUTHORS, usage);
101 for (i = 1; i < argc; /* empty */)
103 char *s = argv[i];
105 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
107 if (xstrtol (&s[2], NULL, 10, &adjustment, "") != LONGINT_OK)
108 error (1, 0, _("invalid option `%s'"), s);
110 minusflag = 1;
111 adjustment_given = 1;
112 ++i;
114 else if (s[0] == '-' && (ISDIGIT (s[1])
115 || (s[1] == '+' && ISDIGIT (s[2]))))
117 if (s[1] == '+')
118 ++s;
119 if (xstrtol (&s[1], NULL, 10, &adjustment, "") != LONGINT_OK)
120 error (1, 0, _("invalid option `%s'"), s);
122 minusflag = 0;
123 adjustment_given = 1;
124 ++i;
126 else
128 int optc;
129 char **fake_argv = argv + i - 1;
131 /* Initialize getopt_long's internal state. */
132 optind = 0;
134 if ((optc = getopt_long (argc - (i - 1), fake_argv, "+n:",
135 longopts, NULL)) != -1)
137 switch (optc)
139 case '?':
140 usage (1);
142 case 'n':
143 if (xstrtol (optarg, NULL, 10, &adjustment, "")
144 != LONGINT_OK)
145 error (1, 0, _("invalid priority `%s'"), optarg);
147 minusflag = 0;
148 adjustment_given = 1;
149 break;
153 i += optind - 1;
155 if (optc == EOF)
156 break;
160 if (minusflag)
161 adjustment = -adjustment;
162 if (!adjustment_given)
163 adjustment = 10;
165 if (i == argc)
167 if (adjustment_given)
169 error (0, 0, _("a command must be given with an adjustment"));
170 usage (1);
172 /* No command given; print the priority. */
173 errno = 0;
174 current_priority = GET_PRIORITY ();
175 if (current_priority == -1 && errno != 0)
176 error (1, errno, _("cannot get priority"));
177 printf ("%d\n", current_priority);
178 exit (0);
181 #ifndef NICE_PRIORITY
182 errno = 0;
183 current_priority = GET_PRIORITY ();
184 if (current_priority == -1 && errno != 0)
185 error (1, errno, _("cannot get priority"));
186 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
187 #else
188 if (nice (adjustment) == -1)
189 #endif
190 error (1, errno, _("cannot set priority"));
192 execvp (argv[i], &argv[i]);
195 int exit_status = (errno == ENOENT ? 127 : 126);
196 error (0, errno, "%s", argv[i]);
197 exit (exit_status);