aboutsummaryrefslogtreecommitdiff
path: root/debian/patches/0055_nx-X11_imake-Werror-format-security.full.patch
blob: 6e5b4c80ee42e882826b9effe4c71430e117d3f1 (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
Description: Fix FTBFS when compiled with -Werror=format-security
Author: Orion Poplawski <orion@cora.nwra.com>
diff -up a/nx-X11/config/imake/imake.c b/nx-X11/config/imake/imake.c
--- a/nx-X11/config/imake/imake.c	2014-01-04 13:39:35.000000000 -0700
+++ b/nx-X11/config/imake/imake.c	2014-01-24 13:55:53.940697330 -0700
@@ -7,8 +7,6 @@
  * be passed to the template file.                                         *
  *                                                                         *
  ***************************************************************************/
-/* $XFree86: xc/config/imake/imake.c,v 3.63tsi Exp $ */
-
 /*
  *
 Copyright (c) 1985, 1986, 1987, 1998 The Open Group
@@ -153,23 +151,14 @@ in this Software without prior written a
 #endif
 #include <stdlib.h>
 #include <stdio.h>
-#ifdef MONOLITH
-# include "Xosdefs.h"
-#else
-# include <X11/Xosdefs.h>
-#endif
+#include <stdarg.h>
+#include <X11/Xfuncproto.h>
+#include <X11/Xosdefs.h>
 #include <string.h>
 #include <ctype.h>
 #ifdef WIN32
 # include "Xw32defs.h"
 #endif
-#if 0
-#ifndef X_NOT_POSIX
-# ifndef _POSIX_SOURCE
-#  define _POSIX_SOURCE
-# endif
-#endif
-#endif
 #include <sys/types.h>
 #include <fcntl.h>
 #ifdef X_NOT_POSIX
@@ -231,22 +220,16 @@ typedef union wait	waitType;
 #  define WIFEXITED(w) waitCode(w)
 # endif
 #endif /* X_NOT_POSIX */
-# include <stdlib.h>
-#if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
-char *malloc(), *realloc();
-#endif /* macII */
+#include <stdlib.h>
 #include <errno.h>
 #ifdef __minix_vmd
-#define USE_FREOPEN		1
+# define USE_FREOPEN		1
 #endif
 
-#if !((defined(sun) && !defined(SVR4)) || defined(macII))
-#define USE_STRERROR		1
-#endif
 #ifndef WIN32
-#include <sys/utsname.h>
+# include <sys/utsname.h>
 #else
-#include <windows.h>
+# include <windows.h>
 #endif
 #ifndef SYS_NMLN
 # ifdef _SYS_NMLN
@@ -256,39 +239,22 @@ char *malloc(), *realloc();
 # endif
 #endif
 #if defined(linux) || defined(__GNU__) || defined(__GLIBC__)
-#include <limits.h>
-#include <stdio.h>
+# include <limits.h>
+# include <stdio.h>
 #endif
 #ifdef __QNX__
-#include <unix.h>
-#endif
-
-/*
- * This define of strerror is copied from (and should be identical to)
- * Xos.h, which we don't want to include here for bootstrapping reasons.
- */
-#ifndef USE_STRERROR
-# ifndef strerror
-extern char *sys_errlist[];
-extern int sys_nerr;
-#  define strerror(n) \
-    (((n) >= 0 && (n) < sys_nerr) ? sys_errlist[n] : "unknown error")
-# endif
+# include <unix.h>
 #endif
 
 #if defined(__NetBSD__)		/* see code clock in init() below */
-#include <sys/utsname.h>
-#endif
-
-#if !(defined(Lynx) || defined(__Lynx__) || (defined(SVR4) && !defined(sun))) && !defined (__CYGWIN__)
-#define HAS_MKSTEMP
+# include <sys/utsname.h>
 #endif
 
 typedef unsigned char boolean;
 #define TRUE		1
 #define FALSE		0
 
-# include "imakemdep.h"
+#include "imakemdep.h"
 #ifdef CROSSCOMPILE
 # include "imakemdep_cpp.h"
 #endif
@@ -305,7 +271,7 @@ int xvariables[10];
 #endif
 
 #ifndef PATH_MAX
-#define PATH_MAX 1024
+# define PATH_MAX 1024
 #endif
 
 /*
@@ -331,11 +297,13 @@ void KludgeOutputLine(char **), KludgeRe
 # endif
 #endif
 
-char *cpp = NULL;
+const char *cpp = NULL;
 
-char	*tmpMakefile = "/tmp/Imf.XXXXXX";
-char	*tmpImakefile = "/tmp/IIf.XXXXXX";
-char	*make_argv[ ARGUMENTS ] = {
+const char	*tmpMakefile;
+const char	*tmpMakefileTemplate = "/tmp/Imf.XXXXXX";
+const char	*tmpImakefile;
+const char	*tmpImakefileTemplate = "/tmp/IIf.XXXXXX";
+const char	*make_argv[ ARGUMENTS ] = {
 #ifdef WIN32
     "nmake"
 #else
@@ -345,53 +313,53 @@ char	*make_argv[ ARGUMENTS ] = {
 
 int	make_argindex;
 int	cpp_argindex;
-char	*Imakefile = NULL;
-char	*Makefile = "Makefile";
-char	*Template = "Imake.tmpl";
-char	*ImakefileC = "Imakefile.c";
+const char	*Imakefile = NULL;
+const char	*Makefile = "Makefile";
+const char	*Template = "Imake.tmpl";
+const char	*ImakefileC = "Imakefile.c";
 boolean haveImakefileC = FALSE;
-char	*cleanedImakefile = NULL;
-char	*program;
-char	*FindImakefile(char *Imakefile);
-char	*ReadLine(FILE *tmpfd, char *tmpfname);
-char	*CleanCppInput(char *imakefile);
-char	*Strdup(char *cp);
+const char	*cleanedImakefile = NULL;
+const char	*program;
+const char	*FindImakefile(const char *Imakefile);
+char	*ReadLine(FILE *tmpfd, const char *tmpfname);
+const char	*CleanCppInput(const char *imakefile);
+char	*Strdup(const char *cp);
 char	*Emalloc(int size);
-void	LogFatalI(char *s, int i), LogFatal(char *x0, char *x1),
-	LogMsg(char *x0, char *x1);
+void	LogFatal(const char *x0, ...) _X_ATTRIBUTE_PRINTF(1, 2);
+void	LogMsg(const char *x0, ...) _X_ATTRIBUTE_PRINTF(1, 2);
 
 void	showit(FILE *fd);
 void	wrapup(void);
 void	init(void);
-void	AddMakeArg(char *arg);
-void	AddCppArg(char *arg);
+void	AddMakeArg(const char *arg);
+void	AddCppArg(const char *arg);
 #ifdef CROSSCOMPILE
 char	*CrossCompileCPP(void);
 #endif
 void	SetOpts(int argc, char **argv);
-void	CheckImakefileC(char *masterc);
-void	cppit(char *imakefile, char *template, char *masterc,
-	      FILE *outfd, char *outfname);
+void	CheckImakefileC(const char *masterc);
+void	cppit(const char *imakefile, const char *template, const char *masterc,
+	      FILE *outfd, const char *outfname);
 void	makeit(void);
-void	CleanCppOutput(FILE *tmpfd, char *tmpfname);
+void	CleanCppOutput(FILE *tmpfd, const char *tmpfname);
 boolean isempty(char *line);
-void	writetmpfile(FILE *fd, char *buf, int cnt, char *fname);
+void	writetmpfile(FILE *fd, const char *buf, int cnt, const char *fname);
 #ifdef SIGNALRETURNSINT
 int	catch(int sig);
 #else
 void	catch(int sig);
 #endif
-void	showargs(char **argv);
-boolean optional_include(FILE *inFile, char *defsym, char *fname);
-void	  doit(FILE *outfd, char *cmd, char **argv);
+void	showargs(const char **argv);
+boolean optional_include(FILE *inFile, const char *defsym, const char *fname);
+void	  doit(FILE *outfd, const char *cmd, const char **argv);
 boolean define_os_defaults(FILE *inFile);
 #ifdef CROSSCOMPILE
 static void get_cross_compile_dir(FILE *inFile);
 #endif
 #ifdef CROSSCOMPILEDIR
-char *CrossCompileDir = CROSSCOMPILEDIR;
+const char *CrossCompileDir = CROSSCOMPILEDIR;
 #else
-char *CrossCompileDir = "";
+const char *CrossCompileDir = "";
 #endif
 boolean CrossCompiling = FALSE;
 
@@ -428,24 +396,25 @@ main(int argc, char *argv[])
 		if ((tmpfd = fopen(tmpMakefile, "w+")) == NULL)
 		   LogFatal("Cannot create temporary file %s.", tmpMakefile);
 	} else {
-#ifdef HAS_MKSTEMP
+#ifdef HAVE_MKSTEMP
 		int fd;
 #endif
-		tmpMakefile = Strdup(tmpMakefile);
-#ifndef HAS_MKSTEMP
-		if (mktemp(tmpMakefile) == NULL ||
-		    (tmpfd = fopen(tmpMakefile, "w+")) == NULL) {
-		   LogFatal("Cannot create temporary file %s.", tmpMakefile);
+		char *tmpMakefileName = Strdup(tmpMakefileTemplate);
+#ifndef HAVE_MKSTEMP
+		if (mktemp(tmpMakefileName) == NULL ||
+		    (tmpfd = fopen(tmpMakefileName, "w+")) == NULL) {
+		   LogFatal("Cannot create temporary file %s.", tmpMakefileName);
 		}
 #else
-		fd = mkstemp(tmpMakefile);
+		fd = mkstemp(tmpMakefileName);
 		if (fd == -1 || (tmpfd = fdopen(fd, "w+")) == NULL) {
 		   if (fd != -1) {
-		      unlink(tmpMakefile); close(fd);
+		      unlink(tmpMakefileName); close(fd);
 		   }
-		   LogFatal("Cannot create temporary file %s.", tmpMakefile);
+		   LogFatal("Cannot create temporary file %s.", tmpMakefileName);
 		}
 #endif
+		tmpMakefile = tmpMakefileName;
 	}
 	AddMakeArg("-f");
 	AddMakeArg( tmpMakefile );
@@ -498,7 +467,7 @@ void
 catch(int sig)
 {
 	errno = 0;
-	LogFatalI("Signal %d.", sig);
+	LogFatal("Signal %d.", sig);
 }
 
 /*
@@ -572,21 +541,21 @@ init(void)
 }
 
 void
-AddMakeArg(char *arg)
+AddMakeArg(const char *arg)
 {
 	errno = 0;
 	if (make_argindex >= ARGUMENTS-1)
-		LogFatal("Out of internal storage.", "");
+		LogFatal("Out of internal storage.");
 	make_argv[ make_argindex++ ] = arg;
 	make_argv[ make_argindex ] = NULL;
 }
 
 void
-AddCppArg(char *arg)
+AddCppArg(const char *arg)
 {
 	errno = 0;
 	if (cpp_argindex >= ARGUMENTS-1)
-		LogFatal("Out of internal storage.", "");
+		LogFatal("Out of internal storage.");
 	cpp_argv[ cpp_argindex++ ] = arg;
 	cpp_argv[ cpp_argindex ] = NULL;
 }
@@ -618,7 +587,7 @@ SetOpts(int argc, char **argv)
 		    else {
 			argc--, argv++;
 			if (! argc)
-			    LogFatal("No description arg after -f flag", "");
+			    LogFatal("No description arg after -f flag");
 			Imakefile = argv[0];
 		    }
 		} else if (argv[0][1] == 's') {
@@ -628,7 +597,7 @@ SetOpts(int argc, char **argv)
 		    else {
 			argc--, argv++;
 			if (!argc)
-			    LogFatal("No description arg after -s flag", "");
+			    LogFatal("No description arg after -s flag");
 			Makefile = ((argv[0][0] == '-') && !argv[0][1]) ?
 			    NULL : argv[0];
 		    }
@@ -642,7 +611,7 @@ SetOpts(int argc, char **argv)
 		    else {
 			argc--, argv++;
 			if (! argc)
-			    LogFatal("No description arg after -T flag", "");
+			    LogFatal("No description arg after -T flag");
 			Template = argv[0];
 		    }
 		} else if (argv[0][1] == 'C') {
@@ -651,7 +620,7 @@ SetOpts(int argc, char **argv)
 		    else {
 			argc--, argv++;
 			if (! argc)
-			    LogFatal("No imakeCfile arg after -C flag", "");
+			    LogFatal("No imakeCfile arg after -C flag");
 			ImakefileC = argv[0];
 		    }
 		} else if (argv[0][1] == 'v') {
@@ -667,10 +636,10 @@ SetOpts(int argc, char **argv)
 	    if (!cpp)
 	    {
 		AddCppArg("-E");
-#ifdef __GNUC__
+#  ifdef __GNUC__
 		if (verbose)
 		    AddCppArg("-v");
-#endif
+#  endif
 		cpp = DEFAULT_CC;
 	    }
 # else
@@ -686,8 +655,8 @@ SetOpts(int argc, char **argv)
 	AddCppArg(ImakefileC);
 }
 
-char *
-FindImakefile(char *Imakefile)
+const char *
+FindImakefile(const char *Imakefile)
 {
 	if (Imakefile) {
 		if (access(Imakefile, R_OK) < 0)
@@ -695,7 +664,7 @@ FindImakefile(char *Imakefile)
 	} else {
 		if (access("Imakefile", R_OK) < 0) {
 			if (access("imakefile", R_OK) < 0)
-				LogFatal("No description file.", "");
+				LogFatal("No description file.");
 			else
 				Imakefile = "imakefile";
 		} else
@@ -704,44 +673,50 @@ FindImakefile(char *Imakefile)
 	return(Imakefile);
 }
 
-void
-LogFatalI(char *s, int i)
+static void _X_ATTRIBUTE_PRINTF(1, 0)
+vLogMsg(const char *fmt, va_list args)
 {
-	/*NOSTRICT*/
-	LogFatal(s, (char *)(long)i);
+	int error_number = errno;
+
+	if (error_number) {
+		fprintf(stderr, "%s: ", program);
+		fprintf(stderr, "%s\n", strerror(error_number));
+	}
+	fprintf(stderr, "%s: ", program);
+	vfprintf(stderr, fmt, args);
+	fprintf(stderr, "\n");
 }
 
 void
-LogFatal(char *x0, char *x1)
+LogFatal(const char *fmt, ...)
 {
 	static boolean entered = FALSE;
+	va_list args;
 
 	if (entered)
 		return;
 	entered = TRUE;
 
-	LogMsg(x0, x1);
+	va_start(args, fmt);
+	vLogMsg(fmt, args);
+	va_end(args);
 	fprintf(stderr, "  Stop.\n");
 	wrapup();
 	exit(1);
 }
 
 void
-LogMsg(char *x0, char *x1)
+LogMsg(const char *fmt, ...)
 {
-	int error_number = errno;
+	va_list args;
 
-	if (error_number) {
-		fprintf(stderr, "%s: ", program);
-		fprintf(stderr, "%s\n", strerror(error_number));
-	}
-	fprintf(stderr, "%s: ", program);
-	fprintf(stderr, x0, x1);
-	fprintf(stderr, "\n");
+	va_start(args, fmt);
+	vLogMsg(fmt, args);
+	va_end(args);
 }
 
 void
-showargs(char **argv)
+showargs(const char **argv)
 {
 	for (; *argv; argv++)
 		fprintf(stderr, "%s ", *argv);
@@ -751,7 +726,7 @@ showargs(char **argv)
 #define ImakefileCHeader "/* imake - temporary file */"
 
 void
-CheckImakefileC(char *masterc)
+CheckImakefileC(const char *masterc)
 {
 	char mkcbuf[1024];
 	FILE *inFile;
@@ -767,7 +742,8 @@ CheckImakefileC(char *masterc)
 			fclose(inFile);
 			LogFatal("Refuse to overwrite: %s", masterc);
 		}
-		fclose(inFile);
+		else
+			fclose(inFile);
 	}
 }
 
@@ -778,7 +754,7 @@ CheckImakefileC(char *masterc)
 #define OverrideWarning "Warning: local file \"%s\" overrides global macros."
 
 boolean
-optional_include(FILE *inFile, char *defsym, char *fname)
+optional_include(FILE *inFile, const char *defsym, const char *fname)
 {
 	errno = 0;
 	if (access(fname, R_OK) == 0) {
@@ -790,7 +766,7 @@ optional_include(FILE *inFile, char *def
 }
 
 void
-doit(FILE *outfd, char *cmd, char **argv)
+doit(FILE *outfd, const char *cmd, const char **argv)
 {
 	int		pid;
 	waitType	status;
@@ -805,18 +781,18 @@ doit(FILE *outfd, char *cmd, char **argv
 	if (status < 0)
 		LogFatal("Cannot spawn %s.", cmd);
 	if (status > 0)
-		LogFatalI("Exit code %d.", status);
+		LogFatal("Exit code %d.", status);
 #else
 	pid = fork();
 	if (pid < 0)
-		LogFatal("Cannot fork.", "");
+		LogFatal("Cannot fork.");
 	if (pid) {	/* parent... simply wait */
 		while (wait(&status) > 0) {
 			errno = 0;
 			if (WIFSIGNALED(status))
-				LogFatalI("Signal %d.", waitSig(status));
+				LogFatal("Signal %d.", waitSig(status));
 			if (WIFEXITED(status) && waitCode(status))
-				LogFatalI("Exit code %d.", waitCode(status));
+				LogFatal("Exit code %d.", waitCode(status));
 		}
 	}
 	else {	/* child... dup and exec cmd */
@@ -832,7 +808,7 @@ doit(FILE *outfd, char *cmd, char **argv
 
 #if !defined WIN32
 static void
-parse_utsname(struct utsname *name, char *fmt, char *result, char *msg)
+parse_utsname(struct utsname *name, const char *fmt, char *result, const char *msg)
 {
   char buf[SYS_NMLN * 5 + 1];
   char *ptr = buf;
@@ -892,7 +868,7 @@ parse_utsname(struct utsname *name, char
 
   /* Just in case... */
   if (strlen(buf) >= sizeof(buf))
-    LogFatal("Buffer overflow parsing uname.", "");
+    LogFatal("Buffer overflow parsing uname.");
 
   /* Parse the buffer.  The sscanf() return value is rarely correct. */
   *result = '\0';
@@ -978,13 +954,23 @@ const char *libc_c=
 static void
 get_libc_version(FILE *inFile)
 {
-  char aout[] = "/tmp/imakeXXXXXX";
+  char aout[4096], *tmpdir;
   FILE *fp;
   const char *format = "%s -o %s -x c -";
   char *cc;
   int len;
   char *command;
 
+  /* If $TMPDIR is defined and has an acceptable length,
+   * use that as tmp dir, else use /tmp.  That fixes
+   * problems with /tmp mounted "noexec".
+   */
+  if((tmpdir = getenv("TMPDIR")) != NULL && strlen(tmpdir) < (4096-13))
+    strcpy(aout, tmpdir);
+  else
+    strcpy(aout, "/tmp");
+  strcat(aout, "/imakeXXXXXX");
+
   /* Pre-create temp file safely */
   {
     /* Linux + ELF has mkstemp() */
@@ -1000,7 +986,8 @@ get_libc_version(FILE *inFile)
     cc = "gcc";
   len = strlen (aout) + strlen (format) + strlen (cc);
   if (len < 128) len = 128;
-  command = alloca (len);
+  if((command = alloca (len)) == NULL)
+    abort();
 
   if (snprintf (command , len, format, cc, aout) == len)
     abort ();
@@ -1015,12 +1002,16 @@ get_libc_version(FILE *inFile)
     abort ();
 
   while (fgets (command, len, fp))
-    fprintf (inFile, command);
+    fputs (command, inFile);
 
   len = pclose (fp);
   remove (aout);
-  if (len)
-    abort ();
+  if (len) {
+      /* handwave furiously */
+      printf("#define DefaultLinuxCLibMajorVersion 6\n");
+      printf("#define DefaultLinuxCLibMinorVersion 12\n");
+      printf("#define DefaultLinuxCLibTeenyVersion 0\n");
+  }
 }
 #endif
 
@@ -1031,14 +1022,14 @@ get_stackprotector(FILE *inFile)
   FILE *fp;
   char *cc;
   char command[1024], buf[1024];
-  
+
   cc = getenv("CC");
   if (cc == NULL) {
     cc = "cc";
   }
   snprintf(command, sizeof(command), "%s -v 2>&1", cc);
   fp = popen(command, "r");
-  if (fp == NULL) 
+  if (fp == NULL)
     abort();
   while (fgets(buf, sizeof(buf), fp)) {
     if (strstr(buf, "propolice") != NULL) {
@@ -1046,11 +1037,10 @@ get_stackprotector(FILE *inFile)
       break;
     }
   }
-  if (pclose(fp)) 
-    abort();
+  pclose(fp);
 }
 #endif
-	
+
 
 #if defined CROSSCOMPILE || defined linux || defined(__GLIBC__)
 static void
@@ -1058,9 +1048,9 @@ get_distrib(FILE *inFile)
 {
   struct stat sb;
 
-  static char* suse = "/etc/SuSE-release";
-  static char* redhat = "/etc/redhat-release";
-  static char* debian = "/etc/debian_version";
+  static const char*   suse = "/etc/SuSE-release";
+  static const char* redhat = "/etc/redhat-release";
+  static const char* debian = "/etc/debian_version";
 
   fprintf (inFile, "%s\n", "#define LinuxUnknown    0");
   fprintf (inFile, "%s\n", "#define LinuxSuSE       1");
@@ -1076,14 +1066,14 @@ get_distrib(FILE *inFile)
   fprintf (inFile, "%s\n", "#define LinuxWare       11");
   fprintf (inFile, "%s\n", "#define LinuxYggdrasil  12");
 
-#ifdef CROSSCOMPILE
+# ifdef CROSSCOMPILE
   if (CrossCompiling) {
       fprintf (inFile, "%s\n",
 	       "#define DefaultLinuxDistribution LinuxUnknown");
       fprintf (inFile, "%s\n", "#define DefaultLinuxDistName Unknown");
       return;
   }
-#endif
+# endif
   if (lstat (suse, &sb) == 0) {
     fprintf (inFile, "%s\n", "#define DefaultLinuxDistribution LinuxSuSE");
     fprintf (inFile, "%s\n", "#define DefaultLinuxDistName SuSE");
@@ -1116,7 +1106,7 @@ get_ld_version(FILE *inFile)
   int ldmajor, ldminor;
   const char *ld = "ld -v";
 
-#ifdef CROSSCOMPILE
+# ifdef CROSSCOMPILE
   if (CrossCompiling) {
       char cmd[PATH_MAX];
       strcpy (cmd, CrossCompileDir);
@@ -1124,7 +1114,7 @@ get_ld_version(FILE *inFile)
       strcat (cmd,ld);
       ldprog = popen (cmd, "r");
   } else
-#endif
+# endif
       ldprog = popen (ld, "r");
 
   if (ldprog) {
@@ -1192,7 +1182,7 @@ get_binary_format(FILE *inFile)
  * Returns: 0 if successful, -1 if not.
  */
 static int
-ask_sun_compiler_for_versions(const char *cmd, const char *path, 
+ask_sun_compiler_for_versions(const char *cmd, const char *path,
   int *cmajor, int *cminor)
 {
   char buf[BUFSIZ];
@@ -1201,7 +1191,7 @@ ask_sun_compiler_for_versions(const char
   FILE* ccproc;
   const char vflag[] = " -V 2>&1";
   int retval = -1;
-  
+
   int len = strlen(cmd) + sizeof(vflag);
 
   if (path != NULL) {
@@ -1229,7 +1219,7 @@ ask_sun_compiler_for_versions(const char
 		  }
 	      }
 	      if (retval != 0) {
-		  fprintf(stderr, 
+		  fprintf(stderr,
 		    "warning: could not parse version number in output of:\n"
 		    "         %s\n", cmdtorun);
 	      }
@@ -1247,23 +1237,22 @@ get_sun_compiler_versions (FILE *inFile)
 {
   const char* sunpro_path = "/opt/SUNWspro/bin";
   int cmajor, cminor, found = 0;
-  struct stat sb;
 
   /* If cross-compiling, only check CrossCompilerDir for compilers.
-   * If not cross-compiling, first check cc in users $PATH, 
+   * If not cross-compiling, first check cc in users $PATH,
    * then try /opt/SUNWspro if not found in the users $PATH
    */
 
-#if defined CROSSCOMPILE
+# if defined CROSSCOMPILE
   if (CrossCompiling) {
       if (ask_sun_compiler_for_versions("cc", CrossCompileDir,
 	&cmajor, &cminor) == 0) {
 	      found = 1;
       }
-  } 
+  }
   else
-#endif
-  {    
+# endif
+  {
       if (ask_sun_compiler_for_versions("cc", NULL, &cmajor, &cminor) == 0) {
 	  found = 1;
       } else if (ask_sun_compiler_for_versions("cc", sunpro_path,
@@ -1282,22 +1271,22 @@ get_sun_compiler_versions (FILE *inFile)
 
   /* Now do it again for C++ compiler (CC) */
   found = 0;
-#if defined CROSSCOMPILE
+# if defined CROSSCOMPILE
   if (CrossCompiling) {
       if (ask_sun_compiler_for_versions("CC", CrossCompileDir,
 	&cmajor, &cminor) == 0) {
 	      found = 1;
       }
-  } 
+  }
   else
-#endif
-  {    
+# endif
+  {
       if (ask_sun_compiler_for_versions("CC", NULL, &cmajor, &cminor) == 0) {
 	  found = 1;
       } else if (ask_sun_compiler_for_versions("CC", sunpro_path,
 	&cmajor, &cminor) == 0) {
 	  found = 1;
-	  fprintf(inFile, 
+	  fprintf(inFile,
 		"#define DefaultSunProCplusplusCompilerDir %s", sunpro_path);
       }
   }
@@ -1318,7 +1307,7 @@ static void
 get_gcc_version(FILE *inFile, char *name)
 {
     fprintf (inFile, "#define HasGcc 1\n");
-#ifdef CROSSCOMPILE
+# ifdef CROSSCOMPILE
     if (CrossCompiling)
     {
 	if (gnu_c > 1) {
@@ -1329,20 +1318,20 @@ get_gcc_version(FILE *inFile, char *name
 	fprintf (inFile, "#define GccMajorVersion %d\n", gnu_c);
 	fprintf (inFile, "#define GccMinorVersion %d\n", gnu_c_minor);
     } else
-#endif
+# endif
     {
-#if __GNUC__ > 1
+# if __GNUC__ > 1
 	fprintf (inFile, "#define HasGcc2 1\n");
-# if __GNUC__ > 2
+#  if __GNUC__ > 2
 	fprintf (inFile, "#define HasGcc3 1\n");
+#  endif
 # endif
-#endif
 	fprintf (inFile, "#define GccMajorVersion %d\n", __GNUC__);
 	fprintf (inFile, "#define GccMinorVersion %d\n", __GNUC_MINOR__);
     }
-#if defined(HAS_MERGE_CONSTANTS)
+# if defined(HAS_MERGE_CONSTANTS)
     fprintf (inFile, "#define HasGccMergeConstants %d\n", HAS_MERGE_CONSTANTS);
-#endif
+# endif
 }
 #endif
 
@@ -1350,8 +1339,8 @@ static boolean
 get_gcc(char *cmd)
 {
   struct stat sb;
-    static char* gcc_path[] = {
-# if defined(linux) || \
+    static const char* gcc_path[] = {
+#if defined(linux) || \
      defined(__NetBSD__) || \
      defined(__OpenBSD__) || \
      defined(__FreeBSD__) || \
@@ -1362,14 +1351,14 @@ get_gcc(char *cmd)
      defined(__GNU__) || \
      defined(__GLIBC__)
 	"/usr/bin/cc",	/* for Linux PostIncDir */
-# endif
+#endif
 	"/usr/local/bin/gcc",
 	"/opt/gnu/bin/gcc",
 	"/usr/pkg/bin/gcc"
     };
 
 #ifdef CROSSCOMPILE
-    static char* cross_cc_name[] = {
+    static const char* cross_cc_name[] = {
 	"cc",
 	"gcc"
     };
@@ -1399,7 +1388,7 @@ get_gcc(char *cmd)
     return FALSE;
 }
 
-#if defined CROSSCOMPILE || !defined __UNIXOS2__
+#ifdef CROSSCOMPILE
 static void
 get_gcc_incdir(FILE *inFile, char* name)
 {
@@ -1428,14 +1417,14 @@ get_gcc_incdir(FILE *inFile, char* name)
 boolean
 define_os_defaults(FILE *inFile)
 {
-#if defined CROSSCOMPILE || ( !defined(WIN32) && !defined(__UNIXOS2__) )
-#ifdef CROSSCOMPILE
-#ifdef __GNUC__
+#if defined CROSSCOMPILE || !defined(WIN32)
+# ifdef CROSSCOMPILE
+#  ifdef __GNUC__
   if (1)
-#else
+#  else
   if ((sys != win32) && (sys != emx))
-#endif
-#endif
+#  endif
+# endif
     {
 # if (defined(DEFAULT_OS_NAME) || defined(DEFAULT_OS_MAJOR_REV) || \
      defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
@@ -1444,16 +1433,16 @@ define_os_defaults(FILE *inFile)
 	char buf[SYS_NMLN * 5 + 1];
 
 	/* Obtain the system information. */
-#ifdef CROSSCOMPILE
+#  ifdef CROSSCOMPILE
       if (!CrossCompiling)
-#endif
+#  endif
       {
 	  if (uname(&uts_name) < 0)
-	      LogFatal("Cannot invoke uname", "");
+	      LogFatal("Cannot invoke uname");
 	  else
 	      name = &uts_name;
       }
-#if defined CROSSCOMPILE && (defined linux || defined(__GLIBC__))
+#  if defined CROSSCOMPILE && (defined linux || defined(__GLIBC__))
       else {
 	  strncpy(uts_name.sysname,cross_uts_sysname,SYS_NMLN);
 	  strncpy(uts_name.release,cross_uts_release,SYS_NMLN);
@@ -1461,14 +1450,14 @@ define_os_defaults(FILE *inFile)
 	  strncpy(uts_name.machine,cross_uts_machine,SYS_NMLN);
 	  name = &uts_name;
       }
-#endif
-# ifdef __FreeBSD__
+#  endif
+#  ifdef __FreeBSD__
        /* Override for compiling in chroot of other OS version, such as
         * in the bento build cluster.
         */
        {
 	 char *e;
-	 if ((e = getenv("OSREL")) != NULL && 
+	 if ((e = getenv("OSREL")) != NULL &&
 	     strlen(name->sysname) + strlen(e) + 1 < SYS_NMLN) {
 	  strcpy(name->release, e);
 	  strcpy(name->version, name->sysname);
@@ -1476,7 +1465,7 @@ define_os_defaults(FILE *inFile)
 	  strcat(name->version, e);
 	 }
        }
-# endif
+#  endif
 
 #  if defined DEFAULT_OS_NAME
 #   if defined CROSSCOMPILE
@@ -1648,10 +1637,8 @@ define_os_defaults(FILE *inFile)
 	  char name[PATH_MAX];
 	  if (get_gcc(name)) {
 	      get_gcc_version (inFile,name);
-#  if defined CROSSCOMPILE || !defined __UNIXOS2__
-#   if defined CROSSCOMPILE
+#  if defined CROSSCOMPILE
 	      if (sys != emx)
-#   endif
 		  get_gcc_incdir(inFile,name);
 #  endif
 	  }
@@ -1664,7 +1651,7 @@ define_os_defaults(FILE *inFile)
 	  get_binary_format(inFile);
 # endif
     }
-#endif /* !WIN32 && !__UNIXOS2__*/
+#endif /* !WIN32 */
 #if defined WIN32
 # ifdef CROSSCOMPILE
   else if (sys == win32 && !CrossCompiling)
@@ -1688,8 +1675,6 @@ define_os_defaults(FILE *inFile)
 #endif /* WIN32 */
 #ifdef CROSSCOMPILE
   else if (sys == emx)
-#endif
-#if defined CROSSCOMPILE || defined __UNIXOS2__
     {
       fprintf(inFile, "#define DefaultOSMajorVersion 4\n");
       fprintf(inFile, "#define DefaultOSMinorVersion 0\n");
@@ -1703,8 +1688,8 @@ define_os_defaults(FILE *inFile)
 }
 
 void
-cppit(char *imakefile, char *template, char *masterc,
-      FILE *outfd, char *outfname)
+cppit(const char *imakefile, const char *template, const char *masterc,
+      FILE *outfd, const char *outfname)
 {
 	FILE	*inFile;
 
@@ -1721,8 +1706,11 @@ cppit(char *imakefile, char *template, c
 	    fprintf(inFile, IncludeFmt, ImakeTmplSym) < 0 ||
 	    optional_include(inFile, "IMAKE_ADMIN_MACROS", "adminmacros") ||
 	    optional_include(inFile, "IMAKE_LOCAL_MACROS", "localmacros") ||
-	    fflush(inFile) ||
-	    fclose(inFile))
+	    fflush(inFile)) {
+		fclose(inFile);
+		LogFatal("Cannot write to %s.", masterc);
+	}
+	else if (fclose(inFile))
 		LogFatal("Cannot write to %s.", masterc);
 	/*
 	 * Fork and exec cpp
@@ -1737,8 +1725,8 @@ makeit(void)
 	doit(NULL, make_argv[0], make_argv);
 }
 
-char *
-CleanCppInput(char *imakefile)
+const char *
+CleanCppInput(const char *imakefile)
 {
 	FILE	*outFile = NULL;
 	FILE	*inFile;
@@ -1792,28 +1780,29 @@ CleanCppInput(char *imakefile)
 		    strcmp(ptoken, "pragma") &&
 		    strcmp(ptoken, "undef")) {
 		    if (outFile == NULL) {
-#ifdef HAS_MKSTEMP
+#ifdef HAVE_MKSTEMP
 			int fd;
 #endif
-			tmpImakefile = Strdup(tmpImakefile);
-#ifndef HAS_MKSTEMP
-			if (mktemp(tmpImakefile) == NULL ||
-			    (outFile = fopen(tmpImakefile, "w+")) == NULL) {
+			char *tmpImakefileName = Strdup(tmpImakefileTemplate);
+#ifndef HAVE_MKSTEMP
+			if (mktemp(tmpImakefileName) == NULL ||
+			    (outFile = fopen(tmpImakefileName, "w+")) == NULL) {
 			    LogFatal("Cannot open %s for write.",
-				tmpImakefile);
+				tmpImakefileName);
 			}
 #else
-			fd=mkstemp(tmpImakefile);
+			fd=mkstemp(tmpImakefileName);
 			if (fd != -1)
 			    outFile = fdopen(fd, "w");
 			if (outFile == NULL) {
 			    if (fd != -1) {
-			       unlink(tmpImakefile); close(fd);
+			       unlink(tmpImakefileName); close(fd);
 			    }
 			    LogFatal("Cannot open %s for write.",
-				tmpImakefile);
+				tmpImakefileName);
 			}
 #endif
+			tmpImakefile = tmpImakefileName;
 		    }
 		    writetmpfile(outFile, punwritten, pbuf-punwritten,
 				 tmpImakefile);
@@ -1838,7 +1827,7 @@ CleanCppInput(char *imakefile)
 }
 
 void
-CleanCppOutput(FILE *tmpfd, char *tmpfname)
+CleanCppOutput(FILE *tmpfd, const char *tmpfname)
 {
 	char	*input;
 	int	blankline = 0;
@@ -1960,7 +1949,7 @@ isempty(char *line)
 
 /*ARGSUSED*/
 char *
-ReadLine(FILE *tmpfd, char *tmpfname)
+ReadLine(FILE *tmpfd, const char *tmpfname)
 {
 	static boolean	initialized = FALSE;
 	static char	*buf, *pline, *end;
@@ -1988,10 +1977,10 @@ ReadLine(FILE *tmpfd, char *tmpfname)
 		fseek(tmpfd, 0, 0);
 #if defined(SYSV) || defined(WIN32) || defined(USE_FREOPEN)
 		tmpfd = freopen(tmpfname, "w+", tmpfd);
-#ifdef WIN32
+# ifdef WIN32
 		if (! tmpfd) /* if failed try again */
 			tmpfd = freopen(tmpfname, "w+", fp);
-#endif
+# endif
 		if (! tmpfd)
 			LogFatal("cannot reopen %s\n", tmpfname);
 #else	/* !SYSV */
@@ -1999,8 +1988,6 @@ ReadLine(FILE *tmpfd, char *tmpfname)
 #endif	/* !SYSV */
 		initialized = TRUE;
 	    fprintf (tmpfd, "# Makefile generated by imake - do not edit!\n");
-	    fprintf (tmpfd, "# %s\n",
-		"$Xorg: imake.c,v 1.6 2001/02/09 02:03:15 xorgcvs Exp $");
 	}
 
 	for (p1 = pline; p1 < end; p1++) {
@@ -2036,7 +2023,7 @@ ReadLine(FILE *tmpfd, char *tmpfname)
 }
 
 void
-writetmpfile(FILE *fd, char *buf, int cnt, char *fname)
+writetmpfile(FILE *fd, const char *buf, int cnt, const char *fname)
 {
 	if (fwrite(buf, sizeof(char), cnt, fd) == -1)
 		LogFatal("Cannot write to %s.", fname);
@@ -2048,7 +2035,7 @@ Emalloc(int size)
 	char	*p;
 
 	if ((p = malloc(size)) == NULL)
-		LogFatalI("Cannot allocate %d bytes", size);
+		LogFatal("Cannot allocate %d bytes", size);
 	return(p);
 }
 
@@ -2066,10 +2053,10 @@ KludgeOutputLine(char **pline)
 		break;
 	    case ' ':	/*May need a tab*/
 	    default:
-#ifdef CROSSCOMPILE
+# ifdef CROSSCOMPILE
 		if (inline_syntax)
-#endif
-#if defined CROSSCOMPILE || defined INLINE_SYNTAX
+# endif
+# if defined CROSSCOMPILE || defined INLINE_SYNTAX
 		{
 		    if (*p == '<' && p[1] == '<') { /* inline file close */
 			InInline--;
@@ -2077,7 +2064,7 @@ KludgeOutputLine(char **pline)
 			break;
 		    }
 		}
-#endif
+# endif
 		/*
 		 * The following cases should not be treated as beginning of
 		 * rules:
@@ -2116,26 +2103,26 @@ KludgeOutputLine(char **pline)
 			quotechar = ']';
 			break;
 		    case '=':
-#ifdef CROSSCOMPILE
+# ifdef CROSSCOMPILE
 			if (remove_cpp_leadspace)
-#endif
-#if defined CROSSCOMPILE || defined REMOVE_CPP_LEADSPACE
+# endif
+# if defined CROSSCOMPILE || defined REMOVE_CPP_LEADSPACE
 			{
 			    if (!InRule && **pline == ' ') {
 				while (**pline == ' ')
 				    (*pline)++;
 			    }
 			}
-#endif
+# endif
 			goto breakfor;
-#if defined CROSSCOMPILE || defined INLINE_SYNTAX
+# if defined CROSSCOMPILE || defined INLINE_SYNTAX
 		    case '<':
 			if (inline_syntax) {
 			    if (p[1] == '<') /* inline file start */
 				InInline++;
 			}
 			break;
-#endif
+# endif
 		    case ':':
 			if (p[1] == '=')
 			    goto breakfor;
@@ -2159,7 +2146,7 @@ KludgeResetRule(void)
 }
 #endif
 char *
-Strdup(char *cp)
+Strdup(const char *cp)
 {
 	char *new = Emalloc(strlen(cp) + 1);