unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Eli Zaretskii <eliz@gnu.org>
Cc: sds@gnu.org, Vitalie Spinu <spinuvit@gmail.com>, emacs-devel@gnu.org
Subject: Re: feature request: view part of file
Date: Sat, 19 Jan 2013 02:18:56 -0800	[thread overview]
Message-ID: <50FA7310.5070809@cs.ucla.edu> (raw)
In-Reply-To: <83boclr7bu.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 957 bytes --]

On 01/18/2013 10:54 PM, Eli Zaretskii wrote:
> It's not a bug, it's a fundamental limitation of how Emacs accesses
> buffers.  Any valid buffer position _must_ fit into the width of an
> Emacs integer.

Sure, but Vitalie's not asking for large buffer positions,
just for large file offsets.  Emac's a bit squirrelly in this
area, as it represents large file offsets by using floating
point numbers, but if it's going to be squirrelly it should
be *consistently* squirrelly, so I installed the attached patch
into the trunk as bzr 111554.

If 64-bit EMACS_INT were the default Vitalie wouldn't have had this
problem.  We have a simple fix (change configure so that --with-wide-int
is the default, see Bug#8794) but this most likely would slow Emacs a bit.
Stefan was skeptical and asked for benchmarks, but maybe
I should boost the priority of this.  Or, more drastically,
maybe we should bite the bullet and add bignums (Bug#8611);
it can't be *that* hard.

[-- Attachment #2: float-offset.txt --]
[-- Type: text/plain, Size: 5678 bytes --]

=== modified file 'doc/lispref/ChangeLog'
--- doc/lispref/ChangeLog	2013-01-10 03:43:02 +0000
+++ doc/lispref/ChangeLog	2013-01-19 09:57:59 +0000
@@ -1,3 +1,9 @@
+2013-01-19  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Allow floating-point file offsets.
+	* files.texi (Reading from Files, Writing to Files):
+	Say that file offsets can be numbers, not just integers.
+
 2013-01-09  Glenn Morris  <rgm@gnu.org>
 
 	* commands.texi (Interactive Codes):

=== modified file 'doc/lispref/files.texi'
--- doc/lispref/files.texi	2013-01-04 02:42:08 +0000
+++ doc/lispref/files.texi	2013-01-19 09:57:59 +0000
@@ -533,9 +533,9 @@
 file name and its last save file modtime.  This feature is used by
 @code{find-file-noselect} and you probably should not use it yourself.
 
-If @var{beg} and @var{end} are non-@code{nil}, they should be integers
-specifying the portion of the file to insert.  In this case, @var{visit}
-must be @code{nil}.  For example,
+If @var{beg} and @var{end} are non-@code{nil}, they should be numbers
+that are byte offsets specifying the portion of the file to insert.
+In this case, @var{visit} must be @code{nil}.  For example,
 
 @example
 (insert-file-contents filename nil 0 500)
@@ -605,8 +605,8 @@
 this case.
 
 If @var{append} is non-@code{nil}, then the specified text is appended
-to the existing file contents (if any).  If @var{append} is an
-integer, @code{write-region} seeks to that byte offset from the start
+to the existing file contents (if any).  If @var{append} is a
+number, @code{write-region} seeks to that byte offset from the start
 of the file and writes the data from there.
 
 If @var{mustbenew} is non-@code{nil}, then @code{write-region} asks

=== modified file 'src/ChangeLog'
--- src/ChangeLog	2013-01-19 08:49:17 +0000
+++ src/ChangeLog	2013-01-19 09:57:59 +0000
@@ -1,3 +1,12 @@
+2013-01-19  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Allow floating-point file offsets.
+	Problem reported by Vitalie Spinu in
+	<http://lists.gnu.org/archive/html/emacs-devel/2013-01/msg00411.html>.
+	* fileio.c (emacs_lseek): Remove.
+	(file_offset): New function.
+	(Finsert_file_contents, Fwrite_region): Use it.
+
 2013-01-19  Chong Yidong  <cyd@gnu.org>
 
 	* emacs.c (Fkill_emacs): Set waiting_for_input to 0 to avoid

=== modified file 'src/fileio.c'
--- src/fileio.c	2013-01-19 04:44:34 +0000
+++ src/fileio.c	2013-01-19 09:57:59 +0000
@@ -3443,19 +3443,25 @@
   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.  */
+/* Return the file offset that VAL represents, checking for type
+   errors and overflow.  */
 static off_t
-emacs_lseek (int fd, EMACS_INT offset, int whence)
+file_offset (Lisp_Object val)
 {
-  /* Use "&" rather than "&&" to suppress a bogus GCC warning; see
-     <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43772>.  */
-  if (! ((offset >= TYPE_MINIMUM (off_t)) & (offset <= TYPE_MAXIMUM (off_t))))
+  if (RANGED_INTEGERP (0, val, TYPE_MAXIMUM (off_t)))
+    return XINT (val);
+
+  if (FLOATP (val))
     {
-      errno = EINVAL;
-      return -1;
+      double v = XFLOAT_DATA (val);
+      if (0 <= v
+	  && (sizeof (off_t) < sizeof v
+	      ? v <= TYPE_MAXIMUM (off_t)
+	      : v < TYPE_MAXIMUM (off_t)))
+	return v;
     }
-  return lseek (fd, offset, whence);
+
+  wrong_type_argument (intern ("file-offset"), val);
 }
 
 /* Return a special time value indicating the error number ERRNUM.  */
@@ -3606,20 +3612,12 @@
     }
 
   if (!NILP (beg))
-    {
-      if (! RANGED_INTEGERP (0, beg, TYPE_MAXIMUM (off_t)))
-	wrong_type_argument (intern ("file-offset"), beg);
-      beg_offset = XFASTINT (beg);
-    }
+    beg_offset = file_offset (beg);
   else
     beg_offset = 0;
 
   if (!NILP (end))
-    {
-      if (! RANGED_INTEGERP (0, end, TYPE_MAXIMUM (off_t)))
-	wrong_type_argument (intern ("file-offset"), end);
-      end_offset = XFASTINT (end);
-    }
+    end_offset = file_offset (end);
   else
     {
       if (not_regular)
@@ -4714,7 +4712,7 @@
 instead of any buffer contents; END is ignored.
 
 Optional fourth argument APPEND if non-nil means
-  append to existing file contents (if any).  If it is an integer,
+  append to existing file contents (if any).  If it is a number,
   seek to that offset in the file before writing.
 Optional fifth argument VISIT, if t or a string, means
   set the last-save-file-modtime of buffer to this file's modtime
@@ -4743,6 +4741,7 @@
   (Lisp_Object start, Lisp_Object end, Lisp_Object filename, Lisp_Object append, Lisp_Object visit, Lisp_Object lockname, Lisp_Object mustbenew)
 {
   int desc;
+  off_t offset;
   bool ok;
   int save_errno = 0;
   const char *fn;
@@ -4864,13 +4863,14 @@
   encoded_filename = ENCODE_FILE (filename);
 
   fn = SSDATA (encoded_filename);
+  offset = 0;
   desc = -1;
   if (!NILP (append))
-#ifdef DOS_NT
-    desc = emacs_open (fn, O_WRONLY | O_BINARY, 0);
-#else  /* not DOS_NT */
-    desc = emacs_open (fn, O_WRONLY, 0);
-#endif /* not DOS_NT */
+    {
+      if (NUMBERP (append))
+	offset = file_offset (append);
+      desc = emacs_open (fn, O_WRONLY | O_BINARY, 0);
+    }
 
   if (desc < 0 && (NILP (append) || errno == ENOENT))
 #ifdef DOS_NT
@@ -4897,14 +4897,9 @@
 
   record_unwind_protect (close_file_unwind, make_number (desc));
 
-  if (!NILP (append) && !NILP (Ffile_regular_p (filename)))
+  if (!NILP (append))
     {
-      off_t ret;
-
-      if (NUMBERP (append))
-	ret = emacs_lseek (desc, XINT (append), SEEK_CUR);
-      else
-	ret = lseek (desc, 0, SEEK_END);
+      off_t ret = lseek (desc, offset, NUMBERP (append) ? SEEK_SET : SEEK_END);
       if (ret < 0)
 	{
 #ifdef CLASH_DETECTION


  reply	other threads:[~2013-01-19 10:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-13 20:52 feature request: view part of file Sam Steingold
2012-06-13 23:16 ` Stefan Monnier
2012-06-14 16:20   ` Sam Steingold
2012-06-14 16:53     ` Mathias Dahl
2012-06-14 17:32     ` Stephen J. Turnbull
2012-06-14 18:21       ` Paul Eggert
     [not found]     ` <jwvd3517qww.fsf-monnier+emacs@gnu.org>
     [not found]       ` <CABrcCQ5zDfB2tw9DRrwpCZmDqHPc+BB6W5w9ULNU95e_v4yyJw@mail.gmail.com>
     [not found]         ` <87395xu768.fsf@gnu.org>
     [not found]           ` <CABrcCQ6rpG9qhsCO+ZEpTiNqbQRtg-PBeb=q_B5F8YgrGxoWKA@mail.gmail.com>
     [not found]             ` <jwvvcit67sc.fsf-monnier+emacs@gnu.org>
2012-06-14 19:34               ` Sam Steingold
2012-06-14 21:29 ` Sam Steingold
2012-06-18 20:34   ` Štěpán Němec
2012-07-19 17:58     ` Samuel Bronson
2012-07-19 19:38       ` Stephen J. Turnbull
2012-08-04 11:58   ` Andrey Kotlarski
2013-01-18 23:30   ` Vitalie Spinu
2013-01-18 23:52     ` Vitalie Spinu
2013-01-19  6:54       ` Eli Zaretskii
2013-01-19 10:18         ` Paul Eggert [this message]
2013-01-19 10:51           ` Eli Zaretskii
2013-01-19 12:47             ` Paul Eggert
2013-01-19 13:47               ` Eli Zaretskii
2013-01-19 19:00                 ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=50FA7310.5070809@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=sds@gnu.org \
    --cc=spinuvit@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).