home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved */
-
- /**** name=offsetof ****/
- #define main TEST_offsetof
- /* End offsetof */
- #undef main
- /**** name=_offsetof ****/
- #define main TEST__offsetof
-
- #include <stdio.h>
- void main() {
- struct s{
- char x, y;
- int a[10];
- } x;
- printf("Offset of x is %d\n",_offsetof(struct s,x));
- printf("Offset of y is %d\n",_offsetof(struct s,y));
- printf("Offset of a is %d\n",_offsetof(x ,a));
- }
- /* End _offsetof */
- #undef main
- /**** name=_open ****/
- #define main TEST__open
-
- #include <io.h>
- #include <fcntl.h>
- #include <sys\stat.h>
- #include <stdio.h>
-
- void main() {
- int pan;
- pan = _open("test.dat",O_RDONLY | O_TEXT);
- if (pan != -1)
- printf("file test.dat exists, handle=%d\n",pan);
- else
- printf("file test.dat cannot be opened,"
- " error code=%d\n",pan);
-
- pan=_open("open.tmp",
- O_CREAT | O_BINARY | O_EXCL,
- S_IREAD | S_IWRITE);
- if (pan == -1)
- printf("cannot create file open.tmp,"
- " error code=%d\n",pan);
- else
- printf("file open.tmp has been created,"
- " handle=%d\n",pan);
-
- pan=_open("open.tmp",
- O_CREAT | O_BINARY | O_TRUNC,
- S_IREAD | S_IWRITE);
- if (pan == -1)
- printf("cannot truncate file open.tmp,"
- " error code=%d\n",pan);
- else
- printf("file open.tmp has been truncated,"
- " handle=%d\n",pan);
- }
-
- /* End _open */
- #undef main
- /**** name=_outpX ****/
- #define main TEST__outpX
-
- #include <conio.h>
- #define MHZ 20
-
- void main() {
- volatile int j;
- int k, n;
-
- printf("Listen to the tone.\n");
-
- for (n = 0; n < 500; n++) {
- k = inp(0x61) & 0xfc;
- outp(0x61, k | 2);
- for (j = 0; j < 5 * MHZ; j++); /* Listen */
- outp(0x61, k);
- for (j = 0; j < 10 * MHZ; j++); /* to */
- outp(0x61, k | 2);
- for (j = 0; j < 10 * MHZ; j++); /* the */
- outp(0x61, k);
- for (j = 0; j < 20 * MHZ; j++); /* tone. */
- }
- }
- /* End _outp* */
- #undef main
- #undef MHZ
- /**** name=perror ****/
- #define main TEST_perror
-
- /*
- The program below attempts to open file exists.not, and exercises perror
- if the open fails.
- */
-
- #include <stdio.h>
-
- void main() {
- FILE *FP1;
- if ((FP1 = fopen("exists.not", "r")) == NULL) {
- perror("exists.not");
- }
- else {
- printf("File exists, cannot test perror.\n");
- fclose(FP1);
- }
- }
- /* End perror */
- #undef main
- /**** name=printf ****/
- #define main TEST_printf
-
- /*
- A similar example for fprintf shows the similarity of the two functions.
- */
-
- #include <stdio.h>
-
- void main() {
- char s[14] = "like this one", c = '*';
- int i = 10, x = 255, o = 45;
- double d = 3.1415927, nf = -3.449123;
-
- printf("printf prints strings (%s),\n", s);
- printf("decimal numbers (%d),", i);
- printf(" hex numbers (%04X),\n", x);
- printf("floating-point numbers (%+.4e)\n", d);
- printf("percent signs (%%),\n");
- printf("characters (%-5c),\n", c);
- printf("negative floating-points (% 010.2e),\n", nf);
- printf("and even octal numbers (%-+#5o).", o);
- }
-
- /* End printf */
- #undef main
- /**** name=putc ****/
- #define main TEST_putc
- /* End putc */
- #undef main
- /**** name=_putch ****/
- #define main TEST__putch
-
- #include <stdio.h>
- #include <conio.h>
- #include <ctype.h>
-
- void main() {
- char * c = "This is how _putch works.";
-
- for (; *c; c++)
- _putch(*c);
- }
- /* End _putch */
- #undef main
- /**** name=putchar ****/
- #define main TEST_putchar
-
- #include <stdio.h>
-
- void main() {
- char c[] = "This is an array of chars.\n";
- int i = 0;
-
- while (c[i] != '\0') {
- putchar(c[i]);
- i++;
- }
- }
- /* End putchar */
- #undef main
- /**** name=_putenv ****/
- #define main TEST__putenv
-
- /*
- This program displays the environment variables, changes the value of
- the PATH variable, adds a new variable, and redisplays the values.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
-
- void printenviron() {
- int i = 0;
- /* _environ is defined in stdlib.h. It is a pointer to an array of
- strings containing DOS environment variables. */
- while (_environ[i])
- printf("%s\n", _environ[i++]);
- }
-
- void main(void) {
- /* Print status of all environment variables. */
- printf("Current environment variables:\n\n");
- printenviron();
- /* Change one and add a new one. */
- _putenv("PATH=c:\\highc\\small");
- _putenv("NEWENVIRONVAR=SOME\\DIR\\SPECIFICATION");
- /* Print status again. */
- printf("\nNew environment variables:\n\n");
- printenviron();
- }
- /* End _putenv */
- #undef main
- /**** name=puts ****/
- #define main TEST_puts
- /* End puts */
- #undef main
- /**** name=qsort ****/
- #define main TEST_qsort
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- struct info {
- char name[8];
- int age;
- char phone[9];
- };
-
- /* Unsorted info array: */
- struct info data[] = {
- { "Anne", 33, "555-5552" },
- { "Fred", 27, "555-1221" },
- { "Sonya", 36, "555-1976" },
- { "Frank", 30, "555-1965" },
- { "Carol", 32, "555-4299" },
- { "Alice", 19, "555-7979" },
- { "Larry", 33, "555-8235" },
- };
-
- static int compare_function(
- const struct info *item1,
- const struct info *item2
- ) {
- if (item1->age < item2->age) /* Compare by age. */
- return (-1);
- else if (item1->age > item2->age)
- return (1);
- else /* Then compare by name. */
- return (strcmp(item1->name, item2->name));
- }
-
- void main() {
- int i;
- qsort(data, sizeof(data)/sizeof(* data),
- sizeof(*data), compare_function);
- for (i = 0; i < sizeof(data)/sizeof(* data); i++)
- printf("%-8s %d, %s\n", data[i].name,
- data[i].age,
- data[i].phone);
- }
-
-
- /* End qsort */
- #undef main
- /**** name=rand ****/
- #define main TEST_rand
-
- /*
- This example uses rand() to select numbers from zero through nine, then
- counts the number of each.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- int i, dist[10] = {0,0,0,0,0,0,0,0,0,0};
-
- for(i = 0; i < 10000; i++)
- dist[rand() % 10]++;
- printf("0 1 2 3 4 "
- "5 6 7 8 9\n");
- for(i = 0; i < 10; i++)
- printf("%04d ", dist[i]);
- printf("\n");
- }
- /* End rand */
- #undef main
- /**** name=_read ****/
- #define main TEST__read
-
- #include <io.h>
- #include <fcntl.h>
- #include <sys\stat.h>
- #include <stdio.h>
- #include <conio.h>
-
- void main() {
- int pan;
- char c;
-
- puts("Testing _read...\n");
- pan = _open("test.dat", O_RDONLY | O_BINARY);
- if (pan != -1) {
- printf("File test.dat exists, "
- "handle = %d.\n",pan);
- while(_read(pan, &c, 1) > 0)
- _putch(c);
- }
- else
- printf("File test.dat cannot"
- " be opened, error code = %d.\n",pan);
- }
- /* End _read */
- #undef main
- /**** name=realloc ****/
- #define main TEST_realloc
-
- #include <stdlib.h>
- #include <stdio.h>
-
- void main() {
- char * ptr = NULL;
- int siz, k, j = 0;
-
- for(siz = 25; siz <= 100; siz += 25) {
- ptr = realloc(ptr, siz); /* Allocate more space. */
- for(k = j; k < siz; k++)
- ptr[k] = j = k; /* Fill space. */
- }
- for(j = 0; j < 100; j += 10)
- printf("Position %d contains %d.\n",
- j, ptr[j]);
- free(ptr); /* Free the space. */
- }
- /* End realloc */
- #undef main
- /**** name=remove ****/
- #define main TEST_remove
-
- #include <stdio.h>
- void main() {
- FILE *FP1;
- char tmp[L_tmpnam];
-
- if ((FP1 = fopen(tmpnam(tmp), "w+")) == NULL) {
- perror("Cannot open temporary file.");
- return;
- }
- fclose(FP1);
- printf("Created temporary file: %s.\n",tmp);
-
- if (remove(tmp))
- perror("Cannot remove temporary file.");
-
- printf("Removed temporary file: %s.\n",tmp);
- }
- /* End remove */
- #undef main
- /**** name=rename ****/
- #define main TEST_rename
-
- #include <stdio.h>
- #include <stdlib.h>
-
- void main() {
- char old[] = "aname.tmp";
- char new[] = "rename.tmp";
- FILE * FP = fopen(old,"w");
-
- if (FP != NULL) fclose(FP);
-
- if(rename(old, new) != 0) {
- perror("Failed to rename file.");
- return;
- }
- printf("File was successfully renamed.\n");
- }
- /* End rename */
- #undef main
- /**** name=rewind ****/
- #define main TEST_rewind
-
- /*
- The program below prints test.dat, starting with the first 10
- characters. It rewinds, then prints the first 20 characters, rewinds,
- prints the first thirty characters, and so on.
- */
- #include <stdio.h>
-
- int main() {
- int i, j = 64;
- FILE *FP;
-
- if((FP = fopen("test.dat","r")) == NULL) {
- perror("File I/O error");
- return -1;
- }
-
- while(!feof(FP)) {
- rewind(FP);
- for(i = 0; i < j; i++)
- printf("%c", (signed char) getc(FP));
- j += 64;
- }
- fclose(FP);
- }
- /* End rewind */
- #undef main
- /**** name=_rmdir ****/
- #define main TEST__rmdir
-
- /*
- This program creates a new directory and then deletes it.
- */
-
- #include <stdio.h>
- #include <direct.h>
-
- void main() {
- /* Create a new directory. */
- if (mkdir("c:\\rmdir") == -1)
- perror("Error in mkdir.");
- else {
- printf("Directory c:\\rmdir "
- "successfully created.\n");
-
- /* Remove newly created directory. */
- if (_rmdir("c:\\rmdir") == -1)
- perror("Error in _rmdir.");
- else
- printf("Directory c:\\rmdir removed.\n");
- }
- }
-
- /* End _rmdir */
- #undef main
- /**** name=_rmemcpy ****/
- #define main TEST__rmemcpy
-
- #include <stdio.h>
- #include <string.h>
-
- void main() {
- char dest[100];
- char *source = "This is the string to be copied.";
-
- strcpy(dest, "This will be overwritten.\n");
-
- printf("source before: %s\n", source);
- printf("dest before: %s\n", dest);
-
- _rmemcpy(dest, source, strlen(source)+1);
-
- printf("source after: %s\n", source);
- printf("dest after: %s\n", dest);
- }
- /* End _rmemcpy */
- #undef main
- /**** name=_rotX ****/
- #define main TEST__rotX
-
- #include <stdlib.h>
- #include <stdio.h>
-
- void main() {
- int k=1;
- int j=1;
-
- printf("Rotate left and right by "
- "one bit at a time:\n");
- do {
- printf("%8x ... %8x\n", k, j);
- k = _rotl(k, 1);
- j = _rotr(j, 1);
- } while (k != 1);
- }
- /* End _rot* */
- #undef main
- /**** name=_rstrcpy ****/
- #define main TEST__rstrcpy
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char string1[50] = "";
- char string2[50] = "abcdefghijklmnopqrstuvwxyz";
-
- printf("String 1: %s\n"
- "String 2: %s\n", string1, string2);
- _rstrcpy(string1, string2);
- printf("String 1: %s\n"
- "String 2: %s\n", string1, string2);
- }
-
- /* End _rstrcpy */
- #undef main
- /**** name=_rstrncpy ****/
- #define main TEST__rstrncpy
-
- /*
- This example copies the first eight characters of one string into another
- string.
- */
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char string1[] = "Original string.";
- char string2[] = "Modified will replace original.";
-
- puts(string1);
- puts(string2);
- _rstrncpy(string1, string2, 8);
- puts(string1);
- puts(string2);
- }
- /* End _rstrncpy */
- #undef main
-
- /*****names*****/
-
- char * names[]={
- "_offsetof",
- "_open",
- "_outpX",
- "perror",
- "printf",
- "_putch",
- "putchar",
- "_putenv",
- "qsort",
- "rand",
- "_read",
- "realloc",
- "remove",
- "rename",
- "rewind",
- "_rmdir",
- "_rmemcpy",
- "_rotX",
- "_rstrcpy",
- "_rstrncpy",
- "",""};
- int nextfunum;
- void main() {
- char ans[90];
- for (;;) {
- for (int j=0;j< 20;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__offsetof();break;
- case 2:TEST__open();break;
- case 3:TEST__outpX();break;
- case 4:TEST_perror();break;
- case 5:TEST_printf();break;
- case 6:TEST__putch();break;
- case 7:TEST_putchar();break;
- case 8:TEST__putenv();break;
- case 9:TEST_qsort();break;
- case 10:TEST_rand();break;
- case 11:TEST__read();break;
- case 12:TEST_realloc();break;
- case 13:TEST_remove();break;
- case 14:TEST_rename();break;
- case 15:TEST_rewind();break;
- case 16:TEST__rmdir();break;
- case 17:TEST__rmemcpy();break;
- case 18:TEST__rotX();break;
- case 19:TEST__rstrcpy();break;
- case 20:TEST__rstrncpy();break;
- default:printf("I don't recognize that answer\n");nextfunum=-1;break;
- }
- printf("\n\npress enter to select another function\n");
- gets(ans);
- }
- }
-