11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3187291Sdwmalonestatic const char copyright[] = 321590Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\ 331590Srgrimes The Regents of the University of California. All rights reserved.\n"; 341590Srgrimes#endif /* not lint */ 351590Srgrimes 3687628Sdwmalone#if 0 371590Srgrimes#ifndef lint 3887628Sdwmalonestatic char sccsid[] = "@(#)nice.c 8.2 (Berkeley) 4/16/94"; 391590Srgrimes#endif /* not lint */ 4087628Sdwmalone#endif 411590Srgrimes 4287628Sdwmalone#include <sys/cdefs.h> 4387628Sdwmalone__FBSDID("$FreeBSD: releng/10.2/usr.bin/nice/nice.c 216370 2010-12-11 08:32:16Z joel $"); 4487628Sdwmalone 451590Srgrimes#include <sys/types.h> 461590Srgrimes#include <sys/time.h> 471590Srgrimes#include <sys/resource.h> 481590Srgrimes 491590Srgrimes#include <ctype.h> 501590Srgrimes#include <err.h> 511590Srgrimes#include <errno.h> 5296481Stjr#include <limits.h> 531590Srgrimes#include <stdio.h> 541590Srgrimes#include <stdlib.h> 551590Srgrimes#include <unistd.h> 561590Srgrimes#include <string.h> 571590Srgrimes 581590Srgrimes#define DEFNICE 10 591590Srgrimes 6092921Simpvoid usage(void); 611590Srgrimes 621590Srgrimesint 63102944Sdwmalonemain(int argc, char *argv[]) 641590Srgrimes{ 6596481Stjr long niceness = DEFNICE; 6696481Stjr int ch; 6796481Stjr char *ep; 681590Srgrimes 6996481Stjr /* Obsolescent syntax: -number, --number */ 7096481Stjr if (argc >= 2 && argv[1][0] == '-' && (argv[1][1] == '-' || 7196481Stjr isdigit((unsigned char)argv[1][1])) && strcmp(argv[1], "--") != 0) 7296481Stjr if (asprintf(&argv[1], "-n%s", argv[1] + 1) < 0) 7396481Stjr err(1, "asprintf"); 7419331Swosch 7596481Stjr while ((ch = getopt(argc, argv, "n:")) != -1) { 7696481Stjr switch (ch) { 7796481Stjr case 'n': 7896481Stjr errno = 0; 7996481Stjr niceness = strtol(optarg, &ep, 10); 8096481Stjr if (ep == optarg || *ep != '\0' || errno || 8196481Stjr niceness < INT_MIN || niceness > INT_MAX) 8296481Stjr errx(1, "%s: invalid nice value", optarg); 8396481Stjr break; 8496481Stjr default: 8596481Stjr usage(); 8696481Stjr } 8748566Sbillf } 8896481Stjr argc -= optind; 8996481Stjr argv += optind; 901590Srgrimes 9196481Stjr if (argc == 0) 921590Srgrimes usage(); 931590Srgrimes 941590Srgrimes errno = 0; 951590Srgrimes niceness += getpriority(PRIO_PROCESS, 0); 961590Srgrimes if (errno) 9796502Stjr warn("getpriority"); 9896502Stjr else if (setpriority(PRIO_PROCESS, 0, (int)niceness)) 9996502Stjr warn("setpriority"); 10096481Stjr execvp(*argv, argv); 10196481Stjr err(errno == ENOENT ? 127 : 126, "%s", *argv); 1021590Srgrimes} 1031590Srgrimes 1041590Srgrimesvoid 105102944Sdwmaloneusage(void) 1061590Srgrimes{ 10796481Stjr 108146466Sru (void)fprintf(stderr, 109146466Sru "usage: nice [-n increment] utility [argument ...]\n"); 1101590Srgrimes exit(1); 1111590Srgrimes} 112