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)
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> */
27 #include <sys/types.h>
29 # include <sys/time.h>
30 # include <sys/resource.h>
34 #include "long-options.h"
38 # define GET_PRIORITY() nice (0)
40 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
43 static int isinteger
PARAMS ((char *s
));
44 static void usage
PARAMS ((int status
));
46 /* The name this program was run with. */
49 static struct option
const longopts
[] =
51 {"adjustment", required_argument
, NULL
, 'n'},
56 main (int argc
, char **argv
)
61 int adjustment_given
= 0;
64 program_name
= argv
[0];
65 setlocale (LC_ALL
, "");
66 bindtextdomain (PACKAGE
, LOCALEDIR
);
69 parse_long_options (argc
, argv
, "nice", GNU_PACKAGE
, VERSION
, usage
);
71 for (i
= 1; i
< argc
; /* empty */)
75 if (s
[0] == '-' && s
[1] == '-' && ISDIGIT (s
[2]))
77 if (!isinteger (&s
[2]))
78 error (1, 0, _("invalid option `%s'"), s
);
81 /* FIXME: use xstrtol */
82 adjustment
= atoi (&s
[2]);
86 else if (s
[0] == '-' && (ISDIGIT (s
[1])
87 || (s
[1] == '+' && ISDIGIT (s
[2]))))
91 if (!isinteger (&s
[1]))
92 error (1, 0, _("invalid option `%s'"), s
);
95 /* FIXME: use xstrtol */
96 adjustment
= atoi (&s
[1]);
103 char **fake_argv
= argv
+ i
- 1;
105 /* Initialize getopt_long's internal state. */
108 if ((optc
= getopt_long (argc
- (i
- 1), fake_argv
, "+n:",
109 longopts
, NULL
)) != -1)
117 if (!isinteger (optarg
))
118 error (1, 0, _("invalid priority `%s'"), optarg
);
121 /* FIXME: use xstrtol */
122 adjustment
= atoi (optarg
);
123 adjustment_given
= 1;
136 adjustment
= -adjustment
;
137 if (!adjustment_given
)
142 if (adjustment_given
)
144 error (0, 0, _("a command must be given with an adjustment"));
147 /* No command given; print the priority. */
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
);
156 #ifndef NICE_PRIORITY
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
))
163 if (nice (adjustment
) == -1)
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,
177 if (*s
== '-' || *s
== '+')
194 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
198 printf (_("Usage: %s [OPTION]... [COMMAND [ARG]...]\n"), program_name
);
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>."));