* src/font.c (font_find_for_lface) [HAVE_NS]: Ignore case.
[bpt/emacs.git] / src / fileio.c
index 4a4935b..5f7a8ad 100644 (file)
@@ -26,18 +26,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <setjmp.h>
 #include <unistd.h>
 
-#if !defined (S_ISLNK) && defined (S_IFLNK)
-#  define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
-#endif
-
-#if !defined (S_ISFIFO) && defined (S_IFIFO)
-#  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
-#endif
-
-#if !defined (S_ISREG) && defined (S_IFREG)
-#  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
-#endif
-
 #ifdef HAVE_PWD_H
 #include <pwd.h>
 #endif
@@ -74,7 +62,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #ifdef DOS_NT
 /* On Windows, drive letters must be alphabetic - on DOS, the Netware
-   redirector allows the six letters between 'Z' and 'a' as well. */
+   redirector allows the six letters between 'Z' and 'a' as well.  */
 #ifdef MSDOS
 #define IS_DRIVE(x) ((x) >= 'A' && (x) <= 'z')
 #endif
@@ -82,7 +70,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #define IS_DRIVE(x) isalpha ((unsigned char) (x))
 #endif
 /* Need to lower-case the drive letter, or else expanded
-   filenames will sometimes compare inequal, because
+   filenames will sometimes compare unequal, because
    `expand-file-name' doesn't always down-case the drive letter.  */
 #define DRIVE_LETTER(x) (tolower ((unsigned char) (x)))
 #endif
@@ -95,73 +83,72 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "commands.h"
 
-#ifndef S_ISLNK
-#  define lstat stat
-#endif
-
 #ifndef FILE_SYSTEM_CASE
 #define FILE_SYSTEM_CASE(filename)  (filename)
 #endif
 
 /* Nonzero during writing of auto-save files */
-int auto_saving;
+static int auto_saving;
 
 /* Set by auto_save_1 to mode of original file so Fwrite_region will create
    a new file with the same mode as the original */
-int auto_save_mode_bits;
+static int auto_save_mode_bits;
 
 /* Set by auto_save_1 if an error occurred during the last auto-save. */
-int auto_save_error_occurred;
+static int auto_save_error_occurred;
 
 /* The symbol bound to coding-system-for-read when
    insert-file-contents is called for recovering a file.  This is not
    an actual coding system name, but just an indicator to tell
    insert-file-contents to use `emacs-mule' with a special flag for
    auto saving and recovering a file.  */
-Lisp_Object Qauto_save_coding;
+static Lisp_Object Qauto_save_coding;
 
 /* Property name of a file name handler,
    which gives a list of operations it handles..  */
-Lisp_Object Qoperations;
+static Lisp_Object Qoperations;
 
 /* Lisp functions for translating file formats */
-Lisp_Object Qformat_decode, Qformat_annotate_function;
+static Lisp_Object Qformat_decode, Qformat_annotate_function;
 
 /* Lisp function for setting buffer-file-coding-system and the
    multibyteness of the current buffer after inserting a file.  */
-Lisp_Object Qafter_insert_file_set_coding;
+static Lisp_Object Qafter_insert_file_set_coding;
 
-Lisp_Object Qwrite_region_annotate_functions;
+static Lisp_Object Qwrite_region_annotate_functions;
 /* Each time an annotation function changes the buffer, the new buffer
    is added here.  */
-Lisp_Object Vwrite_region_annotation_buffers;
+static Lisp_Object Vwrite_region_annotation_buffers;
 
 #ifdef HAVE_FSYNC
 #endif
 
-Lisp_Object Qdelete_by_moving_to_trash;
+static Lisp_Object Qdelete_by_moving_to_trash;
 
 /* Lisp function for moving files to trash.  */
-Lisp_Object Qmove_file_to_trash;
+static Lisp_Object Qmove_file_to_trash;
 
 /* Lisp function for recursively copying directories.  */
-Lisp_Object Qcopy_directory;
+static Lisp_Object Qcopy_directory;
 
 /* Lisp function for recursively deleting directories.  */
-Lisp_Object Qdelete_directory;
+static Lisp_Object Qdelete_directory;
 
 #ifdef WINDOWSNT
 #endif
 
-Lisp_Object Qfile_error, Qfile_already_exists, Qfile_date_error;
-Lisp_Object Qexcl;
+Lisp_Object Qfile_error;
+static Lisp_Object Qfile_already_exists, Qfile_date_error;
+static Lisp_Object Qexcl;
 Lisp_Object Qfile_name_history;
 
-Lisp_Object Qcar_less_than_car;
+static Lisp_Object Qcar_less_than_car;
 
-static int a_write (int, Lisp_Object, int, int,
+static Lisp_Object Fmake_symbolic_link (Lisp_Object, Lisp_Object, Lisp_Object);
+static int a_write (int, Lisp_Object, EMACS_INT, EMACS_INT,
                     Lisp_Object *, struct coding_system *);
-static int e_write (int, Lisp_Object, int, int, struct coding_system *);
+static int e_write (int, Lisp_Object, EMACS_INT, EMACS_INT,
+                   struct coding_system *);
 
 \f
 void
@@ -194,7 +181,7 @@ report_file_error (const char *string, Lisp_Object data)
 
            str = SSDATA (errstring);
            c = STRING_CHAR ((unsigned char *) str);
-           Faset (errstring, make_number (0), make_number (DOWNCASE (c)));
+           Faset (errstring, make_number (0), make_number (downcase (c)));
          }
 
        xsignal (Qfile_error,
@@ -220,42 +207,43 @@ restore_point_unwind (Lisp_Object location)
 }
 
 \f
-Lisp_Object Qexpand_file_name;
-Lisp_Object Qsubstitute_in_file_name;
-Lisp_Object Qdirectory_file_name;
-Lisp_Object Qfile_name_directory;
-Lisp_Object Qfile_name_nondirectory;
-Lisp_Object Qunhandled_file_name_directory;
-Lisp_Object Qfile_name_as_directory;
-Lisp_Object Qcopy_file;
-Lisp_Object Qmake_directory_internal;
-Lisp_Object Qmake_directory;
-Lisp_Object Qdelete_directory_internal;
+static Lisp_Object Qexpand_file_name;
+static Lisp_Object Qsubstitute_in_file_name;
+static Lisp_Object Qdirectory_file_name;
+static Lisp_Object Qfile_name_directory;
+static Lisp_Object Qfile_name_nondirectory;
+static Lisp_Object Qunhandled_file_name_directory;
+static Lisp_Object Qfile_name_as_directory;
+static Lisp_Object Qcopy_file;
+static Lisp_Object Qmake_directory_internal;
+static Lisp_Object Qmake_directory;
+static Lisp_Object Qdelete_directory_internal;
 Lisp_Object Qdelete_file;
-Lisp_Object Qrename_file;
-Lisp_Object Qadd_name_to_file;
-Lisp_Object Qmake_symbolic_link;
+static Lisp_Object Qrename_file;
+static Lisp_Object Qadd_name_to_file;
+static Lisp_Object Qmake_symbolic_link;
 Lisp_Object Qfile_exists_p;
-Lisp_Object Qfile_executable_p;
-Lisp_Object Qfile_readable_p;
-Lisp_Object Qfile_writable_p;
-Lisp_Object Qfile_symlink_p;
-Lisp_Object Qaccess_file;
+static Lisp_Object Qfile_executable_p;
+static Lisp_Object Qfile_readable_p;
+static Lisp_Object Qfile_writable_p;
+static Lisp_Object Qfile_symlink_p;
+static Lisp_Object Qaccess_file;
 Lisp_Object Qfile_directory_p;
-Lisp_Object Qfile_regular_p;
-Lisp_Object Qfile_accessible_directory_p;
-Lisp_Object Qfile_modes;
-Lisp_Object Qset_file_modes;
-Lisp_Object Qset_file_times;
-Lisp_Object Qfile_selinux_context;
-Lisp_Object Qset_file_selinux_context;
-Lisp_Object Qfile_newer_than_file_p;
+static Lisp_Object Qfile_regular_p;
+static Lisp_Object Qfile_accessible_directory_p;
+static Lisp_Object Qfile_modes;
+static Lisp_Object Qset_file_modes;
+static Lisp_Object Qset_file_times;
+static Lisp_Object Qfile_selinux_context;
+static Lisp_Object Qset_file_selinux_context;
+static Lisp_Object Qfile_newer_than_file_p;
 Lisp_Object Qinsert_file_contents;
 Lisp_Object Qwrite_region;
-Lisp_Object Qverify_visited_file_modtime;
-Lisp_Object Qset_visited_file_modtime;
+static Lisp_Object Qverify_visited_file_modtime;
+static Lisp_Object Qset_visited_file_modtime;
 
-DEFUN ("find-file-name-handler", Ffind_file_name_handler, Sfind_file_name_handler, 2, 2, 0,
+DEFUN ("find-file-name-handler", Ffind_file_name_handler,
+       Sfind_file_name_handler, 2, 2, 0,
        doc: /* Return FILENAME's handler function for OPERATION, if it has one.
 Otherwise, return nil.
 A file name is handled if one of the regular expressions in
@@ -287,7 +275,7 @@ use the standard functions without calling themselves recursively.  */)
       if (CONSP (elt))
        {
          Lisp_Object string = XCAR (elt);
-         int match_pos;
+         EMACS_INT match_pos;
          Lisp_Object handler = XCDR (elt);
          Lisp_Object operations = Qnil;
 
@@ -350,7 +338,7 @@ Given a Unix syntax file name, returns a string ending in slash.  */)
 
   while (p != beg && !IS_DIRECTORY_SEP (p[-1])
 #ifdef DOS_NT
-        /* only recognise drive specifier at the beginning */
+        /* only recognize drive specifier at the beginning */
         && !(p[-1] == ':'
              /* handle the "/:d:foo" and "/:foo" cases correctly  */
              && ((p == beg + 2 && !IS_DIRECTORY_SEP (*beg))
@@ -413,7 +401,7 @@ or the entire name if it contains no slash.  */)
 
   while (p != beg && !IS_DIRECTORY_SEP (p[-1])
 #ifdef DOS_NT
-        /* only recognise drive specifier at beginning */
+        /* only recognize drive specifier at beginning */
         && !(p[-1] == ':'
              /* handle the "/:d:foo" case correctly  */
              && (p == beg + 2 || (p == beg + 4 && IS_DIRECTORY_SEP (*beg))))
@@ -449,14 +437,12 @@ get a current directory to run processes in.  */)
 }
 
 \f
-char *
-file_name_as_directory (char *out, char *in)
+static char *
+file_name_as_directory (char *out, const char *in)
 {
-  int size = strlen (in) - 1;
-
-  strcpy (out, in);
+  ptrdiff_t len = strlen (in);
 
-  if (size < 0)
+  if (len == 0)
     {
       out[0] = '.';
       out[1] = '/';
@@ -464,11 +450,13 @@ file_name_as_directory (char *out, char *in)
       return out;
     }
 
+  strcpy (out, in);
+
   /* For Unix syntax, Append a slash if necessary */
-  if (!IS_DIRECTORY_SEP (out[size]))
+  if (!IS_DIRECTORY_SEP (out[len - 1]))
     {
-      out[size + 1] = DIRECTORY_SEP;
-      out[size + 2] = '\0';
+      out[len] = DIRECTORY_SEP;
+      out[len + 1] = '\0';
     }
 #ifdef DOS_NT
   dostounix_filename (out);
@@ -512,10 +500,10 @@ For a Unix-syntax file name, just appends a slash.  */)
  * Value is nonzero if the string output is different from the input.
  */
 
-int
+static int
 directory_file_name (char *src, char *dst)
 {
-  long slen;
+  ptrdiff_t slen;
 
   slen = strlen (src);
 
@@ -599,9 +587,9 @@ make_temp_name (Lisp_Object prefix, int base64_p)
 {
   Lisp_Object val;
   int len, clen;
-  int pid;
+  printmax_t pid;
   char *p, *data;
-  char pidbuf[20];
+  char pidbuf[INT_BUFSIZE_BOUND (printmax_t)];
   int pidlen;
 
   CHECK_STRING (prefix);
@@ -611,7 +599,7 @@ make_temp_name (Lisp_Object prefix, int base64_p)
      three are incremented if the file already exists.  This ensures
      262144 unique file names per PID per PREFIX.  */
 
-  pid = (int) getpid ();
+  pid = getpid ();
 
   if (base64_p)
     {
@@ -623,8 +611,7 @@ make_temp_name (Lisp_Object prefix, int base64_p)
   else
     {
 #ifdef HAVE_LONG_FILE_NAMES
-      sprintf (pidbuf, "%d", pid);
-      pidlen = strlen (pidbuf);
+      pidlen = sprintf (pidbuf, "%"pMd, pid);
 #else
       pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6;
       pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6;
@@ -655,7 +642,7 @@ make_temp_name (Lisp_Object prefix, int base64_p)
 
   if (!make_temp_name_count_initialized_p)
     {
-      make_temp_name_count = (unsigned) time (NULL);
+      make_temp_name_count = time (NULL);
       make_temp_name_count_initialized_p = 1;
     }
 
@@ -744,18 +731,19 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
 {
   /* These point to SDATA and need to be careful with string-relocation
      during GC (via DECODE_FILE).  */
-  char *nm, *newdir;
+  char *nm;
+  const char *newdir;
   /* This should only point to alloca'd data.  */
   char *target;
 
-  int tlen;
+  ptrdiff_t tlen;
   struct passwd *pw;
 #ifdef DOS_NT
   int drive = 0;
   int collapse_newdir = 1;
   int is_escaped = 0;
 #endif /* DOS_NT */
-  int length;
+  ptrdiff_t length;
   Lisp_Object handler, result;
   int multibyte;
   Lisp_Object hdir;
@@ -770,7 +758,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
 
   /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted.  */
   if (NILP (default_directory))
-    default_directory = B_ (current_buffer, directory);
+    default_directory = BVAR (current_buffer, directory);
   if (! STRINGP (default_directory))
     {
 #ifdef DOS_NT
@@ -1029,21 +1017,23 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
   if (!newdir && drive)
     {
       /* Get default directory if needed to make nm absolute. */
+      char *adir = NULL;
       if (!IS_DIRECTORY_SEP (nm[0]))
        {
-         newdir = alloca (MAXPATHLEN + 1);
-         if (!getdefdir (toupper (drive) - 'A' + 1, newdir))
-           newdir = NULL;
+         adir = alloca (MAXPATHLEN + 1);
+         if (!getdefdir (toupper (drive) - 'A' + 1, adir))
+           adir = NULL;
        }
-      if (!newdir)
+      if (!adir)
        {
          /* Either nm starts with /, or drive isn't mounted. */
-         newdir = alloca (4);
-         newdir[0] = DRIVE_LETTER (drive);
-         newdir[1] = ':';
-         newdir[2] = '/';
-         newdir[3] = 0;
+         adir = alloca (4);
+         adir[0] = DRIVE_LETTER (drive);
+         adir[1] = ':';
+         adir[2] = '/';
+         adir[3] = 0;
        }
+      newdir = adir;
     }
 #endif /* DOS_NT */
 
@@ -1090,7 +1080,7 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
             when we have pointers into lisp strings, we accomplish this
             indirectly by prepending newdir to nm if necessary, and using
             cwd (or the wd of newdir's drive) as the new newdir. */
-
+         char *adir;
          if (IS_DRIVE (newdir[0]) && IS_DEVICE_SEP (newdir[1]))
            {
              drive = (unsigned char) newdir[0];
@@ -1103,14 +1093,15 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
              strcat (tmp, nm);
              nm = tmp;
            }
-         newdir = alloca (MAXPATHLEN + 1);
+         adir = alloca (MAXPATHLEN + 1);
          if (drive)
            {
-             if (!getdefdir (toupper (drive) - 'A' + 1, newdir))
+             if (!getdefdir (toupper (drive) - 'A' + 1, adir))
                newdir = "/";
            }
          else
-           getwd (newdir);
+           getwd (adir);
+         newdir = adir;
        }
 
       /* Strip off drive name from prefix, if present. */
@@ -1127,13 +1118,13 @@ filesystem tree, not (expand-file-name ".."  dirname).  */)
 #ifdef WINDOWSNT
          if (IS_DIRECTORY_SEP (newdir[0]) && IS_DIRECTORY_SEP (newdir[1]))
            {
-             char *p;
-             newdir = strcpy (alloca (strlen (newdir) + 1), newdir);
-             p = newdir + 2;
+             char *adir = strcpy (alloca (strlen (newdir) + 1), newdir);
+             char *p = adir + 2;
              while (*p && !IS_DIRECTORY_SEP (*p)) p++;
              p++;
              while (*p && !IS_DIRECTORY_SEP (*p)) p++;
              *p = 0;
+             newdir = adir;
            }
          else
 #endif
@@ -1322,7 +1313,7 @@ See also the function `substitute-in-file-name'.")
   unsigned char *nm;
 
   register unsigned char *newdir, *p, *o;
-  int tlen;
+  ptrdiff_t tlen;
   unsigned char *target;
   struct passwd *pw;
   int lose;
@@ -1374,7 +1365,7 @@ See also the function `substitute-in-file-name'.")
        unsigned char *user = nm + 1;
        /* Find end of name. */
        unsigned char *ptr = (unsigned char *) strchr (user, '/');
-       int len = ptr ? ptr - user : strlen (user);
+       ptrdiff_t len = ptr ? ptr - user : strlen (user);
        /* Copy the user name into temp storage. */
        o = (unsigned char *) alloca (len + 1);
        memcpy (o, user, len);
@@ -1481,7 +1472,7 @@ search_embedded_absfilename (char *nm, char *endp)
       if ((0
           || IS_DIRECTORY_SEP (p[-1]))
          && file_name_absolute_p (p)
-#if defined (WINDOWSNT) || defined(CYGWIN)
+#if defined (WINDOWSNT) || defined (CYGWIN)
          /* // at start of file name is meaningful in Apollo,
             WindowsNT and Cygwin systems.  */
          && !(IS_DIRECTORY_SEP (p[0]) && p - 1 == nm)
@@ -1562,7 +1553,7 @@ those `/' is discarded.  */)
   if (p)
     /* Start over with the new string, so we check the file-name-handler
        again.  Important with filenames like "/home/foo//:/hello///there"
-       which whould substitute to "/:/hello///there" rather than "/there".  */
+       which would substitute to "/:/hello///there" rather than "/there".  */
     return Fsubstitute_in_file_name
       (make_specified_string (p, -1, endp - p, multibyte));
 
@@ -1680,7 +1671,7 @@ those `/' is discarded.  */)
        else
          {
            Lisp_Object orig, decoded;
-           int orig_length, decoded_length;
+           ptrdiff_t orig_length, decoded_length;
            orig_length = strlen (o);
            orig = make_unibyte_string (o, orig_length);
            decoded = DECODE_FILE (orig);
@@ -1750,7 +1741,7 @@ expand_and_dir_to_file (Lisp_Object filename, Lisp_Object defdir)
 
    If QUICK is nonzero, we ask for y or n, not yes or no.  */
 
-void
+static void
 barf_or_query_if_file_exists (Lisp_Object absname, const char *querystring,
                              int interactive, struct stat *statptr, int quick)
 {
@@ -1764,6 +1755,10 @@ barf_or_query_if_file_exists (Lisp_Object absname, const char *querystring,
      regardless of what access permissions it has.  */
   if (lstat (SSDATA (encoded_filename), &statbuf) >= 0)
     {
+      if (S_ISDIR (statbuf.st_mode))
+       xsignal2 (Qfile_error,
+                 build_string ("File is a directory"), absname);
+
       if (! interactive)
        xsignal2 (Qfile_already_exists,
                  build_string ("File already exists"), absname);
@@ -1817,7 +1812,8 @@ If PRESERVE-SELINUX-CONTEXT is non-nil and SELinux is enabled
 on the system, we copy the SELinux context of FILE to NEWNAME.  */)
   (Lisp_Object file, Lisp_Object newname, Lisp_Object ok_if_already_exists, Lisp_Object keep_time, Lisp_Object preserve_uid_gid, Lisp_Object preserve_selinux_context)
 {
-  int ifd, ofd, n;
+  int ifd, ofd;
+  EMACS_INT n;
   char buf[16 * 1024];
   struct stat st, out_st;
   Lisp_Object handler;
@@ -1922,7 +1918,6 @@ on the system, we copy the SELinux context of FILE to NEWNAME.  */)
                         Fcons (file, Fcons (newname, Qnil)));
     }
 
-#if defined (S_ISREG) && defined (S_ISLNK)
   if (input_file_statable_p)
     {
       if (!(S_ISREG (st.st_mode)) && !(S_ISLNK (st.st_mode)))
@@ -1934,7 +1929,6 @@ on the system, we copy the SELinux context of FILE to NEWNAME.  */)
          report_file_error ("Non-regular file", Fcons (file, Qnil));
        }
     }
-#endif /* S_ISREG && S_ISLNK */
 
 #ifdef MSDOS
   /* System's default file type was set to binary by _fmode in emacs.c.  */
@@ -1943,10 +1937,19 @@ on the system, we copy the SELinux context of FILE to NEWNAME.  */)
                    | (NILP (ok_if_already_exists) ? O_EXCL : 0),
                    S_IREAD | S_IWRITE);
 #else  /* not MSDOS */
-  ofd = emacs_open (SSDATA (encoded_newname),
-                   O_WRONLY | O_TRUNC | O_CREAT
-                   | (NILP (ok_if_already_exists) ? O_EXCL : 0),
-                   0666);
+  {
+    int new_mask = 0666;
+    if (input_file_statable_p)
+      {
+       if (!NILP (preserve_uid_gid))
+         new_mask = 0600;
+       new_mask &= st.st_mode;
+      }
+    ofd = emacs_open (SSDATA (encoded_newname),
+                     (O_WRONLY | O_TRUNC | O_CREAT
+                      | (NILP (ok_if_already_exists) ? O_EXCL : 0)),
+                     new_mask);
+  }
 #endif /* not MSDOS */
   if (ofd < 0)
     report_file_error ("Opening output file", Fcons (newname, Qnil));
@@ -1965,9 +1968,22 @@ on the system, we copy the SELinux context of FILE to NEWNAME.  */)
      owner and group.  */
   if (input_file_statable_p)
     {
-      if (! NILP (preserve_uid_gid))
-       fchown (ofd, st.st_uid, st.st_gid);
-      fchmod (ofd, st.st_mode & 07777);
+      int mode_mask = 07777;
+      if (!NILP (preserve_uid_gid))
+       {
+         /* Attempt to change owner and group.  If that doesn't work
+            attempt to change just the group, as that is sometimes allowed.
+            Adjust the mode mask to eliminate setuid or setgid bits
+            that are inappropriate if the owner and group are wrong.  */
+         if (fchown (ofd, st.st_uid, st.st_gid) != 0)
+           {
+             mode_mask &= ~06000;
+             if (fchown (ofd, -1, st.st_gid) == 0)
+               mode_mask |= 02000;
+           }
+       }
+      if (fchmod (ofd, st.st_mode & mode_mask) != 0)
+       report_file_error ("Doing chmod", Fcons (newname, Qnil));
     }
 #endif /* not MSDOS */
 
@@ -2059,7 +2075,6 @@ DEFUN ("delete-directory-internal", Fdelete_directory_internal,
   (Lisp_Object directory)
 {
   const char *dir;
-  Lisp_Object handler;
   Lisp_Object encoded_dir;
 
   CHECK_STRING (directory);
@@ -2196,14 +2211,11 @@ This is what happens in interactive use with M-x.  */)
       if (errno == EXDEV)
        {
           int count;
-#ifdef S_IFLNK
           symlink_target = Ffile_symlink_p (file);
           if (! NILP (symlink_target))
             Fmake_symbolic_link (symlink_target, newname,
                                  NILP (ok_if_already_exists) ? Qnil : Qt);
-          else
-#endif
-         if (!NILP (Ffile_directory_p (file)))
+         else if (!NILP (Ffile_directory_p (file)))
            call4 (Qcopy_directory, file, newname, Qt, Qnil);
          else
            /* We have already prompted if it was an integer, so don't
@@ -2215,11 +2227,7 @@ This is what happens in interactive use with M-x.  */)
          count = SPECPDL_INDEX ();
          specbind (Qdelete_by_moving_to_trash, Qnil);
 
-         if (!NILP (Ffile_directory_p (file))
-#ifdef S_IFLNK
-             && NILP (symlink_target)
-#endif
-             )
+         if (!NILP (Ffile_directory_p (file)) && NILP (symlink_target))
            call2 (Qdelete_directory, file, Qt);
          else
            Fdelete_file (file, Qnil);
@@ -2329,7 +2337,6 @@ This happens for interactive use with M-x.  */)
     RETURN_UNGCPRO (call4 (handler, Qmake_symbolic_link, filename,
                           linkname, ok_if_already_exists));
 
-#ifdef S_IFLNK
   encoded_filename = ENCODE_FILE (filename);
   encoded_linkname = ENCODE_FILE (linkname);
 
@@ -2351,17 +2358,17 @@ This happens for interactive use with M-x.  */)
              return Qnil;
            }
        }
+      if (errno == ENOSYS)
+       {
+         UNGCPRO;
+         xsignal1 (Qfile_error,
+                   build_string ("Symbolic links are not supported"));
+       }
 
       report_file_error ("Making symbolic link", list2 (filename, linkname));
     }
   UNGCPRO;
   return Qnil;
-
-#else
-  UNGCPRO;
-  xsignal1 (Qfile_error, build_string ("Symbolic links are not supported"));
-
-#endif /* S_IFLNK */
 }
 
 \f
@@ -2381,8 +2388,6 @@ static int
 check_executable (char *filename)
 {
 #ifdef DOS_NT
-  int len = strlen (filename);
-  char *suffix;
   struct stat st;
   if (stat (filename, &st) < 0)
     return 0;
@@ -2408,7 +2413,7 @@ check_writable (const char *filename)
   struct stat st;
   if (stat (filename, &st) < 0)
     return 0;
-  return (st.st_mode & S_IWRITE || (st.st_mode & S_IFMT) == S_IFDIR);
+  return (st.st_mode & S_IWRITE || S_ISDIR (st.st_mode));
 #else /* not MSDOS */
 #ifdef HAVE_EUIDACCESS
   return (euidaccess (filename, 2) >= 0);
@@ -2492,7 +2497,7 @@ See also `file-exists-p' and `file-attributes'.  */)
 
   absname = ENCODE_FILE (absname);
 
-#if defined(DOS_NT) || defined(macintosh)
+#if defined (DOS_NT) || defined (macintosh)
   /* Under MS-DOS, Windows, and Macintosh, open does not work for
      directories.  */
   if (access (SDATA (absname), 0) == 0)
@@ -2500,7 +2505,7 @@ See also `file-exists-p' and `file-attributes'.  */)
   return Qnil;
 #else /* not DOS_NT and not macintosh */
   flags = O_RDONLY;
-#if defined (S_ISFIFO) && defined (O_NONBLOCK)
+#ifdef O_NONBLOCK
   /* Opening a fifo without O_NONBLOCK can wait.
      We don't want to wait.  But we don't want to mess wth O_NONBLOCK
      except in the case of a fifo, on a system which handles it.  */
@@ -2555,7 +2560,7 @@ DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
      should check ACLs though, which do affect this.  */
   if (stat (SDATA (dir), &statbuf) < 0)
     return Qnil;
-  return (statbuf.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
+  return S_ISDIR (statbuf.st_mode) ? Qt : Qnil;
 #else
   return (check_writable (!NILP (dir) ? SSDATA (dir) : "")
          ? Qt : Qnil);
@@ -2602,6 +2607,9 @@ points to a nonexistent file.  */)
   (Lisp_Object filename)
 {
   Lisp_Object handler;
+  char *buf;
+  Lisp_Object val;
+  char readlink_buf[READLINK_BUFSIZE];
 
   CHECK_STRING (filename);
   filename = Fexpand_file_name (filename, Qnil);
@@ -2612,51 +2620,19 @@ points to a nonexistent file.  */)
   if (!NILP (handler))
     return call2 (handler, Qfile_symlink_p, filename);
 
-#ifdef S_IFLNK
-  {
-  char *buf;
-  int bufsize;
-  int valsize;
-  Lisp_Object val;
-
   filename = ENCODE_FILE (filename);
 
-  bufsize = 50;
-  buf = NULL;
-  do
-    {
-      bufsize *= 2;
-      buf = (char *) xrealloc (buf, bufsize);
-      memset (buf, 0, bufsize);
-
-      errno = 0;
-      valsize = readlink (SSDATA (filename), buf, bufsize);
-      if (valsize == -1)
-       {
-#ifdef ERANGE
-         /* HP-UX reports ERANGE if buffer is too small.  */
-         if (errno == ERANGE)
-           valsize = bufsize;
-         else
-#endif
-           {
-             xfree (buf);
-             return Qnil;
-           }
-       }
-    }
-  while (valsize >= bufsize);
+  buf = emacs_readlink (SSDATA (filename), readlink_buf);
+  if (! buf)
+    return Qnil;
 
-  val = make_string (buf, valsize);
+  val = build_string (buf);
   if (buf[0] == '/' && strchr (buf, ':'))
     val = concat2 (build_string ("/:"), val);
-  xfree (buf);
+  if (buf != readlink_buf)
+    xfree (buf);
   val = DECODE_FILE (val);
   return val;
-  }
-#else /* not S_IFLNK */
-  return Qnil;
-#endif /* not S_IFLNK */
 }
 
 DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0,
@@ -2669,7 +2645,7 @@ See `file-symlink-p' to distinguish symlinks.  */)
   struct stat st;
   Lisp_Object handler;
 
-  absname = expand_and_dir_to_file (filename, B_ (current_buffer, directory));
+  absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory));
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
@@ -2681,10 +2657,11 @@ See `file-symlink-p' to distinguish symlinks.  */)
 
   if (stat (SSDATA (absname), &st) < 0)
     return Qnil;
-  return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
+  return S_ISDIR (st.st_mode) ? Qt : Qnil;
 }
 
-DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, Sfile_accessible_directory_p, 1, 1, 0,
+DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p,
+       Sfile_accessible_directory_p, 1, 1, 0,
        doc: /* Return t if file FILENAME names a directory you can open.
 For the value to be t, FILENAME must specify the name of a directory as a file,
 and the directory must allow you to open files in it.  In order to use a
@@ -2722,7 +2699,7 @@ See `file-symlink-p' to distinguish symlinks.  */)
   struct stat st;
   Lisp_Object handler;
 
-  absname = expand_and_dir_to_file (filename, B_ (current_buffer, directory));
+  absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory));
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
@@ -2744,12 +2721,12 @@ See `file-symlink-p' to distinguish symlinks.  */)
 
     if (result < 0)
       return Qnil;
-    return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
+    return S_ISREG (st.st_mode) ? Qt : Qnil;
   }
 #else
   if (stat (SSDATA (absname), &st) < 0)
     return Qnil;
-  return (st.st_mode & S_IFMT) == S_IFREG ? Qt : Qnil;
+  return S_ISREG (st.st_mode) ? Qt : Qnil;
 #endif
 }
 \f
@@ -2769,7 +2746,7 @@ if file does not exist, is not accessible, or SELinux is disabled */)
   context_t context;
 #endif
 
-  absname = expand_and_dir_to_file (filename, B_ (current_buffer, directory));
+  absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory));
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
@@ -2786,7 +2763,7 @@ if file does not exist, is not accessible, or SELinux is disabled */)
 #if HAVE_LIBSELINUX
   if (is_selinux_enabled ())
     {
-      conlength = lgetfilecon (SDATA (absname), &con);
+      conlength = lgetfilecon (SSDATA (absname), &con);
       if (conlength > 0)
        {
          context = context_new (con);
@@ -2805,7 +2782,7 @@ if file does not exist, is not accessible, or SELinux is disabled */)
     }
 #endif
 
-  return Flist (sizeof(values) / sizeof(values[0]), values);
+  return Flist (sizeof (values) / sizeof (values[0]), values);
 }
 \f
 DEFUN ("set-file-selinux-context", Fset_file_selinux_context,
@@ -2815,19 +2792,20 @@ as a list ("user", "role", "type", "range"). Has no effect if SELinux
 is disabled. */)
   (Lisp_Object filename, Lisp_Object context)
 {
-  Lisp_Object absname, encoded_absname;
+  Lisp_Object absname;
   Lisp_Object handler;
+#if HAVE_LIBSELINUX
+  Lisp_Object encoded_absname;
   Lisp_Object user = CAR_SAFE (context);
   Lisp_Object role = CAR_SAFE (CDR_SAFE (context));
   Lisp_Object type = CAR_SAFE (CDR_SAFE (CDR_SAFE (context)));
   Lisp_Object range = CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (context))));
-#if HAVE_LIBSELINUX
   security_context_t con;
   int fail, conlength;
   context_t parsed_con;
 #endif
 
-  absname = Fexpand_file_name (filename, B_ (current_buffer, directory));
+  absname = Fexpand_file_name (filename, BVAR (current_buffer, directory));
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
@@ -2835,47 +2813,47 @@ is disabled. */)
   if (!NILP (handler))
     return call3 (handler, Qset_file_selinux_context, absname, context);
 
-  encoded_absname = ENCODE_FILE (absname);
-
 #if HAVE_LIBSELINUX
   if (is_selinux_enabled ())
     {
       /* Get current file context. */
-      conlength = lgetfilecon (SDATA (encoded_absname), &con);
+      encoded_absname = ENCODE_FILE (absname);
+      conlength = lgetfilecon (SSDATA (encoded_absname), &con);
       if (conlength > 0)
        {
          parsed_con = context_new (con);
          /* Change the parts defined in the parameter.*/
          if (STRINGP (user))
            {
-             if (context_user_set (parsed_con, SDATA (user)))
+             if (context_user_set (parsed_con, SSDATA (user)))
                error ("Doing context_user_set");
            }
          if (STRINGP (role))
            {
-             if (context_role_set (parsed_con, SDATA (role)))
+             if (context_role_set (parsed_con, SSDATA (role)))
                error ("Doing context_role_set");
            }
          if (STRINGP (type))
            {
-             if (context_type_set (parsed_con, SDATA (type)))
+             if (context_type_set (parsed_con, SSDATA (type)))
                error ("Doing context_type_set");
            }
          if (STRINGP (range))
            {
-             if (context_range_set (parsed_con, SDATA (range)))
+             if (context_range_set (parsed_con, SSDATA (range)))
                error ("Doing context_range_set");
            }
 
          /* Set the modified context back to the file. */
-         fail = lsetfilecon (SDATA (encoded_absname), context_str (parsed_con));
+         fail = lsetfilecon (SSDATA (encoded_absname),
+                             context_str (parsed_con));
          if (fail)
            report_file_error ("Doing lsetfilecon", Fcons (absname, Qnil));
 
          context_free (parsed_con);
        }
       else
-       report_file_error("Doing lgetfilecon", Fcons (absname, Qnil));
+       report_file_error ("Doing lgetfilecon", Fcons (absname, Qnil));
 
       if (con)
        freecon (con);
@@ -2894,7 +2872,7 @@ Return nil, if file does not exist or is not accessible.  */)
   struct stat st;
   Lisp_Object handler;
 
-  absname = expand_and_dir_to_file (filename, B_ (current_buffer, directory));
+  absname = expand_and_dir_to_file (filename, BVAR (current_buffer, directory));
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
@@ -2923,7 +2901,7 @@ symbolic notation, like the `chmod' command from GNU Coreutils.  */)
   Lisp_Object absname, encoded_absname;
   Lisp_Object handler;
 
-  absname = Fexpand_file_name (filename, B_ (current_buffer, directory));
+  absname = Fexpand_file_name (filename, BVAR (current_buffer, directory));
   CHECK_NUMBER (mode);
 
   /* If the file name has special constructs in it,
@@ -2934,7 +2912,7 @@ symbolic notation, like the `chmod' command from GNU Coreutils.  */)
 
   encoded_absname = ENCODE_FILE (absname);
 
-  if (chmod (SSDATA (encoded_absname), XINT (mode)) < 0)
+  if (chmod (SSDATA (encoded_absname), XINT (mode) & 07777) < 0)
     report_file_error ("Doing chmod", Fcons (absname, Qnil));
 
   return Qnil;
@@ -2970,28 +2948,28 @@ The value is an integer.  */)
 \f
 
 DEFUN ("set-file-times", Fset_file_times, Sset_file_times, 1, 2, 0,
-       doc: /* Set times of file FILENAME to TIME.
+       doc: /* Set times of file FILENAME to TIMESTAMP.
 Set both access and modification times.
 Return t on success, else nil.
-Use the current time if TIME is nil.  TIME is in the format of
+Use the current time if TIMESTAMP is nil.  TIMESTAMP is in the format of
 `current-time'. */)
-  (Lisp_Object filename, Lisp_Object time)
+  (Lisp_Object filename, Lisp_Object timestamp)
 {
   Lisp_Object absname, encoded_absname;
   Lisp_Object handler;
   time_t sec;
   int usec;
 
-  if (! lisp_time_argument (time, &sec, &usec))
+  if (! lisp_time_argument (timestamp, &sec, &usec))
     error ("Invalid time specification");
 
-  absname = Fexpand_file_name (filename, B_ (current_buffer, directory));
+  absname = Fexpand_file_name (filename, BVAR (current_buffer, directory));
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
   handler = Ffind_file_name_handler (absname, Qset_file_times);
   if (!NILP (handler))
-    return call3 (handler, Qset_file_times, absname, time);
+    return call3 (handler, Qset_file_times, absname, timestamp);
 
   encoded_absname = ENCODE_FILE (absname);
 
@@ -3007,8 +2985,7 @@ Use the current time if TIME is nil.  TIME is in the format of
         struct stat st;
 
         /* Setting times on a directory always fails.  */
-        if (stat (SDATA (encoded_absname), &st) == 0
-            && (st.st_mode & S_IFMT) == S_IFDIR)
+        if (stat (SSDATA (encoded_absname), &st) == 0 && S_ISDIR (st.st_mode))
           return Qnil;
 #endif
         report_file_error ("Setting file times", Fcons (absname, Qnil));
@@ -3047,8 +3024,8 @@ otherwise, if FILE2 does not exist, the answer is t.  */)
 
   absname1 = Qnil;
   GCPRO2 (absname1, file2);
-  absname1 = expand_and_dir_to_file (file1, B_ (current_buffer, directory));
-  absname2 = expand_and_dir_to_file (file2, B_ (current_buffer, directory));
+  absname1 = expand_and_dir_to_file (file1, BVAR (current_buffer, directory));
+  absname2 = expand_and_dir_to_file (file2, BVAR (current_buffer, directory));
   UNGCPRO;
 
   /* If the file name has special constructs in it,
@@ -3075,10 +3052,6 @@ otherwise, if FILE2 does not exist, the answer is t.  */)
   return (mtime1 > st.st_mtime) ? Qt : Qnil;
 }
 \f
-#ifdef DOS_NT
-Lisp_Object Qfind_buffer_file_type;
-#endif /* DOS_NT */
-
 #ifndef READ_BUF_SIZE
 #define READ_BUF_SIZE (64 << 10)
 #endif
@@ -3116,8 +3089,8 @@ decide_coding_unwind (Lisp_Object unwind_data)
   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 
   /* Now we are safe to change the buffer's multibyteness directly.  */
-  B_ (current_buffer, enable_multibyte_characters) = multibyte;
-  B_ (current_buffer, undo_list) = undo_list;
+  BVAR (current_buffer, enable_multibyte_characters) = multibyte;
+  BVAR (current_buffer, undo_list) = undo_list;
 
   return Qnil;
 }
@@ -3160,6 +3133,21 @@ 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)
+{
+  /* Use "&" rather than "&&" to suppress a bogus GCC warning; see
+     <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43772>.  */
+  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,
@@ -3191,6 +3179,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
   EMACS_INT inserted = 0;
   int nochange = 0;
   register EMACS_INT how_much;
+  off_t beg_offset, end_offset;
   register EMACS_INT unprocessed;
   int count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
@@ -3198,6 +3187,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
   Lisp_Object p;
   EMACS_INT total = 0;
   int not_regular = 0;
+  int save_errno = 0;
   char read_buf[READ_BUF_SIZE];
   struct coding_system coding;
   char buffer[1 << 14];
@@ -3212,7 +3202,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
   if (current_buffer->base_buffer && ! NILP (visit))
     error ("Cannot do file visiting in an indirect buffer");
 
-  if (!NILP (B_ (current_buffer, read_only)))
+  if (!NILP (BVAR (current_buffer, read_only)))
     Fbarf_if_buffer_read_only ();
 
   val = Qnil;
@@ -3260,18 +3250,18 @@ variable `last-coding-system-used' to the coding system actually used.  */)
   if (stat (SSDATA (filename), &st) < 0)
 #endif /* WINDOWSNT */
     {
-      if (fd >= 0) emacs_close (fd);
     badopen:
+      save_errno = errno;
       if (NILP (visit))
        report_file_error ("Opening input file", Fcons (orig_filename, Qnil));
       st.st_mtime = -1;
+      st.st_size = -1;
       how_much = 0;
       if (!NILP (Vcoding_system_for_read))
        Fset (Qbuffer_file_coding_system, Vcoding_system_for_read);
       goto notfound;
     }
 
-#ifdef S_IFREG
   /* This code will need to be changed in order to work on named
      pipes, and it's probably just not worth it.  So we should at
      least signal an error.  */
@@ -3286,7 +3276,6 @@ variable `last-coding-system-used' to the coding system actually used.  */)
        xsignal2 (Qfile_error,
                  build_string ("not a regular file"), orig_filename);
     }
-#endif
 
   if (fd < 0)
     if ((fd = emacs_open (SSDATA (filename), O_RDONLY, 0)) < 0)
@@ -3298,13 +3287,6 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
   record_unwind_protect (close_file_unwind, make_number (fd));
 
-  /* Can happen on any platform that uses long as type of off_t, but allows
-     file sizes to exceed 2Gb, so give a suitable message.  */
-  if (! not_regular && st.st_size < 0)
-    error ("Maximum buffer size exceeded");
-
-  /* Prevent redisplay optimizations.  */
-  current_buffer->clip_changed = 1;
 
   if (!NILP (visit))
     {
@@ -3315,38 +3297,64 @@ variable `last-coding-system-used' to the coding system actually used.  */)
     }
 
   if (!NILP (beg))
-    CHECK_NUMBER (beg);
+    {
+      if (! (RANGED_INTEGERP (0, beg, TYPE_MAXIMUM (off_t))))
+       wrong_type_argument (intern ("file-offset"), beg);
+      beg_offset = XFASTINT (beg);
+    }
   else
-    XSETFASTINT (beg, 0);
+    beg_offset = 0;
 
   if (!NILP (end))
-    CHECK_NUMBER (end);
+    {
+      if (! (RANGED_INTEGERP (0, end, TYPE_MAXIMUM (off_t))))
+       wrong_type_argument (intern ("file-offset"), end);
+      end_offset = XFASTINT (end);
+    }
   else
     {
-      if (! not_regular)
+      if (not_regular)
+       end_offset = TYPE_MAXIMUM (off_t);
+      else
        {
-         XSETINT (end, st.st_size);
-
-         /* Arithmetic overflow can occur if an Emacs integer cannot
-            represent the file size, or if the calculations below
-            overflow.  The calculations below double the file size
-            twice, so check that it can be multiplied by 4 safely.  */
-         if (XINT (end) != st.st_size
-             /* Actually, it should test either INT_MAX or LONG_MAX
-                depending on which one is used for EMACS_INT.  But in
-                any case, in practice, this test is redundant with the
-                one above.
-                || st.st_size > INT_MAX / 4 */)
-           error ("Maximum buffer size exceeded");
+         end_offset = st.st_size;
+
+         /* A negative size can happen on a platform that allows file
+            sizes greater than the maximum off_t value.  */
+         if (end_offset < 0)
+           buffer_overflow ();
 
          /* The file size returned from stat may be zero, but data
             may be readable nonetheless, for example when this is a
             file in the /proc filesystem.  */
-         if (st.st_size == 0)
-           XSETINT (end, READ_BUF_SIZE);
+         if (end_offset == 0)
+           end_offset = READ_BUF_SIZE;
+       }
+    }
+
+  /* Check now whether the buffer will become too large,
+     in the likely case where the file's length is not changing.
+     This saves a lot of needless work before a buffer overflow.  */
+  if (! not_regular)
+    {
+      /* The likely offset where we will stop reading.  We could read
+        more (or less), if the file grows (or shrinks) as we read it.  */
+      off_t likely_end = min (end_offset, st.st_size);
+
+      if (beg_offset < likely_end)
+       {
+         ptrdiff_t buf_bytes =
+           Z_BYTE - (!NILP (replace) ? ZV_BYTE - BEGV_BYTE  : 0);
+         ptrdiff_t buf_growth_max = BUF_BYTES_MAX - buf_bytes;
+         off_t likely_growth = likely_end - beg_offset;
+         if (buf_growth_max < likely_growth)
+           buffer_overflow ();
        }
     }
 
+  /* Prevent redisplay optimizations.  */
+  current_buffer->clip_changed = 1;
+
   if (EQ (Vcoding_system_for_read, Qauto_save_coding))
     {
       coding_system = coding_inherit_eol_type (Qutf_8_emacs, Qunix);
@@ -3381,7 +3389,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);
@@ -3394,25 +3402,25 @@ variable `last-coding-system-used' to the coding system actually used.  */)
              else if (nread > 0)
                {
                  struct buffer *prev = current_buffer;
-                 Lisp_Object buffer;
+                 Lisp_Object workbuf;
                  struct buffer *buf;
 
                  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
 
-                 buffer = Fget_buffer_create (build_string (" *code-converting-work*"));
-                 buf = XBUFFER (buffer);
+                 workbuf = Fget_buffer_create (build_string (" *code-converting-work*"));
+                 buf = XBUFFER (workbuf);
 
                  delete_all_overlays (buf);
-                 B_ (buf, directory) = B_ (current_buffer, directory);
-                 B_ (buf, read_only) = Qnil;
-                 B_ (buf, filename) = Qnil;
-                 B_ (buf, undo_list) = Qt;
+                 BVAR (buf, directory) = BVAR (current_buffer, directory);
+                 BVAR (buf, read_only) = Qnil;
+                 BVAR (buf, filename) = Qnil;
+                 BVAR (buf, undo_list) = Qt;
                  eassert (buf->overlays_before == NULL);
                  eassert (buf->overlays_after == NULL);
 
                  set_buffer_internal (buf);
                  Ferase_buffer ();
-                 B_ (buf, enable_multibyte_characters) = Qnil;
+                 BVAR (buf, enable_multibyte_characters) = Qnil;
 
                  insert_1_both ((char *) read_buf, nread, nread, 0, 0, 0);
                  TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
@@ -3425,7 +3433,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));
                }
@@ -3450,7 +3458,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       else
        CHECK_CODING_SYSTEM (coding_system);
 
-      if (NILP (B_ (current_buffer, enable_multibyte_characters)))
+      if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
        /* We must suppress all character code conversion except for
           end-of-line conversion.  */
        coding_system = raw_text_coding_system (coding_system);
@@ -3490,9 +3498,9 @@ variable `last-coding-system-used' to the coding system actually used.  */)
         give up on handling REPLACE in the optimized way.  */
       int giveup_match_end = 0;
 
-      if (XINT (beg) != 0)
+      if (beg_offset != 0)
        {
-         if (lseek (fd, XINT (beg), 0) < 0)
+         if (lseek (fd, beg_offset, SEEK_SET) < 0)
            report_file_error ("Setting file position",
                               Fcons (orig_filename, Qnil));
        }
@@ -3540,7 +3548,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       immediate_quit = 0;
       /* If the file matches the buffer completely,
         there's no need to replace anything.  */
-      if (same_at_start - BEGV_BYTE == XINT (end))
+      if (same_at_start - BEGV_BYTE == end_offset - beg_offset)
        {
          emacs_close (fd);
          specpdl_ptr--;
@@ -3555,16 +3563,17 @@ variable `last-coding-system-used' to the coding system actually used.  */)
         already found that decoding is necessary, don't waste time.  */
       while (!giveup_match_end)
        {
-         EMACS_INT total_read, nread, bufpos, curpos, trial;
+         int total_read, nread, bufpos, trial;
+         off_t curpos;
 
          /* At what file position are we now scanning?  */
-         curpos = XINT (end) - (ZV_BYTE - same_at_end);
+         curpos = end_offset - (ZV_BYTE - same_at_end);
          /* If the entire file matches the buffer tail, stop the scan.  */
          if (curpos == 0)
            break;
          /* How much can we scan in the next step?  */
          trial = min (curpos, sizeof buffer);
-         if (lseek (fd, curpos - trial, 0) < 0)
+         if (lseek (fd, curpos - trial, SEEK_SET) < 0)
            report_file_error ("Setting file position",
                               Fcons (orig_filename, Qnil));
 
@@ -3598,7 +3607,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
                 we cannot use this method; giveup and try the other.  */
              if (same_at_end > same_at_start
                  && FETCH_BYTE (same_at_end - 1) >= 0200
-                 && ! NILP (B_ (current_buffer, enable_multibyte_characters))
+                 && ! NILP (BVAR (current_buffer, enable_multibyte_characters))
                  && (CODING_MAY_REQUIRE_DECODING (&coding)))
                giveup_match_end = 1;
              break;
@@ -3617,27 +3626,28 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
          /* Extend the start of non-matching text area to multibyte
              character boundary.  */
-         if (! NILP (B_ (current_buffer, enable_multibyte_characters)))
+         if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
            while (same_at_start > BEGV_BYTE
                   && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start)))
              same_at_start--;
 
          /* Extend the end of non-matching text area to multibyte
              character boundary.  */
-         if (! NILP (B_ (current_buffer, enable_multibyte_characters)))
+         if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
            while (same_at_end < ZV_BYTE
                   && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end)))
              same_at_end++;
 
          /* Don't try to reuse the same piece of text twice.  */
          overlap = (same_at_start - BEGV_BYTE
-                    - (same_at_end + st.st_size - ZV));
+                    - (same_at_end
+                       + (! NILP (end) ? end_offset : st.st_size) - ZV_BYTE));
          if (overlap > 0)
            same_at_end += overlap;
 
          /* Arrange to read only the nonmatching middle part of the file.  */
-         XSETFASTINT (beg, XINT (beg) + (same_at_start - BEGV_BYTE));
-         XSETFASTINT (end, XINT (end) - (ZV_BYTE - same_at_end));
+         beg_offset += same_at_start - BEGV_BYTE;
+         end_offset -= ZV_BYTE - same_at_end;
 
          del_range_byte (same_at_start, same_at_end, 0);
          /* Insert from the file at the proper position.  */
@@ -3672,16 +3682,18 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       EMACS_INT bufpos;
       unsigned char *decoded;
       EMACS_INT temp;
+      EMACS_INT this = 0;
       int this_count = SPECPDL_INDEX ();
-      int multibyte = ! NILP (B_ (current_buffer, enable_multibyte_characters));
+      int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
       Lisp_Object conversion_buffer;
+      struct gcpro gcpro1;
 
       conversion_buffer = code_conversion_save (1, multibyte);
 
       /* First read the whole file, performing code conversion into
         CONVERSION_BUFFER.  */
 
-      if (lseek (fd, XINT (beg), 0) < 0)
+      if (lseek (fd, beg_offset, SEEK_SET) < 0)
        report_file_error ("Setting file position",
                           Fcons (orig_filename, Qnil));
 
@@ -3695,10 +3707,9 @@ variable `last-coding-system-used' to the coding system actually used.  */)
        {
          /* We read one bunch by one (READ_BUF_SIZE bytes) to allow
             quitting while reading a huge while.  */
-         /* try is reserved in some compilers (Microsoft C) */
+         /* `try'' is reserved in some compilers (Microsoft C).  */
          EMACS_INT trytry = min (total - how_much,
                                  READ_BUF_SIZE - unprocessed);
-         EMACS_INT this;
 
          /* Allow quitting out of the actual I/O.  */
          immediate_quit = 1;
@@ -3707,11 +3718,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
          immediate_quit = 0;
 
          if (this <= 0)
-           {
-             if (this < 0)
-               how_much = this;
-             break;
-           }
+           break;
 
          how_much += this;
 
@@ -3734,7 +3741,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       /* At this point, HOW_MUCH should equal TOTAL, or should be <= 0
         if we couldn't read the file.  */
 
-      if (how_much < 0)
+      if (this < 0)
        error ("IO error reading %s: %s",
               SDATA (orig_filename), emacs_strerror (errno));
 
@@ -3778,7 +3785,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
       /* Extend the start of non-matching text area to the previous
         multibyte character boundary.  */
-      if (! NILP (B_ (current_buffer, enable_multibyte_characters)))
+      if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
        while (same_at_start > BEGV_BYTE
               && ! CHAR_HEAD_P (FETCH_BYTE (same_at_start)))
          same_at_start--;
@@ -3795,7 +3802,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
       /* Extend the end of non-matching text area to the next
         multibyte character boundary.  */
-      if (! NILP (B_ (current_buffer, enable_multibyte_characters)))
+      if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
        while (same_at_end < ZV_BYTE
               && ! CHAR_HEAD_P (FETCH_BYTE (same_at_end)))
          same_at_end++;
@@ -3853,26 +3860,17 @@ variable `last-coding-system-used' to the coding system actually used.  */)
     }
 
   if (! not_regular)
-    {
-      register Lisp_Object temp;
-
-      total = XINT (end) - XINT (beg);
-
-      /* Make sure point-max won't overflow after this insertion.  */
-      XSETINT (temp, total);
-      if (total != XINT (temp))
-       error ("Maximum buffer size exceeded");
-    }
+    total = end_offset - beg_offset;
   else
     /* For a special file, all we can do is guess.  */
     total = READ_BUF_SIZE;
 
-  if (NILP (visit) && inserted > 0)
+  if (NILP (visit) && total > 0)
     {
 #ifdef CLASH_DETECTION
-      if (!NILP (B_ (current_buffer, file_truename))
+      if (!NILP (BVAR (current_buffer, file_truename))
          /* Make binding buffer-file-name to nil effective.  */
-         && !NILP (B_ (current_buffer, filename))
+         && !NILP (BVAR (current_buffer, filename))
          && SAVE_MODIFF >= MODIFF)
        we_locked_file = 1;
 #endif /* CLASH_DETECTION */
@@ -3883,9 +3881,9 @@ variable `last-coding-system-used' to the coding system actually used.  */)
   if (GAP_SIZE < total)
     make_gap (total - GAP_SIZE);
 
-  if (XINT (beg) != 0 || !NILP (replace))
+  if (beg_offset != 0 || !NILP (replace))
     {
-      if (lseek (fd, XINT (beg), 0) < 0)
+      if (lseek (fd, beg_offset, SEEK_SET) < 0)
        report_file_error ("Setting file position",
                           Fcons (orig_filename, Qnil));
     }
@@ -3912,7 +3910,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
        if (not_regular)
          {
-           Lisp_Object val;
+           Lisp_Object nbytes;
 
            /* Maybe make more room.  */
            if (gap_size < trytry)
@@ -3927,15 +3925,16 @@ variable `last-coding-system-used' to the coding system actually used.  */)
            non_regular_fd = fd;
            non_regular_inserted = inserted;
            non_regular_nbytes = trytry;
-           val = internal_condition_case_1 (read_non_regular, Qnil, Qerror,
-                                            read_non_regular_quit);
-           if (NILP (val))
+           nbytes = internal_condition_case_1 (read_non_regular,
+                                               Qnil, Qerror,
+                                               read_non_regular_quit);
+           if (NILP (nbytes))
              {
                read_quit = 1;
                break;
              }
 
-           this = XINT (val);
+           this = XINT (nbytes);
          }
        else
          {
@@ -3977,7 +3976,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
     {
 #ifdef CLASH_DETECTION
       if (we_locked_file)
-       unlock_file (B_ (current_buffer, file_truename));
+       unlock_file (BVAR (current_buffer, file_truename));
 #endif
       Vdeactivate_mark = old_Vdeactivate_mark;
     }
@@ -4026,13 +4025,13 @@ variable `last-coding-system-used' to the coding system actually used.  */)
             care of marker adjustment.  By this way, we can run Lisp
             program safely before decoding the inserted text.  */
          Lisp_Object unwind_data;
-         int count = SPECPDL_INDEX ();
+         int count1 = SPECPDL_INDEX ();
 
-         unwind_data = Fcons (B_ (current_buffer, enable_multibyte_characters),
-                              Fcons (B_ (current_buffer, undo_list),
+         unwind_data = Fcons (BVAR (current_buffer, enable_multibyte_characters),
+                              Fcons (BVAR (current_buffer, undo_list),
                                      Fcurrent_buffer ()));
-         B_ (current_buffer, enable_multibyte_characters) = Qnil;
-         B_ (current_buffer, undo_list) = Qt;
+         BVAR (current_buffer, enable_multibyte_characters) = Qnil;
+         BVAR (current_buffer, undo_list) = Qt;
          record_unwind_protect (decide_coding_unwind, unwind_data);
 
          if (inserted > 0 && ! NILP (Vset_auto_coding_function))
@@ -4053,7 +4052,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
              if (CONSP (coding_system))
                coding_system = XCAR (coding_system);
            }
-         unbind_to (count, Qnil);
+         unbind_to (count1, Qnil);
          inserted = Z_BYTE - BEG_BYTE;
        }
 
@@ -4062,7 +4061,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       else
        CHECK_CODING_SYSTEM (coding_system);
 
-      if (NILP (B_ (current_buffer, enable_multibyte_characters)))
+      if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
        /* We must suppress all character code conversion except for
           end-of-line conversion.  */
        coding_system = raw_text_coding_system (coding_system);
@@ -4080,10 +4079,10 @@ variable `last-coding-system-used' to the coding system actually used.  */)
          && NILP (replace))
        /* Visiting a file with these coding system makes the buffer
           unibyte. */
-       B_ (current_buffer, enable_multibyte_characters) = Qnil;
+       BVAR (current_buffer, enable_multibyte_characters) = Qnil;
     }
 
-  coding.dst_multibyte = ! NILP (B_ (current_buffer, enable_multibyte_characters));
+  coding.dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
   if (CODING_MAY_REQUIRE_DECODING (&coding)
       && (inserted > 0 || CODING_REQUIRE_FLUSHING (&coding)))
     {
@@ -4103,18 +4102,6 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
   /* Now INSERTED is measured in characters.  */
 
-#ifdef DOS_NT
-  /* Use the conversion type to determine buffer-file-type
-     (find-buffer-file-type is now used to help determine the
-     conversion).  */
-  if ((VECTORP (CODING_ID_EOL_TYPE (coding.id))
-       || EQ (CODING_ID_EOL_TYPE (coding.id), Qunix))
-      && ! CODING_REQUIRE_DECODING (&coding))
-    B_ (current_buffer, buffer_file_type) = Qt;
-  else
-    B_ (current_buffer, buffer_file_type) = Qnil;
-#endif
-
  handled:
 
   if (deferred_remove_unwind_protect)
@@ -4124,24 +4111,24 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
   if (!NILP (visit))
     {
-      if (!EQ (B_ (current_buffer, undo_list), Qt) && !nochange)
-       B_ (current_buffer, undo_list) = Qnil;
+      if (!EQ (BVAR (current_buffer, undo_list), Qt) && !nochange)
+       BVAR (current_buffer, undo_list) = Qnil;
 
       if (NILP (handler))
        {
          current_buffer->modtime = st.st_mtime;
          current_buffer->modtime_size = st.st_size;
-         B_ (current_buffer, filename) = orig_filename;
+         BVAR (current_buffer, filename) = orig_filename;
        }
 
       SAVE_MODIFF = MODIFF;
       BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF;
-      XSETFASTINT (B_ (current_buffer, save_length), Z - BEG);
+      XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG);
 #ifdef CLASH_DETECTION
       if (NILP (handler))
        {
-         if (!NILP (B_ (current_buffer, file_truename)))
-           unlock_file (B_ (current_buffer, file_truename));
+         if (!NILP (BVAR (current_buffer, file_truename)))
+           unlock_file (BVAR (current_buffer, file_truename));
          unlock_file (filename);
        }
 #endif /* CLASH_DETECTION */
@@ -4168,14 +4155,14 @@ variable `last-coding-system-used' to the coding system actually used.  */)
   if (inserted > 0)
     {
       /* Don't run point motion or modification hooks when decoding.  */
-      int count = SPECPDL_INDEX ();
+      int count1 = SPECPDL_INDEX ();
       EMACS_INT old_inserted = inserted;
       specbind (Qinhibit_point_motion_hooks, Qt);
       specbind (Qinhibit_modification_hooks, Qt);
 
       /* Save old undo list and don't record undo for decoding.  */
-      old_undo = B_ (current_buffer, undo_list);
-      B_ (current_buffer, undo_list) = Qt;
+      old_undo = BVAR (current_buffer, undo_list);
+      BVAR (current_buffer, undo_list) = Qt;
 
       if (NILP (replace))
        {
@@ -4263,7 +4250,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
 
       if (NILP (visit))
        {
-         B_ (current_buffer, undo_list) = old_undo;
+         BVAR (current_buffer, undo_list) = old_undo;
          if (CONSP (old_undo) && inserted != old_inserted)
            {
              /* Adjust the last undo record for the size change during
@@ -4278,9 +4265,9 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       else
        /* If undo_list was Qt before, keep it that way.
           Otherwise start with an empty undo_list.  */
-       B_ (current_buffer, undo_list) = EQ (old_undo, Qt) ? Qt : Qnil;
+       BVAR (current_buffer, undo_list) = EQ (old_undo, Qt) ? Qt : Qnil;
 
-      unbind_to (count, Qnil);
+      unbind_to (count1, Qnil);
     }
 
   /* Call after-change hooks for the inserted text, aside from the case
@@ -4297,6 +4284,7 @@ variable `last-coding-system-used' to the coding system actually used.  */)
       && current_buffer->modtime == -1)
     {
       /* If visiting nonexistent file, return nil.  */
+      errno = save_errno;
       report_file_error ("Opening input file", Fcons (orig_filename, Qnil));
     }
 
@@ -4332,8 +4320,8 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
   Lisp_Object eol_parent = Qnil;
 
   if (auto_saving
-      && NILP (Fstring_equal (B_ (current_buffer, filename),
-                             B_ (current_buffer, auto_save_file_name))))
+      && NILP (Fstring_equal (BVAR (current_buffer, filename),
+                             BVAR (current_buffer, auto_save_file_name))))
     {
       val = Qutf_8_emacs;
       eol_parent = Qunix;
@@ -4362,12 +4350,12 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
       int using_default_coding = 0;
       int force_raw_text = 0;
 
-      val = B_ (current_buffer, buffer_file_coding_system);
+      val = BVAR (current_buffer, buffer_file_coding_system);
       if (NILP (val)
          || NILP (Flocal_variable_p (Qbuffer_file_coding_system, Qnil)))
        {
          val = Qnil;
-         if (NILP (B_ (current_buffer, enable_multibyte_characters)))
+         if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
            force_raw_text = 1;
        }
 
@@ -4388,7 +4376,7 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
        {
          /* If we still have not decided a coding system, use the
             default value of buffer-file-coding-system.  */
-         val = B_ (current_buffer, buffer_file_coding_system);
+         val = BVAR (current_buffer, buffer_file_coding_system);
          using_default_coding = 1;
        }
 
@@ -4412,9 +4400,9 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
         format, we use that of
         `default-buffer-file-coding-system'.  */
       if (! using_default_coding
-         && ! NILP (B_ (&buffer_defaults, buffer_file_coding_system)))
+         && ! NILP (BVAR (&buffer_defaults, buffer_file_coding_system)))
        val = (coding_inherit_eol_type
-              (val, B_ (&buffer_defaults, buffer_file_coding_system)));
+              (val, BVAR (&buffer_defaults, buffer_file_coding_system)));
 
       /* If we decide not to encode text, use `raw-text' or one of its
         subsidiaries.  */
@@ -4425,7 +4413,7 @@ choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object file
   val = coding_inherit_eol_type (val, eol_parent);
   setup_coding_system (val, coding);
 
-  if (!STRINGP (start) && !NILP (B_ (current_buffer, selective_display)))
+  if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display)))
     coding->mode |= CODING_MODE_SELECTIVE_DISPLAY;
   return val;
 }
@@ -4484,9 +4472,6 @@ This calls `write-region-annotate-functions' at the start, and
   int quietly = !NILP (visit);
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
   struct buffer *given_buffer;
-#ifdef DOS_NT
-  int buffer_file_type = O_BINARY;
-#endif /* DOS_NT */
   struct coding_system coding;
 
   if (current_buffer->base_buffer && visiting)
@@ -4529,8 +4514,8 @@ This calls `write-region-annotate-functions' at the start, and
       if (visiting)
        {
          SAVE_MODIFF = MODIFF;
-         XSETFASTINT (B_ (current_buffer, save_length), Z - BEG);
-         B_ (current_buffer, filename) = visit_file;
+         XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG);
+         BVAR (current_buffer, filename) = visit_file;
        }
       UNGCPRO;
       return val;
@@ -4596,7 +4581,7 @@ This calls `write-region-annotate-functions' at the start, and
   desc = -1;
   if (!NILP (append))
 #ifdef DOS_NT
-    desc = emacs_open (fn, O_WRONLY | buffer_file_type, 0);
+    desc = emacs_open (fn, O_WRONLY | O_BINARY, 0);
 #else  /* not DOS_NT */
     desc = emacs_open (fn, O_WRONLY, 0);
 #endif /* not DOS_NT */
@@ -4604,7 +4589,7 @@ This calls `write-region-annotate-functions' at the start, and
   if (desc < 0 && (NILP (append) || errno == ENOENT))
 #ifdef DOS_NT
   desc = emacs_open (fn,
-                    O_WRONLY | O_CREAT | buffer_file_type
+                    O_WRONLY | O_CREAT | O_BINARY
                     | (EQ (mustbenew, Qexcl) ? O_EXCL : O_TRUNC),
                     S_IREAD | S_IWRITE);
 #else  /* not DOS_NT */
@@ -4628,16 +4613,18 @@ This calls `write-region-annotate-functions' at the start, and
 
   if (!NILP (append) && !NILP (Ffile_regular_p (filename)))
     {
-      long ret;
+      off_t 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
+         save_errno = errno;
          if (!auto_saving) unlock_file (lockname);
+         errno = save_errno;
 #endif /* CLASH_DETECTION */
          UNGCPRO;
          report_file_error ("Lseek error", Fcons (filename, Qnil));
@@ -4743,15 +4730,15 @@ This calls `write-region-annotate-functions' at the start, and
   if (visiting)
     {
       SAVE_MODIFF = MODIFF;
-      XSETFASTINT (B_ (current_buffer, save_length), Z - BEG);
-      B_ (current_buffer, filename) = visit_file;
+      XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG);
+      BVAR (current_buffer, filename) = visit_file;
       update_mode_lines++;
     }
   else if (quietly)
     {
       if (auto_saving
-         && ! NILP (Fstring_equal (B_ (current_buffer, filename),
-                                   B_ (current_buffer, auto_save_file_name))))
+         && ! NILP (Fstring_equal (BVAR (current_buffer, filename),
+                                   BVAR (current_buffer, auto_save_file_name))))
        SAVE_MODIFF = MODIFF;
 
       return Qnil;
@@ -4833,10 +4820,10 @@ build_annotations (Lisp_Object start, Lisp_Object end)
     }
 
   /* Now do the same for annotation functions implied by the file-format */
-  if (auto_saving && (!EQ (B_ (current_buffer, auto_save_file_format), Qt)))
-    p = B_ (current_buffer, auto_save_file_format);
+  if (auto_saving && (!EQ (BVAR (current_buffer, auto_save_file_format), Qt)))
+    p = BVAR (current_buffer, auto_save_file_format);
   else
-    p = B_ (current_buffer, file_format);
+    p = BVAR (current_buffer, file_format);
   for (i = 0; CONSP (p); p = XCDR (p), ++i)
     {
       struct buffer *given_buffer = current_buffer;
@@ -4875,11 +4862,13 @@ build_annotations (Lisp_Object start, Lisp_Object end)
    The return value is negative in case of system call failure.  */
 
 static int
-a_write (int desc, Lisp_Object string, int pos, register int nchars, Lisp_Object *annot, struct coding_system *coding)
+a_write (int desc, Lisp_Object string, EMACS_INT pos,
+        register EMACS_INT nchars, Lisp_Object *annot,
+        struct coding_system *coding)
 {
   Lisp_Object tem;
-  int nextpos;
-  int lastpos = pos + nchars;
+  EMACS_INT nextpos;
+  EMACS_INT lastpos = pos + nchars;
 
   while (NILP (*annot) || CONSP (*annot))
     {
@@ -4919,7 +4908,8 @@ a_write (int desc, Lisp_Object string, int pos, register int nchars, Lisp_Object
    are indexes to the string STRING.  */
 
 static int
-e_write (int desc, Lisp_Object string, int start, int end, struct coding_system *coding)
+e_write (int desc, Lisp_Object string, EMACS_INT start, EMACS_INT end,
+        struct coding_system *coding)
 {
   if (STRINGP (string))
     {
@@ -4950,8 +4940,8 @@ e_write (int desc, Lisp_Object string, int start, int end, struct coding_system
        }
       else
        {
-         int start_byte = CHAR_TO_BYTE (start);
-         int end_byte = CHAR_TO_BYTE (end);
+         EMACS_INT start_byte = CHAR_TO_BYTE (start);
+         EMACS_INT end_byte = CHAR_TO_BYTE (end);
 
          coding->src_multibyte = (end - start) < (end_byte - start_byte);
          if (CODING_REQUIRE_ENCODING (coding))
@@ -5015,17 +5005,17 @@ See Info node `(elisp)Modification Time' for more details.  */)
       b = XBUFFER (buf);
     }
 
-  if (!STRINGP (B_ (b, filename))) return Qt;
+  if (!STRINGP (BVAR (b, filename))) return Qt;
   if (b->modtime == 0) return Qt;
 
   /* If the file name has special constructs in it,
      call the corresponding file handler.  */
-  handler = Ffind_file_name_handler (B_ (b, filename),
+  handler = Ffind_file_name_handler (BVAR (b, filename),
                                     Qverify_visited_file_modtime);
   if (!NILP (handler))
     return call2 (handler, Qverify_visited_file_modtime, buf);
 
-  filename = ENCODE_FILE (B_ (b, filename));
+  filename = ENCODE_FILE (BVAR (b, filename));
 
   if (stat (SSDATA (filename), &st) < 0)
     {
@@ -5039,7 +5029,7 @@ See Info node `(elisp)Modification Time' for more details.  */)
   if ((st.st_mtime == b->modtime
        /* If both are positive, accept them if they are off by one second.  */
        || (st.st_mtime > 0 && b->modtime > 0
-          && (st.st_mtime == b->modtime + 1
+          && (st.st_mtime - 1 == b->modtime
               || st.st_mtime == b->modtime - 1)))
       && (st.st_size == b->modtime_size
           || b->modtime_size < 0))
@@ -5061,15 +5051,16 @@ Next attempt to save will certainly not complain of a discrepancy.  */)
 DEFUN ("visited-file-modtime", Fvisited_file_modtime,
        Svisited_file_modtime, 0, 0, 0,
        doc: /* Return the current buffer's recorded visited file modification time.
-The value is a list of the form (HIGH LOW), like the time values
-that `file-attributes' returns.  If the current buffer has no recorded
-file modification time, this function returns 0.
+The value is a list of the form (HIGH LOW), like the time values that
+`file-attributes' returns.  If the current buffer has no recorded file
+modification time, this function returns 0.  If the visited file
+doesn't exist, HIGH will be -1.
 See Info node `(elisp)Modification Time' for more details.  */)
   (void)
 {
   if (! current_buffer->modtime)
     return make_number (0);
-  return make_time ((time_t) current_buffer->modtime);
+  return make_time (current_buffer->modtime);
 }
 
 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime,
@@ -5084,7 +5075,7 @@ An argument specifies the modification time value to use
 {
   if (!NILP (time_list))
     {
-      current_buffer->modtime = cons_to_long (time_list);
+      CONS_TO_INTEGER (time_list, time_t, current_buffer->modtime);
       current_buffer->modtime_size = -1;
     }
   else
@@ -5093,7 +5084,7 @@ An argument specifies the modification time value to use
       struct stat st;
       Lisp_Object handler;
 
-      filename = Fexpand_file_name (B_ (current_buffer, filename), Qnil);
+      filename = Fexpand_file_name (BVAR (current_buffer, filename), Qnil);
 
       /* If the file name has special constructs in it,
         call the corresponding file handler.  */
@@ -5114,8 +5105,8 @@ An argument specifies the modification time value to use
   return Qnil;
 }
 \f
-Lisp_Object
-auto_save_error (Lisp_Object error)
+static Lisp_Object
+auto_save_error (Lisp_Object error_val)
 {
   Lisp_Object args[3], msg;
   int i, nbytes;
@@ -5128,8 +5119,8 @@ auto_save_error (Lisp_Object error)
   ring_bell (XFRAME (selected_frame));
 
   args[0] = build_string ("Auto-saving %s: %s");
-  args[1] = B_ (current_buffer, name);
-  args[2] = Ferror_message_string (error);
+  args[1] = BVAR (current_buffer, name);
+  args[2] = Ferror_message_string (error_val);
   msg = Fformat (3, args);
   GCPRO1 (msg);
   nbytes = SBYTES (msg);
@@ -5150,7 +5141,7 @@ auto_save_error (Lisp_Object error)
   return Qnil;
 }
 
-Lisp_Object
+static Lisp_Object
 auto_save_1 (void)
 {
   struct stat st;
@@ -5159,19 +5150,19 @@ auto_save_1 (void)
   auto_save_mode_bits = 0666;
 
   /* Get visited file's mode to become the auto save file's mode.  */
-  if (! NILP (B_ (current_buffer, filename)))
+  if (! NILP (BVAR (current_buffer, filename)))
     {
-      if (stat (SSDATA (B_ (current_buffer, filename)), &st) >= 0)
+      if (stat (SSDATA (BVAR (current_buffer, filename)), &st) >= 0)
        /* But make sure we can overwrite it later!  */
-       auto_save_mode_bits = st.st_mode | 0600;
-      else if ((modes = Ffile_modes (B_ (current_buffer, filename)),
+       auto_save_mode_bits = (st.st_mode | 0600) & 0777;
+      else if ((modes = Ffile_modes (BVAR (current_buffer, filename)),
                INTEGERP (modes)))
        /* Remote files don't cooperate with stat.  */
-       auto_save_mode_bits = XINT (modes) | 0600;
+       auto_save_mode_bits = (XINT (modes) | 0600) & 0777;
     }
 
   return
-    Fwrite_region (Qnil, Qnil, B_ (current_buffer, auto_save_file_name), Qnil,
+    Fwrite_region (Qnil, Qnil, BVAR (current_buffer, auto_save_file_name), Qnil,
                   NILP (Vauto_save_visited_file_name) ? Qlambda : Qt,
                   Qnil, Qnil);
 }
@@ -5229,7 +5220,7 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
   (Lisp_Object no_message, Lisp_Object current_only)
 {
   struct buffer *old = current_buffer, *b;
-  Lisp_Object tail, buf;
+  Lisp_Object tail, buf, hook;
   int auto_saved = 0;
   int do_handled_files;
   Lisp_Object oquit;
@@ -5259,8 +5250,8 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
   /* No GCPRO needed, because (when it matters) all Lisp_Object variables
      point to non-strings reached from Vbuffer_alist.  */
 
-  if (!NILP (Vrun_hooks))
-    call1 (Vrun_hooks, intern ("auto-save-hook"));
+  hook = intern ("auto-save-hook");
+  Frun_hooks (1, &hook);
 
   if (STRINGP (Vauto_save_list_file_name))
     {
@@ -5312,18 +5303,18 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
        /* Record all the buffers that have auto save mode
           in the special file that lists them.  For each of these buffers,
           Record visited name (if any) and auto save name.  */
-       if (STRINGP (B_ (b, auto_save_file_name))
+       if (STRINGP (BVAR (b, auto_save_file_name))
            && stream != NULL && do_handled_files == 0)
          {
            BLOCK_INPUT;
-           if (!NILP (B_ (b, filename)))
+           if (!NILP (BVAR (b, filename)))
              {
-               fwrite (SDATA (B_ (b, filename)), 1,
-                       SBYTES (B_ (b, filename)), stream);
+               fwrite (SDATA (BVAR (b, filename)), 1,
+                       SBYTES (BVAR (b, filename)), stream);
              }
            putc ('\n', stream);
-           fwrite (SDATA (B_ (b, auto_save_file_name)), 1,
-                   SBYTES (B_ (b, auto_save_file_name)), stream);
+           fwrite (SDATA (BVAR (b, auto_save_file_name)), 1,
+                   SBYTES (BVAR (b, auto_save_file_name)), stream);
            putc ('\n', stream);
            UNBLOCK_INPUT;
          }
@@ -5340,13 +5331,13 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
        /* Check for auto save enabled
           and file changed since last auto save
           and file changed since last real save.  */
-       if (STRINGP (B_ (b, auto_save_file_name))
+       if (STRINGP (BVAR (b, auto_save_file_name))
            && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
            && BUF_AUTOSAVE_MODIFF (b) < BUF_MODIFF (b)
            /* -1 means we've turned off autosaving for a while--see below.  */
-           && XINT (B_ (b, save_length)) >= 0
+           && XINT (BVAR (b, save_length)) >= 0
            && (do_handled_files
-               || NILP (Ffind_file_name_handler (B_ (b, auto_save_file_name),
+               || NILP (Ffind_file_name_handler (BVAR (b, auto_save_file_name),
                                                  Qwrite_region))))
          {
            EMACS_TIME before_time, after_time;
@@ -5354,29 +5345,29 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
            EMACS_GET_TIME (before_time);
 
            /* If we had a failure, don't try again for 20 minutes.  */
-           if (b->auto_save_failure_time >= 0
+           if (b->auto_save_failure_time > 0
                && EMACS_SECS (before_time) - b->auto_save_failure_time < 1200)
              continue;
 
            set_buffer_internal (b);
            if (NILP (Vauto_save_include_big_deletions)
-               && (XFASTINT (B_ (b, save_length)) * 10
+               && (XFASTINT (BVAR (b, save_length)) * 10
                    > (BUF_Z (b) - BUF_BEG (b)) * 13)
                /* A short file is likely to change a large fraction;
                   spare the user annoying messages.  */
-               && XFASTINT (B_ (b, save_length)) > 5000
+               && XFASTINT (BVAR (b, save_length)) > 5000
                /* These messages are frequent and annoying for `*mail*'.  */
-               && !EQ (B_ (b, filename), Qnil)
+               && !EQ (BVAR (b, filename), Qnil)
                && NILP (no_message))
              {
                /* It has shrunk too much; turn off auto-saving here.  */
                minibuffer_auto_raise = orig_minibuffer_auto_raise;
                message_with_string ("Buffer %s has shrunk a lot; auto save disabled in that buffer until next real save",
-                                    B_ (b, name), 1);
+                                    BVAR (b, name), 1);
                minibuffer_auto_raise = 0;
                /* Turn off auto-saving until there's a real save,
                   and prevent any more warnings.  */
-               XSETINT (B_ (b, save_length), -1);
+               XSETINT (BVAR (b, save_length), -1);
                Fsleep_for (make_number (1), Qnil);
                continue;
              }
@@ -5385,7 +5376,7 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
            internal_condition_case (auto_save_1, Qt, auto_save_error);
            auto_saved++;
            BUF_AUTOSAVE_MODIFF (b) = BUF_MODIFF (b);
-           XSETFASTINT (B_ (current_buffer, save_length), Z - BEG);
+           XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG);
            set_buffer_internal (old);
 
            EMACS_GET_TIME (after_time);
@@ -5432,8 +5423,8 @@ No auto-save file will be written until the buffer changes again.  */)
   /* FIXME: This should not be called in indirect buffers, since
      they're not autosaved.  */
   BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF;
-  XSETFASTINT (B_ (current_buffer, save_length), Z - BEG);
-  current_buffer->auto_save_failure_time = -1;
+  XSETFASTINT (BVAR (current_buffer, save_length), Z - BEG);
+  current_buffer->auto_save_failure_time = 0;
   return Qnil;
 }
 
@@ -5442,7 +5433,7 @@ DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure,
        doc: /* Clear any record of a recent auto-save failure in the current buffer.  */)
   (void)
 {
-  current_buffer->auto_save_failure_time = -1;
+  current_buffer->auto_save_failure_time = 0;
   return Qnil;
 }
 
@@ -5481,7 +5472,7 @@ before any other event (mouse or keypress) is handled.  */)
 Lisp_Object
 Fread_file_name (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object initial, Lisp_Object predicate)
 {
-  struct gcpro gcpro1, gcpro2;
+  struct gcpro gcpro1;
   Lisp_Object args[7];
 
   GCPRO1 (default_filename);
@@ -5499,97 +5490,50 @@ Fread_file_name (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filena
 void
 syms_of_fileio (void)
 {
-  Qoperations = intern_c_string ("operations");
-  Qexpand_file_name = intern_c_string ("expand-file-name");
-  Qsubstitute_in_file_name = intern_c_string ("substitute-in-file-name");
-  Qdirectory_file_name = intern_c_string ("directory-file-name");
-  Qfile_name_directory = intern_c_string ("file-name-directory");
-  Qfile_name_nondirectory = intern_c_string ("file-name-nondirectory");
-  Qunhandled_file_name_directory = intern_c_string ("unhandled-file-name-directory");
-  Qfile_name_as_directory = intern_c_string ("file-name-as-directory");
-  Qcopy_file = intern_c_string ("copy-file");
-  Qmake_directory_internal = intern_c_string ("make-directory-internal");
-  Qmake_directory = intern_c_string ("make-directory");
-  Qdelete_directory_internal = intern_c_string ("delete-directory-internal");
-  Qdelete_file = intern_c_string ("delete-file");
-  Qrename_file = intern_c_string ("rename-file");
-  Qadd_name_to_file = intern_c_string ("add-name-to-file");
-  Qmake_symbolic_link = intern_c_string ("make-symbolic-link");
-  Qfile_exists_p = intern_c_string ("file-exists-p");
-  Qfile_executable_p = intern_c_string ("file-executable-p");
-  Qfile_readable_p = intern_c_string ("file-readable-p");
-  Qfile_writable_p = intern_c_string ("file-writable-p");
-  Qfile_symlink_p = intern_c_string ("file-symlink-p");
-  Qaccess_file = intern_c_string ("access-file");
-  Qfile_directory_p = intern_c_string ("file-directory-p");
-  Qfile_regular_p = intern_c_string ("file-regular-p");
-  Qfile_accessible_directory_p = intern_c_string ("file-accessible-directory-p");
-  Qfile_modes = intern_c_string ("file-modes");
-  Qset_file_modes = intern_c_string ("set-file-modes");
-  Qset_file_times = intern_c_string ("set-file-times");
-  Qfile_selinux_context = intern_c_string("file-selinux-context");
-  Qset_file_selinux_context = intern_c_string("set-file-selinux-context");
-  Qfile_newer_than_file_p = intern_c_string ("file-newer-than-file-p");
-  Qinsert_file_contents = intern_c_string ("insert-file-contents");
-  Qwrite_region = intern_c_string ("write-region");
-  Qverify_visited_file_modtime = intern_c_string ("verify-visited-file-modtime");
-  Qset_visited_file_modtime = intern_c_string ("set-visited-file-modtime");
-  Qauto_save_coding = intern_c_string ("auto-save-coding");
-
-  staticpro (&Qoperations);
-  staticpro (&Qexpand_file_name);
-  staticpro (&Qsubstitute_in_file_name);
-  staticpro (&Qdirectory_file_name);
-  staticpro (&Qfile_name_directory);
-  staticpro (&Qfile_name_nondirectory);
-  staticpro (&Qunhandled_file_name_directory);
-  staticpro (&Qfile_name_as_directory);
-  staticpro (&Qcopy_file);
-  staticpro (&Qmake_directory_internal);
-  staticpro (&Qmake_directory);
-  staticpro (&Qdelete_directory_internal);
-  staticpro (&Qdelete_file);
-  staticpro (&Qrename_file);
-  staticpro (&Qadd_name_to_file);
-  staticpro (&Qmake_symbolic_link);
-  staticpro (&Qfile_exists_p);
-  staticpro (&Qfile_executable_p);
-  staticpro (&Qfile_readable_p);
-  staticpro (&Qfile_writable_p);
-  staticpro (&Qaccess_file);
-  staticpro (&Qfile_symlink_p);
-  staticpro (&Qfile_directory_p);
-  staticpro (&Qfile_regular_p);
-  staticpro (&Qfile_accessible_directory_p);
-  staticpro (&Qfile_modes);
-  staticpro (&Qset_file_modes);
-  staticpro (&Qset_file_times);
-  staticpro (&Qfile_selinux_context);
-  staticpro (&Qset_file_selinux_context);
-  staticpro (&Qfile_newer_than_file_p);
-  staticpro (&Qinsert_file_contents);
-  staticpro (&Qwrite_region);
-  staticpro (&Qverify_visited_file_modtime);
-  staticpro (&Qset_visited_file_modtime);
-  staticpro (&Qauto_save_coding);
-
-  Qfile_name_history = intern_c_string ("file-name-history");
+  DEFSYM (Qoperations, "operations");
+  DEFSYM (Qexpand_file_name, "expand-file-name");
+  DEFSYM (Qsubstitute_in_file_name, "substitute-in-file-name");
+  DEFSYM (Qdirectory_file_name, "directory-file-name");
+  DEFSYM (Qfile_name_directory, "file-name-directory");
+  DEFSYM (Qfile_name_nondirectory, "file-name-nondirectory");
+  DEFSYM (Qunhandled_file_name_directory, "unhandled-file-name-directory");
+  DEFSYM (Qfile_name_as_directory, "file-name-as-directory");
+  DEFSYM (Qcopy_file, "copy-file");
+  DEFSYM (Qmake_directory_internal, "make-directory-internal");
+  DEFSYM (Qmake_directory, "make-directory");
+  DEFSYM (Qdelete_directory_internal, "delete-directory-internal");
+  DEFSYM (Qdelete_file, "delete-file");
+  DEFSYM (Qrename_file, "rename-file");
+  DEFSYM (Qadd_name_to_file, "add-name-to-file");
+  DEFSYM (Qmake_symbolic_link, "make-symbolic-link");
+  DEFSYM (Qfile_exists_p, "file-exists-p");
+  DEFSYM (Qfile_executable_p, "file-executable-p");
+  DEFSYM (Qfile_readable_p, "file-readable-p");
+  DEFSYM (Qfile_writable_p, "file-writable-p");
+  DEFSYM (Qfile_symlink_p, "file-symlink-p");
+  DEFSYM (Qaccess_file, "access-file");
+  DEFSYM (Qfile_directory_p, "file-directory-p");
+  DEFSYM (Qfile_regular_p, "file-regular-p");
+  DEFSYM (Qfile_accessible_directory_p, "file-accessible-directory-p");
+  DEFSYM (Qfile_modes, "file-modes");
+  DEFSYM (Qset_file_modes, "set-file-modes");
+  DEFSYM (Qset_file_times, "set-file-times");
+  DEFSYM (Qfile_selinux_context, "file-selinux-context");
+  DEFSYM (Qset_file_selinux_context, "set-file-selinux-context");
+  DEFSYM (Qfile_newer_than_file_p, "file-newer-than-file-p");
+  DEFSYM (Qinsert_file_contents, "insert-file-contents");
+  DEFSYM (Qwrite_region, "write-region");
+  DEFSYM (Qverify_visited_file_modtime, "verify-visited-file-modtime");
+  DEFSYM (Qset_visited_file_modtime, "set-visited-file-modtime");
+  DEFSYM (Qauto_save_coding, "auto-save-coding");
+
+  DEFSYM (Qfile_name_history, "file-name-history");
   Fset (Qfile_name_history, Qnil);
-  staticpro (&Qfile_name_history);
 
-  Qfile_error = intern_c_string ("file-error");
-  staticpro (&Qfile_error);
-  Qfile_already_exists = intern_c_string ("file-already-exists");
-  staticpro (&Qfile_already_exists);
-  Qfile_date_error = intern_c_string ("file-date-error");
-  staticpro (&Qfile_date_error);
-  Qexcl = intern_c_string ("excl");
-  staticpro (&Qexcl);
-
-#ifdef DOS_NT
-  Qfind_buffer_file_type = intern_c_string ("find-buffer-file-type");
-  staticpro (&Qfind_buffer_file_type);
-#endif /* DOS_NT */
+  DEFSYM (Qfile_error, "file-error");
+  DEFSYM (Qfile_already_exists, "file-already-exists");
+  DEFSYM (Qfile_date_error, "file-date-error");
+  DEFSYM (Qexcl, "excl");
 
   DEFVAR_LISP ("file-name-coding-system", Vfile_name_coding_system,
               doc: /* *Coding system for encoding file names.
@@ -5607,15 +5551,10 @@ instead use `file-name-coding-system' to get a constant encoding
 of file names regardless of the current language environment.  */);
   Vdefault_file_name_coding_system = Qnil;
 
-  Qformat_decode = intern_c_string ("format-decode");
-  staticpro (&Qformat_decode);
-  Qformat_annotate_function = intern_c_string ("format-annotate-function");
-  staticpro (&Qformat_annotate_function);
-  Qafter_insert_file_set_coding = intern_c_string ("after-insert-file-set-coding");
-  staticpro (&Qafter_insert_file_set_coding);
-
-  Qcar_less_than_car = intern_c_string ("car-less-than-car");
-  staticpro (&Qcar_less_than_car);
+  DEFSYM (Qformat_decode, "format-decode");
+  DEFSYM (Qformat_annotate_function, "format-annotate-function");
+  DEFSYM (Qafter_insert_file_set_coding, "after-insert-file-set-coding");
+  DEFSYM (Qcar_less_than_car, "car-less-than-car");
 
   Fput (Qfile_error, Qerror_conditions,
        Fpurecopy (list2 (Qfile_error, Qerror)));
@@ -5694,9 +5633,7 @@ After `write-region' completes, Emacs calls the function stored in
 current when building the annotations (i.e., at least once), with that
 buffer current.  */);
   Vwrite_region_annotate_functions = Qnil;
-  staticpro (&Qwrite_region_annotate_functions);
-  Qwrite_region_annotate_functions
-    = intern_c_string ("write-region-annotate-functions");
+  DEFSYM (Qwrite_region_annotate_functions, "write-region-annotate-functions");
 
   DEFVAR_LISP ("write-region-post-annotation-function",
               Vwrite_region_post_annotation_function,
@@ -5760,12 +5697,10 @@ This includes interactive calls to `delete-file' and
 `delete-directory' and the Dired deletion commands.  */);
   delete_by_moving_to_trash = 0;
   Qdelete_by_moving_to_trash = intern_c_string ("delete-by-moving-to-trash");
-  Qmove_file_to_trash = intern_c_string ("move-file-to-trash");
-  staticpro (&Qmove_file_to_trash);
-  Qcopy_directory = intern_c_string ("copy-directory");
-  staticpro (&Qcopy_directory);
-  Qdelete_directory = intern_c_string ("delete-directory");
-  staticpro (&Qdelete_directory);
+
+  DEFSYM (Qmove_file_to_trash, "move-file-to-trash");
+  DEFSYM (Qcopy_directory, "copy-directory");
+  DEFSYM (Qdelete_directory, "delete-directory");
 
   defsubr (&Sfind_file_name_handler);
   defsubr (&Sfile_name_directory);