*
* $Id$
* Derived from:
- * $FreeBSD: src/lib/libutil/flopen.c,v 1.4 2007/05/10 15:01:42 des Exp $
+ * $FreeBSD: src/lib/libutil/flopen.c,v 1.5 2007/05/23 08:12:34 des Exp $
*/
#include <sys/file.h>
int
flopen(const char *path, int flags, ...)
{
- int fd, operation, serrno;
+ int fd, operation, serrno, truncate;
struct stat sb, fsb;
mode_t mode;
if (flags & O_NONBLOCK)
operation |= LOCK_NB;
+ truncate = (flags & O_TRUNC);
+ flags |= ~O_TRUNC;
+
for (;;) {
if ((fd = open(path, flags, mode)) == -1)
/* non-existent or no access */
close(fd);
continue;
}
+ if (truncate && ftruncate(fd, 0) != 0) {
+ /* can't happen [tm] */
+ serrno = errno;
+ close(fd);
+ errno = serrno;
+ return (-1);
+ }
return (fd);
}
}