xref: /illumos-gate/usr/src/cmd/nice/nice.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2000-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*
32 **	nice
33 */
34 
35 
36 #include	<stdio.h>
37 #include	<locale.h>
38 #include	<stdlib.h>
39 #include	<string.h>
40 #include	<unistd.h>
41 #include	<errno.h>
42 
43 
44 static void usage(void);
45 
46 int
47 main(int argc, char *argv[])
48 {
49 	long	nicarg = 10;
50 
51 	(void) setlocale(LC_ALL, "");
52 #if !defined(TEXT_DOMAIN)
53 #define	TEXT_DOMAIN	"SYS_TEST"
54 #endif
55 	(void) textdomain(TEXT_DOMAIN);
56 
57 
58 	if (argc < 2)
59 		usage();
60 
61 
62 	if (argv[1][0] == '-') {
63 		if (strcmp(argv[1], "--") == 0) {
64 			argv++;
65 			argc--;
66 		} else {
67 			register char	*p = argv[1];
68 			char	*nicarg_str;
69 			char 	*end_ptr;
70 
71 			if (*++p == 'n') {	/* -n55 new form, XCU4 */
72 				/*
73 				 * for situations like -n-10
74 				 * else case assigns p instead of argv
75 				 */
76 				if (!(*++p)) {
77 					/* Next arg is priority */
78 					argv++;
79 					argc--;
80 					if (argc < 2)
81 						usage();
82 					nicarg_str = argv[1];
83 				} else {
84 					/* Priority embedded eg. -n-10 */
85 					nicarg_str = p;
86 				}
87 			} else {	/* -55 obs form, XCU4 */
88 				nicarg_str = &argv[1][1];
89 			}
90 			nicarg = strtol(nicarg_str, &end_ptr, 10);
91 			if (*end_ptr) {
92 				(void) fprintf(stderr,
93 				gettext("nice: argument must be numeric.\n"));
94 				usage();
95 			}
96 
97 			if( --argc < 2 )
98 				usage();
99 
100 			argv++;
101 			if (strcmp(argv[1], "--") == 0) {
102 				argv++;
103 				argc--;
104 			}
105 		}
106 	}
107 
108 	if (argc < 2)
109 		usage();
110 
111 	errno = 0;
112 	if (nice(nicarg) == -1) {
113 		/*
114 		 * Could be an error or a legitimate return value.
115 		 * The only error we care about is EINVAL, which will
116 		 * be returned by the scheduling class we are in if
117 		 * nice is invalid for this class.
118 		 * For any error other than EINVAL
119 		 * we will go ahead and exec the command even though
120 		 * the priority change failed.
121 		 */
122 		if (errno == EINVAL) {
123 			(void) fprintf(stderr, gettext(
124 			    "nice: invalid operation; "
125 			    "scheduling class does not support nice\n"));
126 			return (2);
127 		}
128 	}
129 	(void) execvp(argv[1], &argv[1]);
130 	(void) fprintf(stderr, gettext("%s: %s\n"), strerror(errno), argv[1]);
131 	/*
132 	 * POSIX.2 exit status:
133 	 * 127 if utility is not found.
134 	 * 126 if utility cannot be invoked.
135 	 */
136 	return (errno == ENOENT || errno == ENOTDIR ? 127 : 126);
137 }
138 
139 static void
140 usage()
141 {
142 	(void) fprintf(stderr,
143 	gettext("nice: usage: nice [-n increment] utility [argument ...]\n"));
144 	exit(2);
145 }
146