home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / sysv386 / 13338 < prev    next >
Encoding:
Text File  |  1992-08-19  |  2.1 KB  |  101 lines

  1. Path: sparky!uunet!ogicse!das-news.harvard.edu!spdcc!dirtydog.ima.isc.com!newsserver.pixel.kodak.com!laidbak!stevea
  2. From: stevea@i88.isc.com (Steve Alexander)
  3. Newsgroups: comp.unix.sysv386
  4. Subject: Re: BSD compatibility libraries/modules for System V (3.2)
  5. Message-ID: <1992Aug19.130604.15586@i88.isc.com>
  6. Date: 19 Aug 92 13:06:04 GMT
  7. Article-I.D.: i88.1992Aug19.130604.15586
  8. References: <1992Aug19.003841.18005@mintaka.lcs.mit.edu>
  9. Sender: usenet@i88.isc.com (Usenet News)
  10. Organization: INTERACTIVE Systems Corporation, Naperville, IL
  11. Lines: 87
  12. Nntp-Posting-Host: ozzy.i88.isc.com
  13.  
  14. In article <1992Aug19.003841.18005@mintaka.lcs.mit.edu> feoh@hal.gnu.ai.mit.edu (Chris Patti) writes:
  15. >    I'm currently doing a port from BSD 4.3 to SysV 3.2 and I've run up 
  16. >against a brick wall: ftruncate().. The code I use desperately needs this call
  17. >and there's no easy SysV r3.2 equivalent.
  18.  
  19. [ This should probably be in the FAQ :-]
  20.  
  21. /*
  22.  * This works only on System V 3.1 and greater, and relies on an
  23.  * undocumented fcntl.  Use at your own risk.
  24.  */
  25.  
  26. #include <sys/types.h>
  27. #include <errno.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30.  
  31. extern void exit();
  32.  
  33. main(argc, argv)
  34.     int    argc;
  35.     char    **argv;
  36. {
  37.     int        f, i, r;
  38.     long        trunclen, maxlen;
  39.     char        buf[1024];
  40.  
  41.     if (argc < 4)
  42.         error("usage: f file maxlen trunclen");
  43.  
  44.     maxlen = atol(argv[2]);    
  45.     maxlen /= sizeof(buf);
  46.  
  47.     trunclen = atol(argv[3]);    
  48.  
  49.     f = open(argv[1], O_RDWR|O_CREAT, 0644);
  50.     if (f < 0)
  51.         error("open");
  52.  
  53.     /* write maxlen * 1024 bytes */
  54.  
  55.     for (i = 0; i < maxlen; i++)
  56.         if (write(f, buf, sizeof(buf)) < 0)
  57.             error("write");
  58.  
  59.     /* truncate to trunclen */
  60.     r = ftruncate(f, trunclen);
  61.     if (r < 0)
  62.         error("ftruncate");
  63.     
  64.     r = close(f);
  65.     if (r < 0)
  66.         error("close");
  67.  
  68.     exit(0);
  69. /*NOTREACHED*/
  70. }
  71.  
  72. int
  73. ftruncate(f, l)
  74.     int    f;
  75.     long    l;
  76. {
  77.     struct    flock    fl;
  78.  
  79.     fl.l_whence = 0;
  80.     fl.l_len = 0;
  81.     fl.l_start = l;
  82.     fl.l_type = F_WRLCK;
  83.  
  84.     return fcntl(f, F_FREESP, &fl);
  85.  
  86. }
  87.  
  88. error(s)
  89.     char    *s;
  90. {
  91.     extern void perror();
  92.     if (errno)
  93.         perror(s);
  94.     else
  95.         (void)fprintf(stderr,"%s\n",s);
  96.     exit(1);
  97. }
  98. -- 
  99. Steve Alexander, Software Technologies Group    | stevea@i88.isc.com
  100. INTERACTIVE Systems Corporation, Naperville, IL | ...!{sun,ico}!laidbak!stevea
  101.