#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 3_7a.c 3_7a.cmp 3_7abtst.c 3_7b.c 3_7b.cmp 3_7c.c 3_7c.cmp 3_7ctst.c 3_7d.c 3_7d.cmp 3_7detst.c 3_7dtst.c 3_7e.c 3_7e.cmp 3_7f.c 3_7f.cmp 3_7fgtst.c 3_7g.c 3_7g.cmp makefile # # 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:59:26 EDT 1990 # echo x - 3_7a.c sed 's/^X//' > 3_7a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Count the characters within the string. // Version 1 int strlen(const char *s) { int i = 0; while (*s++) i++; return i; } !EOF! ls -l 3_7a.c echo x - 3_7a.cmp sed 's/^X//' > 3_7a.cmp << '!EOF!' str='hello there', len=11 str='', len=0 str='This is a test', len=14 str='The quick brown fox jumped over the lazy brown dog', len=50 !EOF! ls -l 3_7a.cmp echo x - 3_7abtst.c sed 's/^X//' > 3_7abtst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #ifdef TSTA # include "3_7a.c" #endif #ifdef TSTB # include "3_7b.c" #endif char *x[] = { "hello there", "", "This is a test", "The quick brown fox jumped over the lazy brown dog", 0 }; main() { for (char **xp = x; *xp; xp++) cout << "str='" << *xp << "', len=" << strlen(*xp) << "\n"; return 0; } !EOF! ls -l 3_7abtst.c echo x - 3_7b.c sed 's/^X//' > 3_7b.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Count the characters within the string. // Version 2 int strlen(const char *s) { const char *save = s; while (*s++) ; return s - save - 1; } !EOF! ls -l 3_7b.c echo x - 3_7b.cmp sed 's/^X//' > 3_7b.cmp << '!EOF!' str='hello there', len=11 str='', len=0 str='This is a test', len=14 str='The quick brown fox jumped over the lazy brown dog', len=50 !EOF! ls -l 3_7b.cmp echo x - 3_7c.c sed 's/^X//' > 3_7c.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Count the characters within the string. // Version 3 // Look in the first maxlen characters for a null // character. Return its position (the length of // the string), or maxlen if it is not found. int strnlen(const char *s, int maxlen) { if (!s) return 0; int savemaxlen = maxlen; while (maxlen-- > 0) if (*s++ == '\0') return savemaxlen - maxlen - 1; return savemaxlen; } !EOF! ls -l 3_7c.c echo x - 3_7c.cmp sed 's/^X//' > 3_7c.cmp << '!EOF!' str='hello there', max=0, len=0 str='', max=0, len=0 str='This is a test', max=0, len=0 str='The quick brown fox jumped over the lazy brown dog', max=0, len=0 str='hello there', max=4, len=4 str='', max=4, len=0 str='This is a test', max=4, len=4 str='The quick brown fox jumped over the lazy brown dog', max=4, len=4 str='hello there', max=8, len=8 str='', max=8, len=0 str='This is a test', max=8, len=8 str='The quick brown fox jumped over the lazy brown dog', max=8, len=8 str='hello there', max=12, len=11 str='', max=12, len=0 str='This is a test', max=12, len=12 str='The quick brown fox jumped over the lazy brown dog', max=12, len=12 str='hello there', max=16, len=11 str='', max=16, len=0 str='This is a test', max=16, len=14 str='The quick brown fox jumped over the lazy brown dog', max=16, len=16 !EOF! ls -l 3_7c.cmp echo x - 3_7ctst.c sed 's/^X//' > 3_7ctst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include "3_7c.c" char *x[] = { "hello there", "", "This is a test", "The quick brown fox jumped over the lazy brown dog", 0 }; main() { for (int i = 0; i < 17; i += 4) for (char **xp = x; *xp; xp++) cout << "str='" << *xp << "', max=" << i << ", len=" << strnlen(*xp, i) << "\n"; return 0; } !EOF! ls -l 3_7ctst.c echo x - 3_7d.c sed 's/^X//' > 3_7d.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Copy the string s2 into s1. // Version 1 void strcpy(char *s1, const char *s2) { while (*s1++ = *s2++) ; } !EOF! ls -l 3_7d.c echo x - 3_7d.cmp sed 's/^X//' > 3_7d.cmp << '!EOF!' str='' b=abcdefghijklmnopqrstuvwxyz b=abc str='hello there' b=abcdefghijklmnopqrstuvwxyz b=abchello there str='This is a test' b=abcdefghijklmnopqrstuvwxyz b=abcThis is a test str='The quick brown fox jumped over the lazy brown dog' b=abcdefghijklmnopqrstuvwxyz b=abcThe quick brown fox jumped over the lazy brown dog !EOF! ls -l 3_7d.cmp echo x - 3_7detst.c sed 's/^X//' > 3_7detst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #ifdef TSTD #include "3_7d.c" #endif #ifdef TSTE #include "3_7e.c" #endif void* memcpy(void*, const void*, int); char a[] = "abcdefghijklmnopqrstuvwxyz"; char b[3 * sizeof(a)]; char *x[] = { "", "hello there", "This is a test", "The quick brown fox jumped over the lazy brown dog", 0 }; main() { for (char **xp = x; *xp; xp++) { memcpy(b, a, sizeof a); cout << "str='" << *xp << "'\n"; cout << "b=" << b << "\n"; #ifdef TSTD strcpy(b+3, *xp); #endif #ifdef TSTE strncpy(b+3, sizeof(a)-3, *xp); #endif cout << "b=" << b << "\n"; } return 0; } !EOF! ls -l 3_7detst.c echo x - 3_7dtst.c sed 's/^X//' > 3_7dtst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include void* memcpy(void*, const void*, int); char a[] = "abcdefghijklmnopqrstuvwxyz"; char b[sizeof a]; main() { memcpy(b, a, sizeof a); strcpy(a+3, ""); cout << "a=" << a << "\n"; memcpy(b, a, sizeof a); strcpy(a+3, "QRSTUVWXYZ"); cout << "a=" << a << "\n"; return 0; } !EOF! ls -l 3_7dtst.c echo x - 3_7e.c sed 's/^X//' > 3_7e.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Copy the string s2 into s1. // Version 2 void strncpy(char *s1, int maxlen, const char *s2) { if (s1 && s2) { while (--maxlen) if (!(*s1++ = *s2++)) return; if (maxlen > 0) *s1 = '\0'; } } !EOF! ls -l 3_7e.c echo x - 3_7e.cmp sed 's/^X//' > 3_7e.cmp << '!EOF!' str='' b=abcdefghijklmnopqrstuvwxyz b=abc str='hello there' b=abcdefghijklmnopqrstuvwxyz b=abchello there str='This is a test' b=abcdefghijklmnopqrstuvwxyz b=abcThis is a test str='The quick brown fox jumped over the lazy brown dog' b=abcdefghijklmnopqrstuvwxyz b=abcThe quick brown fox jum !EOF! ls -l 3_7e.cmp echo x - 3_7f.c sed 's/^X//' > 3_7f.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Compare two strings. Return <0, >0 or 0 // when the first string is lexically // less than, greater than, or equal to // the second, respectively. int strcmp(const char *s1, const char *s2) { if (s1 && s2) { while (*s1) if (*s1++ != *s2++) #ifdef TSTG /* DELETE */ # include "3_7g.c" /* DELETE */ #else /* DELETE */ return s1[-1] - s2[-1]; #endif /* DELETE */ return *s1 - *s2; } else error("NULL pointer encountered"); } !EOF! ls -l 3_7f.c echo x - 3_7f.cmp sed 's/^X//' > 3_7f.cmp << '!EOF!' a='abcd', b='abcd' cmp=0 a='abc', b='abcd' cmp=-100 a='abcd', b='abc' cmp=100 a='abdc', b='abcd' cmp=1 a='abdc', b='abcda' cmp=1 a='abdc', b='abc' cmp=1 !EOF! ls -l 3_7f.cmp echo x - 3_7fgtst.c sed 's/^X//' > 3_7fgtst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include #include "3_7f.c" struct sp { char *a, *b; } x[] = { { "abcd", "abcd" }, { "abc", "abcd" }, { "abcd", "abc" }, { "abdc", "abcd" }, { "abdc", "abcda" }, { "abdc", "abc" }, { 0, 0 } }; main() { for (sp *xp = x; xp->a; xp++) { cout << "a='" << xp->a << "', b='" << xp->b << "'\n"; cout << "cmp=" << strcmp(xp->a, xp->b) << "\n"; } return 0; } !EOF! ls -l 3_7fgtst.c echo x - 3_7g.c sed 's/^X//' > 3_7g.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ return *--s1 - *--s2; !EOF! ls -l 3_7g.c echo x - 3_7g.cmp sed 's/^X//' > 3_7g.cmp << '!EOF!' a='abcd', b='abcd' cmp=0 a='abc', b='abcd' cmp=-100 a='abcd', b='abc' cmp=100 a='abdc', b='abcd' cmp=1 a='abdc', b='abcda' cmp=1 a='abdc', b='abc' cmp=1 !EOF! ls -l 3_7g.cmp echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC ERROR= ../../error.o all: 3_7atst 3_7btst 3_7ctst 3_7dtst 3_7etst 3_7ftst 3_7gtst 3_7atst: 3_7abtst.c 3_7a.c # strlen $(CC) -DTSTA 3_7abtst.c -o 3_7atst $(ERROR) 3_7btst: 3_7abtst.c 3_7b.c # strlen $(CC) -DTSTB 3_7abtst.c -o 3_7btst $(ERROR) 3_7ctst: 3_7ctst.c 3_7c.c # strnlen $(CC) 3_7ctst.c -o 3_7ctst $(ERROR) 3_7dtst: 3_7detst.c 3_7d.c # strcpy $(CC) -DTSTD 3_7detst.c -o 3_7dtst $(ERROR) 3_7etst: 3_7detst.c 3_7e.c # strncpy $(CC) -DTSTE 3_7detst.c -o 3_7etst $(ERROR) 3_7ftst: 3_7fgtst.c 3_7f.c # strcmp $(CC) -DTSTF 3_7fgtst.c -o 3_7ftst $(ERROR) 3_7gtst: 3_7fgtst.c 3_7f.c 3_7g.c # strcmp ret2 $(CC) -DTSTG 3_7fgtst.c -o 3_7gtst $(ERROR) test: all 3_7a.cmp 3_7b.cmp 3_7c.cmp 3_7d.cmp 3_7e.cmp 3_7f.cmp 3_7g.cmp 3_7atst > 3_7a.out cmp 3_7a.out 3_7a.cmp 3_7btst > 3_7b.out cmp 3_7b.out 3_7b.cmp 3_7ctst > 3_7c.out cmp 3_7c.out 3_7c.cmp 3_7dtst > 3_7d.out cmp 3_7d.out 3_7d.cmp 3_7etst > 3_7e.out cmp 3_7e.out 3_7e.cmp 3_7ftst > 3_7f.out cmp 3_7f.out 3_7f.cmp 3_7gtst > 3_7g.out cmp 3_7g.out 3_7g.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0