From Liam Foy:

- Check for errno being set to ENOTDIR from execvp. This
  will stop an incorrect value being returned if a component
  of the new process image file's path is not a directory.
- Cleanup and KNF
This commit is contained in:
christos 2005-10-05 21:20:46 +00:00
parent c81227bb08
commit c2bd98ebb5
1 changed files with 27 additions and 22 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: nice.c,v 1.12 2003/08/07 11:15:24 agc Exp $ */
/* $NetBSD: nice.c,v 1.13 2005/10/05 21:20:46 christos Exp $ */
/*
* Copyright (c) 1989 The Regents of the University of California.
@ -40,15 +40,16 @@ __COPYRIGHT(
#if 0
static char sccsid[] = "@(#)nice.c 5.4 (Berkeley) 6/1/90";
#endif
__RCSID("$NetBSD: nice.c,v 1.12 2003/08/07 11:15:24 agc Exp $");
__RCSID("$NetBSD: nice.c,v 1.13 2005/10/05 21:20:46 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <locale.h>
#include <ctype.h>
#include <errno.h>
@ -57,18 +58,18 @@ __RCSID("$NetBSD: nice.c,v 1.12 2003/08/07 11:15:24 agc Exp $");
#define DEFNICE 10
int main __P((int, char **));
static void usage __P((void));
static void usage(void) __attribute__((__noreturn__));
int
main(argc, argv)
int argc;
char **argv;
main(int argc, char **argv)
{
char *ep;
int niceness = DEFNICE;
int c;
long tmp;
setlocale(LC_ALL, "");
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
/* handle obsolete -number syntax */
if (argc > 1 && argv[1][0] == '-' &&
@ -80,16 +81,20 @@ main(argc, argv)
while ((c = getopt (argc, argv, "n:")) != -1) {
switch (c) {
case 'n':
niceness = atoi (optarg);
errno = 0;
tmp = strtol(optarg, &ep, 10);
if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX)
errx(EXIT_FAILURE, "invalid argument: `%s'",
optarg);
niceness = (int)tmp;
break;
case '?':
default:
usage();
break;
}
}
argc -= optind; argv += optind;
argc -= optind;
argv += optind;
if (argc == 0)
usage();
@ -97,23 +102,23 @@ main(argc, argv)
errno = 0;
niceness += getpriority(PRIO_PROCESS, 0);
if (errno) {
err (1, "getpriority");
err(EXIT_FAILURE, "getpriority");
/* NOTREACHED */
}
if (setpriority(PRIO_PROCESS, 0, niceness)) {
warn ("setpriority");
if (setpriority(PRIO_PROCESS, 0, niceness) == -1) {
warn("setpriority");
}
execvp(argv[0], &argv[0]);
err ((errno == ENOENT) ? 127 : 126, "%s", argv[0]);
(void)execvp(argv[0], &argv[0]);
err((errno == ENOENT || errno == ENOTDIR) ? 127 : 126, "%s", argv[0]);
/* NOTREACHED */
}
static void
usage()
usage(void)
{
(void)fprintf(stderr,
"usage: nice [ -n increment ] utility [ argument ...]\n");
exit(1);
"Usage: %s [ -n increment ] utility [ argument ...]\n",
getprogname());
exit(EXIT_FAILURE);
}