merge with 1.8.1d
[coreutils.git] / src / nice.c
blob3cdc073f0cf5adcc188c8803a4f6b95036ca4547
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)
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* David MacKenzie <djm@gnu.ai.mit.edu> */
20 #ifdef HAVE_CONFIG_H
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
25 #include <config.h>
26 #else
27 #include "config.h"
28 #endif
29 #endif
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <sys/types.h>
34 #ifndef NICE_PRIORITY
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #endif
39 #include "version.h"
40 #include "system.h"
42 #ifdef NICE_PRIORITY
43 #define GET_PRIORITY() nice (0)
44 #else
45 #define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
46 #endif
48 void error ();
50 static int isinteger ();
51 static void usage ();
53 /* The name this program was run with. */
54 char *program_name;
56 /* If non-zero, display usage information and exit. */
57 static int show_help;
59 /* If non-zero, print the version on standard output and exit. */
60 static int show_version;
62 static struct option const longopts[] =
64 {"adjustment", required_argument, NULL, 'n'},
65 {"help", no_argument, &show_help, 1},
66 {"version", no_argument, &show_version, 1},
67 {NULL, 0, NULL, 0}
70 void
71 main (argc, argv)
72 int argc;
73 char **argv;
75 int current_priority;
76 int adjustment = 0;
77 int minusflag = 0;
78 int adjustment_given = 0;
79 int optc;
81 program_name = argv[0];
83 while ((optc = getopt_long (argc, argv, "+0123456789-n:", longopts,
84 (int *) 0)) != EOF)
86 switch (optc)
88 case '?':
89 usage ();
91 case 0:
92 break;
94 case 'n':
95 if (!isinteger (optarg))
96 error (1, 0, "invalid priority `%s'", optarg);
97 adjustment = atoi (optarg);
98 adjustment_given = 1;
99 break;
101 case '-':
102 minusflag = 1;
103 break;
105 default:
106 adjustment = adjustment * 10 + optc - '0';
107 adjustment_given = 1;
111 if (show_version)
113 printf ("%s\n", version_string);
114 exit (0);
117 if (show_help)
118 usage ();
120 if (minusflag)
121 adjustment = -adjustment;
122 if (!adjustment_given)
123 adjustment = 10;
125 if (optind == argc)
127 if (adjustment_given)
128 usage ();
129 /* No command given; print the priority. */
130 errno = 0;
131 current_priority = GET_PRIORITY ();
132 if (current_priority == -1 && errno != 0)
133 error (1, errno, "cannot get priority");
134 printf ("%d\n", current_priority);
135 exit (0);
138 #ifndef NICE_PRIORITY
139 errno = 0;
140 current_priority = GET_PRIORITY ();
141 if (current_priority == -1 && errno != 0)
142 error (1, errno, "cannot get priority");
143 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
144 #else
145 if (nice (adjustment) == -1)
146 #endif
147 error (1, errno, "cannot set priority");
149 execvp (argv[optind], &argv[optind]);
150 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
153 /* Return nonzero if S represents a (possibly signed) decimal integer,
154 zero if not. */
156 static int
157 isinteger (s)
158 char *s;
160 if (*s == '-')
161 ++s;
162 if (*s == 0)
163 return 0;
164 while (*s)
166 if (*s < '0' || *s > '9')
167 return 0;
168 ++s;
170 return 1;
173 static void
174 usage ()
176 fprintf (stderr, "\
177 Usage: %s [-n adjustment] [-adjustment] [--adjustment=adjustment]\n\
178 [command [arg...]]\n",
179 program_name);
180 exit (1);