home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 1.ddi / MWHC.001 / 9 < prev    next >
Encoding:
Text File  |  1992-03-02  |  9.9 KB  |  445 lines

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