*** empty log message ***
[coreutils.git] / src / nice.c
blob0a9209781534a4d9fa97fd3e94cb44818d8a4f07
1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 1990-1999 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 "long-options.h"
34 #include "error.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "nice"
39 #define AUTHORS "David MacKenzie"
41 #ifdef NICE_PRIORITY
42 # define GET_PRIORITY() nice (0)
43 #else
44 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
45 #endif
47 static int isinteger PARAMS ((char *s));
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 printf (_("\
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 --help display this help and exit\n\
75 --version output version information and exit\n"));
76 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
78 exit (status);
81 int
82 main (int argc, char **argv)
84 int current_priority;
85 int adjustment = 0;
86 int minusflag = 0;
87 int adjustment_given = 0;
88 int i;
90 program_name = argv[0];
91 setlocale (LC_ALL, "");
92 bindtextdomain (PACKAGE, LOCALEDIR);
93 textdomain (PACKAGE);
95 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
96 AUTHORS, usage);
98 for (i = 1; i < argc; /* empty */)
100 char *s = argv[i];
102 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
104 if (!isinteger (&s[2]))
105 error (1, 0, _("invalid option `%s'"), s);
107 minusflag = 1;
108 /* FIXME: use xstrtol */
109 adjustment = atoi (&s[2]);
110 adjustment_given = 1;
111 ++i;
113 else if (s[0] == '-' && (ISDIGIT (s[1])
114 || (s[1] == '+' && ISDIGIT (s[2]))))
116 if (s[1] == '+')
117 ++s;
118 if (!isinteger (&s[1]))
119 error (1, 0, _("invalid option `%s'"), s);
121 minusflag = 0;
122 /* FIXME: use xstrtol */
123 adjustment = atoi (&s[1]);
124 adjustment_given = 1;
125 ++i;
127 else
129 int optc;
130 char **fake_argv = argv + i - 1;
132 /* Initialize getopt_long's internal state. */
133 optind = 0;
135 if ((optc = getopt_long (argc - (i - 1), fake_argv, "+n:",
136 longopts, NULL)) != -1)
138 switch (optc)
140 case '?':
141 usage (1);
143 case 'n':
144 if (!isinteger (optarg))
145 error (1, 0, _("invalid priority `%s'"), optarg);
147 minusflag = 0;
148 /* FIXME: use xstrtol */
149 adjustment = atoi (optarg);
150 adjustment_given = 1;
151 break;
155 i += optind - 1;
157 if (optc == EOF)
158 break;
162 if (minusflag)
163 adjustment = -adjustment;
164 if (!adjustment_given)
165 adjustment = 10;
167 if (i == argc)
169 if (adjustment_given)
171 error (0, 0, _("a command must be given with an adjustment"));
172 usage (1);
174 /* No command given; print the priority. */
175 errno = 0;
176 current_priority = GET_PRIORITY ();
177 if (current_priority == -1 && errno != 0)
178 error (1, errno, _("cannot get priority"));
179 printf ("%d\n", current_priority);
180 exit (0);
183 #ifndef NICE_PRIORITY
184 errno = 0;
185 current_priority = GET_PRIORITY ();
186 if (current_priority == -1 && errno != 0)
187 error (1, errno, _("cannot get priority"));
188 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
189 #else
190 if (nice (adjustment) == -1)
191 #endif
192 error (1, errno, _("cannot set priority"));
194 execvp (argv[i], &argv[i]);
195 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[i]);
198 /* Return nonzero if S represents a (possibly signed) decimal integer,
199 zero if not. */
201 static int
202 isinteger (char *s)
204 if (*s == '-' || *s == '+')
205 ++s;
206 if (*s == 0)
207 return 0;
208 while (*s)
210 if (!ISDIGIT (*s))
211 return 0;
212 ++s;
214 return 1;