/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002 Tim J. Robbins.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* c99 -- compile standard C programs30*31* This is essentially a wrapper around the system C compiler that forces32* the compiler into C99 mode and handles some of the standard libraries33* specially.34*/3536#include <sys/types.h>3738#include <err.h>39#include <stdio.h>40#include <stdlib.h>41#include <string.h>42#include <unistd.h>4344static char **args;45static u_int cargs, nargs;4647static void addarg(const char *);48static void addlib(const char *);49static void usage(void) __dead2;5051int52main(int argc, char *argv[])53{54int ch, i;5556args = NULL;57cargs = nargs = 0;5859while ((ch = getopt(argc, argv, "cD:EgI:L:o:O:sU:l:")) != -1) {60if (ch == 'l') {61/* Gone too far. Back up and get out. */62if (argv[optind - 1][0] == '-')63optind -= 1;64else65optind -= 2;66break;67} else if (ch == '?')68usage();69}7071addarg("/usr/bin/cc");72addarg("-std=iso9899:1999");73addarg("-pedantic");74for (i = 1; i < optind; i++)75addarg(argv[i]);76while (i < argc) {77if (strncmp(argv[i], "-l", 2) == 0) {78if (argv[i][2] != '\0')79addlib(argv[i++] + 2);80else {81if (argv[++i] == NULL)82usage();83addlib(argv[i++]);84}85} else86addarg(argv[i++]);87}88execv("/usr/bin/cc", args);89err(1, "/usr/bin/cc");90}9192static void93addarg(const char *item)94{95if (nargs + 1 >= cargs) {96cargs += 16;97if ((args = realloc(args, sizeof(*args) * cargs)) == NULL)98err(1, "malloc");99}100if ((args[nargs++] = strdup(item)) == NULL)101err(1, "strdup");102args[nargs] = NULL;103}104105static void106addlib(const char *lib)107{108109if (strcmp(lib, "pthread") == 0)110/* FreeBSD's gcc uses -pthread instead of -lpthread. */111addarg("-pthread");112else if (strcmp(lib, "rt") == 0)113/* librt functionality is in libc or unimplemented. */114;115else if (strcmp(lib, "xnet") == 0)116/* xnet functionality is in libc. */117;118else {119addarg("-l");120addarg(lib);121}122}123124static void125usage(void)126{127(void)fprintf(stderr, "%s\n%s\n",128"usage: c99 [-cEgs] [-D name[=value]] ... [-I directory] ... [-L directory] ...",129" [-o outfile] [-O optlevel] [-U name] ... operand ...");130exit(1);131}132133134