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)
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>
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
__P ((char *s
));
44 static void usage
__P ((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;
63 program_name
= argv
[0];
64 setlocale (LC_ALL
, "");
65 bindtextdomain (PACKAGE
, LOCALEDIR
);
68 parse_long_options (argc
, argv
, "nice", PACKAGE_VERSION
, usage
);
70 for (optind
= 1; optind
< argc
; /* empty */)
76 if (s
[0] == '-' && s
[1] == '-' && ISDIGIT (s
[2]))
78 if (!isinteger (&s
[2]))
79 error (1, 0, _("invalid option `%s'"), s
);
82 /* FIXME: use xstrtol */
83 adjustment
= atoi (&s
[2]);
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]);
99 if ((optc
= getopt_long (argc
, argv
, "+n:",
100 longopts
, (int *) 0)) != EOF
)
108 if (!isinteger (optarg
))
109 error (1, 0, _("invalid priority `%s'"), optarg
);
110 /* FIXME: use xstrtol */
111 adjustment
= atoi (optarg
);
112 adjustment_given
= 1;
123 adjustment
= -adjustment
;
124 if (!adjustment_given
)
129 if (adjustment_given
)
131 error (0, 0, _("a command must be given with an adjustment"));
134 /* No command given; print the priority. */
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
);
143 #ifndef NICE_PRIORITY
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
))
150 if (nice (adjustment
) == -1)
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,
164 if (*s
== '-' || *s
== '+')
181 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
185 printf (_("Usage: %s [OPTION]... [COMMAND [ARG]...]\n"), program_name
);
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"));