home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / nullfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-28  |  1.8 KB  |  66 lines

  1. /* NULLFILE.C illustrates these functions:
  2.  *      chsize          umask           setmode
  3.  *      creat           fstat
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <io.h>
  9. #include <fcntl.h>
  10. #include <sys\types.h>
  11. #include <sys\stat.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14.  
  15. main()
  16. {
  17.     int fhandle;
  18.     long fsize;
  19.     struct stat fstatus;
  20.     char fname[80];
  21.  
  22.     /* Create a file of a specified length. */
  23.     printf( "What dummy file do you want to create: " );
  24.     gets( fname );
  25.     if( !access( fname, 0 ) )
  26.     {
  27.         printf( "File exists" );
  28.         exit( 1 );
  29.     }
  30.  
  31.     /* Mask out write permission. This means that a later call to open
  32.      * will not be able to set write permission. This is not particularly
  33.      * useful in DOS, but umask is provided primarily for compatibility
  34.      * with systems (such as Unix) that allow multiple permission levels.
  35.      */
  36.     umask( S_IWRITE );
  37.  
  38.     /* Despite write request, file is read only because of mask. */
  39.     if( (fhandle = creat( fname, S_IREAD | S_IWRITE )) == -1 )
  40.     {
  41.         printf( "File can't be created" );
  42.         exit( 1 );
  43.     }
  44.  
  45.     /* Since creat uses the default mode (usually text), you must
  46.      * use setmode to make sure the mode is binary.
  47.      */
  48.     setmode( fhandle, O_BINARY );
  49.  
  50.     printf( "How long do you want the file to be? " );
  51.     scanf( "%ld", &fsize );
  52.     chsize( fhandle, fsize );
  53.  
  54.     /* Display statistics. */
  55.     fstat( fhandle, &fstatus );
  56.     printf( "File: %s\n", fname );
  57.     printf( "Size: %ld\n", fstatus.st_size );
  58.     printf( "Drive %c:\n", fstatus.st_dev + 'A' );
  59.     printf( "Permission: %s\n",
  60.             (fstatus.st_mode & S_IWRITE) ? "Read/Write" : "Read Only" );
  61.     printf( "Created: %s", ctime( &fstatus.st_atime ) );
  62.  
  63.     close( fhandle );
  64.     exit( 0 );
  65. }
  66.