home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1990 MetaWare Incorporated; All Rights Reserved. 90-Aug-07 */
-
- /**** 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));
- }
- 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()
-
- /*
- This program shows the difference between tolower and _tolower
- functions.
- */
- #include <stdio.h>
- #include <conio.h>
- #include <ctype.h>
-
- void main() {
- char s1[45] = "teSTInG tolower (#1 @2 !3 &4).";
- char s2[45] = "TEstINg _tolower (#1 @2 !3 &4).";
- int c, i;
-
- c = s1[0]; i=0;
- while (s1[i]) {
- printf("%c",c=tolower(c));
- c = s1[++i];
- }
- printf("\n");
- c = s2[0]; i=0;
- while (s2[i]) {
- if (isupper(c)) printf("%c",c=_tolower(c));
- else printf("%c",c);
- c = s2[++i];
- }
- printf("\n");
- }
-
- /* End tolower */
- #undef main
- /**** name=_tolower ****/
- #define main() TEST__tolower()
- /* End _tolower */
- #undef main
- /**** name=toupper ****/
- #define main() TEST_toupper()
- /* End toupper */
- #undef main
- /**** name=_toupper ****/
- #define main() TEST__toupper()
- /* 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 <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_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()
-
- /*
- The program below copies test.dat to ungetc.tmp, removing occurrences of
- "ab".
- */
-
- #include <stdio.h>
- #define FAILURE (-1)
-
- int main() {
- FILE *FP1, *FP2;
- int c;
-
- puts("Testing ungetc...\n");
- FILE *open(char *f, char *mode) {
- FILE *FP;
- if ((FP = fopen(f,mode)) == NULL) perror(f);
- return FP;
- }
- if(((FP1 = open("test.dat","r")) == NULL) ||
- ((FP2 = open("ungetc.tmp","w")) == NULL))
- return FAILURE;
- else {
- puts("File ungetc.tmp is open.");
- printf("Copying test.dat to ungetc.tmp"
- " removing 'a', 'b'.");
- }
- while (!feof(FP1)) {
- if ((c = fgetc(FP1)) != 'a')
- putc(c, FP2);
- else /* c == 'a' */
- if ((c = fgetc(FP1)) != 'b') {
- ungetc(c, FP1);
- putc('a', FP2);
- }
- }
- }
-
- /* End ungetc */
- #undef main
- #undef FAILURE
- /**** 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];
-
- if ((FP1 = fopen(tmpnam(tmp), "w+")) == NULL) {
- perror("Cannot open temporary file");
- return;
- }
- fclose(FP1);
- if (_unlink(tmp))
- perror("Cannot remove temporary file");
- }
-
- /* End _unlink */
- #undef main
- /**** name=_utime ****/
- #define main() TEST__utime()
-
- #include <utime.h>
- #include <stdio.h>
-
- void main() {
- struct utimbuf tim;
-
- 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;
-
- if ((FP = fopen("vfprint.tmp","w")) == NULL)
- puts("Could not open file vfprint.tmp.");
- char *args[MAXARGS];
- int ptr_no = 0;
- 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() {
- puts("Testing vfprintf...\n");
- 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
- /**** name=_yX ****/
- #define main() TEST__yX()
- /* End _y* */
- #undef main
-
- /*****names*****/
-
- char * names[]={
- "_tell",
- "tolower",
- "_tzset",
- "_ultoa",
- "_umask",
- "ungetc",
- "_ungetch",
- "_unlink",
- "_utime",
- "va_arg",
- "vfprintf",
- "_write",
- "",""};
- int nextfunum;
- void main() {
- char ans[90];
- for (;;) {
- for (int j=0;j< 12;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__tell();break;
- case 2:TEST_tolower();break;
- case 3:TEST__tzset();break;
- case 4:TEST__ultoa();break;
- case 5:TEST__umask();break;
- case 6:TEST_ungetc();break;
- case 7:TEST__ungetch();break;
- case 8:TEST__unlink();break;
- case 9:TEST__utime();break;
- case 10:TEST_va_arg();break;
- case 11:TEST_vfprintf();break;
- case 12: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);
- }
- }
-