*** empty log message ***
[coreutils.git] / src / nice.c
blobfddb4a96e1719c2d55706ce3b078558377a918e9
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 #define NDEBUG
24 #include <assert.h>
26 #include <getopt.h>
27 #include <sys/types.h>
28 #ifndef NICE_PRIORITY
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 #endif
33 #include "system.h"
34 #include "long-options.h"
35 #include "error.h"
37 #ifdef NICE_PRIORITY
38 # define GET_PRIORITY() nice (0)
39 #else
40 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
41 #endif
43 static int isinteger PARAMS ((char *s));
45 /* The name this program was run with. */
46 char *program_name;
48 static struct option const longopts[] =
50 {"adjustment", required_argument, NULL, 'n'},
51 {NULL, 0, NULL, 0}
54 void
55 usage (int status)
57 if (status != 0)
58 fprintf (stderr, _("Try `%s --help' for more information.\n"),
59 program_name);
60 else
62 printf (_("Usage: %s [OPTION]... [COMMAND [ARG]...]\n"), program_name);
63 printf (_("\
64 Run COMMAND with an adjusted scheduling priority.\n\
65 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
66 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
67 \n\
68 -ADJUST increment priority by ADJUST first\n\
69 -n, --adjustment=ADJUST same as -ADJUST\n\
70 --help display this help and exit\n\
71 --version output version information and exit\n"));
72 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
74 exit (status);
77 int
78 main (int argc, char **argv)
80 int current_priority;
81 int adjustment = 0;
82 int minusflag = 0;
83 int adjustment_given = 0;
84 int i;
86 program_name = argv[0];
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEDIR);
89 textdomain (PACKAGE);
91 parse_long_options (argc, argv, "nice", GNU_PACKAGE, VERSION, usage);
93 for (i = 1; i < argc; /* empty */)
95 char *s = argv[i];
97 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
99 if (!isinteger (&s[2]))
100 error (1, 0, _("invalid option `%s'"), s);
102 minusflag = 1;
103 /* FIXME: use xstrtol */
104 adjustment = atoi (&s[2]);
105 adjustment_given = 1;
106 ++i;
108 else if (s[0] == '-' && (ISDIGIT (s[1])
109 || (s[1] == '+' && ISDIGIT (s[2]))))
111 if (s[1] == '+')
112 ++s;
113 if (!isinteger (&s[1]))
114 error (1, 0, _("invalid option `%s'"), s);
116 minusflag = 0;
117 /* FIXME: use xstrtol */
118 adjustment = atoi (&s[1]);
119 adjustment_given = 1;
120 ++i;
122 else
124 int optc;
125 char **fake_argv = argv + i - 1;
127 /* Initialize getopt_long's internal state. */
128 optind = 0;
130 if ((optc = getopt_long (argc - (i - 1), fake_argv, "+n:",
131 longopts, NULL)) != -1)
133 switch (optc)
135 case '?':
136 usage (1);
138 case 'n':
139 if (!isinteger (optarg))
140 error (1, 0, _("invalid priority `%s'"), optarg);
142 minusflag = 0;
143 /* FIXME: use xstrtol */
144 adjustment = atoi (optarg);
145 adjustment_given = 1;
146 break;
150 i += optind - 1;
152 if (optc == EOF)
153 break;
157 if (minusflag)
158 adjustment = -adjustment;
159 if (!adjustment_given)
160 adjustment = 10;
162 if (i == argc)
164 if (adjustment_given)
166 error (0, 0, _("a command must be given with an adjustment"));
167 usage (1);
169 /* No command given; print the priority. */
170 errno = 0;
171 current_priority = GET_PRIORITY ();
172 if (current_priority == -1 && errno != 0)
173 error (1, errno, _("cannot get priority"));
174 printf ("%d\n", current_priority);
175 exit (0);
178 #ifndef NICE_PRIORITY
179 errno = 0;
180 current_priority = GET_PRIORITY ();
181 if (current_priority == -1 && errno != 0)
182 error (1, errno, _("cannot get priority"));
183 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
184 #else
185 if (nice (adjustment) == -1)
186 #endif
187 error (1, errno, _("cannot set priority"));
189 execvp (argv[i], &argv[i]);
190 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[i]);
193 /* Return nonzero if S represents a (possibly signed) decimal integer,
194 zero if not. */
196 static int
197 isinteger (char *s)
199 if (*s == '-' || *s == '+')
200 ++s;
201 if (*s == 0)
202 return 0;
203 while (*s)
205 if (!ISDIGIT (*s))
206 return 0;
207 ++s;
209 return 1;