#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 2_8.c 2_8a.c 2_8a.cmp 2_8a1.c 2_8a2.c 2_8a3.c 2_8a4.c 2_8a5.c 2_8b.c 2_8b.cmp makefile signal.h tstbadalign.h # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 22:57:20 EDT 1990 # echo x - 2_8.c sed 's/^X//' > 2_8.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ 3b20 char 0,255 short -32768,32767 int -2147483648,2147483647 long -2147483648,2147483647 float double unsigned char * int * void * !EOF! ls -l 2_8.c echo x - 2_8a.c sed 's/^X//' > 2_8a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // find the alignment restrictions on // addresses of various types #include #include jmp_buf jmpenv; #include "2_8a4.c" /* EXPAND */ #include int main(int, char**) { #include "2_8a1.c" /* EXPAND4 */ #include "2_8a3.c" /* EXPAND4 */ tstbadalignments(char, "char"); tstbadalignments(short, "short"); tstbadalignments(int, "int"); tstbadalignments(long, "long"); tstbadalignments(float, "float"); tstbadalignments(double, "double"); tstbadalignments(char*, "char*"); tstbadalignments(int*, "int*"); tstbadalignments(void*, "void*"); return 0; } !EOF! ls -l 2_8a.c echo x - 2_8a.cmp sed 's/^X//' > 2_8a.cmp << '!EOF!' Type = char offset = 0 Type = short offset = 0 offset = 1, signal 7 caught Type = int offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught Type = long offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught Type = float offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught Type = double offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught offset = 4 offset = 5, signal 7 caught offset = 6, signal 7 caught offset = 7, signal 7 caught Type = char* offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught Type = int* offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught Type = void* offset = 0 offset = 1, signal 7 caught offset = 2, signal 7 caught offset = 3, signal 7 caught !EOF! ls -l 2_8a.cmp echo x - 2_8a1.c sed 's/^X//' > 2_8a1.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ union foo { double f; // force maximal alignment long l; char a[ sizeof(double) > sizeof(long) ? 2*sizeof(double) : 2*sizeof(long)]; } foovar; !EOF! ls -l 2_8a1.c echo x - 2_8a2.c sed 's/^X//' > 2_8a2.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ long *ip, ivar; cout << "Type = long\n"; for (offset = 0; offset < sizeof(long); offset++) { cout << "\toffset = " << offset; ip = (long *)&foovar.a[offset]; ivar = *ip; cout << "\n"; } !EOF! ls -l 2_8a2.c echo x - 2_8a3.c sed 's/^X//' > 2_8a3.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // initialize the signal catcher (void) signal(SIGEMT, trapsig); !EOF! ls -l 2_8a3.c echo x - 2_8a4.c sed 's/^X//' > 2_8a4.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // print a message each time a signal is caught #include void trapsig(int signo) { signal(signo, trapsig); // reset signal cout << ", signal " << signo << " caught"; longjmp(jmpenv, 1); } !EOF! ls -l 2_8a4.c echo x - 2_8a5.c sed 's/^X//' > 2_8a5.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // // this macro will test an assignment // to an address of each possible offset // for a given type #define tstbadalignments(type,typestr) \ { \ type *ip; \ type ivar; \ int offset; \ cout << "Type = " << typestr << "\n"; \ for (offset = 0; \ offset < sizeof(type); \ offset++) \ { \ cout << "\toffset = " << offset; \ ip = (type *)&foovar.a[offset]; \ if (!setjmp(jmpenv)) \ ivar = *ip; \ cout << "\n"; \ } \ } !EOF! ls -l 2_8a5.c echo x - 2_8b.c sed 's/^X//' > 2_8b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include int main(int, char**) { printf("maxshort=%d\n", MAXSHORT); printf("minshort=%d\n", HIBITS); printf("maxlong=%d\n", MAXLONG); printf("minlong=%d\n", HIBITL); printf("maxshort+1=%d\n", MAXSHORT + 1); printf("maxlong+1=%d\n", MAXLONG + 1); return 0; } !EOF! ls -l 2_8b.c echo x - 2_8b.cmp sed 's/^X//' > 2_8b.cmp << '!EOF!' maxshort=32767 minshort=-32768 maxlong=2147483647 minlong=-2147483648 maxshort+1=32768 maxlong+1=-2147483648 !EOF! ls -l 2_8b.cmp echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: 2_8a 2_8b 2_8a: 2_8a.c 2_8a4.c 2_8a1.c 2_8a3.c tstbadalign.h $(CC) -I. 2_8a.c -o 2_8a 2_8b: 2_8b.c $(CC) 2_8b.c -o 2_8b test: all 2_8a.cmp 2_8b.cmp 2_8a > 2_8a.out cmp 2_8a.out 2_8a.cmp 2_8b > 2_8b.out cmp 2_8b.out 2_8b.cmp echo tests done !EOF! ls -l makefile echo x - signal.h sed 's/^X//' > signal.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* @(#) signal.h 1.6 1/20/87 09:40:05 */ /*ident "@(#)cfront:incl/signal.h 1.6"*/ #ifndef SIGNALH #define SIGNALH #include typedef void (*SIG_PF) (...); extern int (*sigset (int, SIG_PF))(); extern int (*ssignal(int, SIG_PF))(); extern int (*signal (int, SIG_PF))(); extern int gsignal (int); extern int kill (int, int); #endif !EOF! ls -l signal.h echo x - tstbadalign.h sed 's/^X//' > tstbadalign.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // // this macro will test an assignment // to an address of each possible offset // for a given type #define tstbadalignments(type,typestr) \ { \ type *ip; \ type ivar; \ int offset; \ cout << "Type = " << typestr << "\n"; \ for (offset = 0; \ offset < sizeof(type); \ offset++) \ { \ cout << "\toffset = " << offset; \ ip = (type *)&foovar.a[offset]; \ if (!setjmp(jmpenv)) \ ivar = *ip; \ cout << "\n"; \ } \ } !EOF! ls -l tstbadalign.h # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0