home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-13 */
-
- /**** name=_offsetof ****/
- #define main() TEST__offsetof()
-
- #include <stdio.h>
- void main() {
- struct s{
- char x, y;
- int a[10];
- } x;
- printf("%d\n",_offsetof(struct s,x)); /* Is zero. */
- printf("%d\n",_offsetof(struct s,y)); /* Is one. */
- printf("%d\n",_offsetof(x ,a)); /* Is four. */
- }
- /* 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;
-
- 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 fragment below attempts to open exists.not, and complains if
- the open fails.
- */
-
- #include <stdio.h>
- #define FAILURE (-1)
- #define OKAY (1)
-
- int main() {
- FILE *FP1;
- if ((FP1 = fopen("exists.not", "r")) == NULL) {
- perror("exists.not");
- return(FAILURE);
- }
- else {
- printf("File exists, cannot test perror.\n");
- return(OKAY);
- }
- }
-
- /* End perror */
- #undef main
- #undef FAILURE
- #undef OKAY
- /**** 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()
-
- /*
- This program tests functions _putch(), isalpha(), isupper(), toupper(),
- tolower(), and isdigit(). It reads input from standard input and prints
- output to the screen. The program prints all alphabetic characters in
- uppercase.
- */
-
- #include <stdio.h>
- #include <conio.h>
- #include <ctype.h>
-
- void main() {
- int c, i=0;
- puts("Testing _putch...\n");
- puts("Enter characters, (';' to terminate).");
-
- while ((c = getche()) != ';') { /* Terminate at
- semicolon. */
- if (isalpha(c)) {
- if (isupper(c))
- _putch(c);
- else if (islower(c))
- _putch(toupper(c));
- else printf("\nERROR in isalpha().\n");
- }
- else if (isdigit(c))
- _putch(c);
- else _putch(toupper(tolower(c)));
- i++;
- }
- printf("\nProgram terminated at semicolon.\n");
- printf("Number of characters read: %d.\n",i);
- }
-
- /* End _putch */
- #undef main
- /**** name=putchar ****/
- #define main() TEST_putchar()
- /* 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;
- while (_environ[i])
- printf("%s\n\n", _environ[i++]);
- }
-
- void main() {
- /* Print status of all environment variables. */
- printf("Current environment variables:\n");
- printenviron();
- /* Change one and add a new one. */
- _putenv("PATH=c:\\highc\\small");
- _putenv("NEWENVIRONVAR=SOME\\DIR\\SPECIFICATION");
- /* Print status again. */
- printf("New environment variables:\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 program checks the robustness of the rand() function. It calls
- rand() LOOPS times. The program computes the modulus MOD of the number
- returned from the rand() function and keeps a frequency count of
- numbers.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define LOOPS 1000
- #define MOD 10
-
- void main() {
- int i, k;
- int freq[MOD]; /* Used for occurrence frequency. */
-
- puts("Testing rand...\n");
-
- for (i=0; i < MOD; i++)
- freq[i] = 0;
-
- for (i = 0; i <LOOPS; i++) {
- k = rand() % MOD;
- freq[k]++;
- }
-
- for (i = 0; i < MOD; i++)
- printf("freq[%d] => %d\n",
- i, freq[i]);
- }
-
- /* End rand */
- #undef main
- #undef LOOPS
- #undef MOD
- /**** 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() {
- typedef long grade;
- grade *grade_ptr;
-
- puts("Testing realloc...\n");
- grade_ptr = (long *) calloc (1, sizeof(grade));
- *grade_ptr = 2;
- grade_ptr = NULL;
-
- if((grade_ptr = (long *)
- realloc(grade_ptr, sizeof(long))) == NULL)
- puts("Failed to reallocate a Null pointer.");
- else
- printf("Grade after reallocation of NULL is"
- " %d.\n", *grade_ptr);
- *grade_ptr = 3;
- printf("Grade is now %d.\n", *grade_ptr);
- }
-
- /* End realloc */
- #undef main
- /**** name=remove ****/
- #define main() TEST_remove()
-
- /*
- This program opens a temporary file and renames it to input.tmp. Later,
- it renames the file back to temporary file and removes it. This example
- illustrates the use of remove, tmpnam, and rename.
- */
- #include <stdio.h>
- #define FAILURE (-1)
-
- int main() {
- FILE *FP1;
- int flag;
- char tmp[L_tmpnam], new[L_tmpnam]="input.tmp";
- puts("Testing remove...\n");
- if ((FP1 = fopen(tmpnam(tmp), "w+")) == NULL) {
- perror("Cannot open temporary file.");
- return FAILURE;
- }
-
- else
- puts("Temporary file is now open.");
-
- flag = rename(tmp,new);
- if (!flag) /* Rename was successful. */
- printf("File renamed as ==>%s. \n",new);
- else
- printf("File is not renamed to %s.\n",new);
-
- flag = rename(new,tmp);
- fclose(FP1);
-
- if (remove(tmp))
- perror("Cannot remove temporary file.");
- else
- puts("Temporary file is now removed.");
- }
-
- /* End remove */
- #undef main
- #undef FAILURE
- /**** name=rename ****/
- #define main() TEST_rename()
- /* End rename */
- #undef main
- /**** name=rewind ****/
- #define main() TEST_rewind()
-
- /*
- The program below copies test.dat, a block at a time, to rewind1.tmp,
- then to rewind2.tmp.
- */
- #include <stdio.h>
- #define FAILURE (-1)
- #define BLOCKSIZE (512)
-
- int main() {
- FILE *FP1, *FP2, *FP3;
- char buf[BLOCKSIZE]; int i;
- FILE *open(char *f,char *mode) {
- FILE *FP;
- if ((FP = fopen(f,mode)) == NULL) perror(f);
- return FP;
- }
- if (NULL == (FP1 = open("test.dat","r")) ||
- NULL == (FP2 = open("rewind1.tmp","w")) ||
- NULL == (FP3 = open("rewind2.tmp","w"))
- ) return FAILURE;
- while ((i = fread(buf, 1, BLOCKSIZE, FP1)) != 0)
- fwrite(buf, 1, i, FP2);
- rewind(FP1);
- while ((i = fread(buf, 1, BLOCKSIZE, FP1)) != 0)
- fwrite(buf, 1, i, FP3);
- }
-
- /* End rewind */
- #undef main
- #undef FAILURE
- #undef BLOCKSIZE
- /**** 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));
-
- 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] = "abcdefghijklmnopqrstuvwxyz";
- char string2[50] = "abcdefghijklmnopqrstuvwxyz";
- char *source1 = string1, *dest1 = &string1[9];
- char *source2 = string2, *dest2 = &string2[9];
-
- _rstrcpy(dest1, source1);
- puts(string1);
- _fill_char(&string2[27], 23, '\0'); /* For puts. */
- strcpy(dest2, source2);
- puts(string2);
- }
-
- /* End _rstrcpy */
- #undef main
- /**** name=_rstrncpy ****/
- #define main() TEST__rstrncpy()
-
- #include <string.h>
- #include <stdio.h>
-
- void main() {
- char string1[50] = "abcdefghijklmnopqrstuvwxyz";
- char string2[50] = "abcdefghijklmnopqrstuvwxyz";
- char *source1 = string1, *dest1 = &string1[9];
- char *source2 = string2, *dest2 = &string2[9];
-
- _rstrncpy(dest1, source1, 14);
- puts(string1);
- strncpy(dest2, source2, 14);
- puts(string2);
- }
-
- /* End _rstrncpy */
- #undef main
-
- /*****names*****/
-
- char * names[]={
- "_offsetof",
- "_open",
- "_outpX",
- "perror",
- "printf",
- "_putch",
- "_putenv",
- "qsort",
- "rand",
- "_read",
- "realloc",
- "remove",
- "rewind",
- "_rmdir",
- "_rmemcpy",
- "_rotX",
- "_rstrcpy",
- "_rstrncpy",
- "",""};
- int nextfunum;
- void main() {
- char ans[90];
- for (;;) {
- for (int j=0;j< 18;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__putenv();break;
- case 8:TEST_qsort();break;
- case 9:TEST_rand();break;
- case 10:TEST__read();break;
- case 11:TEST_realloc();break;
- case 12:TEST_remove();break;
- case 13:TEST_rewind();break;
- case 14:TEST__rmdir();break;
- case 15:TEST__rmemcpy();break;
- case 16:TEST__rotX();break;
- case 17:TEST__rstrcpy();break;
- case 18: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);
- }
- }
-