/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* This is the Posix.2 mandated C compiler. Basically, a hook to the4* cc(1) command.5*6* Copyright (c) 2001 by Jens Schweikhardt7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29*/3031#include <sys/cdefs.h>32#include <err.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>3637#include <unistd.h>3839#define CC "/usr/bin/cc" /* The big kahuna doing the actual work. */40#define N_ARGS_PREPENDED (sizeof(args_prepended) / sizeof(args_prepended[0]))4142/*43* We do not add -D_POSIX_SOURCE here because any POSIX source is supposed to44* define it before inclusion of POSIX headers. This has the additional45* benefit of making c89 -D_ANSI_SOURCE do the right thing (or any other46* -D_FOO_SOURCE feature test macro we support.)47*/48static const char *args_prepended[] = {49"-std=iso9899:199409",50"-pedantic"51};5253static void usage(void);5455/*56* Prepend the strings from args_prepended[] to the arg list; parse options,57* accepting only the POSIX c89 mandated options. Then exec cc to do the58* actual work.59*/60int61main(int argc, char **argv)62{63int Argc, i;64size_t j;65union {66const char **a;67char * const *b;68} Argv;6970Argc = 0;71Argv.a = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv.a);72if (Argv.a == NULL)73err(1, "malloc");74Argv.a[Argc++] = CC;75for (j = 0; j < N_ARGS_PREPENDED; ++j)76Argv.a[Argc++] = args_prepended[j];77while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {78if (i == '?')79usage();80if (i == 'l') {81if (argv[optind - 1][0] == '-') /* -llib */82optind -= 1;83else /* -l lib */84optind -= 2;85break; /* -llib or -l lib starts the operands. */86}87}88if (argc == optind) {89warnx("missing operand");90usage();91}9293/* Append argv[1..] at the end of Argv[].a. */94for (i = 1; i <= argc; ++i)95Argv.a[Argc++] = argv[i];96(void)execv(CC, Argv.b);97err(1, "execv(" CC ")");98}99100static void101usage(void)102{103fprintf(stderr,104"usage: c89 [-cEgOs] [-D name[=value]] ... [-I directory] ... [-L directory] ...\n"105" [-o outfile] [-U name] ... operand ...\n"106"\n"107" where operand is one or more of file.c, file.o, file.a\n"108" or -llibrary\n");109exit(1);110}111112113