.
[coreutils.git] / src / nice.c
blobd5f60511de833d9260d3aea8377322d8d4df1f5b
1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 90, 91, 92, 93, 94, 95, 1996 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 __P ((char *s));
44 static void usage __P ((int status));
46 /* The name this program was run with. */
47 char *program_name;
49 static struct option const longopts[] =
51 {"adjustment", required_argument, NULL, 'n'},
52 {NULL, 0, NULL, 0}
55 int
56 main (int argc, char **argv)
58 int current_priority;
59 int adjustment = 0;
60 int minusflag = 0;
61 int adjustment_given = 0;
63 program_name = argv[0];
64 setlocale (LC_ALL, "");
65 bindtextdomain (PACKAGE, LOCALEDIR);
66 textdomain (PACKAGE);
68 parse_long_options (argc, argv, "nice", PACKAGE_VERSION, usage);
70 for (optind = 1; optind < argc; /* empty */)
72 char *s;
74 s = argv[optind];
76 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
78 if (!isinteger (&s[2]))
79 error (1, 0, _("invalid option `%s'"), s);
81 minusflag = 1;
82 /* FIXME: use xstrtol */
83 adjustment = atoi (&s[2]);
84 adjustment_given = 1;
85 ++optind;
87 else if (s[0] == '-' && ISDIGIT (s[1]))
89 if (!isinteger (&s[1]))
90 error (1, 0, _("invalid option `%s'"), s);
91 /* FIXME: use xstrtol */
92 adjustment = atoi (&s[1]);
93 adjustment_given = 1;
94 ++optind;
96 else
98 int optc;
99 if ((optc = getopt_long (argc, argv, "+n:",
100 longopts, (int *) 0)) != EOF)
102 switch (optc)
104 case '?':
105 usage (1);
107 case 'n':
108 if (!isinteger (optarg))
109 error (1, 0, _("invalid priority `%s'"), optarg);
110 /* FIXME: use xstrtol */
111 adjustment = atoi (optarg);
112 adjustment_given = 1;
113 break;
117 if (optc == EOF)
118 break;
122 if (minusflag)
123 adjustment = -adjustment;
124 if (!adjustment_given)
125 adjustment = 10;
127 if (optind == argc)
129 if (adjustment_given)
131 error (0, 0, _("a command must be given with an adjustment"));
132 usage (1);
134 /* No command given; print the priority. */
135 errno = 0;
136 current_priority = GET_PRIORITY ();
137 if (current_priority == -1 && errno != 0)
138 error (1, errno, _("cannot get priority"));
139 printf ("%d\n", current_priority);
140 exit (0);
143 #ifndef NICE_PRIORITY
144 errno = 0;
145 current_priority = GET_PRIORITY ();
146 if (current_priority == -1 && errno != 0)
147 error (1, errno, _("cannot get priority"));
148 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
149 #else
150 if (nice (adjustment) == -1)
151 #endif
152 error (1, errno, _("cannot set priority"));
154 execvp (argv[optind], &argv[optind]);
155 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
158 /* Return nonzero if S represents a (possibly signed) decimal integer,
159 zero if not. */
161 static int
162 isinteger (char *s)
164 if (*s == '-' || *s == '+')
165 ++s;
166 if (*s == 0)
167 return 0;
168 while (*s)
170 if (!ISDIGIT (*s))
171 return 0;
172 ++s;
174 return 1;
177 static void
178 usage (int status)
180 if (status != 0)
181 fprintf (stderr, _("Try `%s --help' for more information.\n"),
182 program_name);
183 else
185 printf (_("Usage: %s [OPTION]... [COMMAND [ARG]...]\n"), program_name);
186 printf (_("\
187 Run COMMAND with an adjusted scheduling priority.\n\
188 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
189 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
191 -ADJUST increment priority by ADJUST first\n\
192 -n, --adjustment=ADJUST same as -ADJUST\n\
193 --help display this help and exit\n\
194 --version output version information and exit\n"));
196 exit (status);