Merged from emacs@sv.gnu.org. Last-minute emacsclient rewrites be damned!
[bpt/emacs.git] / src / fileio.c
index 1cd996d..e6a4314 100644 (file)
@@ -1,7 +1,7 @@
 /* File IO for GNU Emacs.
    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996,
                  1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
-                 2005 Free Software Foundation, Inc.
+                 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -77,6 +77,7 @@ extern int errno;
 #include "charset.h"
 #include "coding.h"
 #include "window.h"
+#include "blockinput.h"
 #include "frame.h"
 #include "dispextern.h"
 
@@ -281,7 +282,7 @@ report_file_error (string, data)
     switch (errorno)
       {
       case EEXIST:
-       Fsignal (Qfile_already_exists, Fcons (errstring, data));
+       xsignal (Qfile_already_exists, Fcons (errstring, data));
        break;
       default:
        /* System error messages are capitalized.  Downcase the initial
@@ -289,7 +290,7 @@ report_file_error (string, data)
        if (SREF (errstring, 1) != '/')
          SSET (errstring, 0, DOWNCASE (SREF (errstring, 0)));
 
-       Fsignal (Qfile_error,
+       xsignal (Qfile_error,
                 Fcons (build_string (string), Fcons (errstring, data)));
       }
 }
@@ -1249,9 +1250,9 @@ See also the function `substitute-in-file-name'.  */)
              }
            else
 #endif /* NO_HYPHENS_IN_FILENAMES */
-             if (lbrack > rbrack &&
-                 ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
-                  (p[1] == '.' || p[1] == ']' || p[1] == '>')))
+             if (lbrack > rbrack
+                 && ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<')
+                     && (p[1] == '.' || p[1] == ']' || p[1] == '>')))
                lose = 1;
 #ifdef NO_HYPHENS_IN_FILENAMES
              else
@@ -1388,7 +1389,9 @@ See also the function `substitute-in-file-name'.  */)
          bcopy ((char *) nm, o, p - nm);
          o [p - nm] = 0;
 
+         BLOCK_INPUT;
          pw = (struct passwd *) getpwnam (o + 1);
+         UNBLOCK_INPUT;
          if (pw)
            {
              newdir = (unsigned char *) pw -> pw_dir;
@@ -1618,8 +1621,8 @@ See also the function `substitute-in-file-name'.  */)
          else if (*p == '-' && *o != '.')
            *--p = '.';
        }
-      else if (p[0] == '-' && o[-1] == '.' &&
-              (p[1] == '.' || p[1] == ']' || p[1] == '>'))
+      else if (p[0] == '-' && o[-1] == '.'
+              && (p[1] == '.' || p[1] == ']' || p[1] == '>'))
        /* flush .foo.- ; leave - if stopped by '[' or '<' */
        {
          do
@@ -1634,9 +1637,9 @@ See also the function `substitute-in-file-name'.  */)
       else
        {
 #ifdef NO_HYPHENS_IN_FILENAMES
-         if (*p == '-' &&
-             o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
-             p[1] != ']' && p[1] != '>' && p[1] != '.')
+         if (*p == '-'
+             && o[-1] != '[' && o[-1] != '<' && o[-1] != '.'
+             && p[1] != ']' && p[1] != '>' && p[1] != '.')
            *p = '_';
 #endif /* NO_HYPHENS_IN_FILENAMES */
          *o++ = *p++;
@@ -1646,8 +1649,7 @@ See also the function `substitute-in-file-name'.  */)
        {
          *o++ = *p++;
        }
-      else if (IS_DIRECTORY_SEP (p[0])
-              && p[1] == '.'
+      else if (p[1] == '.'
               && (IS_DIRECTORY_SEP (p[2])
                   || p[2] == 0))
        {
@@ -1657,7 +1659,7 @@ See also the function `substitute-in-file-name'.  */)
            *o++ = *p;
          p += 2;
        }
-      else if (IS_DIRECTORY_SEP (p[0]) && p[1] == '.' && p[2] == '.'
+      else if (p[1] == '.' && p[2] == '.'
               /* `/../' is the "superroot" on certain file systems.
                  Turned off on DOS_NT systems because they have no
                  "superroot" and because this causes us to produce
@@ -1677,14 +1679,9 @@ See also the function `substitute-in-file-name'.  */)
            ++o;
          p += 3;
        }
-      else if (p > target
-              && IS_DIRECTORY_SEP (p[0]) && IS_DIRECTORY_SEP (p[1]))
-       {
-         /* Collapse multiple `/' in a row.  */
-         *o++ = *p++;
-         while (IS_DIRECTORY_SEP (*p))
-           ++p;
-       }
+      else if (p > target && IS_DIRECTORY_SEP (p[1]))
+       /* Collapse multiple `/' in a row.  */
+       p++;
       else
        {
          *o++ = *p++;
@@ -1825,9 +1822,9 @@ See also the function `substitute-in-file-name'.")
              }
            else
 #endif /* VMS4_4 */
-             if (lbrack > rbrack &&
-                 ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
-                  (p[1] == '.' || p[1] == ']' || p[1] == '>')))
+             if (lbrack > rbrack
+                 && ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<')
+                     && (p[1] == '.' || p[1] == ']' || p[1] == '>')))
                lose = 1;
 #ifndef VMS4_4
              else
@@ -1925,7 +1922,9 @@ See also the function `substitute-in-file-name'.")
        o[len] = 0;
 
        /* Look up the user name. */
+       BLOCK_INPUT;
        pw = (struct passwd *) getpwnam (o + 1);
+       UNBLOCK_INPUT;
        if (!pw)
          error ("\"%s\" isn't a registered user", o + 1);
 
@@ -1994,8 +1993,8 @@ See also the function `substitute-in-file-name'.")
          else if (*p == '-' && *o != '.')
            *--p = '.';
        }
-      else if (p[0] == '-' && o[-1] == '.' &&
-              (p[1] == '.' || p[1] == ']' || p[1] == '>'))
+      else if (p[0] == '-' && o[-1] == '.'
+              && (p[1] == '.' || p[1] == ']' || p[1] == '>'))
        /* flush .foo.- ; leave - if stopped by '[' or '<' */
        {
          do
@@ -2010,9 +2009,9 @@ See also the function `substitute-in-file-name'.")
       else
        {
 #ifndef VMS4_4
-         if (*p == '-' &&
-             o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
-             p[1] != ']' && p[1] != '>' && p[1] != '.')
+         if (*p == '-'
+             && o[-1] != '[' && o[-1] != '<' && o[-1] != '.'
+             && p[1] != ']' && p[1] != '>' && p[1] != '.')
            *p = '_';
 #endif /* VMS4_4 */
          *o++ = *p++;
@@ -2032,8 +2031,8 @@ See also the function `substitute-in-file-name'.")
          o = target;
          p++;
        }
-      else if (p[0] == '/' && p[1] == '.' &&
-              (p[2] == '/' || p[2] == 0))
+      else if (p[0] == '/' && p[1] == '.'
+              && (p[2] == '/' || p[2] == 0))
        p += 2;
       else if (!strncmp (p, "/..", 3)
               /* `/../' is the "superroot" on certain file systems.  */
@@ -2119,10 +2118,11 @@ search_embedded_absfilename (nm, endp)
              /* If we have ~user and `user' exists, discard
                 everything up to ~.  But if `user' does not exist, leave
                 ~user alone, it might be a literal file name.  */
-             if ((pw = getpwnam (o + 1)))
+             BLOCK_INPUT;
+             pw = getpwnam (o + 1);
+             UNBLOCK_INPUT;
+             if (pw)
                return p;
-             else
-               xfree (pw);
            }
          else
            return p;
@@ -2386,9 +2386,8 @@ barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick)
   if (lstat (SDATA (encoded_filename), &statbuf) >= 0)
     {
       if (! interactive)
-       Fsignal (Qfile_already_exists,
-                Fcons (build_string ("File already exists"),
-                       Fcons (absname, Qnil)));
+       xsignal2 (Qfile_already_exists,
+                 build_string ("File already exists"), absname);
       GCPRO1 (absname);
       tem = format2 ("File %s already exists; %s anyway? ",
                     absname, build_string (querystring));
@@ -2398,9 +2397,8 @@ barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick)
        tem = do_yes_or_no_p (tem);
       UNGCPRO;
       if (NILP (tem))
-       Fsignal (Qfile_already_exists,
-                Fcons (build_string ("File already exists"),
-                       Fcons (absname, Qnil)));
+       xsignal2 (Qfile_already_exists,
+                 build_string ("File already exists"), absname);
       if (statptr)
        *statptr = statbuf;
     }
@@ -2412,32 +2410,31 @@ barf_or_query_if_file_exists (absname, querystring, interactive, statptr, quick)
   return;
 }
 
-DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 6,
+DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 5,
        "fCopy file: \nGCopy %s to file: \np\nP",
        doc: /* Copy FILE to NEWNAME.  Both args must be strings.
 If NEWNAME names a directory, copy FILE there.
-Signals a `file-already-exists' error if file NEWNAME already exists,
-unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
-A number as third arg means request confirmation if NEWNAME already exists.
-This is what happens in interactive use with M-x.
-Always sets the file modes of the output file to match the input file.
+
+This function always sets the file modes of the output file to match
+the input file.
+
+The optional third argument OK-IF-ALREADY-EXISTS specifies what to do
+if file NEWNAME already exists.  If OK-IF-ALREADY-EXISTS is nil, we
+signal a `file-already-exists' error without overwriting.  If
+OK-IF-ALREADY-EXISTS is a number, we request confirmation from the user
+about overwriting; this is what happens in interactive use with M-x.
+Any other value for OK-IF-ALREADY-EXISTS means to overwrite the
+existing file.
 
 Fourth arg KEEP-TIME non-nil means give the output file the same
 last-modified time as the old one.  (This works on only some systems.)
 
 A prefix arg makes KEEP-TIME non-nil.
 
-The optional fifth arg MUSTBENEW, if non-nil, insists on a check
-for an existing file with the same name.  If MUSTBENEW is `excl',
-that means to get an error if the file already exists; never overwrite.
-If MUSTBENEW is neither nil nor `excl', that means ask for
-confirmation before overwriting, but do go ahead and overwrite the file
-if the user confirms.
-
 If PRESERVE-UID-GID is non-nil, we try to transfer the
 uid and gid of FILE to NEWNAME.  */)
-  (file, newname, ok_if_already_exists, keep_time, mustbenew, preserve_uid_gid)
-     Lisp_Object file, newname, ok_if_already_exists, keep_time, mustbenew;
+  (file, newname, ok_if_already_exists, keep_time, preserve_uid_gid)
+     Lisp_Object file, newname, ok_if_already_exists, keep_time;
      Lisp_Object preserve_uid_gid;
 {
   int ifd, ofd, n;
@@ -2454,9 +2451,6 @@ uid and gid of FILE to NEWNAME.  */)
   CHECK_STRING (file);
   CHECK_STRING (newname);
 
-  if (!NILP (mustbenew) && !EQ (mustbenew, Qexcl))
-    barf_or_query_if_file_exists (newname, "overwrite", 1, 0, 1);
-
   if (!NILP (Ffile_directory_p (newname)))
     newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname);
   else
@@ -2506,9 +2500,8 @@ uid and gid of FILE to NEWNAME.  */)
        {
          /* Restore original attributes.  */
          SetFileAttributes (filename, attributes);
-         Fsignal (Qfile_date_error,
-                  Fcons (build_string ("Cannot set file date"),
-                         Fcons (newname, Qnil)));
+         xsignal2 (Qfile_date_error,
+                   build_string ("Cannot set file date"), newname);
        }
       /* Restore original attributes.  */
       SetFileAttributes (filename, attributes);
@@ -2559,12 +2552,12 @@ uid and gid of FILE to NEWNAME.  */)
   /* System's default file type was set to binary by _fmode in emacs.c.  */
   ofd = emacs_open (SDATA (encoded_newname),
                    O_WRONLY | O_TRUNC | O_CREAT
-                   | (EQ (mustbenew, Qexcl) ? O_EXCL : 0),
+                   | (NILP (ok_if_already_exists) ? O_EXCL : 0),
                    S_IREAD | S_IWRITE);
 #else  /* not MSDOS */
   ofd = emacs_open (SDATA (encoded_newname),
                    O_WRONLY | O_TRUNC | O_CREAT
-                   | (EQ (mustbenew, Qexcl) ? O_EXCL : 0),
+                   | (NILP (ok_if_already_exists) ? O_EXCL : 0),
                    0666);
 #endif /* not MSDOS */
 #endif /* VMS */
@@ -2604,9 +2597,8 @@ uid and gid of FILE to NEWNAME.  */)
          EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
          if (set_file_times (SDATA (encoded_newname),
                              atime, mtime))
-           Fsignal (Qfile_date_error,
-                    Fcons (build_string ("Cannot set file date"),
-                           Fcons (newname, Qnil)));
+           xsignal2 (Qfile_date_error,
+                     build_string ("Cannot set file date"), newname);
        }
     }
 
@@ -2658,7 +2650,7 @@ DEFUN ("make-directory-internal", Fmake_directory_internal,
 #else
   if (mkdir (dir, 0777) != 0)
 #endif
-    report_file_error ("Creating directory", Flist (1, &directory));
+    report_file_error ("Creating directory", list1 (directory));
 
   return Qnil;
 }
@@ -2684,7 +2676,7 @@ DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete
   dir = SDATA (encoded_dir);
 
   if (rmdir (dir) != 0)
-    report_file_error ("Removing directory", Flist (1, &directory));
+    report_file_error ("Removing directory", list1 (directory));
 
   return Qnil;
 }
@@ -2702,9 +2694,9 @@ If file has multiple names, it continues to exist with the other names.  */)
   GCPRO1 (filename);
   if (!NILP (Ffile_directory_p (filename))
       && NILP (Ffile_symlink_p (filename)))
-    Fsignal (Qfile_error,
-            Fcons (build_string ("Removing old name: is a directory"),
-                   Fcons (filename, Qnil)));
+    xsignal2 (Qfile_error,
+             build_string ("Removing old name: is a directory"),
+             filename);
   UNGCPRO;
   filename = Fexpand_file_name (filename, Qnil);
 
@@ -2715,7 +2707,7 @@ If file has multiple names, it continues to exist with the other names.  */)
   encoded_file = ENCODE_FILE (filename);
 
   if (0 > unlink (SDATA (encoded_file)))
-    report_file_error ("Removing old name", Flist (1, &filename));
+    report_file_error ("Removing old name", list1 (filename));
   return Qnil;
 }
 
@@ -2732,8 +2724,10 @@ int
 internal_delete_file (filename)
      Lisp_Object filename;
 {
-  return NILP (internal_condition_case_1 (Fdelete_file, filename,
-                                         Qt, internal_delete_file_1));
+  Lisp_Object tem;
+  tem = internal_condition_case_1 (Fdelete_file, filename,
+                                  Qt, internal_delete_file_1);
+  return NILP (tem);
 }
 \f
 DEFUN ("rename-file", Frename_file, Srename_file, 2, 3,
@@ -2747,9 +2741,6 @@ This is what happens in interactive use with M-x.  */)
      (file, newname, ok_if_already_exists)
      Lisp_Object file, newname, ok_if_already_exists;
 {
-#ifdef NO_ARG_ARRAY
-  Lisp_Object args[2];
-#endif
   Lisp_Object handler;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
   Lisp_Object encoded_file, encoded_newname, symlink_target;
@@ -2760,7 +2751,13 @@ This is what happens in interactive use with M-x.  */)
   CHECK_STRING (newname);
   file = Fexpand_file_name (file, Qnil);
 
-  if (!NILP (Ffile_directory_p (newname)))
+  if ((!NILP (Ffile_directory_p (newname)))
+#ifdef DOS_NT
+      /* If the file names are identical but for the case,
+        don't attempt to move directory to itself. */
+      && (NILP (Fstring_equal (Fdowncase (file), Fdowncase (newname))))
+#endif
+      )
     newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname);
   else
     newname = Fexpand_file_name (newname, Qnil);
@@ -2807,20 +2804,12 @@ This is what happens in interactive use with M-x.  */)
                        /* We have already prompted if it was an integer,
                           so don't have copy-file prompt again.  */
                        NILP (ok_if_already_exists) ? Qnil : Qt,
-                       Qt, Qnil, Qt);
+                       Qt, Qt);
 
          Fdelete_file (file);
        }
       else
-#ifdef NO_ARG_ARRAY
-       {
-         args[0] = file;
-         args[1] = newname;
-         report_file_error ("Renaming", Flist (2, args));
-       }
-#else
-       report_file_error ("Renaming", Flist (2, &file));
-#endif
+       report_file_error ("Renaming", list2 (file, newname));
     }
   UNGCPRO;
   return Qnil;
@@ -2836,9 +2825,6 @@ This is what happens in interactive use with M-x.  */)
      (file, newname, ok_if_already_exists)
      Lisp_Object file, newname, ok_if_already_exists;
 {
-#ifdef NO_ARG_ARRAY
-  Lisp_Object args[2];
-#endif
   Lisp_Object handler;
   Lisp_Object encoded_file, encoded_newname;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
@@ -2878,15 +2864,7 @@ This is what happens in interactive use with M-x.  */)
 
   unlink (SDATA (newname));
   if (0 > link (SDATA (encoded_file), SDATA (encoded_newname)))
-    {
-#ifdef NO_ARG_ARRAY
-      args[0] = file;
-      args[1] = newname;
-      report_file_error ("Adding new name", Flist (2, args));
-#else
-      report_file_error ("Adding new name", Flist (2, &file));
-#endif
-    }
+    report_file_error ("Adding new name", list2 (file, newname));
 
   UNGCPRO;
   return Qnil;
@@ -2904,9 +2882,6 @@ This happens for interactive use with M-x.  */)
      (filename, linkname, ok_if_already_exists)
      Lisp_Object filename, linkname, ok_if_already_exists;
 {
-#ifdef NO_ARG_ARRAY
-  Lisp_Object args[2];
-#endif
   Lisp_Object handler;
   Lisp_Object encoded_filename, encoded_linkname;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
@@ -2962,13 +2937,7 @@ This happens for interactive use with M-x.  */)
            }
        }
 
-#ifdef NO_ARG_ARRAY
-      args[0] = filename;
-      args[1] = linkname;
-      report_file_error ("Making symbolic link", Flist (2, args));
-#else
-      report_file_error ("Making symbolic link", Flist (2, &filename));
-#endif
+      report_file_error ("Making symbolic link", list2 (filename, linkname));
     }
   UNGCPRO;
   return Qnil;
@@ -3394,8 +3363,10 @@ searchable directory.  */)
 }
 
 DEFUN ("file-regular-p", Ffile_regular_p, Sfile_regular_p, 1, 1, 0,
-       doc: /* Return t if file FILENAME is the name of a regular file.
-This is the sort of file that holds an ordinary stream of data bytes.  */)
+       doc: /* Return t if FILENAME names a regular file.
+This is the sort of file that holds an ordinary stream of data bytes.
+Symbolic links to regular files count as regular files.
+See `file-symlink-p' to distinguish symlinks.  */)
      (filename)
      Lisp_Object filename;
 {
@@ -3844,9 +3815,8 @@ actually used.  */)
        goto notfound;
 
       if (! NILP (replace) || ! NILP (beg) || ! NILP (end))
-       Fsignal (Qfile_error,
-                Fcons (build_string ("not a regular file"),
-                       Fcons (orig_filename, Qnil)));
+       xsignal2 (Qfile_error,
+                 build_string ("not a regular file"), orig_filename);
     }
 #endif
 
@@ -4329,11 +4299,8 @@ actually used.  */)
        {
          xfree (conversion_buffer);
          coding_free_composition_data (&coding);
-         if (how_much == -1)
-           error ("IO error reading %s: %s",
-                  SDATA (orig_filename), emacs_strerror (errno));
-         else if (how_much == -2)
-           error ("maximum buffer size exceeded");
+         error ("IO error reading %s: %s",
+                SDATA (orig_filename), emacs_strerror (errno));
        }
 
       /* Compare the beginning of the converted file
@@ -4418,6 +4385,8 @@ actually used.  */)
 
       /* Set `inserted' to the number of inserted characters.  */
       inserted = PT - temp;
+      /* Set point before the inserted characters.  */
+      SET_PT_BOTH (temp, same_at_start);
 
       xfree (conversion_buffer);
       emacs_close (fd);
@@ -4552,6 +4521,8 @@ actually used.  */)
 #endif
       Vdeactivate_mark = old_Vdeactivate_mark;
     }
+  else
+    Vdeactivate_mark = Qt;
 
   /* Make the text read part of the buffer.  */
   GAP_SIZE -= inserted;
@@ -4718,9 +4689,8 @@ actually used.  */)
        }
 #endif /* CLASH_DETECTION */
       if (not_regular)
-       Fsignal (Qfile_error,
-                Fcons (build_string ("not a regular file"),
-                       Fcons (orig_filename, Qnil)));
+       xsignal2 (Qfile_error,
+                 build_string ("not a regular file"), orig_filename);
     }
 
   if (set_coding_system)
@@ -4846,6 +4816,8 @@ choose_write_coding_system (start, end, filename,
       /* ... but with the special flag to indicate not to strip off
         leading code of eight-bit-control chars.  */
       coding->flags = 1;
+      /* We force LF for end-of-line because that is faster.  */
+      coding->eol_type = CODING_EOL_LF;
       goto done_setup_coding;
     }
   else if (!NILP (Vcoding_system_for_write))
@@ -4938,6 +4910,8 @@ choose_write_coding_system (start, end, filename,
   setup_coding_system (Fcheck_coding_system (val), coding);
 
  done_setup_coding:
+  if (coding->eol_type == CODING_EOL_UNDECIDED)
+    coding->eol_type = system_eol_type;
   if (!STRINGP (start) && !NILP (current_buffer->selective_display))
     coding->mode |= CODING_MODE_SELECTIVE_DISPLAY;
 }
@@ -5009,6 +4983,7 @@ This does code conversion according to the value of
   if (!NILP (start) && !STRINGP (start))
     validate_region (&start, &end);
 
+  visit_file = Qnil;
   GCPRO5 (start, filename, visit, visit_file, lockname);
 
   filename = Fexpand_file_name (filename, Qnil);
@@ -5797,6 +5772,7 @@ auto_save_error (error)
       Fsleep_for (make_number (1), Qnil);
     }
 
+  SAFE_FREE ();
   UNGCPRO;
   return Qnil;
 }
@@ -5850,7 +5826,11 @@ static Lisp_Object
 do_auto_save_make_dir (dir)
      Lisp_Object dir;
 {
-  return call2 (Qmake_directory, dir, Qt);
+  Lisp_Object mode;
+
+  call2 (Qmake_directory, dir, Qt);
+  XSETFASTINT (mode, 0700);
+  return Fset_file_modes (dir, mode);
 }
 
 static Lisp_Object
@@ -6048,7 +6028,7 @@ A non-nil CURRENT-ONLY argument means save only current buffer.  */)
        {
          /* If we are going to restore an old message,
             give time to read ours.  */
-         sit_for (1, 0, 0, 0, 0);
+         sit_for (make_number (1), 0, 0);
          restore_message ();
        }
       else
@@ -6223,9 +6203,9 @@ DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_inte
            {
              Lisp_Object tem = XCAR (all);
              int len;
-             if (STRINGP (tem) &&
-                 (len = SCHARS (tem), len > 0) &&
-                 IS_DIRECTORY_SEP (SREF (tem, len-1)))
+             if (STRINGP (tem)
+                 && (len = SBYTES (tem), len > 0)
+                 && IS_DIRECTORY_SEP (SREF (tem, len-1)))
                comp = Fcons (tem, comp);
            }
        }
@@ -6233,13 +6213,17 @@ DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_inte
 #endif
        {
          /* Must do it the hard (and slow) way.  */
+         Lisp_Object tem;
          GCPRO3 (all, comp, specdir);
          count = SPECPDL_INDEX ();
          record_unwind_protect (read_file_name_cleanup, current_buffer->directory);
          current_buffer->directory = realdir;
          for (comp = Qnil; CONSP (all); all = XCDR (all))
-           if (!NILP (call1 (Vread_file_name_predicate, XCAR (all))))
-             comp = Fcons (XCAR (all), comp);
+           {
+             tem = call1 (Vread_file_name_predicate, XCAR (all));
+             if (!NILP (tem))
+               comp = Fcons (XCAR (all), comp);
+           }
          unbind_to (count, Qnil);
          UNGCPRO;
        }
@@ -6324,7 +6308,7 @@ and `read-file-name-function'.  */)
   /* If dir starts with user's homedir, change that to ~. */
   homedir = (char *) egetenv ("HOME");
 #ifdef DOS_NT
-  /* homedir can be NULL in temacs, since Vprocess_environment is not
+  /* homedir can be NULL in temacs, since Vglobal_environment is not
      yet set up.  We shouldn't crash in that case.  */
   if (homedir != 0)
     {
@@ -6613,19 +6597,17 @@ of file names regardless of the current language environment.  */);
   staticpro (&Qcar_less_than_car);
 
   Fput (Qfile_error, Qerror_conditions,
-       Fcons (Qfile_error, Fcons (Qerror, Qnil)));
+       list2 (Qfile_error, Qerror));
   Fput (Qfile_error, Qerror_message,
        build_string ("File error"));
 
   Fput (Qfile_already_exists, Qerror_conditions,
-       Fcons (Qfile_already_exists,
-              Fcons (Qfile_error, Fcons (Qerror, Qnil))));
+       list3 (Qfile_already_exists, Qfile_error, Qerror));
   Fput (Qfile_already_exists, Qerror_message,
        build_string ("File already exists"));
 
   Fput (Qfile_date_error, Qerror_conditions,
-       Fcons (Qfile_date_error,
-              Fcons (Qfile_error, Fcons (Qerror, Qnil))));
+       list3 (Qfile_date_error, Qfile_error, Qerror));
   Fput (Qfile_date_error, Qerror_message,
        build_string ("Cannot set file date"));