1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 1990, 1991 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* David MacKenzie <djm@ai.mit.edu> */
22 #include <sys/types.h>
25 #include <sys/resource.h>
31 static int isinteger ();
34 /* The name this program was run with. */
37 static struct option const longopts[] =
39 {"adjustment", 1, NULL, 'n'},
51 int adjustment_given = 0;
54 program_name = argv[0];
56 while ((optc = getopt_long (argc, argv, "+0123456789-n:", longopts,
65 if (!isinteger (optarg))
66 error (1, 0, "invalid priority `%s'", optarg);
67 adjustment = atoi (optarg);
76 adjustment = adjustment * 10 + optc - '0';
82 adjustment = -adjustment;
83 if (!adjustment_given)
90 /* No command given; print the priority. */
93 current_priority = getpriority (PRIO_PROCESS, 0);
95 current_priority = nice (0);
97 if (current_priority == -1 && errno != 0)
98 error (1, errno, "cannot get priority");
99 printf ("%d\n", current_priority);
104 #ifndef NICE_PRIORITY
105 current_priority = getpriority (PRIO_PROCESS, 0);
107 current_priority = nice (0);
109 if (current_priority == -1 && errno != 0)
110 error (1, errno, "cannot get priority");
112 #ifndef NICE_PRIORITY
113 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
115 if (nice (adjustment) == -1)
117 error (1, errno, "cannot set priority");
119 execvp (argv[optind], &argv[optind]);
120 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
123 /* Return nonzero if S represents a (possibly signed) decimal integer,
136 if (*s < '0' || *s > '9')
147 Usage: %s [-n adjustment] [-adjustment] [--adjustment=adjustment]\n\
148 [command [arg...]]\n",