home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-03-02 | 30.7 KB | 1,280 lines |
- /* Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-13 */
-
- /**** name=scanf ****/
- #define main() TEST_scanf()
-
- /*
- A similar example is given for fscanf, showing the slight difference
- between the two functions.
- */
-
- #include <stdio.h>
-
- void main() {
- char s1[11], s2[19] = "but missed eleven.";
- int i, j;
-
- printf("Please type:\nscanf read 10 %11 and then "
- "quit.z and garbage?\n");
- scanf("%11c%d %%%d %[^z]z", s1, &i, &j, s2);
- printf("%11s%d, %s%s",
- s1, i, j == 11 ? "11, " : "", s2);
- }
-
- /* End scanf */
- #undef main
- /**** name=_searchenv ****/
- #define main() TEST__searchenv()
-
- /*
- This program searches for the location of file stdio.h using the IPATH
- environment variable.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- void main() {
- char buffer[70];
-
- _searchenv("stdio.h", "IPATH", buffer);
- if (strcmp(buffer, "") == 0)
- puts("File not found.\n");
- else
- printf("The file is in directory: %s.\n",
- buffer);
- }
-
- /* End _searchenv */
- #undef main
- /**** name=_segread ****/
- #define main() TEST__segread()
-
- /*
- This program displays the current values of the segment registers.
- */
-
- #include <stdio.h>
- #include <dos.h>
-
- void main() {
- struct SREGS segs;
- unsigned cs, ds, es, ss;
-
- _segread(&segs);
- cs = segs.cs;
- ds = segs.ds;
- es = segs.es;
- ss = segs.ss;
- printf("Current values of segment registers:\n");
- printf("code segment: %x\ndata segment:"
- " %x\nextra segment: %x\n"
- "stack segment: %x\n", cs, ds, es, ss);
- }
-
- /* End _segread */
- #undef main
- /**** name=setjmp ****/
- #define main() TEST_setjmp()
- /* End setjmp */
- #undef main
- /**** name=_setmode ****/
- #define main() TEST__setmode()
-
- /*
- The program below copies a line from standard
- input to standard output without doing any text
- conversions.
- */
-
- #include <stdio.h>
-
- void TEST__setmode() {
- char line[256];
-
- _setmode(stdin, _BINARY);
- _setmode(stdout, _BINARY);
-
- printf("\nWaiting for input: ");
- if (gets(line))
- puts(line);
- }
-
- /* End _setmode */
- #undef main
- /**** name=setvbuf ****/
- #define main() TEST_setvbuf()
-
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- char buffer[256];
- size_t size;
- FILE *FP1, *FP2;
-
- size = sizeof(buffer);
-
- if ((FP1 = fopen("setvbuf1.tmp","a")) == NULL)
- printf("Unable to open setvbuf1.tmp.\n");
- if ((FP2 = fopen("setvbuf2.tmp","w")) == NULL)
- printf("Unable to open setvbuf2.tmp.\n");
- if (setvbuf(FP1,buffer,_IOFBF,size) != 0)
- printf("setvbuf failed on setvbuf1.tmp\n");
- else
- printf("setvbuf was successful.\n");
- if (setvbuf(FP2,NULL,_IONBF,0) != 0)
- printf("setvbuf failed on setvbuf2.tmp\n");
- else
- printf("setvbuf was successful.\n");
- fclose(FP1);
- fclose(FP2);
- }
-
- /* End setvbuf */
- #undef main
- /**** name=_skip_char ****/
- #define main() TEST__skip_char()
-
- #include <stdio.h>
-
- void main() {
- char p[10] = "TESTING.";
- char q[10] = "NNNNNNT.";
- unsigned sl = 10, n, m;
- char c = 'T';
- char d = 'N';
-
- n=_skip_char(p, sl, c);
- m=_skip_char(q, sl, d);
- printf("n = %u, m = %u\n", n, m);
- }
-
- /* End _skip_char */
- #undef main
- /**** name=_sopen ****/
- #define main() TEST__sopen()
-
- #include <io.h>
- #include <fcntl.h>
- #include <share.h>
- #include <sys/stat.h>
- #include <stdio.h>
-
- void main() {
- int pan;
- pan = _sopen("test.dat",O_RDONLY | O_TEXT,SH_DENYWR);
- if (pan != -1)
- printf("File test.dat exists, handle=%d\n", pan);
- else
- printf("File test.dat cannot be sopened,"
- " error code=%d\n",pan);
-
- pan = _sopen("sopen.tmp",
- O_CREAT | O_BINARY | O_EXCL,SH_DENYWR,
- S_IREAD | S_IWRITE);
- if (pan == -1)
- printf("Cannot create file sopen.tmp,"
- " error code=%d\n",pan);
- else
- printf("File sopen.tmp has been created,"
- " handle=%d\n",pan);
- close(pan);
-
- pan = _sopen("sopen.tmp",
- O_CREAT | O_BINARY | O_TRUNC,SH_DENYWR,
- S_IREAD | S_IWRITE);
- if (pan == -1)
- printf("Cannot truncate file sopen.tmp,"
- " error code=%d\n",pan);
- else
- printf("File sopen.tmp has been truncated,"
- " handle=%d\n",pan);
- }
-
- /* End _sopen */
- #undef main
- /**** name=_spawnX ****/
- #define main() TEST__spawnX()
-
- /*
- Spawn a child process chosen by the user.
- */
-
- #include <stdio.h>
- #include <process.h>
- #include <string.h>
-
- void main() {
- char path[30];
- int retcode, i;
- char *args[10];
-
- printf("Enter name of program to spawn"
- " (with arguments): ");
- scanf("%[^'\n']", path);
- args[0] = strtok(path, " -\\/,");
- i=1;
- while ((args[i] = strtok(NULL, " -\\/,")) != NULL)
- i++;
- printf("Spawning subprocess %s...\n", path);
- if ((retcode = _spawnv(P_WAIT, path, args)) == 0)
- puts("The child process terminated normally.");
- else if (retcode == -1)
- puts("The child process was not started.");
- else
- puts("The child process had an abnormal exit.");
- }
-
- /* End _spawn* */
- #undef main
- /**** name=_splitpath ****/
- #define main() TEST__splitpath()
-
- /*
- Create a full pathname in a buffer, then call _splitpath to break it
- into its components.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- char path_buffer[_MAX_PATH];
- char drive[_MAX_DRIVE];
- char dir[_MAX_DIR];
- char fname[_MAX_FNAME];
- char ext[_MAX_EXT];
-
- /* Create a full pathname in path_buffer. */
- _makepath(path_buffer, "d", "highc\\inc\\",
- "makpath", "c");
- printf("Path created with _makepath: %s\n\n",
- path_buffer);
-
- /* Now split into parts. */
- _splitpath(path_buffer, drive, dir, fname, ext);
- printf("Path extracted with _splitpath:\n");
- printf(" drive: %s\n", drive);
- printf(" dir : %s\n", dir);
- printf(" fname: %s\n", fname);
- printf(" ext : %s\n", ext);
- }
-
- /* End _splitpath */
- #undef main
- /**** name=sprintf ****/
- #define main() TEST_sprintf()
-
- /*
- A similar example for fprintf shows the slight difference between the
- two functions.
- */
-
- #include <stdio.h>
-
- void main() {
- char String[200];
- char *S = String;
- char s[14] = "like this one";
- int i = 10, x = 255, n;
- double d = 3.1415927;
-
- sprintf(S, "sprintf prints strings (%s),%n",s,&n);
- S += n; /* The next call to sprintf must be */
- /* passed the address of the NUL */
- /* written by the last call. */
- sprintf(S, "\ndecimal numbers (%d),%n", i, &n);
- S += n;
- sprintf(S, " hex numbers (%04X),\n%n", x, &n);
- S += n;
- sprintf(S, "percent signs (%%), and\n%n", &n);
- S += n;
- sprintf(S, "floating-point numbers (%+.4e).", d);
- puts(S);
- }
-
- /* End sprintf */
- #undef main
- /**** name=_srotX ****/
- #define main() TEST__srotX()
-
- #include <stdlib.h>
- #include <stdio.h>
- void main() {
- unsigned short int k=1, m=0x8000;
- printf("Rotate left and right by"
- " one bit at a time:\n");
- do {
- printf("\t%4.4x\t\t%4.4x\n", k, m);
- k = _srotl(k, 1);
- m = _srotr(m, 1);
- } while (k != 1);
- }
-
- /* End _srot* */
- #undef main
- /**** name=sscanf ****/
- #define main() TEST_sscanf()
-
- /*
- A similar example for fscanf shows the slight difference between the two
- functions.
- */
-
- #include <stdio.h>
-
- void main() {
- char s1[11];
- char s2[19] = "but missed eleven.";
- char s3[19] = "but missed eleven.";
- char input1[39] = "scanf read 10 %11 and quit.za";
- char input2[39] = "It read 9, 10 11 and quit.za";
- int i, j = 2, k = 2;
-
- sscanf(input1,"%11c%d %%%d %[^z]z",s1,&i,&j,s2);
- printf("%11s%d, %s%s\n", s1, i,
- j == 11 ? "11, " : "", s2);
- sscanf(input2,"%11c%d %%%d %[^z]z",s1,&i, &k,s3);
- printf("%11s%d, %s%s\n", s1, i,
- k == 11 ? "11, " : "", s3);
- }
-
- /* End sscanf */
- #undef main
- /**** name=_stat ****/
- #define main() TEST__stat()
-
- /*
- Display information about a user-specified file.
- */
-
- #include <time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <dos.h>
- #include <string.h>
- #include <stdlib.h>
-
- void main() {
- struct _stat buf;
- char fname[25];
- printf("Stats on file name: ");
- gets(fname);
- if (_stat(fname, &buf) != 0)
- perror("Problem getting info.");
- else {
- (buf.st_mode & S_IFDIR) ? puts("directory")
- : puts("not a directory");
- (buf.st_mode & S_IFREG) ? puts("ordinary file")
- : puts("not an ordinary file");
- (buf.st_mode & S_IWRITE)? puts("read/write file")
- : puts("read-only file");
- (buf.st_mode & S_IEXEC) ? puts("executable file")
- : puts("not an executable file");
- printf("File size : %ld\n", buf.st_size);
- printf("Drive number : %d\n", buf.st_dev);
- printf("Time modified: %s", ctime(&buf.st_atime));
- }
- }
-
- /* End _stat */
- #undef main
- /**** name=_status87 ****/
- #define main() TEST__status87()
-
- /*
- Program to test _status87 and _stat387.
- */
-
- #include <stdio.h>
- #include <float.h>
- #include <math.h>
-
- void main() {
- #ifdef _I387
-
- double a = 1e-40, b;
- float x, y;
-
- /*
- Exceptions are sticky, what do they look like starting out?
- */
- printf("Status = %.4x - start\n",_status87());
- printf("Stat387= %.4x - start\n",_stat387());
-
- _clear87(); /* Clear whatever is there. */
-
- printf("Status = %.4x - clear\n",_status87());
- printf("Stat387= %.4x - clear\n",_stat387());
-
- /* Force underflow by assigning a double to */
-
- /* a float. */
- y = a;
- printf("Status = %.4x - inexact,"
- " underflow\n",_status87());
- printf("Stat387= %.4x - inexact,"
- " underflow\n",_stat387());
-
- /* y became denormal on underflow, assign it */
- /* to a double. */
- /* Notice that the previous exceptions stuck. */
-
- b = y;
- printf("Status = %.4x - inexact,"
- " underflow, denormal\n",_status87());
- printf("Stat387= %.4x - inexact,"
- " underflow, denormal\n",_stat387());
-
- _clear87(); /* Clean up the mess. */
- printf("Status = %.4x - clear\n",_status87());
- printf("Stat387= %.4x - clear\n",_stat387());
- #endif
- }
-
- /* End _status87 */
- #undef main
- /**** name=_stkdmp ****/
- #define main() TEST__stkdmp()
-
- #include <stdio.h>
- /*
- Call a few functions which will call _stkdmp and display their calling
- information. Note that the program will write to the file handle given
- in main, though this is not obvious.
- */
- void f1(int n) { _stkdmp(n); }
- void f2(int n, int m) { f1(n+m); }
- void f3(int j) { f2(j-3, 3); }
-
- void main() {
- /*
- Write to file handle 1: stdout by default.
- */
- f3(1);
- }
-
- /* End _stkdmp */
- #undef main
- /**** name=strcat ****/
- #define main() TEST_strcat()
-
- /*
- Collect up to ten lines from test.dat into a buffer lines; then print
- the buffer. This example illustrates the use of strcat and strncat.
- */
-
- #include <string.h>
- #include <stdio.h>
- #define LINESIZE (256)
- #define OUTPUTLIMIT (3000)
-
- int main() {
- int line_count = 0;
- FILE *F1;
- char lines[OUTPUTLIMIT] = "", line[LINESIZE], *s;
- int i = OUTPUTLIMIT, j = 0;
- if ((F1 = fopen("test.dat","r"))==NULL) {
- perror("test.dat");
- return 2;
- }
- while (line_count < 10) {
- line_count++;
- /* Read a line and point at it. */
- s = fgets(line, LINESIZE , F1);
- /* If no line was read, we're done. */
- if (s == NULL) break;
- /* Append to lines as much of */
- /* the line as will fit. */
- if (i < (j = strlen(line))) {
- strncat(lines, line, i);
- break;
- }
- else {
- strcat(lines, line);
- i -= j;
- }
- }
- puts(lines); /* Print the lines. */
- }
-
- /* End strcat */
- #undef main
- #undef LINESIZE
- #undef OUTPUTLIMIT
- /**** name=_strcats ****/
- #define main() TEST__strcats()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char s1[65] = "This ";
- char s2[] = "string ";
- char s3[] = "contains ";
-
- char s4[] = "fifty ";
- char s5[] = "characters ";
- char s6[] = "more ";
- char s7[] = "or ";
- char s8[] = "less";
- char s9[] = ".\n";
- char s10[] = "More in fact than can be printed.";
- _strcats(65, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, NULL);
- puts(s1);
- }
-
- /* End _strcats */
- #undef main
- /**** name=strchr ****/
- #define main() TEST_strchr()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char *s1 = "Testing for strchr.";
- char *s2 = "This is correctly tested.";
- char *s;
-
- s = strchr(s1, (int)'t');
- puts(s);
- s = strchr(s2, (int)'c');
- puts(s);
- }
-
- /* End strchr */
- #undef main
- /**** name=_strXcmpX ****/
- #define main() TEST__strXcmpX()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[] = "tHIS iS a tEST Z";
- char b[] = "This Is A Test z";
- char c[] = "[]";
- char d[] = "{}";
- char e[] = "1234";
- char f[] = "5678";
-
- if (_strcmpi(a,b) == 0)
- puts("_strcmpi equal test ok.");
- else puts("_strcmpi equal test failed.");
- if (_strcmpi(c,d) == 0)
- puts("_strcmpi unequal test failed.");
- else puts("_strcmpi unequal test ok.");
- if (_strcmpi(e,e) == 0)
- puts("_strcmpi equal2 test ok.");
- else puts("_strcmpi equal2 test failed.");
- if (_strcmpi(e,f) == 0)
- puts("_strcmpi unequal test failed.");
- else puts("_strcmpi unequal test ok.");
- }
-
- /* End _str*cmp* */
- #undef main
- /**** name=strcmp ****/
- #define main() TEST_strcmp()
-
- /*
- The program below compares test.dat and test2.dat line by line.
- */
-
- #include <string.h>
- #include <stdio.h>
-
- #define EQUAL (1)
- #define UNEQUAL (0)
- #define FAILURE (-1)
- #define TRUE (1)
- #define LINESIZE (256)
-
- int main() {
- FILE *F1, *F2;
- char line1[LINESIZE], line2[LINESIZE], *s1, *s2;
- int i = 1;
- /* Open the files to be compared. If the */
- /* files cannot be opened, return an error */
- /* code to the calling environment. */
- if ((F1 = fopen("test.dat","r")) == NULL) {
- perror("Cannot open test.dat");
- return FAILURE;
- }
- if ((F2 = fopen("test2.dat","r")) == NULL) {
- perror("Cannot open test2.dat");
- return FAILURE;
- }
- while (TRUE) {
- /* Read a line from each file. */
- s1 = fgets(line1, LINESIZE, F1);
- s2 = fgets(line2, LINESIZE, F2);
- /* A NULL return from fgets means end-of- */
- /* file was found. If both strings are */
- /* NULL, the files are equal, if only one */
- /* is NULL, they are not equal. */
- if ((s1 == NULL) || (s2 == NULL)) {
- if (s1 == s2) break;
- printf("Files diverge at line %d.", i);
- return UNEQUAL;
- }
- /* Compare the strings. */
-
- if (strcmp(line1,line2)) {
- printf("Files diverge at line %d.", i);
- return UNEQUAL;
- }
- else i++;
- }
- printf("Files are identical.");
- return EQUAL;
- }
-
- /* End strcmp */
- #undef main
- #undef EQUAL
- #undef UNEQUAL
- #undef FAILURE
- #undef TRUE
- #undef LINESIZE
- /**** name=strcpy ****/
- #define main() TEST_strcpy()
-
- /*
- The function below returns the last word of the string line in the
- string word. This example illustrates the use of strcpy and strrchr.
- */
-
- #include <string.h>
-
- char *last_word (char *line, char *word) {
- char *s;
-
- /* Find the last space in line. */
- s = strrchr(line,' ');
- /* If not found, line itself is the last word */
- if (s == NULL) s = line;
- /* Else the last word starts after the space. */
- else s++;
- /* Copy the word into word. */
- return strcpy(word, s);
- }
-
- void main() {
- char word[50];
- puts(last_word("ignore all words until strcpy-ok",word));
- }
- /* End strcpy */
- #undef main
- /**** name=strcspn ****/
- #define main() TEST_strcspn()
- /* End strcspn */
- #undef main
- /**** name=_strdup ****/
- #define main() TEST__strdup()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[100];
- int j;
-
- for (j = 0; j < 99; j++)
- a[j] = 1;
- a[99] = 0;
- j = 0;
- do j++;
- while (NULL != _strdup(a));
- printf("_strdup %d00 bytes before running"
- " out of heap.\n",j);
- }
-
- /* End _strdup */
- #undef main
- /**** name=Xstrerror ****/
- #define main() TEST_Xstrerror()
-
- /*
- This program prints a few system error messages.
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- void main() {
- int i;
-
- /* Print error messages in strerror format. */
- puts("A few error messages:\n");
- for(i = 0; i <= 10; i++)
- printf(strerror(i));
-
- /* Now use _strerror format. */
- puts("\n\nA few more with a different format:\n");
- for(i = 0; i <= 10; i++) {
- errno = i;
- printf(_strerror("This is an error message"));
- }
- }
-
- /* End *strerror */
- #undef main
- /**** name=strlen ****/
- #define main() TEST_strlen()
-
- /*
- The function below copies the individual words of the line line to a
- buffer, NUL-terminating each copy. The words are indexed by an array
- pword of pointers. The number of words found is returned. This example
- illustrates the use of strlen, strncpy, and strpbrk.
- */
-
- #include <string.h>
-
- int wordify (char *line, char *word, char **pword) {
- char *s = line;
- int i = 0, j;
- /* While not at the end of the string: */
- while (s != NULL) {
- pword[i++] = word;
- /* Find the end of a word. */
- s = strpbrk(line, " ");
- /* Find the length of the word. */
- j = (s == NULL ? strlen(line) : s - line);
- /* Copy the word. */
- strncpy(word,line,j);
- word[j] = 0;
- word += j + 1;
- line += j + 1;
- }
- return (i);
- }
- void main() {
- char *pword[30];
- char wrd[50];
- printf("number of words is: %d\n",
- wordify("these are some words",wrd,pword));
- }
- /* End strlen */
- #undef main
- /**** name=_strlwr ****/
- #define main() TEST__strlwr()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[97];
- char *c;
- int j;
-
- for (j = 0; j < 64; j++)
- a[j] = j + 33;
- a[64] = '\n';
- for (j = 65; j < 95; j++)
- a[j] = j + 32;
- a[96] = 0;
- printf("The original string:\n\n%s\n\n",a);
- c = _strlwr(a);
- printf(" The _strlwr string:\n\n%s\n\n",a);
- printf(" The result string:\n\n%s\n",c);
- }
-
- /* End _strlwr */
- #undef main
- /**** name=strncat ****/
- #define main() TEST_strncat()
-
- #include <stdio.h>
- #include <string.h>
- void main() {
- char a[50] = "Testing for ";
- char b[30] = "strncat successfully failed";
- char c[10] = ".";
- char buf[50];
-
- strcpy (buf,"Testing for strncat successful");
- if (strcmp(buf, strncat(a, b, (size_t)18)) == 0)
- puts(a);
- else puts(strncat(a, b, (size_t)30));
- strncat(a, c, (size_t)30);
- puts (a);
- strncat(buf, a, 50 - strlen(buf));
- puts(buf);
- }
-
- /* End strncat */
- #undef main
- /**** name=_strncat ****/
- #define main() TEST__strncat()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char string1[50] =
- "This string contains forty characters. ";
- char string2[50] =
- "Plus ten. This sentence will be ignored.";
- _strncat(string1, string2, 50);
- puts(string1);
- }
-
- /* End _strncat */
- #undef main
- /**** name=strncmp ****/
- #define main() TEST_strncmp()
- /* End strncmp */
- #undef main
- /**** name=strncpy ****/
- #define main() TEST_strncpy()
- /* End strncpy */
- #undef main
- /**** name=_strnicmp ****/
- #define main() TEST__strnicmp()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[] = "tHIS iS a tEST Z1";
- char b[] = "This Is A Test z2";
- char aa[] = "tHIS iS a tEST ZA1";
- char bb[] = "This Is A Test za2";
- char c[] = "[]", d[] = "{}";
- char e[] = "1234", f[] = "5678";
-
- if (_strnicmp(a,b,(size_t)16) == 0)
- puts("_strnicmp equal test ok");
- else puts("_strnicmp equal test failed");
- if (_strnicmp(c,d,(size_t)16) == 0)
- puts("_strnicmp unequal test failed");
- else puts("_strnicmp unequal test ok");
- if (_strnicmp(e,e,(size_t)16) == 0)
- puts("_strnicmp equal2 test ok");
- else puts("_strnicmp equal2 test failed");
- if (_strnicmp(e,f,(size_t)16) == 0)
- puts("_strnicmp unequal test failed");
- else puts("_strnicmp unequal test ok");
- if (_strnicmp(a,b,(size_t)17) == 0)
- puts("_strnicmp equal3 test failed");
- else puts("_strnicmp equal3 test ok");
- if (_strnicmp(a,b,(size_t)18) == 0)
- puts("_strnicmp equal4 test failed");
- else puts("_strnicmp equal4 test ok");
- if (_strnicmp(aa,bb,(size_t)17) == 0)
- puts("_strnicmp equal5 test ok");
- else puts("_strnicmp equal5 test failed");
- }
-
- /* End _strnicmp */
- #undef main
- /**** name=_strnset ****/
- #define main() TEST__strnset()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[] = "**failed** _strnset is working";
- char *b;
-
- b = _strnset(a, 32, 10);
- puts(a);
- puts(b);
- b = _strnset(a, '-', 55);
- b = _strnset(a, 32, 11);
- puts(a);
- }
-
- /* End _strnset */
- #undef main
- /**** name=strpbrk ****/
- #define main() TEST_strpbrk()
- /* End strpbrk */
- #undef main
- /**** name=strrchr ****/
- #define main() TEST_strrchr()
-
- #include <stdio.h>
- #include <string.h>
-
- void main() {
- char *s = "This is a test for strrchr.";
- char *p = " ";
- int c = ' ';
-
- p = strrchr(s, c);
- puts(p);
-
- c = 's';
- p = strrchr(s, c);
- puts(p);
-
- c = 'i';
- p = strrchr(s, c);
- puts(p);
-
- c = 'r';
- p = strrchr(s, c);
- puts(p);
- }
-
- /* End strrchr */
- #undef main
- /**** name=_strrev ****/
- #define main() TEST__strrev()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[] = "abcdefghijklmnopqrstuvwxyz";
- char b[] = "123456789";
-
- puts("_strrev - string reversal");
- puts(a); puts(_strrev(a));
- puts(b); puts(_strrev(b));
- }
-
- /* End _strrev */
- #undef main
- /**** name=_strset ****/
- #define main() TEST__strset()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[] = "_strset is working";
- char *b;
-
- puts(a);
- b = _strset(a, '-');
- puts(b);
- b = _strset(a, '+');
- puts(a);
- }
-
- /* End _strset */
- #undef main
- /**** name=strstr ****/
- #define main() TEST_strstr()
-
- #include <stdio.h>
- #include <string.h>
-
- void main() {
- char *s1 = "This is testing";
- char *s2 = "This is testing for";
- char *s3 = "This is testing for strstr.";
- char *s4 = " is";
- char *s5 = " for";
- char *s6 = " strstr";
- char *nul = "";
- char *buf = " ";
- char *sp;
-
- puts("Testing strstr...");
- printf("1:%s.\n", strstr(s1, s4));
-
- if ((sp = strstr(s1, s5)) != NULL)
- printf("2:%s.\n", sp);
- printf("3:%s.\n", strstr(s2, s5));
-
- printf("4:%s.\n", strstr(s3, s6));
-
- printf("5:%s.\n", strstr(s1, nul));
-
- if ((sp = strstr(s2, s6)) != NULL)
- printf("6:%s.\n", sp);
- }
-
- /* End strstr */
- #undef main
- /**** name=_strtime ****/
- #define main() TEST__strtime()
-
- #include <time.h>
- #include <stdio.h>
-
- void main() {
- char a[15];
-
- _strtime(&a[0]);
- puts("The time is --");
- puts(a);
- }
-
- /* End _strtime */
- #undef main
- /**** name=strtod ****/
- #define main() TEST_strtod()
-
- /*
- The program below computes the areas of a series of circles, given their
- diameters. A similar example for atof shows how the two functions
- differ.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #define INPUTS (5)
-
- void main() {
- char diameters[] = "1.03 67.94 9.2032e27 4 8e-32";
- char *diameter = diameters;
- double pi = 3.141593;
- double diam;
- double area[INPUTS];
- int i = 0;
-
- /* While not at end of string. */
- while (*diameter) {
- diam = strtod(diameter, &diameter);
- area[i] = pi * diam * diam / 4.0;
- printf("\ndiam=%-20.5g \tarea=%g",
- diam, area[i++]);
- }
- }
-
- /* End strtod */
- #undef main
- #undef INPUTS
- /**** name=strtok ****/
- #define main() TEST_strtok()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char separator[12] = "fwy\n 0ru b", sentence[80] =
- "fwThisfruisb frabucomplete0sentence.0\n by itself.\n";
- printf("%s ", strtok(sentence,separator));
- printf("%s ", strtok(NULL,separator));
- printf("%s ", strtok(NULL,separator));
- printf("%s ", strtok(NULL,separator));
- printf("%s", strtok(NULL,separator));
- printf("%s", strtok(NULL,"bits of icy leaf\n"));
- }
-
- /* End strtok */
- #undef main
- /**** name=strtol ****/
- #define main() TEST_strtol()
-
- /*
- The program below computes the number of miles travelled during various
- time units at a speed of 60 miles per hour. This example illustrates
- the use of strtol and atol. A similar example appears for atoi, showing
- how these functions differ.
- */
-
- #include <stdlib.h>
- #include <stdio.h>
- #define INPUTS 5
- #define WEEK "604800"
- #define DAY "86400"
- #define HOUR "07020" /* Octal */
- #define MINUTE "0x3c" /* Hexadecimal */
- #define SECOND "1"
-
- void main() {
- char *sec_list[] = {WEEK, DAY, HOUR, MINUTE, SECOND};
- char *stopstring;
- unsigned long seconds = 0;
- int i = 0;
-
- while (sec_list[i] && *sec_list[i]) {
- seconds += strtol(sec_list[i], &stopstring, 0);
- i++;
- }
- printf("Total number of seconds in a week, day, hour"
- ", minute, and second = %ld.\n", seconds);
- }
-
- /* End strtol */
- #undef main
- #undef INPUTS
- #undef WEEK
- #undef DAY
- #undef HOUR
- #undef MINUTE
- #undef SECOND
- /**** name=_strupr ****/
- #define main() TEST__strupr()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char a[97];
- char *c;
- int j;
-
- for (j = 0; j < 64; j++)
- a[j] = j + 33;
- a[64] = '\n';
- for (j = 65;j < 95; j++)
- a[j] = j + 32;
- a[96] = 0;
- printf("The original string:\n\n%s\n\n",a);
- c = _strupr(a);
- printf(" The _strupr string:\n\n%s\n\n",a);
- printf(" The result string:\n\n%s\n",c);
- }
-
- /* End _strupr */
- #undef main
- /**** name=_swab ****/
- #define main() TEST__swab()
-
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- char a[] = "abcdefghijkl";
- char b[13];
-
- b[12] = 0;
- _swab(a, b, 12);
- puts("_swab - swap bytes");
- puts(a);
- puts(b);
- }
-
- /* End _swab */
- #undef main
- /**** name=system ****/
- #define main() TEST_system()
-
- /*
- Compile and run this example with the following commands:
-
- hc386 system.c -maxreal 0ffffh
- run386 system
-
- -- or --
-
- hc386 system.c
- run386 -maxreal 0ffffh system
- */
-
- #include <stdlib.h>
- void main() {
- system("dir /w");
- }
-
- /* End system */
- #undef main
-
- /*****names*****/
-
- char * names[]={
- "scanf",
- "_searchenv",
- "_segread",
- "_setmode",
- "setvbuf",
- "_skip_char",
- "_sopen",
- "_spawnX",
- "_splitpath",
- "sprintf",
- "_srotX",
- "sscanf",
- "_stat",
- "_status87",
- "_stkdmp",
- "strcat",
- "_strcats",
- "strchr",
- "_strXcmpX",
- "strcmp",
- "strcpy",
- "_strdup",
- "Xstrerror",
- "strlen",
- "_strlwr",
- "strncat",
- "_strncat",
- "_strnicmp",
- "_strnset",
- "strrchr",
- "_strrev",
- "_strset",
- "strstr",
- "_strtime",
- "strtod",
- "strtok",
- "strtol",
- "_strupr",
- "_swab",
- "system",
- "",""};
- int nextfunum;
- void main() {
- char ans[90];
- for (;;) {
- for (int j=0;j< 40;j++)
- if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
- else printf("%4d %-21s",j+1,names[j]);
- printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
- gets(ans);
- if (ans[0] != 0) nextfunum=atoi(ans);
- printf("\n\n\n");
- switch(nextfunum) {
- case 0:exit(0);
- case 1:TEST_scanf();break;
- case 2:TEST__searchenv();break;
- case 3:TEST__segread();break;
- case 4:TEST__setmode();break;
- case 5:TEST_setvbuf();break;
- case 6:TEST__skip_char();break;
- case 7:TEST__sopen();break;
- case 8:TEST__spawnX();break;
- case 9:TEST__splitpath();break;
- case 10:TEST_sprintf();break;
- case 11:TEST__srotX();break;
- case 12:TEST_sscanf();break;
- case 13:TEST__stat();break;
- case 14:TEST__status87();break;
- case 15:TEST__stkdmp();break;
- case 16:TEST_strcat();break;
- case 17:TEST__strcats();break;
- case 18:TEST_strchr();break;
- case 19:TEST__strXcmpX();break;
- case 20:TEST_strcmp();break;
- case 21:TEST_strcpy();break;
- case 22:TEST__strdup();break;
- case 23:TEST_Xstrerror();break;
- case 24:TEST_strlen();break;
- case 25:TEST__strlwr();break;
- case 26:TEST_strncat();break;
- case 27:TEST__strncat();break;
- case 28:TEST__strnicmp();break;
- case 29:TEST__strnset();break;
- case 30:TEST_strrchr();break;
- case 31:TEST__strrev();break;
- case 32:TEST__strset();break;
- case 33:TEST_strstr();break;
- case 34:TEST__strtime();break;
- case 35:TEST_strtod();break;
- case 36:TEST_strtok();break;
- case 37:TEST_strtol();break;
- case 38:TEST__strupr();break;
- case 39:TEST__swab();break;
- case 40:TEST_system();break;
- default:printf("I don't recognize that answer\n");nextfunum=-1;break;
- }
- printf("\n\npress enter to select another function\n");
- gets(ans);
- }
- }
-