home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / trunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-22  |  1.2 KB  |  71 lines

  1. /* trunc.c (emx+gcc) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <getopt.h>
  6. #include <fcntl.h>
  7. #include <io.h>
  8.  
  9. #define FALSE 0
  10. #define TRUE  1
  11.  
  12.  
  13. static void usage (void)
  14. {
  15.   puts ("Usage: trunc [-c] <file> <size>");
  16.   exit (1);
  17. }
  18.  
  19.  
  20. int main (int argc, char *argv[])
  21. {
  22.   char *p;
  23.   int c, n, opt_c, handle;
  24.  
  25.   opt_c = FALSE;
  26.   opterr = FALSE;
  27.   while ((c = getopt (argc, argv, "c")) != EOF)
  28.     switch (c)
  29.       {
  30.       case 'c':
  31.         opt_c = TRUE;
  32.         break;
  33.       default:
  34.         usage ();
  35.       }
  36.   if (argc - optind != 2)
  37.     usage ();
  38.   errno = 0;
  39.   n = strtol (argv[optind+1], &p, 10);
  40.   if (n < 0 || errno != 0 || *p != 0)
  41.     usage ();
  42.   if (opt_c)
  43.     {
  44.       handle = open (argv[optind], O_RDWR);
  45.       if (handle == -1)
  46.         {
  47.           perror ("open");
  48.           return (2);
  49.         }
  50.       else if (chsize (handle, n) != 0)
  51.         {
  52.           perror ("chsize");
  53.           return (2);
  54.         }
  55.       else if (close (handle) != 0)
  56.         {
  57.           perror ("close");
  58.           return (2);
  59.         }
  60.     }
  61.   else
  62.     {
  63.       if (truncate (argv[optind], n) != 0)
  64.         {
  65.           perror ("truncate");
  66.           return (2);
  67.         }
  68.     }
  69.   return (0);
  70. }
  71.