c99.c revision 247953
1104615Stjr/*- 2104615Stjr * Copyright (c) 2002 Tim J. Robbins. 3104615Stjr * All rights reserved. 4104615Stjr * 5104615Stjr * Redistribution and use in source and binary forms, with or without 6104615Stjr * modification, are permitted provided that the following conditions 7104615Stjr * are met: 8104615Stjr * 1. Redistributions of source code must retain the above copyright 9104615Stjr * notice, this list of conditions and the following disclaimer. 10104615Stjr * 2. Redistributions in binary form must reproduce the above copyright 11104615Stjr * notice, this list of conditions and the following disclaimer in the 12104615Stjr * documentation and/or other materials provided with the distribution. 13104615Stjr * 14104615Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15104615Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16104615Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17104615Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18104615Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19104615Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20104615Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21104615Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22104615Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23104615Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24104615Stjr * SUCH DAMAGE. 25104615Stjr */ 26104615Stjr 27104615Stjr/* 28104615Stjr * c99 -- compile standard C programs 29104615Stjr * 30104615Stjr * This is essentially a wrapper around the system C compiler that forces 31104647Stjr * the compiler into C99 mode and handles some of the standard libraries 32104647Stjr * specially. 33104615Stjr */ 34104615Stjr 35104615Stjr#include <sys/cdefs.h> 36104615Stjr__FBSDID("$FreeBSD: head/usr.bin/c99/c99.c 247953 2013-03-07 21:37:23Z dim $"); 37104615Stjr 38104615Stjr#include <sys/types.h> 39104615Stjr 40104615Stjr#include <err.h> 41104615Stjr#include <stdio.h> 42104615Stjr#include <stdlib.h> 43104615Stjr#include <string.h> 44104615Stjr#include <unistd.h> 45104615Stjr 46227153Sedstatic char **args; 47227153Sedstatic u_int cargs, nargs; 48104615Stjr 49227153Sedstatic void addarg(const char *); 50227153Sedstatic void addlib(const char *); 51227153Sedstatic void usage(void); 52104615Stjr 53104615Stjrint 54104615Stjrmain(int argc, char *argv[]) 55104615Stjr{ 56104647Stjr int ch, i; 57104615Stjr 58104615Stjr args = NULL; 59104615Stjr cargs = nargs = 0; 60104615Stjr 61104647Stjr while ((ch = getopt(argc, argv, "cD:EgI:L:o:O:sU:l:")) != -1) { 62104647Stjr if (ch == 'l') { 63104647Stjr /* Gone too far. Back up and get out. */ 64104647Stjr if (argv[optind - 1][0] == '-') 65104647Stjr optind -= 1; 66104647Stjr else 67104647Stjr optind -= 2; 68104647Stjr break; 69104647Stjr } else if (ch == '?') 70104647Stjr usage(); 71104647Stjr } 72104647Stjr 73247953Sdim addarg("/usr/bin/cc"); 74104615Stjr addarg("-std=iso9899:1999"); 75104615Stjr addarg("-pedantic"); 76104647Stjr for (i = 1; i < optind; i++) 77104615Stjr addarg(argv[i]); 78104647Stjr while (i < argc) { 79104647Stjr if (strncmp(argv[i], "-l", 2) == 0) { 80104647Stjr if (argv[i][2] != '\0') 81104647Stjr addlib(argv[i++] + 2); 82104647Stjr else { 83104647Stjr if (argv[++i] == NULL) 84104647Stjr usage(); 85104647Stjr addlib(argv[i++]); 86104647Stjr } 87104647Stjr } else 88104647Stjr addarg(argv[i++]); 89104647Stjr } 90104615Stjr execv("/usr/bin/cc", args); 91104615Stjr err(1, "/usr/bin/cc"); 92104615Stjr} 93104615Stjr 94227153Sedstatic void 95104615Stjraddarg(const char *item) 96104615Stjr{ 97140226Sstefanf if (nargs + 1 >= cargs) { 98104615Stjr cargs += 16; 99104615Stjr if ((args = realloc(args, sizeof(*args) * cargs)) == NULL) 100104615Stjr err(1, "malloc"); 101104615Stjr } 102104615Stjr if ((args[nargs++] = strdup(item)) == NULL) 103104615Stjr err(1, "strdup"); 104104615Stjr args[nargs] = NULL; 105104615Stjr} 106104615Stjr 107227153Sedstatic void 108104647Stjraddlib(const char *lib) 109104647Stjr{ 110104647Stjr 111104647Stjr if (strcmp(lib, "pthread") == 0) 112104647Stjr /* FreeBSD's gcc uses -pthread instead of -lpthread. */ 113104647Stjr addarg("-pthread"); 114104647Stjr else if (strcmp(lib, "rt") == 0) 115104647Stjr /* librt functionality is in libc or unimplemented. */ 116104647Stjr ; 117104647Stjr else if (strcmp(lib, "xnet") == 0) 118104647Stjr /* xnet functionality is in libc. */ 119104647Stjr ; 120104647Stjr else { 121104647Stjr addarg("-l"); 122104647Stjr addarg(lib); 123104647Stjr } 124104647Stjr} 125104647Stjr 126227153Sedstatic void 127104615Stjrusage(void) 128104615Stjr{ 129146466Sru (void)fprintf(stderr, "%s\n%s\n", 130146466Sru"usage: c99 [-cEgs] [-D name[=value]] ... [-I directory] ... [-L directory] ...", 131146466Sru" [-o outfile] [-O optlevel] [-U name] ... operand ..."); 132104615Stjr exit(1); 133104615Stjr} 134