1178888Sjulian/*
2178888Sjulian * Copyright (c) 1989, 1993, 1994
3178888Sjulian *	The Regents of the University of California.  All rights reserved.
4178888Sjulian * Copyright (c) 2008 Cisco Systems, All rights reserved
5178888Sjulian *
6178888Sjulian * Redistribution and use in source and binary forms, with or without
7178888Sjulian * modification, are permitted provided that the following conditions
8178888Sjulian * are met:
9178888Sjulian * 1. Redistributions of source code must retain the above copyright
10178888Sjulian *    notice, this list of conditions and the following disclaimer.
11178888Sjulian * 2. Redistributions in binary form must reproduce the above copyright
12178888Sjulian *    notice, this list of conditions and the following disclaimer in the
13178888Sjulian *    documentation and/or other materials provided with the distribution.
14178888Sjulian * 4. Neither the name of the University nor the names of its contributors
15178888Sjulian *    may be used to endorse or promote products derived from this software
16178888Sjulian *    without specific prior written permission.
17178888Sjulian *
18178888Sjulian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19178888Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20178888Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21178888Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22178888Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23178888Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24178888Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25178888Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26178888Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27178888Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28178888Sjulian * SUCH DAMAGE.
29178888Sjulian *
30178888Sjulian * setfib file skelaton taken from nice.c
31178888Sjulian */
32178888Sjulian
33178888Sjulian#include <sys/cdefs.h>
34178888Sjulian__FBSDID("$FreeBSD$");
35178888Sjulian
36183987Sdelphij#include <ctype.h>
37183987Sdelphij#include <err.h>
38178888Sjulian#include <errno.h>
39178888Sjulian#include <limits.h>
40178888Sjulian#include <stdio.h>
41178888Sjulian#include <stdlib.h>
42178888Sjulian#include <unistd.h>
43183987Sdelphij#include <sys/socket.h>
44178888Sjulian#include <sys/sysctl.h>
45178888Sjulian
46183987Sdelphijstatic void usage(void);
47178888Sjulian
48178888Sjulianint
49178888Sjulianmain(int argc, char *argv[])
50178888Sjulian{
51178888Sjulian	long fib = 0;
52178888Sjulian	int ch;
53178888Sjulian	char *ep;
54178888Sjulian	int	numfibs;
55178916Sjulian	size_t intsize = sizeof(int);
56178888Sjulian
57178888Sjulian        if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
58178888Sjulian		errx(1, "Multiple FIBS not supported");
59178888Sjulian	if (argc < 2)
60178888Sjulian		usage();
61178888Sjulian	ep = argv[1];
62178888Sjulian	/*
63178888Sjulian	 * convert -N or N to -FN. (N is a number)
64178888Sjulian	 */
65178888Sjulian	if (ep[0]== '-' && isdigit((unsigned char)ep[1]))
66178888Sjulian		ep++;
67178888Sjulian	if (isdigit((unsigned char)*ep))
68178888Sjulian               if (asprintf(&argv[1], "-F%s", ep) < 0)
69178888Sjulian                        err(1, "asprintf");
70178888Sjulian
71178888Sjulian	while ((ch = getopt(argc, argv, "F:")) != -1) {
72178888Sjulian		switch (ch) {
73178888Sjulian		case 'F':
74178888Sjulian			errno = 0;
75178888Sjulian			fib = strtol(optarg, &ep, 10);
76178888Sjulian			if (ep == optarg || *ep != '\0' || errno ||
77178888Sjulian			    fib < 0 || fib >= numfibs)
78182853Smatteo				errx(1, "%s: invalid FIB (max %d)",
79178888Sjulian				    optarg, numfibs - 1);
80178888Sjulian			break;
81178888Sjulian		default:
82178888Sjulian			usage();
83178888Sjulian		}
84178888Sjulian	}
85178888Sjulian	argc -= optind;
86178888Sjulian	argv += optind;
87178888Sjulian
88178888Sjulian	if (argc == 0)
89178888Sjulian		usage();
90178888Sjulian
91178888Sjulian	errno = 0;
92180784Sjulian	if (setfib((int)fib))
93178888Sjulian		warn("setfib");
94178888Sjulian	execvp(*argv, argv);
95178888Sjulian	err(errno == ENOENT ? 127 : 126, "%s", *argv);
96178888Sjulian}
97178888Sjulian
98183987Sdelphijstatic void
99178888Sjulianusage(void)
100178888Sjulian{
101178888Sjulian
102178888Sjulian	(void)fprintf(stderr,
103179552Smlaier	    "usage: setfib [-[F]]value command\n");
104178888Sjulian	exit(1);
105178888Sjulian}
106