home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FCHSIZE.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  110 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** Routines for truncating DOS files associated by streams.
  5. **
  6. ** Author   : Eric Coolman, Simple Minded Software
  7. **            modified for SNIPPETS by Bob Stout
  8. **
  9. ** Released to public domain on June 23, 1994.  USE AT OWN RISK.
  10. **
  11. ** Notes:     fChSize works the same with FILE pointers as CHSIZE() does for
  12. **            file handles.
  13. **
  14. **            All routines return the same values as CHSIZE().
  15. */
  16.  
  17.  
  18. #include <stdio.h>                  /* fileno(), ftell(), FILE *        */
  19. #include <io.h>                     /* chsize()                         */
  20. #include "errors.h"                 /* cant()                           */
  21.  
  22. #if defined(__ZTC__) && !defined(__SC__)
  23.  #include "sniptype.h"              /* Error_, Success_                 */
  24.  
  25.  int chsize(int fd, long posn)
  26.  {
  27.        char dummy;
  28.  
  29.        if (-1L == lseek(fd, posn, SEEK_SET))
  30.              return Error_;
  31.        if (-1 == write(fd, &dummy, 0))
  32.              return Error_;
  33.        else    return Success_;
  34.  }
  35. #endif
  36.  
  37. /*
  38. ** Expand or reduce the size of a file
  39. */
  40.  
  41. int fChSize(FILE *stream, long size)
  42. {
  43.       return(chsize(fileno(stream), size));
  44. }
  45.  
  46. /*
  47. ** Truncates the file at the current offset of the FILE pointer
  48. */
  49.  
  50. int fTrunc(FILE *stream)
  51. {
  52.       return(fChSize(stream, ftell(stream)));
  53. }
  54.  
  55. /*
  56. ** Clears contents of stream (zero bytes it)
  57. */
  58.  
  59. int fStub(FILE *stream)
  60. {
  61.       return fChSize(stream, 0L);
  62. }
  63.  
  64.  
  65. #ifdef TEST
  66.  
  67. #include <stdlib.h>     /* atol()         */
  68. #include <ctype.h>      /* toupper()      */
  69.  
  70. void usage(void)
  71. {
  72.       puts("Usage: FCHSIZE { C | S } filename [new_length]\n");
  73.       puts("Where: C  means change the file size, "
  74.            "new size given by new_length");
  75.       puts("       S  means truncate the file to zero bytes");
  76.       exit(EXIT_FAILURE);
  77. }
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81.       FILE *testfile;
  82.       long posn;
  83.  
  84.       if (argc < 3)
  85.             usage();
  86.  
  87.       testfile = cant(argv[2], "r+b");
  88.  
  89.       switch (toupper(argv[1][0]))
  90.       {
  91.       case 'C':
  92.             posn    = atol(argv[3]);
  93.             printf("fChSize(%s, %ld) returned %d\n", argv[1], posn,
  94.                    fChSize(testfile, posn));
  95.             break;
  96.  
  97.       case 'S':
  98.             printf("fStub(%s) returned %d\n", argv[1], fStub(testfile));
  99.             break;
  100.  
  101.       default:
  102.             usage();
  103.       }
  104.  
  105.       fclose(testfile);
  106.       return EXIT_SUCCESS;
  107. }
  108.  
  109. #endif /* TEST */
  110.