File: config.c

package info (click to toggle)
splitvt 1.6.3-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 308 kB
  • ctags: 390
  • sloc: ansic: 4,631; sh: 78; makefile: 38; perl: 15
file content (277 lines) | stat: -rw-r--r-- 7,209 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include	<sys/types.h>
#include	<stdio.h>
#include	<sys/stat.h>
#include	<signal.h>

/*#define DEBUG		/* Provides extra debugging info */

#define VERBOSE_PRINT(X)	if ( verbose ) printf(X)

#define F_OK	0
#define X_OK	1
#define W_OK	2
#define R_OK	4

int exists(dir, file)
char *dir;
char *file;
{
	struct stat statbuf;
	char buffer[BUFSIZ];

	sprintf(buffer, "%s/%s", dir, file);
	if ( stat(buffer, &statbuf) == 0 )
		return(1);
	else
		return(0);
}


main(argc, argv)
int argc;
char *argv[];
{
	int  irix=0, solaris=0, verbose=0, write_utmp;
	char cflags[BUFSIZ], ldflags[BUFSIZ];
	char line[BUFSIZ];
	FILE *makefile;

	if ( argv[1] && strncmp(argv[1], "-v", 2) == 0 )
		verbose=1;

	if ( exists(".", "Makefile") )
	{
		fprintf(stderr, "Makefile already exists.  Continue?  [y/n] ");
		fgets(line, BUFSIZ-1, stdin);
		if ( line[0] == 'y' )
			printf("Continuing...\n");
		else
		{
			printf("Configuration aborted. Exiting.\n");
			exit(0);
		}
	}

	if ( (makefile=fopen("Makefile", "w")) == NULL )
	{
		perror("Can't create Makefile");
		printf("Configuration aborted. Exiting.\n");
		exit(2);
	}

#ifdef DEBUG
	strcpy(cflags, "-g -DDEBUG");
#else
	cflags[0]='\0';
#endif
	ldflags[0]='\0';

#define INCLUDE "/usr/include/"
#ifdef linux
	printf("Aha!  You are running Linux!\n");
	printf("\tCongratulations on the choice of a GNU generation.\n");

	strcat(cflags, " -O2");
#else

#ifdef NeXT
	printf("NeXTStep!  I'm assuming 3.0 or greater...\n");
	strcat(cflags, " -O2");
#undef INCLUDE
#define INCLUDE "/usr/include/bsd/"
#else
	/* Check for HP-UX */
	if ( exists("/", "hp-ux") ) {
		VERBOSE_PRINT("\tI see you are running HP-UX.\n");
		strcat(cflags, " -DHP_UX");
	} else if ( exists("/usr/lib", "libsocket.a") ) {   /* Solaris 2.1 */
		strcat(ldflags, " -lsocket");
		strcat(cflags, " -g -DSOLARIS");
		solaris=1;
		VERBOSE_PRINT("\tI see you are running Solaris.\n");
	}
	else
		strcat(cflags, " -O");

	/* Check for IRIX */
	if ( grep("/usr/include", "unistd.h", "_getpty") )
	{
		strcat(cflags, " -DIRIX");
		irix=1;
		VERBOSE_PRINT("\tI see you are running IRIX.\n");
	}
#endif /* NeXT */
#endif /* linux */

	/* Check for the termcap library */
	if ( exists("/usr/lib", "libtermcap.a") ) {
		strcat(cflags, " -DTERMCAP");
		strcat(ldflags, " -ltermcap");
		VERBOSE_PRINT("\tUsing the termcap library for terminal support.\n");
	}

	/* Check for the ut_host field in the utmp file */
	if ( grep(INCLUDE, "utmp.h", "ut_host") ) {
		strcat(cflags, " -DHAVE_UTHOST");
		VERBOSE_PRINT("\tYour utmp file uses the host field.\n");
	}

	/* Check for termio.h */
	if ( exists(INCLUDE, "termio.h") ) {
		strcat(cflags, " -DHAVE_TERMIO_H");
		VERBOSE_PRINT("\tI will use termio tty structures.\n");
	} else 
		VERBOSE_PRINT("\tI will use sgttyb tty structures.\n");

	/* Check for unistd.h */
	if ( exists(INCLUDE, "unistd.h") )
		strcat(cflags, " -DHAVE_UNISTD_H");

	/* Check for BSD tty compatibility. (HP-UX) */
	/* Check for BSD tty compatibility. (HP-UX) */
	if ( exists(INCLUDE, "sys/bsdtty.h") ) {
		strcat(cflags, " -DHAVE_BSDTTY_H");
		VERBOSE_PRINT("\tI see you have BSD tty support.\n");
	}

	/* Check for ioctl compatibility.  (FreeBSD) */
	if ( exists(INCLUDE, "sys/ioctl_compat.h") ) {
		strcat(cflags, " -DNEED_COMPAT_H");
		VERBOSE_PRINT(
			"\tI will use your ioctl compatibility header.\n");
	}

	/* Check for BSD socket library header (AT&T) */
	if ( exists(INCLUDE, "sys/inet.h") ) {
		strcat(cflags, " -DNEED_INET_H");
		VERBOSE_PRINT("\tI will use your inet compatibility header.\n");
	}

	/* Check for BSD socket library header (AIX) */
	if ( exists(INCLUDE, "sys/select.h") ) {
		strcat(cflags, " -DNEED_SELECT_H");
		VERBOSE_PRINT("\tI will use your sockets definition header.\n");
	}

	/* Check for wait4() (This always seems to be true.) */
	if ( grep(INCLUDE, "sys/wait.h", "wait4") ) {
		strcat(cflags, " -DHAVE_WAIT4");
		VERBOSE_PRINT("\tI will use wait4() instead of waitpid().\n");
	}

	if ( exists("/usr/lib", "libnet.a") )	/* Socket routines */
		strcat(ldflags, " -lnet");
	if ( exists("/usr/lib", "libnsl.a") )	/* AT&T socket library */
		strcat(ldflags, " -lnsl");
	if ( exists("/usr/lib", "libnsl_s.a") )	/* AT&T socket library */
		strcat(ldflags, " -lnsl_s");
	if ( exists("/usr/lib", "libsun.a") )	/* IRIX yp routines */
		strcat(ldflags, " -lsun");

	/* Tell the user what kind of configuration to do */
	if ( (access("/var/run/utmp", (R_OK|W_OK)) == 0) && getuid() )
		write_utmp=1;
	else
		write_utmp=0;

	VERBOSE_PRINT("\n");
	if ( solaris || irix ) {
		if ( write_utmp ) {
			VERBOSE_PRINT(
	"This program doesn't need to be installed set-uid root.\n");
			VERBOSE_PRINT(
	"\nThis program will put entries for its windows in /var/run/utmp.\n");
		} else {
			VERBOSE_PRINT(
	"If installed set-uid root, this program will put entries for its windows\nin /var/run/utmp.\n");
		}
	} else if ( write_utmp ) {
		VERBOSE_PRINT(
	"This program will put entries for its windows in /var/run/utmp.\n");
		VERBOSE_PRINT(
	"\nIf installed set-uid root, this program will change ownership of the\n");
		VERBOSE_PRINT(
	"ttys acquired to the user running this program.\n");
	} else {
		VERBOSE_PRINT(
	"If installed set-uid root, this program will put entries for its windows\n");
		VERBOSE_PRINT(
	"in /var/run/utmp, and will also change ownership of the ttys it acquires to the\n");
		VERBOSE_PRINT(
	"user running this program.\n");
	}
	VERBOSE_PRINT("\n");


	if ( exists("/bin", "csh") )
		strcat(cflags, " -DSHELL=\\\"/bin/csh\\\"");
	else
		strcat(cflags, " -DSHELL=\\\"/bin/sh\\\"");

	fprintf(makefile, 
"# This Makefile has been generated from the Configure script.\n");
	fprintf(makefile, 
"# Shareware copyright 1993, by Sam Lantinga\n\n");
#ifdef linux
	fprintf(makefile, "\nCC = gcc\n");
#endif
	fclose(makefile);
	system("sh scanpty >>Makefile");
	if ( (makefile=fopen("Makefile", "a")) == NULL )
	{
		perror("Can't finish creating Makefile");
		printf("Configuration aborted. Exiting.\n");
		exit(2);
	}
	fprintf(makefile, 
		"PTYOPTS = -DPTYCHAR=$(PTYCHAR) -DHEXDIGIT=$(HEXDIGIT)\n");
	sprintf(line, "\nCFLAGS = %s $(PTYOPTS)\nLIBS = %s\n", cflags, ldflags);
	fprintf(makefile, "%s", line);
	fprintf(makefile, "OBJS = splitvt.o misc.o utmp.o vt100.o videomem.o terminal.o vttest.o vtmouse.o \\\n");
	fprintf(makefile, "       parserc.o lock.o cut-paste.o\n\n");
	fprintf(makefile, "splitvt: $(OBJS)\n");
#if defined(linux) && !defined(DEBUG)
	fprintf(makefile, "\t$(CC) -s -o $@ $(OBJS) $(LIBS)\n");
#else
	fprintf(makefile, "\t$(CC) -o $@ $(OBJS) $(LIBS)\n");
#endif
	fprintf(makefile, "\nclean: \n\trm -f *.o core \n");
	fprintf(makefile, "\nclobber: clean\n\trm -f splitvt Makefile\n");

	fclose(makefile);
	exit(0);
}


/* Yeesh.  I have to write a word grep function.... */

int grep(dir, file, word)
char *dir;
char *file;
char *word;
{
	FILE *fp;
	char *wptr, *ptr, buffer[BUFSIZ];

	sprintf(buffer, "%s/%s", dir, file);
	if ( (fp=fopen(buffer, "r")) == NULL )
		return(0);
	ptr=word;

	while ( fgets(buffer, BUFSIZ-1, fp) != NULL ) {
		for ( wptr=buffer; *wptr; ++wptr) {
			if ( *wptr == *ptr ) {
				++ptr;
				if ( *ptr == '\0' ) {
					(void) fclose(fp);
					return(1);
				}
			}
			else
				ptr=word;
		}
	}
	(void) fclose(fp);
	return(0);
}