]> git.eshelyaron.com Git - emacs.git/commitdiff
insert-file-contents refactor xlseek
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 20 Jul 2025 21:52:46 +0000 (14:52 -0700)
committerEshel Yaron <me@eshelyaron.com>
Fri, 25 Jul 2025 08:11:49 +0000 (10:11 +0200)
* src/fileio.c (xlseek): New arg WHENCE.  All uses changed.
This generalizes xlseek and should simplify future changes.

(cherry picked from commit adbae4f92f91350028ff9f604c1e045c171c9cbb)

src/fileio.c

index 8f25facdcae2e12226e3f6a1fbce8953d7f075b7..3f41e70e66d9be83df8df63deacc77ccae4bf678 100644 (file)
@@ -4038,14 +4038,15 @@ maybe_move_gap (struct buffer *b)
     }
 }
 
-/* In FD, position to POS.  Return POS if successful, otherwise signal
-   an error with FILENAME.  */
+/* In FD, position to POS relative to WHENCE.  Return the resulting
+   position if successful, otherwise signal an error with FILENAME.  */
 static off_t
-xlseek (emacs_fd fd, off_t pos, Lisp_Object filename)
+xlseek (emacs_fd fd, off_t pos, int whence, Lisp_Object filename)
 {
-  if (emacs_fd_lseek (fd, pos, SEEK_SET) < 0)
+  off_t newpos = emacs_fd_lseek (fd, pos, whence);
+  if (newpos < 0)
     report_file_error ("Setting file position", filename);
-  return pos;
+  return newpos;
 }
 
 /* FIXME: insert-file-contents should be split with the top-level moved to
@@ -4448,7 +4449,7 @@ by calling `format-decode', which see.  */)
       bool giveup_match_end = false;
 
       if (beg_offset != curpos)
-       curpos = xlseek (fd, beg_offset, orig_filename);
+       curpos = xlseek (fd, beg_offset, SEEK_SET, orig_filename);
 
       /* Count how many chars at the start of the file
         match the text at the beginning of the buffer.  */
@@ -4559,7 +4560,7 @@ by calling `format-decode', which see.  */)
          if (trial == 0)
            break;
 
-         curpos = xlseek (fd, curpos - trial, orig_filename);
+         curpos = xlseek (fd, -trial, SEEK_CUR, orig_filename);
 
          nread = emacs_full_read (fd, read_buf, trial);
          curpos += nread;
@@ -4680,7 +4681,7 @@ by calling `format-decode', which see.  */)
         CONVERSION_BUFFER.  */
 
       if (beg_offset != curpos)
-       curpos = xlseek (fd, beg_offset, orig_filename);
+       curpos = xlseek (fd, beg_offset, SEEK_SET, orig_filename);
 
       inserted = 0;            /* Bytes put into CONVERSION_BUFFER so far.  */
       unprocessed = 0;         /* Bytes not processed in previous loop.  */
@@ -4866,7 +4867,7 @@ by calling `format-decode', which see.  */)
     }
 
   if (beg_offset != curpos)
-    xlseek (fd, beg_offset, orig_filename);
+    xlseek (fd, beg_offset, SEEK_SET, orig_filename);
   /* curpos effectively goes out of scope now, as it is no longer needed,
      so not bother to update curpos from now on.  */