nice.c (c17fd4b5cf703d807c93b40288c505055bb344ae) | nice.c (7e91d5f9263e60ac76e525afc99e073ba980d79c) |
---|---|
1/* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 39 unchanged lines hidden (view full) --- 48 49#include <sys/types.h> 50#include <sys/time.h> 51#include <sys/resource.h> 52 53#include <ctype.h> 54#include <err.h> 55#include <errno.h> | 1/* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 39 unchanged lines hidden (view full) --- 48 49#include <sys/types.h> 50#include <sys/time.h> 51#include <sys/resource.h> 52 53#include <ctype.h> 54#include <err.h> 55#include <errno.h> |
56#include <limits.h> |
|
56#include <stdio.h> 57#include <stdlib.h> 58#include <unistd.h> 59#include <string.h> 60 61#define DEFNICE 10 62 63void usage(void); 64 65int 66main(argc, argv) 67 int argc; 68 char *argv[]; 69{ | 57#include <stdio.h> 58#include <stdlib.h> 59#include <unistd.h> 60#include <string.h> 61 62#define DEFNICE 10 63 64void usage(void); 65 66int 67main(argc, argv) 68 int argc; 69 char *argv[]; 70{ |
70 int niceness = DEFNICE; | 71 long niceness = DEFNICE; 72 int ch; 73 char *ep; |
71 | 74 |
72 if (argc < 2) 73 usage(); | 75 /* Obsolescent syntax: -number, --number */ 76 if (argc >= 2 && argv[1][0] == '-' && (argv[1][1] == '-' || 77 isdigit((unsigned char)argv[1][1])) && strcmp(argv[1], "--") != 0) 78 if (asprintf(&argv[1], "-n%s", argv[1] + 1) < 0) 79 err(1, "asprintf"); |
74 | 80 |
75 if (argv[1][0] == '-') { 76 if (argv[1][1] == '-' || isdigit(argv[1][1])) { 77 niceness = atoi(argv[1] + 1); 78 ++argv; 79 } else 80 errx(1, "illegal option -- %s", argv[1]); | 81 while ((ch = getopt(argc, argv, "n:")) != -1) { 82 switch (ch) { 83 case 'n': 84 errno = 0; 85 niceness = strtol(optarg, &ep, 10); 86 if (ep == optarg || *ep != '\0' || errno || 87 niceness < INT_MIN || niceness > INT_MAX) 88 errx(1, "%s: invalid nice value", optarg); 89 break; 90 default: 91 usage(); 92 } |
81 } | 93 } |
94 argc -= optind; 95 argv += optind; |
|
82 | 96 |
83 if (argv[1] == NULL) | 97 if (argc == 0) |
84 usage(); 85 86 errno = 0; 87 niceness += getpriority(PRIO_PROCESS, 0); 88 if (errno) 89 err(1, "getpriority"); | 98 usage(); 99 100 errno = 0; 101 niceness += getpriority(PRIO_PROCESS, 0); 102 if (errno) 103 err(1, "getpriority"); |
90 if (setpriority(PRIO_PROCESS, 0, niceness)) | 104 if (setpriority(PRIO_PROCESS, 0, (int)niceness)) |
91 err(1, "setpriority"); | 105 err(1, "setpriority"); |
92 execvp(argv[1], &argv[1]); 93 err(errno == ENOENT ? 127 : 126, "%s", argv[1]); | 106 execvp(*argv, argv); 107 err(errno == ENOENT ? 127 : 126, "%s", *argv); |
94} 95 96void 97usage() 98{ | 108} 109 110void 111usage() 112{ |
99 (void)fprintf(stderr, "usage: nice [-number] command [arguments]\n"); | 113 114 (void)fprintf(stderr, "usage: nice [-n incr] utility [arguments]\n"); |
100 exit(1); 101} | 115 exit(1); 116} |