1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * This is the Posix.2 mandated C compiler.  Basically, a hook to the
5 * cc(1) command.
6 *
7 * Copyright (c) 2001 by Jens Schweikhardt
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <err.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include <unistd.h>
41
42#define	CC "/usr/bin/cc"	/* The big kahuna doing the actual work. */
43#define	N_ARGS_PREPENDED (sizeof(args_prepended) / sizeof(args_prepended[0]))
44
45/*
46 * We do not add -D_POSIX_SOURCE here because any POSIX source is supposed to
47 * define it before inclusion of POSIX headers. This has the additional
48 * benefit of making c89 -D_ANSI_SOURCE do the right thing (or any other
49 * -D_FOO_SOURCE feature test macro we support.)
50 */
51static const char	*args_prepended[] = {
52	"-std=iso9899:199409",
53	"-pedantic"
54};
55
56static void	usage(void);
57
58/*
59 * Prepend the strings from args_prepended[] to the arg list; parse options,
60 * accepting only the POSIX c89 mandated options. Then exec cc to do the
61 * actual work.
62 */
63int
64main(int argc, char **argv)
65{
66	int Argc, i;
67	size_t j;
68	union {
69		const char **a;
70		char * const *b;
71	} Argv;
72
73	Argc = 0;
74	Argv.a = malloc((argc + 1 + N_ARGS_PREPENDED) * sizeof *Argv.a);
75	if (Argv.a == NULL)
76		err(1, "malloc");
77	Argv.a[Argc++] = CC;
78	for (j = 0; j < N_ARGS_PREPENDED; ++j)
79		Argv.a[Argc++] = args_prepended[j];
80	while ((i = getopt(argc, argv, "cD:EgI:l:L:o:OsU:")) != -1) {
81		if (i == '?')
82			usage();
83		if (i == 'l') {
84			if (argv[optind - 1][0] == '-') /* -llib */
85				optind -= 1;
86			else                            /* -l lib */
87				optind -= 2;
88			break; /* -llib or -l lib starts the operands. */
89		}
90	}
91	if (argc == optind) {
92		warnx("missing operand");
93		usage();
94	}
95
96	/* Append argv[1..] at the end of Argv[].a. */
97	for (i = 1; i <= argc; ++i)
98		Argv.a[Argc++] = argv[i];
99	(void)execv(CC, Argv.b);
100	err(1, "execv(" CC ")");
101}
102
103static void
104usage(void)
105{
106	fprintf(stderr,
107"usage: c89 [-cEgOs] [-D name[=value]] ... [-I directory] ... [-L directory] ...\n"
108"           [-o outfile] [-U name] ... operand ...\n"
109"\n"
110"       where operand is one or more of file.c, file.o, file.a\n"
111"       or -llibrary\n");
112	exit(1);
113}
114