home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / streams / handles / test2.c < prev   
Encoding:
C/C++ Source or Header  |  1992-04-01  |  5.6 KB  |  282 lines

  1. /*
  2.          test2.c
  3.  
  4.          Testing file for Turbo C and/or DJGPP
  5.  
  6.  
  7.          W. Metzenthen    17th feb 1992
  8.  
  9.          Modified for Turbo C 1.5,  2nd March 1992.
  10.          Modified for BCC 3.0 30th March 1992.
  11.  */
  12.  
  13.  
  14. #define    __BCC30__    0x400
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <dos.h>
  19. #include <string.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <sys/stat.h>
  23.  
  24.  
  25. static int    fd[500];
  26. static FILE    *stream[500];
  27. static int    FullDebug = 0;
  28.  
  29.  
  30. int measure_files(void);
  31. int measure_streams(void);
  32. void test_files(int n);
  33. void test_streams(int n);
  34.  
  35.  
  36. /*--- main() ----------------------------------------------------------------+
  37.  |                                                                           |
  38.  +---------------------------------------------------------------------------*/
  39. void main(int argc, char *argv[])
  40. {
  41. int    i, maxfd, maxstr;
  42.  
  43.  
  44.  
  45. #ifdef __TURBOC__
  46. /* This is also needed if #pragma startup is not used in close.c */
  47. #if __TURBOC__ <= 0x200
  48. increase_handles();
  49. #endif
  50. #endif
  51.  
  52. if ( argc > 1 )
  53.     {
  54.     for ( i = 1; i < argc; i++ )
  55.         {
  56.         if ( argv[i][0] == '-' )
  57.             {
  58.             switch ( argv[i][1] )
  59.                 {
  60.                 case 'd':
  61.                 case 'D':
  62.                 FullDebug = 1;
  63.                 break;
  64.  
  65.                 default:
  66.                 fprintf(stderr, "Unknown option\n");
  67.                 exit(1);
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.  
  74. if ( (i = open("test2.c", 0)) == -1 )
  75.     {
  76.     fprintf(stderr, 
  77. "ERROR: Can't find \"test2.c\".\n"\
  78. "       (\"test2.c\" must be in the current default directory)\n");
  79.     exit(1);
  80.     }
  81. close(i);
  82.  
  83. maxfd = measure_files();
  84.  
  85. maxstr = measure_streams();
  86.  
  87. #ifdef __TURBOC__
  88. test_files(maxfd);
  89. #if __TURBOC__ > __BCC30__
  90. test_streams(maxstr);
  91. #endif
  92. #else
  93. test_streams(maxstr);
  94. #endif
  95.  
  96. }
  97.  
  98.  
  99.  
  100. /*--- measure_files() -------------------------------------------------------+
  101.  |                                                                           |
  102.  +---------------------------------------------------------------------------*/
  103. int measure_files(void)
  104. {
  105. int    i, j, maxfd;
  106.  
  107.  
  108. for ( i = 0 ; i < 500 ; i++ )
  109.     {
  110.     fd[i] = open("test2.c", 0);
  111.     if ( FullDebug )
  112.         printf("fd[%d] = %d\n", i, fd[i]);
  113.     if ( fd[i] == -1 )
  114.         {
  115.         if ( !FullDebug )
  116.             printf("%d: fd = %d\n", i, fd[i]);
  117.         break;
  118.         }
  119.     }
  120.  
  121. maxfd = i;
  122. printf("%d file opens\n", maxfd);
  123.  
  124. /* Close all of the files */
  125. for ( j = i-1 ; j >= 0 ; j-- )
  126.     {
  127.     if ( FullDebug )
  128.         printf("closing %d\n", fd[j]);
  129.     if ( close(fd[j]) )
  130.         printf("close returned an error\n");
  131.     }
  132.  
  133. return maxfd;
  134.  
  135. }
  136.  
  137.  
  138.  
  139. /*--- measure_streams() -----------------------------------------------------+
  140.  |                                                                           |
  141.  +---------------------------------------------------------------------------*/
  142. int measure_streams(void)
  143. {
  144. int    i, maxstr;
  145.  
  146.  
  147. /* See how many streams we can open */
  148. for ( i = 0 ; i < 500 ; i++ )
  149.     {
  150.     if ( FullDebug )
  151.         {
  152.         printf("%d: ", i);
  153.         fflush(stdout);
  154.         }
  155.     stream[i] = fopen("test2.c", "rt");
  156.     if ( FullDebug )
  157.         {
  158.         printf("stream = %p", stream[i]);
  159.         fflush(stdout);
  160.         }
  161.     if ( stream[i] == NULL )
  162.         {
  163.         if ( FullDebug )
  164.             printf("\n");
  165.           else
  166.             printf("%d: stream = %p\n", i, stream[i]);
  167.         break;
  168.         }
  169.     if ( FullDebug )
  170.         {
  171.         printf(", fd = %d", fileno(stream[i]));
  172.         fflush(stdout);
  173.         }
  174.     if ( FullDebug )
  175.         {
  176.         printf(".\n");
  177.         fflush(stdout);
  178.         }
  179.     }
  180.  
  181. maxstr = i;
  182. printf("%d stream opens\n", maxstr);
  183.  
  184. for ( i = 0 ; i < maxstr ; i++ )
  185.     {
  186.     if ( fclose(stream[i]) )
  187.         {
  188.         fprintf(stderr, "stream close error on stream #%d\n", i);
  189.         printf("ERROR: errno = %d, stream #%d fclose()\n", errno, i);
  190.         exit(1);
  191.         }
  192.     }
  193.  
  194. return maxstr;
  195.  
  196. }
  197.  
  198.  
  199.  
  200. /*--- test_files() ----------------------------------------------------------+
  201.  |                                                                           |
  202.  +---------------------------------------------------------------------------*/
  203. void test_files(int n)
  204. {
  205. int    i;
  206. char    fname[20], string[80];
  207.  
  208.  
  209. for ( i = 0 ; i < n ; i++ )
  210.     {
  211.     sprintf(fname, "fd_%03d.$$$", i);
  212.     fd[i] = open(fname, O_TEXT|O_CREAT|O_RDWR, S_IWRITE);
  213.     if ( FullDebug )
  214.         printf("%d: fd = %d, file = \"%s\"\n", i, fd[i], fname);
  215.     if ( fd[i] == NULL )
  216.         {
  217.         if ( !FullDebug )
  218.             printf("%d: fd = %p\n", i, fd[i]);
  219.         break;
  220.         }
  221.     }
  222.  
  223. if ( i == n ) printf("test_files(): %d files created\n", i);
  224.  
  225. for ( i = 0 ; i < n ; i++ )
  226.     {
  227.     sprintf(string, "This is file \"fd_%03d.$$$\"", i);
  228.     write(fd[i], string, strlen(string));
  229.     if ( close(fd[i]) )
  230.         {
  231.         fprintf(stderr, "fd close error %d on fd #%d\n", errno, i);
  232.         exit(1);
  233.         }
  234.     }
  235.  
  236. }
  237.  
  238.  
  239.  
  240. /*--- test_streams() --------------------------------------------------------+
  241.  |                                                                           |
  242.  +---------------------------------------------------------------------------*/
  243. void test_streams(int n)
  244. {
  245. int    i, count;
  246. char    fname[20];
  247.  
  248.  
  249. for ( i = 0 ; i < n ; i++ )
  250.     {
  251.     sprintf(fname, "str_%03d.$$$", i);
  252.     stream[i] = fopen(fname, "wt");
  253.     if ( FullDebug )
  254.         printf("%d: stream = %p, file = \"%s\"", i, stream[i], fname);
  255.     if ( stream[i] == NULL )
  256.         {
  257.         if ( !FullDebug )
  258.             printf("%d: stream = %p\n", i, stream[i]);
  259.         break;
  260.         }
  261.     if ( FullDebug )
  262.         printf(", fd = %d\n", fileno(stream[i]));
  263.     }
  264.  
  265. if ( i == n ) printf("test_streams(): %d files created\n", i);
  266.  
  267. for ( i = 0 ; i < n ; i++ )
  268.     {
  269.     count = fprintf(stream[i],
  270.         "This is file \"str_%03d.$$$\", fn = %d, stream = %p",
  271.                 i, fileno(stream[i]), stream[i]);
  272.     if ( FullDebug )
  273.         printf("Wrote %d bytes to \"str_%03d.$$$\"\n", count, i);
  274.     if ( fclose(stream[i]) )
  275.         {
  276.         fprintf(stderr, "stream close error on stream #%d\n", i);
  277.         exit(1);
  278.         }
  279.     }
  280.  
  281. }
  282.