xref: /onnv-gate/usr/src/lib/libc/port/gen/truncate.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55891Sraf  * Common Development and Distribution License (the "License").
65891Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215891Sraf 
220Sstevel@tonic-gate /*
235891Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * ftruncate() and truncate() set a file to a specified
340Sstevel@tonic-gate  * length using fcntl(F_FREESP) system call. If the file
350Sstevel@tonic-gate  * was previously longer than length, the bytes past the
360Sstevel@tonic-gate  * length will no longer be accessible. If it was shorter,
370Sstevel@tonic-gate  * bytes not written will be zero filled.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <sys/feature_tests.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
43*6812Sraf #pragma weak _ftruncate64 = ftruncate64
44*6812Sraf #pragma weak _truncate64 = truncate64
45*6812Sraf #define	ftruncate	ftruncate64
46*6812Sraf #define	truncate	truncate64
470Sstevel@tonic-gate #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
480Sstevel@tonic-gate 
49*6812Sraf #include "lint.h"
500Sstevel@tonic-gate #include <unistd.h>
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <fcntl.h>
535891Sraf #include <pthread.h>
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate int
ftruncate(int fildes,off_t len)57*6812Sraf ftruncate(int fildes, off_t len)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	struct flock lck;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	lck.l_whence = 0;	/* offset l_start from beginning of file */
620Sstevel@tonic-gate 	lck.l_start = len;
630Sstevel@tonic-gate 	lck.l_type = F_WRLCK;	/* setting a write lock */
640Sstevel@tonic-gate 	lck.l_len = (off_t)0;	/* until the end of the file address space */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (fcntl(fildes, F_FREESP, &lck) == -1) {
670Sstevel@tonic-gate 		return (-1);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	return (0);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int
truncate(const char * path,off_t len)73*6812Sraf truncate(const char *path, off_t len)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 
765891Sraf 	int rval = 0;
775891Sraf 	int cancel_state;
780Sstevel@tonic-gate 	int fd;
790Sstevel@tonic-gate 
805891Sraf 	/*
815891Sraf 	 * truncate() is not a cancellation point,
825891Sraf 	 * even though it calls open() and close().
835891Sraf 	 */
845891Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
855891Sraf 	if ((fd = open(path, O_WRONLY)) == -1 || ftruncate(fd, len) == -1)
865891Sraf 		rval = -1;
875891Sraf 	if (fd >= 0)
880Sstevel@tonic-gate 		(void) close(fd);
895891Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
905891Sraf 	return (rval);
910Sstevel@tonic-gate }
92