* fileio.c: Don't assume EMACS_INT fits in off_t.
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Jun 2011 01:07:35 +0000 (18:07 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Jun 2011 01:07:35 +0000 (18:07 -0700)
(emacs_lseek): New static function.
(Finsert_file_contents, Fwrite_region): Use it.
Use SEEK_SET, SEEK_CUR, SEEK_END as appropriate.

src/ChangeLog
src/fileio.c

index 35dab79..e477162 100644 (file)
@@ -1,3 +1,10 @@
+2011-06-15  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * fileio.c: Don't assume EMACS_INT fits in off_t.
+       (emacs_lseek): New static function.
+       (Finsert_file_contents, Fwrite_region): Use it.
+       Use SEEK_SET, SEEK_CUR, SEEK_END as appropriate.
+
 2011-06-14  Paul Eggert  <eggert@cs.ucla.edu>
 
        * fns.c (Fload_average): Don't assume 100 * load average fits in int.
index 9a08e11..2e0b383 100644 (file)
@@ -3109,6 +3109,19 @@ read_non_regular_quit (Lisp_Object ignore)
   return Qnil;
 }
 
+/* Reposition FD to OFFSET, based on WHENCE.  This acts like lseek
+   except that it also tests for OFFSET being out of lseek's range.  */
+static off_t
+emacs_lseek (int fd, EMACS_INT offset, int whence)
+{
+  if (! (TYPE_MINIMUM (off_t) <= offset && offset <= TYPE_MAXIMUM (off_t)))
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return lseek (fd, offset, whence);
+}
+
 
 DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
        1, 5, 0,
@@ -3317,7 +3330,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
                  nread = emacs_read (fd, read_buf, 1024);
                  if (nread >= 0)
                    {
-                     if (lseek (fd, st.st_size - (1024 * 3), 0) < 0)
+                     if (lseek (fd, st.st_size - (1024 * 3), SEEK_SET) < 0)
                        report_file_error ("Setting file position",
                                           Fcons (orig_filename, Qnil));
                      nread += emacs_read (fd, read_buf + nread, 1024 * 3);
@@ -3361,7 +3374,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
                  specpdl_ptr--;
 
                  /* Rewind the file for the actual read done later.  */
-                 if (lseek (fd, 0, 0) < 0)
+                 if (lseek (fd, 0, SEEK_SET) < 0)
                    report_file_error ("Setting file position",
                                       Fcons (orig_filename, Qnil));
                }
@@ -3428,7 +3441,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
       if (XINT (beg) != 0)
        {
-         if (lseek (fd, XINT (beg), 0) < 0)
+         if (emacs_lseek (fd, XINT (beg), SEEK_SET) < 0)
            report_file_error ("Setting file position",
                               Fcons (orig_filename, Qnil));
        }
@@ -3500,7 +3513,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
            break;
          /* How much can we scan in the next step?  */
          trial = min (curpos, sizeof buffer);
-         if (lseek (fd, curpos - trial, 0) < 0)
+         if (emacs_lseek (fd, curpos - trial, SEEK_SET) < 0)
            report_file_error ("Setting file position",
                               Fcons (orig_filename, Qnil));
 
@@ -3618,7 +3631,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       /* First read the whole file, performing code conversion into
         CONVERSION_BUFFER.  */
 
-      if (lseek (fd, XINT (beg), 0) < 0)
+      if (emacs_lseek (fd, XINT (beg), SEEK_SET) < 0)
        report_file_error ("Setting file position",
                           Fcons (orig_filename, Qnil));
 
@@ -3817,7 +3830,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
   if (XINT (beg) != 0 || !NILP (replace))
     {
-      if (lseek (fd, XINT (beg), 0) < 0)
+      if (emacs_lseek (fd, XINT (beg), SEEK_SET) < 0)
        report_file_error ("Setting file position",
                           Fcons (orig_filename, Qnil));
     }
@@ -4549,9 +4562,9 @@ This calls `write-region-annotate-functions' at the start, and
       long ret;
 
       if (NUMBERP (append))
-       ret = lseek (desc, XINT (append), 1);
+       ret = emacs_lseek (desc, XINT (append), SEEK_CUR);
       else
-       ret = lseek (desc, 0, 2);
+       ret = lseek (desc, 0, SEEK_END);
       if (ret < 0)
        {
 #ifdef CLASH_DETECTION