import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / truncate.c
blobd3db2cb28ecd6862dc38f47c1618a53008ca0b86
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 * ftruncate() and truncate() set a file to a specified
34 * length using fcntl(F_FREESP) system call. If the file
35 * was previously longer than length, the bytes past the
36 * length will no longer be accessible. If it was shorter,
37 * bytes not written will be zero filled.
40 #include <sys/feature_tests.h>
42 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
43 #pragma weak _ftruncate64 = ftruncate64
44 #pragma weak _truncate64 = truncate64
45 #define ftruncate ftruncate64
46 #define truncate truncate64
47 #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
49 #include "lint.h"
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <fcntl.h>
53 #include <pthread.h>
54 #include <sys/types.h>
56 int
57 ftruncate(int fildes, off_t len)
59 struct flock lck;
61 lck.l_whence = 0; /* offset l_start from beginning of file */
62 lck.l_start = len;
63 lck.l_type = F_WRLCK; /* setting a write lock */
64 lck.l_len = (off_t)0; /* until the end of the file address space */
66 if (fcntl(fildes, F_FREESP, &lck) == -1) {
67 return (-1);
69 return (0);
72 int
73 truncate(const char *path, off_t len)
76 int rval = 0;
77 int cancel_state;
78 int fd;
81 * truncate() is not a cancellation point,
82 * even though it calls open() and close().
84 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
85 if ((fd = open(path, O_WRONLY)) == -1 || ftruncate(fd, len) == -1)
86 rval = -1;
87 if (fd >= 0)
88 (void) close(fd);
89 (void) pthread_setcancelstate(cancel_state, NULL);
90 return (rval);