-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcq.c
executable file
·5316 lines (4940 loc) · 123 KB
/
cq.c
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
struct defs {
int cbits; /* No. of bits per char */
int ibits; /* int */
int sbits; /* short */
int lbits; /* long */
int ubits; /* unsigned */
int fbits; /* float */
int dbits; /* double */
float fprec; /* Smallest number that can be */
float dprec; /* significantly added to 1. */
int flgs; /* Print return codes, by section */
int flgm; /* Announce machine dependencies */
int flgd; /* give explicit diagnostics */
int flgl; /* Report local return codes. */
int rrc; /* recent return code */
int crc; /* Cumulative return code */
char rfs[8]; /* Return from section */
};
main(n,args) /* C REFERENCE MANUAL */
int n;
char **args;
{
/* This program performs a series of tests on a C compiler,
based on information in the
C REFERENCE MANUAL
which appears as Appendix A to the book "The C Programming
Language" by Brian W. Kernighan and Dennis M. Ritchie
(Prentice-Hall, 1978, $10.95). This Appendix is hereafter
referred to as "the Manual".
The rules followed in writing this program are:
1. The entire program is written in legal C, according
to the Manual. It should compile with no error messages,
although some warning messages may be produced by some
compilers. Failure to compile should be interpreted as
a compiler error.
2. The program is clean, in that it does not make use
of any features of the operating system on which it runs,
with the sole exceptions of the printf() function, and an
internal "options" routine, which is easily excised.
3. No global variables are used, except for the spec-
ific purpose of testing the global variable facility.
The program is divided into modules having names of the
form snnn... These modules correspond to those sections of the
Manual, as identified by boldface type headings, in which
there is something to test. For example, s241() corresponds
to section 2.4.1 of the Manual (Integer constants) and tests
the facilities described therein. The module numbering
scheme is ambiguous, especially when it names modules
referring to more than one section; module s7813, for ex-
ample, deals with sections 7.8 through 7.13. Nonetheless,
it is surprisingly easy to find a section in the Manual
corresponding to a section of code, and vice versa.
Note also that there seem to be "holes" in the program,
at least from the point of view that there exist sections in the
Manual for which there is no corresponding code. Such holes
arise from three causes: (a) there is nothing in that partic-
ular section to test, (b) everything in that section is tested
elsewhere, and (c) it was deemed advisable not to check cer-
tain features like preprocessor or listing control features.
Modules are called by a main program main(). The mod-
ules that are called, and the sequence in which they are
called, are determined by two lists in main(), in which the
module names appear. The first list (an extern statement)
declares the module names to be external. The second (a stat-
ic int statement) names the modules and defines the sequence
in which they are called. There is no need for these lists
to be in the same order, but it is probably a good idea to keep
them that way in the interest of clarity. Since there are no
cross-linkages between modules, new modules may be added,
or old ones deleted, simply by editing the lists, with one
exception: section s26, which pokes around at the hardware
trying to figure out the characteristics of the machine that
it is running on, saves information that is subsequently
used by sections s626, s72, and s757. If this program is
to be broken up into smallish pieces, say for running on
a microcomputer, take care to see that s26 is called before
calling any of the latter three sections. The size
of the lists, i.e., the number of modules to be called, is
not explicitly specified as a program parameter, but is
determined dynamically using the sizeof operator.
Communication between the main program and the modules
takes place in two ways. In all cases, a pointer to a structure
is passed to the called module. The structure contains flags
that will determine the type of information to be published
by the module, and fields that may be written in by the
module. The former include "flgm" and "flgd", which, if set
to a nonzero value, specify that machine dependencies are to
be announced or that error messages are to be printed, re-
spectively. The called module's name, and the hardware char-
acteristics probed in s26() comprise the latter.
Also, in all cases, a return code is returned by the called
module. A return code of zero indicates that all has gone well;
nonzero indicates otherwise. Since more than one type of error
may be detected by a module, the return code is a composite
of error indicators, which, individually, are given as numbers
that are powers of two. Thus, a return code of 10 indicates
that two specific errors, 8 and 2, were detected. Whether or
not the codes returned by the modules are printed by the main
program is determined by setting "flgs" to 1 (resp. 0).
The entire logic of the main program is contained in the
half-dozen or so lines at the end. The somewhat cryptic
statement:
d0.rrc = (*sec[j])(pd0);
in the for loop calls the modules. The rest of the code is
reasonably straightforward.
Finally, in each of the modules, there is the following
prologue:
snnn(pd0)
struct defs *pd0;
{
static char snnner[] = "snnn,er%d\n";
static char qsnnn[8] = "snnn ";
char *ps, *pt;
int rc;
rc = 0;
ps = qsnnn;
pt = pd0->rfs;
while(*pt++ = *ps++);
used for housekeeping, handshaking and module initialization.
*/
extern
s22(struct defs *),
s241(struct defs *),
s243(struct defs *),
s244(struct defs *),
s25(struct defs *),
s26(struct defs *),
s4(struct defs *),
s61(struct defs *),
s626(struct defs *),
s71(struct defs *),
s72(struct defs *),
s757(struct defs *),
s7813(struct defs *),
s714(struct defs *),
s715(struct defs *),
s81(struct defs *),
s84(struct defs *),
s85(struct defs *),
s86(struct defs *),
s88(struct defs *),
s9(struct defs *)
;
int j;
static int (*sec[])() = {
s22,
s241,
s243,
s244,
s25,
s26,
s4,
s61,
s626,
s71,
s72,
s757,
s7813,
s714,
s715,
s81,
s84,
s85,
s86,
s88,
s9
};
static struct defs d0, *pd0;
d0.flgs = 1; /* These flags dictate */
d0.flgm = 1; /* the verbosity of */
d0.flgd = 1; /* the program. */
d0.flgl = 1;
pd0 = &d0;
for (j=0; j<sizeof(sec) / sizeof(sec[0]); j++) {
d0.rrc = (*sec[j])(pd0);
d0.crc = d0.crc+d0.rrc;
if(d0.flgs != 0) printf("Section %s returned %d.\n",d0.rfs,d0.rrc);
}
if(d0.crc == 0) printf("\nNo errors detected.\n");
else printf("\nFailed.\n");
return 0;
}
s22(pd0) /* 2.2 Identifiers (Names) */
struct defs *pd0;
{
int a234, a;
int _, _234, A, rc;
static char s22er[] = "s22,er%d\n";
static char qs22[8] = "s22 ";
char *ps, *pt;
/* Initialize */
rc = 0;
ps = qs22;
pt = pd0 -> rfs;
while (*pt++ = *ps++);
/* An identifier is a sequence of letters and digits;
the first character must be a letter. The under-
score _ counts as a letter. */
a=1;
_=2;
_234=3;
a234=4;
if(a+_+_234+a234 != 10) {
rc = rc+1;
if(pd0->flgd != 0) printf(s22er,1);
}
/* Upper and lower case letters are different. */
A = 2;
if (A == a) {
rc = rc+4;
if (pd0->flgd != 0) printf(s22er,4);
}
return(rc);
}
s241(pd0) /* 2.4.1 Integer constants
2.4.2 Explicit long constants */
struct defs *pd0;
{
long pow2();
static char s241er[] = "s241,er%d\n";
static char qs241[8] = "s241 ";
char *ps, *pt;
int rc, j, lrc;
static long g[39] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,6,0,8,0,12,0,16,0,18,0,20,0,24,
0,28,0,30,0,32,0,36};
long d[39], o[39], x[39];
rc = 0;
lrc = 0;
ps = qs241;
pt = pd0 -> rfs;
while (*pt++ = *ps++);
/* An integer constant consisting of a sequence of digits is
taken to be octal if it begins with 0 (digit zero), decimal
otherwise. */
if ( 8 != 010
|| 16 != 020
|| 24 != 030
|| 32 != 040
|| 40 != 050
|| 48 != 060
|| 56 != 070
|| 64 != 0100
|| 72 != 0110
|| 80 != 0120
|| 9 != 0011
|| 17 != 0021
|| 25 != 0031
|| 33 != 0041
|| 41 != 0051
|| 49 != 0061
|| 57 != 0071
|| 65 != 0101
|| 73 != 0111
|| 81 != 0121 ){
rc = rc+1;
if( pd0->flgd != 0 ) printf(s241er,1);
}
/* A sequence of digits preceded by 0x or 0X (digit zero)
is taken to be a hexadecimal integer. The hexadecimal
digits include a or A through f or F with values 10
through 15. */
if ( 0x00abcdef != 0xabcdef
|| 0xabcdef != 0Xabcdef || 0Xabcdef != 0XAbcdef
|| 0XAbcdef != 0XABcdef || 0XABcdef != 0XABCdef
|| 0XABCdef != 0XABCDef || 0XABCDef != 0XABCDEf
|| 0XABCDEf != 0XABCDEF || 0xABCDEF != 11259375 ){
rc = rc+2;
if( pd0->flgd != 0 ) printf(s241er,2);
}
/* A decimal constant whose value exceeds the largest signed
machine integer is taken to be long; an octal or hex con-
stant which exceeds the largest unsigned machine integer
is likewise taken to be long. */
if ( sizeof 010000000000 != sizeof(long) /* 2**30 */
|| sizeof 1073741824 != sizeof(long) /* ditto */
|| sizeof 0x40000000 != sizeof(long) ){ /* " */
rc = rc+4;
if( pd0->flgd != 0 ) printf(s241er,4);
}
/* A decimal, octal, or hexadecimal constant immediately followed
by l (letter ell) or L is a long constant. */
if ( sizeof 67l != sizeof(long)
|| sizeof 67L != sizeof(long)
|| sizeof 067l != sizeof(long)
|| sizeof 067L != sizeof(long)
|| sizeof 0X67l != sizeof(long)
|| sizeof 0x67L != sizeof(long) ){
rc = rc+8;
if( pd0 -> flgd != 0 ) printf(s241er,8);
}
/* Finally, we test to see that decimal (d), octal (o),
and hexadecimal (x) constants representing the same values
agree among themselves, and with computed values, at spec-
ified points over an appropriate range. The points select-
ed here are those with the greatest potential for caus-
ing trouble, i.e., zero, 1-16, and values of 2**n and
2**n - 1 where n is some multiple of 4 or 6. Unfortunately,
just what happens when a value is too big to fit in a
long is undefined; however, it would be nice if what
happened were at least consistent... */
for ( j=0; j<17; j++ ) g[j] = j;
for ( j=18; j<39; ) {
g[j] = pow2(g[j]);
g[j-1] = g[j] - 1;
j = j+2;
}
d[0] = 0; o[0] = 00; x[0] = 0x0;
d[1] = 1; o[1] = 01; x[1] = 0x1;
d[2] = 2; o[2] = 02; x[2] = 0x2;
d[3] = 3; o[3] = 03; x[3] = 0x3;
d[4] = 4; o[4] = 04; x[4] = 0x4;
d[5] = 5; o[5] = 05; x[5] = 0x5;
d[6] = 6; o[6] = 06; x[6] = 0x6;
d[7] = 7; o[7] = 07; x[7] = 0x7;
d[8] = 8; o[8] = 010; x[8] = 0x8;
d[9] = 9; o[9] = 011; x[9] = 0x9;
d[10] = 10; o[10] = 012; x[10] = 0xa;
d[11] = 11; o[11] = 013; x[11] = 0xb;
d[12] = 12; o[12] = 014; x[12] = 0xc;
d[13] = 13; o[13] = 015; x[13] = 0xd;
d[14] = 14; o[14] = 016; x[14] = 0xe;
d[15] = 15; o[15] = 017; x[15] = 0xf;
d[16] = 16; o[16] = 020; x[16] = 0x10;
d[17] = 63; o[17] = 077; x[17] = 0x3f;
d[18] = 64; o[18] = 0100; x[18] = 0x40;
d[19] = 255; o[19] = 0377; x[19] = 0xff;
d[20] = 256; o[20] = 0400; x[20] = 0x100;
d[21] = 4095; o[21] = 07777; x[21] = 0xfff;
d[22] = 4096; o[22] = 010000; x[22] = 0x1000;
d[23] = 65535; o[23] = 0177777; x[23] = 0xffff;
d[24] = 65536; o[24] = 0200000; x[24] = 0x10000;
d[25] = 262143; o[25] = 0777777; x[25] = 0x3ffff;
d[26] = 262144; o[26] = 01000000; x[26] = 0x40000;
d[27] = 1048575; o[27] = 03777777; x[27] = 0xfffff;
d[28] = 1048576; o[28] = 04000000; x[28] = 0x100000;
d[29] = 16777215; o[29] = 077777777; x[29] = 0xffffff;
d[30] = 16777216; o[30] = 0100000000; x[30] = 0x1000000;
d[31] = 268435455; o[31] = 01777777777; x[31] = 0xfffffff;
d[32] = 268435456; o[32] = 02000000000; x[32] = 0x10000000;
d[33] = 1073741823; o[33] = 07777777777; x[33] = 0x3fffffff;
d[34] = 1073741824; o[34] = 010000000000; x[34] = 0x40000000;
d[35] = 4294967295; o[35] = 037777777777; x[35] = 0xffffffff;
d[36] = 4294967296; o[36] = 040000000000; x[36] = 0x100000000;
d[37] = 68719476735; o[37] = 0777777777777; x[37] = 0xfffffffff;
d[38] = 68719476736; o[38] = 01000000000000; x[38] = 0x1000000000;
/* WHEW! */
for (j=0; j<39; j++){
if ( g[j] != d[j]
|| d[j] != o[j]
|| o[j] != x[j]) {
if( pd0 -> flgm != 0 ) {
/* printf(s241er,16); save in case opinions change... */
printf("Decimal and octal/hex constants sometimes give\n");
printf(" different results when assigned to longs.\n");
}
/* lrc = 1; save... */
}
}
if (lrc != 0) rc =16;
return rc;
}
long pow2(n) /* Calculate 2**n by multiplying, not shifting */
long n;
{
long s;
s = 1;
while(n--) s = s*2;
return s;
}
s243(pd0) /* 2.4.3 Character constants */
struct defs *pd0;
{
static char s243er[] = "s243,er%d\n";
static char qs243[8] = "s243 ";
char *ps, *pt;
int rc;
char chars[256];
rc = 0;
ps = qs243;
pt = pd0->rfs;
while(*pt++ = *ps++);
/* One of the problems that arises when testing character constants
is that of definition: What, exactly, is the character set?
In order to guarantee a certain amount of machine independence,
the character set we will use here is the set of characters writ-
able as escape sequences in C, plus those characters used in writ-
ing C programs, i.e.,
letters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ 26
abcdefghijklmnopqrstuvwxyz 26
numbers:
0123456789 10
special characters:
~!"#%&()_=-^|{}[]+;*:<>,.?/ 27
extra special characters:
newline \n
horizontal tab \t
backspace \b
carriage return \r
form feed \f
backslash \\
single quote \' 7
blank & NUL 2
---
98
Any specific implementation of C may of course support additional
characters. */
/* Since the value of a character constant is the numerical value
of the character in the machine's character set, there should
be a one-to-one correspondence between characters and values. */
zerofill(chars);
chars['a'] = 1; chars['A'] = 1; chars['~'] = 1; chars['0'] = 1;
chars['b'] = 1; chars['B'] = 1; chars['!'] = 1; chars['1'] = 1;
chars['c'] = 1; chars['C'] = 1; chars['"'] = 1; chars['2'] = 1;
chars['d'] = 1; chars['D'] = 1; chars['#'] = 1; chars['3'] = 1;
chars['e'] = 1; chars['E'] = 1; chars['%'] = 1; chars['4'] = 1;
chars['f'] = 1; chars['F'] = 1; chars['&'] = 1; chars['5'] = 1;
chars['g'] = 1; chars['G'] = 1; chars['('] = 1; chars['6'] = 1;
chars['h'] = 1; chars['H'] = 1; chars[')'] = 1; chars['7'] = 1;
chars['i'] = 1; chars['I'] = 1; chars['_'] = 1; chars['8'] = 1;
chars['j'] = 1; chars['J'] = 1; chars['='] = 1; chars['9'] = 1;
chars['k'] = 1; chars['K'] = 1; chars['-'] = 1;
chars['l'] = 1; chars['L'] = 1; chars['^'] = 1;
chars['m'] = 1; chars['M'] = 1; chars['|'] = 1; chars['\n'] = 1;
chars['n'] = 1; chars['N'] = 1; chars['\t'] = 1;
chars['o'] = 1; chars['O'] = 1; chars['{'] = 1; chars['\b'] = 1;
chars['p'] = 1; chars['P'] = 1; chars['}'] = 1; chars['\r'] = 1;
chars['q'] = 1; chars['Q'] = 1; chars['['] = 1; chars['\f'] = 1;
chars['r'] = 1; chars['R'] = 1; chars[']'] = 1;
chars['s'] = 1; chars['S'] = 1; chars['+'] = 1; chars['\\'] = 1;
chars['t'] = 1; chars['T'] = 1; chars[';'] = 1; chars['\''] = 1;
chars['u'] = 1; chars['U'] = 1; chars['*'] = 1;
chars['v'] = 1; chars['V'] = 1; chars[':'] = 1; chars['\0'] = 1;
chars['w'] = 1; chars['W'] = 1; chars['<'] = 1; chars[' '] = 1;
chars['x'] = 1; chars['X'] = 1; chars['>'] = 1;
chars['y'] = 1; chars['Y'] = 1; chars[','] = 1;
chars['z'] = 1; chars['Z'] = 1; chars['.'] = 1;
chars['?'] = 1;
chars['/'] = 1;
if(sumof(chars) != 98){
rc = rc+1;
if(pd0->flgd != 0) printf(s243er,1);
}
/* Finally, the escape \ddd consists of the backslash followed
by 1, 2, or 3 octal digits which are taken to specify the
desired character. */
if( '\0' != 0 || '\01' != 1 || '\02' != 2
|| '\03' != 3 || '\04' != 4 || '\05' != 5
|| '\06' != 6 || '\07' != 7 || '\10' != 8
|| '\17' != 15 || '\20' != 16 || '\77' != 63
|| '\100' != 64 || '\177' != 127 ){
rc = rc+8;
if(pd0->flgd != 0) printf(s243er,8);
}
return rc;
}
zerofill(x)
char *x;
{
int j;
for (j=0; j<256; j++) *x++ = 0;
}
sumof(x)
char *x;
{
char *p;
int total, j;
p = x;
total = 0;
for(j=0; j<256; j++) total = total+ *p++;
return total;
}
s244(pd0)
struct defs *pd0;
{
double a[8];
int rc, lrc, j;
static char s244er[] = "s244,er%d\n";
static char qs244[8] = "s244 ";
char *ps, *pt;
ps = qs244;
pt = pd0->rfs;
while(*pt++ = *ps++);
rc = 0;
lrc = 0;
/* Unfortunately, there's not a lot we can do with floating constants.
We can check to see that the various representations can be com-
piled, that the conversion is such that they yield the same hard-
ware representations in all cases, and that all representations
thus checked are double precision. */
a[0] = .1250E+04;
a[1] = 1.250E3;
a[2] = 12.50E02;
a[3] = 125.0e+1;
a[4] = 1250e00;
a[5] = 12500.e-01;
a[6] = 125000e-2;
a[7] = 1250.;
lrc = 0;
for (j=0; j<7; j++) if(a[j] != a[j+1]) lrc = 1;
if(lrc != 0) {
if(pd0->flgd != 0) printf(s244er,1);
rc = rc+1;
}
if ( (sizeof .1250E+04 ) != sizeof(double)
|| (sizeof 1.250E3 ) != sizeof(double)
|| (sizeof 12.50E02 ) != sizeof(double)
|| (sizeof 1.250e+1 ) != sizeof(double)
|| (sizeof 1250e00 ) != sizeof(double)
|| (sizeof 12500.e-01) != sizeof(double)
|| (sizeof 125000e-2 ) != sizeof(double)
|| (sizeof 1250. ) != sizeof(double)){
if(pd0->flgd != 0) printf(s244er,2);
rc = rc+2;
}
return rc;
}
s25(pd0)
struct defs *pd0;
{
char *s, *s2;
int rc, lrc, j;
static char s25er[] = "s25,er%d\n";
static char qs25[8] = "s25 ";
char *ps, *pt;
ps = qs25;
pt = pd0->rfs;
while(*pt++ = *ps++);
rc = 0;
/* A string is a sequence of characters surrounded by double
quotes, as in "...". */
s = "...";
/* A string has type "array of characters" and storage class
static and is initialized with the given characters. */
if ( s[0] != s[1] || s[1] != s[2]
|| s[2] != '.' ) {
rc = rc+1;
if(pd0->flgd != 0) printf(s25er,1);
}
/* The compiler places a null byte \0 at the end of each string
so the program which scans the string can find its end. */
if( s[3] != '\0' ){
rc = rc+4;
if(pd0->flgd != 0) printf(s25er,4);
}
/* In a string, the double quote character " must be preceded
by a \. */
if( ".\"."[1] != '"' ){
rc = rc+8;
if(pd0->flgd != 0) printf(s25er,8);
}
/* In addition, the same escapes described for character constants
may be used. */
s = "\n\t\b\r\f\\\'";
if( s[0] != '\n'
|| s[1] != '\t'
|| s[2] != '\b'
|| s[3] != '\r'
|| s[4] != '\f'
|| s[5] != '\\'
|| s[6] != '\'' ){
rc = rc+16;
if( pd0->flgd != 0) printf(s25er,16);
}
/* Finally, a \ and an immediately following newline are ignored */
s2 = "queep!";
s = "queep!";
lrc = 0;
for (j=0; j<sizeof "queep!"; j++) if(s[j] != s2[j]) lrc = 1;
if (lrc != 0){
rc = rc+32;
if(pd0->flgd != 0) printf(s25er,32);
}
return rc;
}
s26(pd0) /* 2.6 Hardware Characteristics */
struct defs *pd0;
{
static char qs26[8] = "s26 ";
char *ps, *pt;
char c0, c1;
float temp, one, delta;
double tempd, oned;
static char s[] = "%3d bits in %ss.\n";
static char s2[] = "%e is the least number that can be added to 1. (%s).\n";
ps = qs26;
pt = pd0->rfs;
while(*pt++ = *ps++);
/* Here, we shake the machinery a little to see what falls
out. First, we find out how many bits are in a char. */
pd0->cbits = 0;
c0 = 0;
c1 = 1;
while(c0 != c1) {
c1 = c1<<1;
pd0->cbits = pd0->cbits+1;
}
/* That information lets us determine the size of everything else. */
pd0->ibits = pd0->cbits * sizeof(int);
pd0->sbits = pd0->cbits * sizeof(short);
pd0->lbits = pd0->cbits * sizeof(long);
pd0->ubits = pd0->cbits * sizeof(unsigned);
pd0->fbits = pd0->cbits * sizeof(float);
pd0->dbits = pd0->cbits * sizeof(double);
/* We have now almost reconstructed the table in section 2.6, the
exception being the range of the floating point hardware.
Now there are just so many ways to conjure up a floating point
representation system that it's damned near impossible to guess
what's going on by writing a program to interpret bit patterns.
Further, the information isn't all that useful, if we consider
the fact that machines that won't handle numbers between 10**30
and 10**-30 are very hard to find, and that people playing with
numbers outside that range have a lot more to worry about than
just the capacity of the characteristic.
A much more useful measure is the precision, which can be ex-
pressed in terms of the smallest number that can be added to
1. without loss of significance. We calculate that here, for
float and double. */
one = 1.;
delta = 1.;
temp = 0.;
while(temp != one) {
temp = one+delta;
delta = delta/2.;
}
pd0->fprec = delta * 4.;
oned = 1.;
delta = 1.;
tempd = 0.;
while(tempd != oned) {
tempd = oned+delta;
delta = delta/2.;
}
pd0->dprec = delta * 4.;
/* Now, if anyone's interested, we publish the results. */
if(pd0->flgm != 0) {
printf(s,pd0->cbits,"char");
printf(s,pd0->ibits,"int");
printf(s,pd0->sbits,"short");
printf(s,pd0->lbits,"long");
printf(s,pd0->ubits,"unsigned");
printf(s,pd0->fbits,"float");
printf(s,pd0->dbits,"double");
printf(s2,pd0->fprec,"float");
printf(s2,pd0->dprec,"double");
}
/* Since we are only exploring and perhaps reporting, but not
testing any features, we cannot return an error code. */
return 0;
}
int extvar;
s4(pd0) /* 4. What's in a name? */
struct defs *pd0;
{
static char s4er[] = "s4,er%d\n";
static char qs4[8] = "s4 ";
char *ps, *pt;
int j, rc;
short sint; /* short integer, for size test */
int pint; /* plain */
long lint; /* long */
unsigned target;
unsigned int mask;
rc = 0;
ps = qs4;
pt = pd0->rfs;
while(*pt++ = *ps++);
/* There are four declarable storage classes: automatic,
static, external, and register. Automatic variables have
been dealt with extensively thus far, and will not be specif-
ically treated in this section. Register variables are treated
in section s81.
Static variables are local to a block, but retain their
values upon reentry to a block, even after control has left
the block. */
for (j=0; j<3; j++)
if(svtest(j) != zero()){
rc = 1;
if(pd0->flgd != 0) printf(s4er,1);
}
;
/* External variables exist and retain their values throughout
the execution of the entire program, and may be used for comm-
unication between functions, even separately compiled functions.
*/
setev();
if(testev() != 0){
rc=rc+2;
if(pd0->flgd != 0) printf(s4er,2);
}
/*
Characters have been tested elsewhere (in s243).
Up to three sizes of integer, declared short int, int, and
long int, are available. Longer integers provide no less storage
than shorter ones, but implementation may make either short
integers, or long integers, or both, equivalent to plain
integers.
*/
if(sizeof lint < sizeof pint || sizeof pint < sizeof sint){
rc = rc+4;
if(pd0->flgd != 0) printf(s4er,4);
}
/* Unsigned integers, declared unsigned, obey the laws of
arithmetic modulo 2**n, where n is the number of bits in the
implementation */
target = ~0U;
mask = 1;
for(j=0; j<(sizeof target)*pd0->cbits; j++){
mask = mask⌖
target = target>>1;
}
if(mask != 1 || target != 0){
rc = rc+8;
if(pd0->flgd != 0) printf(s4er,8);
}
return rc;
}
svtest(n)
int n;
{
static k;
int rc;
switch (n) {
case 0: k = 1978;
rc = 0;
break;
case 1: if(k != 1978) rc = 1;
else{
k = 1929;
rc = 0;
}
break;
case 2: if(k != 1929) rc = 1;
else rc = 0;
break;
}
return rc;
}
zero(){ /* Returns a value of zero, possibly */
static k; /* with side effects, as it's called */
int rc; /* alternately with svtest, above, */
k = 2; /* and has the same internal storage */
rc = 0; /* requirements. */
return rc;
}
testev(){
if(extvar != 1066) return 1;
else return 0;
}
s61(pd0) /* Characters and integers */
struct defs *pd0;
{
static char s61er[] = "s61,er%d\n";
static char qs61[8] = "s61 ";
short from, shortint;
long int to, longint;
int rc, lrc;
int j;
char fromc, charint;
char *wd, *pc[6];
static char upper_alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char lower_alpha[] = "abcdefghijklmnopqrstuvwxyz";
static char numbers[] = "0123456789";
static char special_characters[] = "~!\"#%&()_=-^|{}[]+;*:<>,.?/";
static char extra_special_characters[] = "\n\t\b\r\f\\\'";
static char blank_and_NUL[] = " \0";
char *ps, *pt;
ps = qs61;
pt = pd0->rfs;
rc = 0;
while (*pt++ = *ps++);
/* A character or a short integer may be used wherever
an integer may be used. In all cases, the value is converted
to integer. This principle is extensively used throughout this
program, and will not be explicitly tested here. */
/* Conversion of a shorter integer to a longer always
involves sign extension. */
from = -19;
to = from;
if(to != -19){
rc = rc+1;
if(pd0->flgd != 0) printf(s61er,1);
}
/* It is guaranteed that a member of the standard char-
acter set is nonnegative. */
pc[0] = upper_alpha;
pc[1] = lower_alpha;
pc[2] = numbers;
pc[3] = special_characters;
pc[4] = extra_special_characters;
pc[5] = blank_and_NUL;
lrc = 0;
for (j=0; j<6; j++)
while(*pc[j]) if(*pc[j]++ < 0) lrc =1;
if(lrc != 0){
rc=rc+2;
if(pd0->flgd != 0) printf(s61er,2);
}
/* When a longer integer is converted to a shorter or
to a char, it is truncated on the left; excess bits are
simply discarded. */
longint = 1048579; /* =2**20+3 */
shortint = longint;
charint = longint;
if((shortint != longint && shortint != 3) ||
(charint != longint && charint != 3)) {
rc = rc+8;
if(pd0->flgd != 0) printf(s61er,8);
}
return rc;
}
s626(pd0) /* 6.2 Float and double */
/* 6.3 Floating and integral */
/* 6.4 Pointers and integers */
/* 6.5 Unsigned */
/* 6.6 Arithmetic conversions */
struct defs *pd0;
{
static char s626er[] = "s626,er%d\n";
static char qs626[8] = "s626 ";
int rc;
char *ps, *pt;
float eps, f1, f2, f3, f4, f;
long lint1, lint2, l, ls;
char c, t[28], t0;
short s;
int is, i, j;
unsigned u, us;
double d, ds;
ps = qs626;
pt = pd0->rfs;
rc = 0;
while (*pt++ = *ps++);
/* Conversions of integral values to floating type are
well-behaved. */
f1 = 1.;
lint1 = 1.;
lint2 = 1.;
for(j=0;j<pd0->lbits-2;j++){
f1 = f1*2;
lint2 = (lint2<<1)|lint1;
}
f2 = lint2;
f1 = (f1-f2)/f1;
if(f1>2.*pd0->fprec){
rc = rc+2;
if(pd0->flgd != 0) printf(s626er,2);
}
/* Pointer-integer combinations are discussed in s74,
"Additive operators". The unsigned-int combination