home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved */
-
- /**** 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 = 5; i <= 10; i++)
- printf(strerror(i));
-
- /* Now use _strerror format. */
- puts("\n\nA few more with a different format:\n");
- for(i = 10; i <= 15; i++) {
- errno = i;
- printf(_strerror("This is an error message"));
- }
- }
- /* End *strerror */
- #undef main
- /**** name=_tell ****/
- #define main TEST__tell
-
- #include <io.h>
- #include <fcntl.h>
- #include <sys\stat.h>
- #include <stdio.h>
- #include <conio.h>
-
- void main() {
- int pan;
- char c;
-
- 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)
- if (c == '=')
- printf("Equal sign found at %ld.\n",
- _tell(pan));
- _close(pan);
- }
- else
- printf("File test.dat cannot be opened,"
- " error code = %d.\n", pan);
- }
- /* End _tell */
- #undef main
- /**** name=time ****/
- #define main TEST_time
- /* End time */
- #undef main
- /**** name=tmpnam ****/
- #define main TEST_tmpnam
- /* End tmpnam */
- #undef main
- /**** name=tolower ****/
- #define main TEST_tolower
-
- #include <stdio.h>
- #include <ctype.h>
-
- void main() {
- char s[45] = "teSTInG TolOWer (#1 @2 !3 &4 {}[]).";
- int i = 0;
-
- printf("Original string: %s\n"
- " Becomes: ",s);
- while(s[i] != '\0') {
- printf("%c", tolower(s[i]));
- i++;
- }
- printf("\n");
- }
- /* End tolower */
- #undef main
- /**** name=_tolower ****/
- #define main TEST__tolower
-
- /*
- This program shows the usage of the _tolower function. Notice the
- isupper() test on the character. You MUST make this test when using
- _tolower.
- */
-
- #include <stdio.h>
- #include <ctype.h>
-
- void main() {
- char s[45] =
- "teSTInG _TolOWer (#1 @2 !3 &4 {}[]).";
- int i = 0;
-
- printf("Original string: %s\n"
- " Becomes: ",s);
- while(s[i] != '\0') {
- if(isupper(s[i]))
- s[i] = _tolower(s[i]);
- i++;
- }
- printf("%s\n", s);
- }
- /* End _tolower */
- #undef main
- /**** name=toupper ****/
- #define main TEST_toupper
-
- #include <stdio.h>
- #include <ctype.h>
-
- void main() {
- char s[45] = "teSTInG TouPPEr (#1 @2 !3 &4 {}[]).";
- int i = 0;
-
- printf("Original string: %s\n"
- " Becomes: ",s);
- while(s[i] != '\0') {
- printf("%c", toupper(s[i]));
- i++;
- }
- printf("\n");
- }
- /* End toupper */
- #undef main
- /**** name=_toupper ****/
- #define main TEST__toupper
-
- /*
- This program shows the usage of the _toupper function. Notice the
- islower() test on the character. You MUST make this test when using
- _toupper.
- */
- #include <stdio.h>
- #include <ctype.h>
-
- void main() {
- char s[45] = "teSTInG _TouPPEr (#1 @2 !3 &4 {}[]).";
- int i = 0;
-
- printf("Original string: %s\n"
- " Becomes: ",s);
- while(s[i] != '\0') {
- if(islower(s[i]))
- s[i] = _toupper(s[i]);
- i++;
- }
- printf("%s\n", s);
- }
- /* End _toupper */
- #undef main
- /**** name=_tzset ****/
- #define main TEST__tzset
-
- #include <time.h>
- #include <stdio.h>
-
- void main() {
- printf("Before _tzset ---\n"
- "tzname0:%s\n"
- "tzname1:%s\n"
- "timezone:%ld\n"
- "daylight:%d\n",
- _tzname[0],_tzname[1],_timezone,_daylight);
- _tzset();
- printf("After _tzset ---\n"
- "tzname0:%s\n"
- "tzname1:%s\n"
- "timezone:%ld\n"
- "daylight:%d\n",
- _tzname[0],_tzname[1],_timezone,_daylight);
- }
-
- /* End _tzset */
- #undef main
- /**** name=_ultoa ****/
- #define main TEST__ultoa
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- void main() {
- char s[55], t[55];
- unsigned long int j;
-
- for (j = 1; j != 0;) {
- puts("Enter a number, (zero to quit)");
- gets(s);
- j = atol((char *)&s);
- printf("The number you entered is: %ld.\n"
- "And in base 10, 10, 2, 16 and 36"
- " it is\n",j);
-
- puts(_ultoa(j, t, 10));
- puts(t);
- puts(_ultoa(j, t, 2));
- puts(_ultoa(j, t, 16));
- puts(_ultoa(j, t, 36));
- }
- }
- /* End _ultoa */
- #undef main
- /**** name=_umask ****/
- #define main TEST__umask
-
- #include <types.h>
- #include <stat.h>
- #include <io.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys\types.h>
- #include <sys\stat.h>
-
- void main() {
- int oldmask, pan;
- oldmask = _umask(S_IWRITE);
-
- pan = open("umasktst.tmp", O_CREAT | O_TRUNC |
- O_RDWR, S_IREAD | S_IWRITE);
- _umask(oldmask);
- if (pan < 0)
- printf("Failed to create umasktst.tmp, "
- "Error code is: %d\n", pan);
- else {
- int ok = write(pan, "this is a test", 15);
- if (ok == -1) printf("write failed\n");
- else {
- close(pan);
- pan = open("umasktst.tmp", O_RDWR);
- if (pan < 0)
- printf("Correctly failed to re-open "
- "umasktst.tmp\n"
- "Error code is: %d\n"
- "errno code is: %d\n", pan, errno);
- else {
- ok = write(pan, "Yet another test", 16);
- if (ok == -1)
- printf("Cannot write to file, "
- "test is ok!!!");
- }
- }
- }
-
- }
-
- /* End _umask */
- #undef main
- /**** name=ungetc ****/
- #define main TEST_ungetc
-
- /*
- This example gets a word from stdin, then uses ungetc to put it back.
- */
-
- #include <stdio.h>
- #include <string.h>
-
- void main() {
- char str[128];
- int c;
-
- puts("Please enter a word.");
- gets(str);
- puts(str);
-
- for(c = strlen(str); c >= 0; c--)
- ungetc(str[c], stdin);
-
- puts("Please press return.");
- gets(str);
- puts(str);
- }
- /* End ungetc */
- #undef main
- /**** name=_ungetch ****/
- #define main TEST__ungetch
-
- #include <conio.h>
- #include <stdio.h>
-
- void main() {
- int c;
-
- puts("Testing _ungetch...\n");
- puts("Please press the comma key.");
- _ungetch('A');
- c = getche();
- putch(c);
- if (c != 'A')
- puts("_ungetch/getch got wrong character.");
- c = getche();
- putch(c);
- if (c != ',') puts("getch did not get comma.");
- }
-
- /* End _ungetch */
- #undef main
- /**** name=_unlink ****/
- #define main TEST__unlink
-
- /*
- The program below opens a temporary file. This example illustrates the
- use of _unlink and tmpnam.
- */
- #include <stdio.h>
-
- void main() {
- FILE *FP1;
- char tmp[L_tmpnam];
-
- /* tmpnam() will create the filename. */
- if ((FP1 = fopen(tmpnam(tmp), "w+")) == NULL) {
- perror("Cannot open temporary file.");
- return;
- }
- else printf("\ntmpnam has created file %s.", tmp);
- fclose(FP1);
- if (_unlink(tmp))
- perror("Cannot remove temporary file.");
- else printf("\nunlink has removed file %s.", tmp);
- }
- /* End _unlink */
- #undef main
- /**** name=_utime ****/
- #define main TEST__utime
-
- #include <fcntl.h>
- #include <io.h>
- #include <stat.h>
- #include <stdio.h>
- #include <utime.h>
-
- void main() {
- struct utimbuf tim;
-
- puts("Testing _utime...\n");
- tim.modtime = 616444012;
-
- int pan = open("utime.tmp", O_CREAT | O_TRUNC
- | O_RDWR, S_IWRITE );
- close(pan);
- if (_utime("utime.tmp", &tim) != 0)
- perror("Error in changing time for utime.tmp.\n");
- else
- printf("Successfully changed the time"
- " for file utime.tmp.\n");
- }
- /* End _utime */
- #undef main
- /**** name=va_arg ****/
- #define main TEST_va_arg
-
- #include <stdarg.h>
- #include <stdio.h>
- #define MAXARGS 100
-
- void out(int n_ptrs, char *strings[]) {
- int i = 0;
- if (n_ptrs < 0) return;
- while (i < n_ptrs) printf("%s", strings[i++]);
- }
-
- void collect_args(int n_args, ...) {
- va_list ap;
- char *args[MAXARGS];
- int ptr_no = 0;
- if (n_args > MAXARGS) n_args = MAXARGS;
- va_start(ap, n_args);
- while (ptr_no < n_args)
- args[ptr_no++] = va_arg(ap, char *);
- va_end(ap);
- out(n_args, args);
- }
-
- void main() {
- collect_args(3, "This ", "strikes me as ",
- "a little weird.\n");
- }
- /* End va_arg */
- #undef main
- #undef MAXARGS
- /**** name=va_end ****/
- #define main TEST_va_end
- /* End va_end */
- #undef main
- /**** name=va_list ****/
- #define main TEST_va_list
- /* End va_list */
- #undef main
- /**** name=va_start ****/
- #define main TEST_va_start
- /* End va_start */
- #undef main
- /**** name=vfprintf ****/
- #define main TEST_vfprintf
-
- #include <stdarg.h>
- #include <stdio.h>
- #define MAXARGS 100
-
- void collect_args2(int n_args, ...) {
- va_list ap;
- FILE *FP;
- char *args[MAXARGS];
- int ptr_no = 0;
-
- if ((FP = fopen("vfprint.tmp","w")) == NULL)
- puts("Could not open file vfprint.tmp.");
- if (n_args > MAXARGS)
- n_args = MAXARGS;
- va_start(ap, n_args);
- while (ptr_no < n_args) {
- vfprintf(FP,"%s\n", ap);
- vprintf("%s\n", ap);
- args[ptr_no++] = va_arg(ap, char *);
- }
- va_end(ap);
- }
-
- void main() {
- collect_args2(3, "This ", "strikes me as ",
- "a little weird.\n");
- }
- /* End vfprintf */
- #undef main
- #undef MAXARGS
- /**** name=_write ****/
- #define main TEST__write
-
- #include <io.h>
- void main() {
- int pan = 1;
- char msg[] = "This is a test of\n"
- "the _write function!\n";
- char *c;
- c = &msg[0];
- while(*c != 0)
- _write(pan, c++, 1);
- c = &msg[0];
- _write(pan, c, 37);
- }
-
- /* End _write */
- #undef main
-
- /*****names*****/
-
- char * names[]={
- "Xstrerror",
- "_tell",
- "tolower",
- "_tolower",
- "toupper",
- "_toupper",
- "_tzset",
- "_ultoa",
- "_umask",
- "ungetc",
- "_ungetch",
- "_unlink",
- "_utime",
- "va_arg",
- "vfprintf",
- "_write",
- "",""};
- int nextfunum;
- void main() {
- char ans[90];
- for (;;) {
- for (int j=0;j< 16;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_Xstrerror();break;
- case 2:TEST__tell();break;
- case 3:TEST_tolower();break;
- case 4:TEST__tolower();break;
- case 5:TEST_toupper();break;
- case 6:TEST__toupper();break;
- case 7:TEST__tzset();break;
- case 8:TEST__ultoa();break;
- case 9:TEST__umask();break;
- case 10:TEST_ungetc();break;
- case 11:TEST__ungetch();break;
- case 12:TEST__unlink();break;
- case 13:TEST__utime();break;
- case 14:TEST_va_arg();break;
- case 15:TEST_vfprintf();break;
- case 16:TEST__write();break;
- default:printf("I don't recognize that answer\n");nextfunum=-1;break;
- }
- printf("\n\npress enter to select another function\n");
- gets(ans);
- }
- }
-