Add tests with filenames containing newline and backslash characters.
[coreutils.git] / src / nice.c
blob457cffa38d9581720bd0f57e3ae2784d77f97681
1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 90,91,92,93,94,95,96,1997 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));
44 static void usage PARAMS ((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;
62 int i;
64 program_name = argv[0];
65 setlocale (LC_ALL, "");
66 bindtextdomain (PACKAGE, LOCALEDIR);
67 textdomain (PACKAGE);
69 parse_long_options (argc, argv, "nice", GNU_PACKAGE, VERSION, usage);
71 for (i = 1; i < argc; /* empty */)
73 char *s = argv[i];
75 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
77 if (!isinteger (&s[2]))
78 error (1, 0, _("invalid option `%s'"), s);
80 minusflag = 1;
81 /* FIXME: use xstrtol */
82 adjustment = atoi (&s[2]);
83 adjustment_given = 1;
84 ++i;
86 else if (s[0] == '-' && (ISDIGIT (s[1])
87 || (s[1] == '+' && ISDIGIT (s[2]))))
89 if (s[1] == '+')
90 ++s;
91 if (!isinteger (&s[1]))
92 error (1, 0, _("invalid option `%s'"), s);
94 minusflag = 0;
95 /* FIXME: use xstrtol */
96 adjustment = atoi (&s[1]);
97 adjustment_given = 1;
98 ++i;
100 else
102 int optc;
103 char **fake_argv = argv + i - 1;
105 /* Initialize getopt_long's internal state. */
106 optind = 0;
108 if ((optc = getopt_long (argc - (i - 1), fake_argv, "+n:",
109 longopts, NULL)) != -1)
111 switch (optc)
113 case '?':
114 usage (1);
116 case 'n':
117 if (!isinteger (optarg))
118 error (1, 0, _("invalid priority `%s'"), optarg);
120 minusflag = 0;
121 /* FIXME: use xstrtol */
122 adjustment = atoi (optarg);
123 adjustment_given = 1;
124 break;
128 i += optind - 1;
130 if (optc == EOF)
131 break;
135 if (minusflag)
136 adjustment = -adjustment;
137 if (!adjustment_given)
138 adjustment = 10;
140 if (i == argc)
142 if (adjustment_given)
144 error (0, 0, _("a command must be given with an adjustment"));
145 usage (1);
147 /* No command given; print the priority. */
148 errno = 0;
149 current_priority = GET_PRIORITY ();
150 if (current_priority == -1 && errno != 0)
151 error (1, errno, _("cannot get priority"));
152 printf ("%d\n", current_priority);
153 exit (0);
156 #ifndef NICE_PRIORITY
157 errno = 0;
158 current_priority = GET_PRIORITY ();
159 if (current_priority == -1 && errno != 0)
160 error (1, errno, _("cannot get priority"));
161 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
162 #else
163 if (nice (adjustment) == -1)
164 #endif
165 error (1, errno, _("cannot set priority"));
167 execvp (argv[i], &argv[i]);
168 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[i]);
171 /* Return nonzero if S represents a (possibly signed) decimal integer,
172 zero if not. */
174 static int
175 isinteger (char *s)
177 if (*s == '-' || *s == '+')
178 ++s;
179 if (*s == 0)
180 return 0;
181 while (*s)
183 if (!ISDIGIT (*s))
184 return 0;
185 ++s;
187 return 1;
190 static void
191 usage (int status)
193 if (status != 0)
194 fprintf (stderr, _("Try `%s --help' for more information.\n"),
195 program_name);
196 else
198 printf (_("Usage: %s [OPTION]... [COMMAND [ARG]...]\n"), program_name);
199 printf (_("\
200 Run COMMAND with an adjusted scheduling priority.\n\
201 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
202 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
204 -ADJUST increment priority by ADJUST first\n\
205 -n, --adjustment=ADJUST same as -ADJUST\n\
206 --help display this help and exit\n\
207 --version output version information and exit\n"));
208 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
210 exit (status);