🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #299

Re: Can one simulate ftruncate() and fchmod() ?

#299
From: William Kucharski
Date: Wed, 08 Apr 1992 22:41
72 lines
2741 bytes
While reading article <1992Apr03.091904.19206@Logix.DE>, I noticed that
jpm@Logix.DE (Jan-Piet Mens) said the following:
 >The subject actually says it all. I need to simulate the BSD
 >calls ftruncate() and fchmod() in SYS5. I'm all out of ideas.
 >Can someone help ?

As far as ftruncate() goes, one from the vaults:

If you have the F_FREESP fcntl on your system (most releases of SVR3.2 do),
you can use the undocumented (in SVR3) F_FREESP command to write a version
of ftruncate():

==================================[ Cut Here ]==================================

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int
ftruncate(fd, length)
int     fd;     /* file descriptor */
off_t   length; /* length to set file to */
{
        extern  long    lseek();

        struct  flock   fl;
        struct  stat    filebuf;

        if (fstat(fd, &filebuf) < 0)
                return(-1);

        if (filebuf.st_size < length) {
                /* extend file length */

                if ((lseek(fd, (length - 1), SEEK_SET)) < 0)
                        return(-1);

                /* write a "0" byte */

                if ((write(fd, "", 1)) != 1)
                        return(-1);
        } else {
                /* truncate length */

                fl.l_whence = 0;
                fl.l_len = 0;
                fl.l_start = length;
                fl.l_type = F_WRLCK;    /* write lock on file space */

                /*
                 * This relies on the UNDOCUMENTED F_FREESP argument to
                 * fcntl(2), which truncates the file so that it ends at the
                 * position indicated by fl.l_start.
                 *
                 * Will minor miracles never cease?
                 */

		if (fcntl(fd, F_FREESP, &fl) < 0)
			return(-1);

        }

        return(0);
}
==================================[ Cut Here ]==================================
-- 
| William Kucharski, Solbourne Computer, Inc.     | Opinions expressed above
| Internet: kucharsk@solbourne.com   Ham: N0OKQ   | are MINE alone, not those
| Snail Mail: 1900 Pike Road, Longmont, CO  80501 | of Solbourne Computer, Inc.
| President, "Just the Ten of Us" Fan Club        | "It's Night 9 with D2 Dave!"
Technical Details
Message-ID: <1992Apr8.224105.220@solbourne.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!stan!kucharsk
References: <1992Apr03.091904.19206@Logix.DE>

Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer,comp.lang.c,comp.unix.questions
Path: arc1-ca.pugleaf.net!sparky!uunet!stan!kucharsk
From: kucharsk@solbourne.com (William Kucharski)
Subject: Re: Can one simulate ftruncate() and fchmod() ?
Message-ID: <1992Apr8.224105.220@solbourne.com>
Organization: Solbourne Computer, Inc., Longmont, CO
References: <1992Apr03.091904.19206@Logix.DE>
Date: Wed, 8 Apr 1992 22:41:05 GMT