2 Truncate a file to zero length. */
28 /* External functions. */
30 extern off_t lseek ();
33 /* Truncate a file to zero length. If this fails, it closes and
34 removes the file. We support a number of different means of
35 truncation, which is probably a waste of time since this function
36 is currently only called when the 'f' protocol resends a file. */
43 #if ! HAVE_FTRUNCATE && ! HAVE_LTRUNC
45 #define HAVE_F_CHSIZE 1
46 #else /* ! defined (F_CHSIZE) */
48 #define HAVE_F_FREESP 1
49 #endif /* defined (F_FREESP) */
50 #endif /* ! defined (F_CHSIZE) */
51 #endif /* ! HAVE_FTRUNCATE && ! HAVE_LTRUNC */
54 esysdep_truncate (e, zname)
60 #if HAVE_FTRUNCATE || HAVE_LTRUNC || HAVE_F_CHSIZE || HAVE_F_FREESP
63 if (! ffilerewind (e))
65 ulog (LOG_ERROR, "rewind: %s", strerror (errno));
66 (void) ffileclose (e);
67 (void) remove (zname);
78 itrunc = ftruncate (o, 0);
81 itrunc = ltrunc (o, (long) 0, SEEK_SET);
84 itrunc = fcntl (o, F_CHSIZE, (off_t) 0);
87 /* This selection is based on an implementation of ftruncate by
88 kucharsk@Solbourne.com (William Kucharski). */
97 itrunc = fcntl (o, F_FREESP, &fl);
104 ulog (LOG_ERROR, "ftruncate: %s", strerror (errno));
107 ulog (LOG_ERROR, "ltrunc: %s", strerror (errno));
110 ulog (LOG_ERROR, "fcntl (F_CHSIZE): %s", strerror (errno));
113 ulog (LOG_ERROR, "fcntl (F_FREESP): %s", strerror (errno));
116 (void) ffileclose (e);
117 (void) remove (zname);
122 #else /* ! (HAVE_FTRUNCATE || HAVE_LTRUNC || HAVE_F_CHSIZE || HAVE_F_FREESP) */
123 (void) ffileclose (e);
124 (void) remove (zname);
126 o = creat ((char *) zname, IPRIVATE_FILE_MODE);
130 ulog (LOG_ERROR, "creat (%s): %s", zname, strerror (errno));
134 if (fcntl (o, F_SETFD, fcntl (o, F_GETFD, 0) | FD_CLOEXEC) < 0)
136 ulog (LOG_ERROR, "fcntl (FD_CLOEXEC): %s", strerror (errno));
142 e = fdopen (o, (char *) BINWRITE);
146 ulog (LOG_ERROR, "fdopen (%s): %s", zname, strerror (errno));
148 (void) remove (zname);
151 #else /* ! USE_STDIO */
153 #endif /* ! USE_STDIO */
156 #endif /* ! (HAVE_FTRUNCATE || HAVE_LTRUNC || HAVE_F_CHSIZE || HAVE_F_FREESP) */