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);
63 return seg_format(fd, nrows, ncols, srows, scols, len, 1);
100 return seg_format(fd, nrows, ncols, srows, scols, len, 0);
103 static int seg_format(
int fd, off_t nrows, off_t ncols,
int srows,
int scols,
109 if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
110 G_warning(
"Segment_format(fd,%" PRId64
",%" PRId64
111 ",%d,%d,%d): illegal value(s)",
112 nrows, ncols, srows, scols, len);
120 size = srows * scols * len;
122 if (
sizeof(off_t) == 4 &&
sizeof(
double) >= 8) {
127 d_size = (double)spr * ((nrows + srows - 1) / srows);
132 d_size += 2 *
sizeof(off_t) + 3 *
sizeof(
int);
134 o_size = (off_t)d_size;
138 if ((
double)o_size != d_size) {
139 G_warning(
_(
"Segment format: file size too large"));
140 G_warning(
_(
"Please recompile with Large File Support (LFS)"));
145 if (lseek(fd, 0L, SEEK_SET) == (off_t)-1) {
148 G_warning(
"Segment_format(): Unable to seek (%s)", strerror(
err));
152 if (!write_off_t(fd, nrows) || !write_off_t(fd, ncols) ||
153 !write_int(fd, srows) || !write_int(fd, scols) || !write_int(fd, len))
157 nbytes = spr * ((nrows + srows - 1) / srows);
162 if (seek_only(fd, nbytes) < 0)
173 if (zero_fill(fd, nbytes) < 0)
179 static int write_int(
int fd,
int n)
182 if (
write(fd, &n,
sizeof(
int)) !=
sizeof(
int)) {
186 G_warning(
"Segment format: Unable to write (%s)", strerror(
err));
189 "Segment format: Unable to write (insufficient disk space?)");
196 static int write_off_t(
int fd, off_t n)
199 if (
write(fd, &n,
sizeof(off_t)) !=
sizeof(off_t)) {
203 G_warning(
"Segment format: Unable to write (%s)", strerror(
err));
206 "Segment format: Unable to write (insufficient disk space?)");
213 static int zero_fill(
int fd, off_t nbytes)
221 n = nbytes > (int)
sizeof(buf) ? (int)
sizeof(buf) : nbytes;
227 n = nbytes > (int)
sizeof(buf) ? (int)
sizeof(buf) : nbytes;
229 if (
write(fd, buf, n) != n) {
233 G_warning(
"segment zero_fill(): Unable to write (%s)",
236 G_warning(
"segment zero_fill(): Unable to write (insufficient "
244 return seek_only(fd, nbytes);
248 static int seek_only(
int fd, off_t nbytes)
257 static const char buf[10];
259 G_debug(3,
"Using new segmentation code...");
261 if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
264 G_warning(
"segment zero_fill(): Unable to seek (%s)", strerror(
err));
268 if (
write(fd, buf, 1) != 1) {
272 G_warning(
"segment zero_fill(): Unable to write (%s)",
275 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)