22 #include "local_proto.h"
24 static int seg_format(
int, off_t, off_t,
int,
int,
int,
int);
25 static int write_int(
int,
int);
26 static int write_off_t(
int, off_t);
27 static int zero_fill(
int, off_t);
28 static int seek_only(
int, off_t);
64 return seg_format(fd, nrows, ncols, srows, scols, len, 1);
102 return seg_format(fd, nrows, ncols, srows, scols, len, 0);
105 static int seg_format(
int fd, off_t nrows, off_t ncols,
int srows,
int scols,
111 if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
112 G_warning(
"Segment_format(fd,%" PRId64
",%" PRId64
113 ",%d,%d,%d): illegal value(s)",
114 nrows, ncols, srows, scols, len);
122 size = srows * scols * len;
124 if (
sizeof(off_t) == 4 &&
sizeof(
double) >= 8) {
129 d_size = (double)spr * ((nrows + srows - 1) / srows);
134 d_size += 2 *
sizeof(off_t) + 3 *
sizeof(
int);
136 o_size = (off_t)d_size;
140 if ((
double)o_size != d_size) {
141 G_warning(
_(
"Segment format: file size too large"));
142 G_warning(
_(
"Please recompile with Large File Support (LFS)"));
147 if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
150 G_warning(
"Segment_format(): Unable to seek (%s)", strerror(
err));
154 if (!write_off_t(fd, nrows) || !write_off_t(fd, ncols) ||
155 !write_int(fd, srows) || !write_int(fd, scols) || !write_int(fd, len))
159 nbytes = spr * ((nrows + srows - 1) / srows);
164 if (seek_only(fd, nbytes) < 0)
175 if (zero_fill(fd, nbytes) < 0)
181 static int write_int(
int fd,
int n)
184 if (write(fd, &n,
sizeof(
int)) !=
sizeof(
int)) {
188 G_warning(
"Segment format: Unable to write (%s)", strerror(
err));
191 "Segment format: Unable to write (insufficient disk space?)");
198 static int write_off_t(
int fd, off_t n)
201 if (write(fd, &n,
sizeof(off_t)) !=
sizeof(off_t)) {
205 G_warning(
"Segment format: Unable to write (%s)", strerror(
err));
208 "Segment format: Unable to write (insufficient disk space?)");
215 static int zero_fill(
int fd, off_t nbytes)
223 n = nbytes > (int)
sizeof(buf) ? (int)
sizeof(buf) : nbytes;
229 n = nbytes > (int)
sizeof(buf) ? (int)
sizeof(buf) : nbytes;
231 if (write(fd, buf, n) != n) {
235 G_warning(
"segment zero_fill(): Unable to write (%s)",
238 G_warning(
"segment zero_fill(): Unable to write (insufficient "
246 return seek_only(fd, nbytes);
250 static int seek_only(
int fd, off_t nbytes)
259 static const char buf[10];
261 G_debug(3,
"Using new segmentation code...");
263 if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
266 G_warning(
"segment zero_fill(): Unable to seek (%s)", strerror(
err));
270 if (write(fd, buf, 1) != 1) {
274 G_warning(
"segment zero_fill(): Unable to write (%s)",
277 G_warning(
"segment zero_fill(): Unable to write (insufficient disk "
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)