Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/usr.bin/c99/c99.c
34677 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2002 Tim J. Robbins.
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
* SUCH DAMAGE.
27
*/
28
29
/*
30
* c99 -- compile standard C programs
31
*
32
* This is essentially a wrapper around the system C compiler that forces
33
* the compiler into C99 mode and handles some of the standard libraries
34
* specially.
35
*/
36
37
#include <sys/types.h>
38
39
#include <err.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <string.h>
43
#include <unistd.h>
44
45
static char **args;
46
static u_int cargs, nargs;
47
48
static void addarg(const char *);
49
static void addlib(const char *);
50
static void usage(void) __dead2;
51
52
int
53
main(int argc, char *argv[])
54
{
55
int ch, i;
56
57
args = NULL;
58
cargs = nargs = 0;
59
60
while ((ch = getopt(argc, argv, "cD:EgI:L:o:O:sU:l:")) != -1) {
61
if (ch == 'l') {
62
/* Gone too far. Back up and get out. */
63
if (argv[optind - 1][0] == '-')
64
optind -= 1;
65
else
66
optind -= 2;
67
break;
68
} else if (ch == '?')
69
usage();
70
}
71
72
addarg("/usr/bin/cc");
73
addarg("-std=iso9899:1999");
74
addarg("-pedantic");
75
for (i = 1; i < optind; i++)
76
addarg(argv[i]);
77
while (i < argc) {
78
if (strncmp(argv[i], "-l", 2) == 0) {
79
if (argv[i][2] != '\0')
80
addlib(argv[i++] + 2);
81
else {
82
if (argv[++i] == NULL)
83
usage();
84
addlib(argv[i++]);
85
}
86
} else
87
addarg(argv[i++]);
88
}
89
execv("/usr/bin/cc", args);
90
err(1, "/usr/bin/cc");
91
}
92
93
static void
94
addarg(const char *item)
95
{
96
if (nargs + 1 >= cargs) {
97
cargs += 16;
98
if ((args = realloc(args, sizeof(*args) * cargs)) == NULL)
99
err(1, "malloc");
100
}
101
if ((args[nargs++] = strdup(item)) == NULL)
102
err(1, "strdup");
103
args[nargs] = NULL;
104
}
105
106
static void
107
addlib(const char *lib)
108
{
109
110
if (strcmp(lib, "pthread") == 0)
111
/* FreeBSD's gcc uses -pthread instead of -lpthread. */
112
addarg("-pthread");
113
else if (strcmp(lib, "rt") == 0)
114
/* librt functionality is in libc or unimplemented. */
115
;
116
else if (strcmp(lib, "xnet") == 0)
117
/* xnet functionality is in libc. */
118
;
119
else {
120
addarg("-l");
121
addarg(lib);
122
}
123
}
124
125
static void
126
usage(void)
127
{
128
(void)fprintf(stderr, "%s\n%s\n",
129
"usage: c99 [-cEgs] [-D name[=value]] ... [-I directory] ... [-L directory] ...",
130
" [-o outfile] [-O optlevel] [-U name] ... operand ...");
131
exit(1);
132
}
133
134