home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!das-news.harvard.edu!spdcc!dirtydog.ima.isc.com!newsserver.pixel.kodak.com!laidbak!stevea
- From: stevea@i88.isc.com (Steve Alexander)
- Newsgroups: comp.unix.sysv386
- Subject: Re: BSD compatibility libraries/modules for System V (3.2)
- Message-ID: <1992Aug19.130604.15586@i88.isc.com>
- Date: 19 Aug 92 13:06:04 GMT
- Article-I.D.: i88.1992Aug19.130604.15586
- References: <1992Aug19.003841.18005@mintaka.lcs.mit.edu>
- Sender: usenet@i88.isc.com (Usenet News)
- Organization: INTERACTIVE Systems Corporation, Naperville, IL
- Lines: 87
- Nntp-Posting-Host: ozzy.i88.isc.com
-
- In article <1992Aug19.003841.18005@mintaka.lcs.mit.edu> feoh@hal.gnu.ai.mit.edu (Chris Patti) writes:
- > I'm currently doing a port from BSD 4.3 to SysV 3.2 and I've run up
- >against a brick wall: ftruncate().. The code I use desperately needs this call
- >and there's no easy SysV r3.2 equivalent.
-
- [ This should probably be in the FAQ :-]
-
- /*
- * This works only on System V 3.1 and greater, and relies on an
- * undocumented fcntl. Use at your own risk.
- */
-
- #include <sys/types.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
-
- extern void exit();
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int f, i, r;
- long trunclen, maxlen;
- char buf[1024];
-
- if (argc < 4)
- error("usage: f file maxlen trunclen");
-
- maxlen = atol(argv[2]);
- maxlen /= sizeof(buf);
-
- trunclen = atol(argv[3]);
-
- f = open(argv[1], O_RDWR|O_CREAT, 0644);
- if (f < 0)
- error("open");
-
- /* write maxlen * 1024 bytes */
-
- for (i = 0; i < maxlen; i++)
- if (write(f, buf, sizeof(buf)) < 0)
- error("write");
-
- /* truncate to trunclen */
- r = ftruncate(f, trunclen);
- if (r < 0)
- error("ftruncate");
-
- r = close(f);
- if (r < 0)
- error("close");
-
- exit(0);
- /*NOTREACHED*/
- }
-
- int
- ftruncate(f, l)
- int f;
- long l;
- {
- struct flock fl;
-
- fl.l_whence = 0;
- fl.l_len = 0;
- fl.l_start = l;
- fl.l_type = F_WRLCK;
-
- return fcntl(f, F_FREESP, &fl);
-
- }
-
- error(s)
- char *s;
- {
- extern void perror();
- if (errno)
- perror(s);
- else
- (void)fprintf(stderr,"%s\n",s);
- exit(1);
- }
- --
- Steve Alexander, Software Technologies Group | stevea@i88.isc.com
- INTERACTIVE Systems Corporation, Naperville, IL | ...!{sun,ico}!laidbak!stevea
-