(do_link): Produce the same sort of one-line output for
[coreutils.git] / src / nice.c
blob3bca81e480f955176db75e5b6e42c807b0b0a05b
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 "error.h"
34 #include "long-options.h"
35 #include "xstrtol.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "nice"
40 #define AUTHORS "David MacKenzie"
42 #ifdef NICE_PRIORITY
43 # define GET_PRIORITY() nice (0)
44 #else
45 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
46 #endif
48 /* The name this program was run with. */
49 char *program_name;
51 static struct option const longopts[] =
53 {"adjustment", required_argument, NULL, 'n'},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != 0)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
66 printf (_("\
67 Run COMMAND with an adjusted scheduling priority.\n\
68 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
69 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
70 \n\
71 -ADJUST increment priority by ADJUST first\n\
72 -n, --adjustment=ADJUST same as -ADJUST\n\
73 --help display this help and exit\n\
74 --version output version information and exit\n"));
75 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
77 exit (status);
80 int
81 main (int argc, char **argv)
83 int current_priority;
84 long int adjustment = 0;
85 int minusflag = 0;
86 int adjustment_given = 0;
87 int i;
89 program_name = argv[0];
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEDIR);
92 textdomain (PACKAGE);
94 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
95 AUTHORS, usage);
97 for (i = 1; i < argc; /* empty */)
99 char *s = argv[i];
101 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
103 if (xstrtol (&s[2], NULL, 10, &adjustment, "") != LONGINT_OK)
104 error (1, 0, _("invalid option `%s'"), s);
106 minusflag = 1;
107 adjustment_given = 1;
108 ++i;
110 else if (s[0] == '-' && (ISDIGIT (s[1])
111 || (s[1] == '+' && ISDIGIT (s[2]))))
113 if (s[1] == '+')
114 ++s;
115 if (xstrtol (&s[1], NULL, 10, &adjustment, "") != LONGINT_OK)
116 error (1, 0, _("invalid option `%s'"), s);
118 minusflag = 0;
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 (xstrtol (optarg, NULL, 10, &adjustment, "")
140 != LONGINT_OK)
141 error (1, 0, _("invalid priority `%s'"), optarg);
143 minusflag = 0;
144 adjustment_given = 1;
145 break;
149 i += optind - 1;
151 if (optc == EOF)
152 break;
156 if (minusflag)
157 adjustment = -adjustment;
158 if (!adjustment_given)
159 adjustment = 10;
161 if (i == argc)
163 if (adjustment_given)
165 error (0, 0, _("a command must be given with an adjustment"));
166 usage (1);
168 /* No command given; print the priority. */
169 errno = 0;
170 current_priority = GET_PRIORITY ();
171 if (current_priority == -1 && errno != 0)
172 error (1, errno, _("cannot get priority"));
173 printf ("%d\n", current_priority);
174 exit (0);
177 #ifndef NICE_PRIORITY
178 errno = 0;
179 current_priority = GET_PRIORITY ();
180 if (current_priority == -1 && errno != 0)
181 error (1, errno, _("cannot get priority"));
182 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
183 #else
184 if (nice (adjustment) == -1)
185 #endif
186 error (1, errno, _("cannot set priority"));
188 execvp (argv[i], &argv[i]);
191 int exit_status = (errno == ENOENT ? 127 : 126);
192 error (0, errno, "%s", argv[i]);
193 exit (exit_status);