xref: /dragonfly/usr.bin/c99/c99.c (revision 5062ee70)
1 /*-
2  * Copyright (c) 2002 Tim J. Robbins.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/usr.bin/c99/c99.c,v 1.4 2005/05/21 09:55:05 ru Exp $
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("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, "xnet") == 0)
114 		/* xnet functionality is in libc. */
115 		;
116 	else {
117 		addarg("-l");
118 		addarg(lib);
119 	}
120 }
121 
122 static void
123 usage(void)
124 {
125 	fprintf(stderr, "%s\n%s\n",
126 "usage: c99 [-cEgs] [-D name[=value]] ... [-I directory] ... [-L directory] ...",
127 "       [-o outfile] [-O optlevel] [-U name] ... operand ...");
128 	exit(1);
129 }
130