Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / nice / nice.c
blobbb6aa29244bed4973076c2dbc0dfe7695e4127b4
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2000-2003 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 ** nice
38 #include <stdio.h>
39 #include <locale.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
46 static void usage(void);
48 int
49 main(int argc, char *argv[])
51 long nicarg = 10;
53 (void) setlocale(LC_ALL, "");
54 #if !defined(TEXT_DOMAIN)
55 #define TEXT_DOMAIN "SYS_TEST"
56 #endif
57 (void) textdomain(TEXT_DOMAIN);
60 if (argc < 2)
61 usage();
64 if (argv[1][0] == '-') {
65 if (strcmp(argv[1], "--") == 0) {
66 argv++;
67 argc--;
68 } else {
69 register char *p = argv[1];
70 char *nicarg_str;
71 char *end_ptr;
73 if (*++p == 'n') { /* -n55 new form, XCU4 */
75 * for situations like -n-10
76 * else case assigns p instead of argv
78 if (!(*++p)) {
79 /* Next arg is priority */
80 argv++;
81 argc--;
82 if (argc < 2)
83 usage();
84 nicarg_str = argv[1];
85 } else {
86 /* Priority embedded eg. -n-10 */
87 nicarg_str = p;
89 } else { /* -55 obs form, XCU4 */
90 nicarg_str = &argv[1][1];
92 nicarg = strtol(nicarg_str, &end_ptr, 10);
93 if (*end_ptr) {
94 (void) fprintf(stderr,
95 gettext("nice: argument must be numeric.\n"));
96 usage();
99 if( --argc < 2 )
100 usage();
102 argv++;
103 if (strcmp(argv[1], "--") == 0) {
104 argv++;
105 argc--;
110 if (argc < 2)
111 usage();
113 errno = 0;
114 if (nice(nicarg) == -1) {
116 * Could be an error or a legitimate return value.
117 * The only error we care about is EINVAL, which will
118 * be returned by the scheduling class we are in if
119 * nice is invalid for this class.
120 * For any error other than EINVAL
121 * we will go ahead and exec the command even though
122 * the priority change failed.
124 if (errno == EINVAL) {
125 (void) fprintf(stderr, gettext(
126 "nice: invalid operation; "
127 "scheduling class does not support nice\n"));
128 return (2);
131 (void) execvp(argv[1], &argv[1]);
132 (void) fprintf(stderr, gettext("%s: %s\n"), strerror(errno), argv[1]);
134 * POSIX.2 exit status:
135 * 127 if utility is not found.
136 * 126 if utility cannot be invoked.
138 return (errno == ENOENT || errno == ENOTDIR ? 127 : 126);
141 static void
142 usage()
144 (void) fprintf(stderr,
145 gettext("nice: usage: nice [-n increment] utility [argument ...]\n"));
146 exit(2);