home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / D4 < prev    next >
Encoding:
Text File  |  1992-04-14  |  11.4 KB  |  513 lines

  1. /*    Copyright (C) 1990-1992 MetaWare Incorporated; All Rights Reserved  */
  2.  
  3. /**** name=Xstrerror ****/
  4. #define main TEST_Xstrerror
  5.  
  6. /*
  7.    This program prints a few system error messages.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. void main() {
  15.    int i;
  16.  
  17.    /* Print error messages in strerror format. */
  18.    puts("A few error messages:\n");
  19.    for(i = 5; i <= 10; i++)
  20.        printf(strerror(i));
  21.  
  22.    /* Now use _strerror format. */
  23.    puts("\n\nA few more with a different format:\n");
  24.    for(i = 10; i <= 15; i++) {
  25.       errno = i;
  26.       printf(_strerror("This is an error message"));
  27.       }
  28.    }
  29. /* End *strerror  */
  30. #undef main
  31. /**** name=_tell ****/
  32. #define main TEST__tell
  33.  
  34. #include <io.h>
  35. #include <fcntl.h>
  36. #include <sys\stat.h>
  37. #include <stdio.h>
  38. #include <conio.h>
  39.  
  40. void main() {
  41.    int pan;
  42.    char c;
  43.  
  44.    pan = _open("test.dat", O_RDONLY | O_BINARY);
  45.    if (pan != -1) {
  46.       printf("File test.dat exists, handle = %d.\n",
  47.                                              pan);
  48.       while(_read(pan, &c, 1) > 0)
  49.          if (c == '=')
  50.             printf("Equal sign found at %ld.\n",
  51.                    _tell(pan));
  52.       _close(pan);
  53.       }
  54.    else
  55.       printf("File test.dat cannot be opened,"
  56.              " error code = %d.\n", pan);
  57.    }
  58. /* End _tell  */
  59. #undef main
  60. /**** name=time ****/
  61. #define main TEST_time
  62. /* End time  */
  63. #undef main
  64. /**** name=tmpnam ****/
  65. #define main TEST_tmpnam
  66. /* End tmpnam  */
  67. #undef main
  68. /**** name=tolower ****/
  69. #define main TEST_tolower
  70.  
  71. #include <stdio.h>
  72. #include <ctype.h>
  73.  
  74. void main() {
  75.    char s[45] = "teSTInG TolOWer  (#1 @2 !3 &4 {}[]).";
  76.    int i = 0;
  77.  
  78.    printf("Original string: %s\n"
  79.           "        Becomes: ",s);
  80.    while(s[i] != '\0') {
  81.       printf("%c", tolower(s[i]));
  82.       i++;
  83.       }
  84.    printf("\n");
  85.    }
  86. /* End tolower  */
  87. #undef main
  88. /**** name=_tolower ****/
  89. #define main TEST__tolower
  90.  
  91. /*
  92.    This program  shows  the  usage  of  the  _tolower function.  Notice the
  93.    isupper() test on the character.  You MUST  make  this  test  when using
  94.    _tolower.
  95. */
  96.  
  97. #include <stdio.h>
  98. #include <ctype.h>
  99.  
  100. void main() {
  101.    char s[45] =
  102.    "teSTInG _TolOWer  (#1 @2 !3 &4 {}[]).";
  103.    int i = 0;
  104.  
  105.    printf("Original string: %s\n"
  106.           "        Becomes: ",s);
  107.    while(s[i] != '\0') {
  108.       if(isupper(s[i]))
  109.          s[i] = _tolower(s[i]);
  110.       i++;
  111.       }
  112.    printf("%s\n", s);
  113.    }
  114. /* End _tolower  */
  115. #undef main
  116. /**** name=toupper ****/
  117. #define main TEST_toupper
  118.  
  119. #include <stdio.h>
  120. #include <ctype.h>
  121.  
  122. void main() {
  123.    char s[45] = "teSTInG TouPPEr  (#1 @2 !3 &4 {}[]).";
  124.    int i = 0;
  125.  
  126.    printf("Original string: %s\n"
  127.           "        Becomes: ",s);
  128.    while(s[i] != '\0') {
  129.       printf("%c", toupper(s[i]));
  130.       i++;
  131.       }
  132.    printf("\n");
  133.    }
  134. /* End toupper  */
  135. #undef main
  136. /**** name=_toupper ****/
  137. #define main TEST__toupper
  138.  
  139. /*
  140.    This program  shows  the  usage  of  the  _toupper function.  Notice the
  141.    islower() test on the character.  You MUST  make  this  test  when using
  142.    _toupper.
  143. */
  144. #include <stdio.h>
  145. #include <ctype.h>
  146.  
  147. void main() {
  148.    char s[45] = "teSTInG _TouPPEr  (#1 @2 !3 &4 {}[]).";
  149.    int i = 0;
  150.  
  151.    printf("Original string: %s\n"
  152.           "        Becomes: ",s);
  153.    while(s[i] != '\0') {
  154.       if(islower(s[i]))
  155.          s[i] = _toupper(s[i]);
  156.       i++;
  157.       }
  158.    printf("%s\n", s);
  159.    }
  160. /* End _toupper  */
  161. #undef main
  162. /**** name=_tzset ****/
  163. #define main TEST__tzset
  164.  
  165. #include <time.h>
  166. #include <stdio.h>
  167.  
  168. void main() {
  169.    printf("Before _tzset ---\n"
  170.           "tzname0:%s\n"
  171.           "tzname1:%s\n"
  172.           "timezone:%ld\n"
  173.           "daylight:%d\n",
  174.           _tzname[0],_tzname[1],_timezone,_daylight);
  175.    _tzset();
  176.    printf("After _tzset ---\n"
  177.           "tzname0:%s\n"
  178.           "tzname1:%s\n"
  179.           "timezone:%ld\n"
  180.           "daylight:%d\n",
  181.           _tzname[0],_tzname[1],_timezone,_daylight);
  182.    }
  183.  
  184. /* End _tzset  */
  185. #undef main
  186. /**** name=_ultoa ****/
  187. #define main TEST__ultoa
  188.  
  189. #include <stdio.h>
  190. #include <string.h>
  191. #include <stdlib.h>
  192.  
  193. void main() {
  194.    char s[55], t[55];
  195.    unsigned long int j;
  196.  
  197.    for (j = 1; j != 0;) {
  198.       puts("Enter a number, (zero to quit)");
  199.       gets(s);
  200.       j = atol((char *)&s);
  201.       printf("The number you entered is: %ld.\n"
  202.              "And in base 10, 10, 2, 16 and 36"
  203.              " it is\n",j);
  204.  
  205.       puts(_ultoa(j, t, 10));
  206.       puts(t);
  207.       puts(_ultoa(j, t, 2));
  208.       puts(_ultoa(j, t, 16));
  209.       puts(_ultoa(j, t, 36));
  210.       }
  211.    }
  212. /* End _ultoa  */
  213. #undef main
  214. /**** name=_umask ****/
  215. #define main TEST__umask
  216.  
  217. #include <types.h>
  218. #include <stat.h>
  219. #include <io.h>
  220. #include <fcntl.h>
  221. #include <stdio.h>
  222. #include <stdlib.h>
  223. #include <sys\types.h>
  224. #include <sys\stat.h>
  225.  
  226. void main() {
  227.    int oldmask, pan;
  228.    oldmask = _umask(S_IWRITE);
  229.  
  230.    pan = open("umasktst.tmp", O_CREAT | O_TRUNC |
  231.                      O_RDWR, S_IREAD | S_IWRITE);
  232.    _umask(oldmask);
  233.    if (pan < 0)
  234.       printf("Failed to create umasktst.tmp, "
  235.              "Error code is: %d\n", pan);
  236.    else {
  237.       int ok = write(pan, "this is a test", 15);
  238.       if (ok == -1) printf("write failed\n");
  239.       else {
  240.          close(pan);
  241.          pan = open("umasktst.tmp", O_RDWR);
  242.          if (pan < 0)
  243.             printf("Correctly failed to re-open "
  244.                    "umasktst.tmp\n"
  245.                    "Error code is: %d\n"
  246.                    "errno code is: %d\n", pan, errno);
  247.          else {
  248.             ok = write(pan, "Yet another test", 16);
  249.             if (ok == -1)
  250.                printf("Cannot write to file, "
  251.                       "test is ok!!!");
  252.             }
  253.          }
  254.       }
  255.  
  256.    }
  257.  
  258. /* End _umask  */
  259. #undef main
  260. /**** name=ungetc ****/
  261. #define main TEST_ungetc
  262.  
  263. /*
  264.    This example gets a word from stdin, then uses ungetc to put it back.
  265. */
  266.  
  267. #include <stdio.h>
  268. #include <string.h>
  269.  
  270. void main() {
  271.    char str[128];
  272.    int c;
  273.  
  274.    puts("Please enter a word.");
  275.    gets(str);
  276.    puts(str);
  277.  
  278.    for(c = strlen(str); c >= 0; c--)
  279.       ungetc(str[c], stdin);
  280.  
  281.    puts("Please press return.");
  282.    gets(str);
  283.    puts(str);
  284.    }
  285. /* End ungetc  */
  286. #undef main
  287. /**** name=_ungetch ****/
  288. #define main TEST__ungetch
  289.  
  290. #include <conio.h>
  291. #include <stdio.h>
  292.  
  293. void main() {
  294.    int c;
  295.  
  296.    puts("Testing _ungetch...\n");
  297.    puts("Please press the comma key.");
  298.    _ungetch('A');
  299.    c = getche();
  300.    putch(c);
  301.    if (c != 'A')
  302.       puts("_ungetch/getch got wrong character.");
  303.    c = getche();
  304.    putch(c);
  305.    if (c != ',') puts("getch did not get comma.");
  306.    }
  307.  
  308. /* End _ungetch  */
  309. #undef main
  310. /**** name=_unlink ****/
  311. #define main TEST__unlink
  312.  
  313. /*
  314.    The program below opens a temporary file.  This example  illustrates the
  315.    use of _unlink and tmpnam.
  316. */
  317. #include <stdio.h>
  318.  
  319. void main() {
  320.    FILE *FP1;
  321.    char tmp[L_tmpnam];
  322.  
  323.    /*  tmpnam() will create the filename. */
  324.    if ((FP1 = fopen(tmpnam(tmp), "w+")) == NULL) {
  325.       perror("Cannot open temporary file.");
  326.       return;
  327.       }
  328.    else printf("\ntmpnam has created file %s.", tmp);
  329.    fclose(FP1);
  330.    if (_unlink(tmp))
  331.       perror("Cannot remove temporary file.");
  332.    else printf("\nunlink has removed file %s.", tmp);
  333.    }
  334. /* End _unlink  */
  335. #undef main
  336. /**** name=_utime ****/
  337. #define main TEST__utime
  338.  
  339. #include <fcntl.h>
  340. #include <io.h>
  341. #include <stat.h>
  342. #include <stdio.h>
  343. #include <utime.h>
  344.  
  345. void main() {
  346.    struct utimbuf tim;
  347.  
  348.    puts("Testing _utime...\n");
  349.    tim.modtime = 616444012;
  350.  
  351.    int pan = open("utime.tmp", O_CREAT | O_TRUNC
  352.                              | O_RDWR, S_IWRITE );
  353.    close(pan);
  354.    if (_utime("utime.tmp", &tim) != 0)
  355.       perror("Error in changing time for utime.tmp.\n");
  356.    else
  357.       printf("Successfully changed the time"
  358.              " for file utime.tmp.\n");
  359.    }
  360. /* End _utime  */
  361. #undef main
  362. /**** name=va_arg ****/
  363. #define main TEST_va_arg
  364.  
  365. #include <stdarg.h>
  366. #include <stdio.h>
  367. #define  MAXARGS 100
  368.  
  369. void out(int n_ptrs, char *strings[]) {
  370.    int i = 0;
  371.    if (n_ptrs < 0) return;
  372.    while (i < n_ptrs) printf("%s", strings[i++]);
  373.    }
  374.  
  375. void collect_args(int n_args, ...) {
  376.    va_list ap;
  377.    char *args[MAXARGS];
  378.    int ptr_no = 0;
  379.    if (n_args > MAXARGS) n_args = MAXARGS;
  380.    va_start(ap, n_args);
  381.    while (ptr_no < n_args)
  382.       args[ptr_no++] = va_arg(ap, char *);
  383.    va_end(ap);
  384.    out(n_args, args);
  385.    }
  386.  
  387. void main() {
  388.    collect_args(3, "This ", "strikes me as ",
  389.                    "a little weird.\n");
  390.    }
  391. /* End va_arg  */
  392. #undef main
  393. #undef   MAXARGS
  394. /**** name=va_end ****/
  395. #define main TEST_va_end
  396. /* End va_end  */
  397. #undef main
  398. /**** name=va_list ****/
  399. #define main TEST_va_list
  400. /* End va_list  */
  401. #undef main
  402. /**** name=va_start ****/
  403. #define main TEST_va_start
  404. /* End va_start  */
  405. #undef main
  406. /**** name=vfprintf ****/
  407. #define main TEST_vfprintf
  408.  
  409. #include <stdarg.h>
  410. #include <stdio.h>
  411. #define  MAXARGS 100
  412.  
  413. void collect_args2(int n_args, ...) {
  414.    va_list ap;
  415.    FILE *FP;
  416.    char *args[MAXARGS];
  417.    int ptr_no = 0;
  418.  
  419.    if ((FP = fopen("vfprint.tmp","w")) == NULL)
  420.       puts("Could not open file vfprint.tmp.");
  421.    if (n_args > MAXARGS)
  422.        n_args = MAXARGS;
  423.    va_start(ap, n_args);
  424.    while (ptr_no < n_args) {
  425.       vfprintf(FP,"%s\n", ap);
  426.       vprintf("%s\n", ap);
  427.       args[ptr_no++] = va_arg(ap, char *);
  428.       }
  429.    va_end(ap);
  430.    }
  431.  
  432. void main() {
  433.    collect_args2(3, "This ", "strikes me as ",
  434.                "a little weird.\n");
  435.    }
  436. /* End vfprintf  */
  437. #undef main
  438. #undef   MAXARGS
  439. /**** name=_write ****/
  440. #define main TEST__write
  441.  
  442. #include <io.h>
  443. void main() {
  444.    int pan = 1;
  445.    char msg[] = "This is a test of\n"
  446.                 "the _write function!\n";
  447.    char *c;
  448.    c = &msg[0];
  449.    while(*c != 0)
  450.       _write(pan, c++, 1);
  451.    c = &msg[0];
  452.    _write(pan, c, 37);
  453.    }
  454.  
  455. /* End _write  */
  456. #undef main
  457.  
  458. /*****names*****/
  459.  
  460. char * names[]={
  461.    "Xstrerror",
  462.    "_tell",
  463.    "tolower",
  464.    "_tolower",
  465.    "toupper",
  466.    "_toupper",
  467.    "_tzset",
  468.    "_ultoa",
  469.    "_umask",
  470.    "ungetc",
  471.    "_ungetch",
  472.    "_unlink",
  473.    "_utime",
  474.    "va_arg",
  475.    "vfprintf",
  476.    "_write",
  477.    "",""};
  478.    int nextfunum;
  479. void main() {
  480.    char ans[90];
  481.    for (;;) {
  482.       for (int j=0;j< 16;j++)
  483.       if (j%3==2) printf("%4d %-21s\n",j+1,names[j]);
  484.       else printf("%4d %-21s",j+1,names[j]);
  485.       printf("\n\nPlease enter a number from the above list (enter=%d, exit=0): ",++nextfunum);
  486.       gets(ans);
  487.       if (ans[0] != 0) nextfunum=atoi(ans);
  488.       printf("\n\n\n");
  489.       switch(nextfunum) {
  490.          case 0:exit(0);
  491.          case 1:TEST_Xstrerror();break;
  492.          case 2:TEST__tell();break;
  493.          case 3:TEST_tolower();break;
  494.          case 4:TEST__tolower();break;
  495.          case 5:TEST_toupper();break;
  496.          case 6:TEST__toupper();break;
  497.          case 7:TEST__tzset();break;
  498.          case 8:TEST__ultoa();break;
  499.          case 9:TEST__umask();break;
  500.          case 10:TEST_ungetc();break;
  501.          case 11:TEST__ungetch();break;
  502.          case 12:TEST__unlink();break;
  503.          case 13:TEST__utime();break;
  504.          case 14:TEST_va_arg();break;
  505.          case 15:TEST_vfprintf();break;
  506.          case 16:TEST__write();break;
  507.          default:printf("I don't recognize that answer\n");nextfunum=-1;break;
  508.          }
  509.       printf("\n\npress enter to select another function\n");
  510.       gets(ans);
  511.       }
  512.    }
  513.