home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / emacs-18.59src.lha / emacs-18.59 / src / fileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-22  |  64.4 KB  |  2,485 lines

  1. /* File IO for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include <sys/types.h>
  23. #ifdef hpux
  24. /* needed by <pwd.h> */
  25. #include <stdio.h>
  26. #undef NULL
  27. #endif
  28. #include <sys/stat.h>
  29.  
  30. #ifdef VMS
  31. #include "vms-pwd.h"
  32. #else
  33. #include <pwd.h>
  34. #endif
  35.  
  36. #include <ctype.h>
  37.  
  38. #ifdef VMS
  39. #include "dir.h"
  40. #include <perror.h>
  41. #include <stddef.h>
  42. #include <string.h>
  43. #endif
  44. #include <errno.h>
  45.  
  46. #ifndef vax11c
  47. extern int errno;
  48. extern char *sys_errlist[];
  49. extern int sys_nerr;
  50. #endif
  51.  
  52. #define err_str(a) ((a) < sys_nerr ? sys_errlist[a] : "unknown error")
  53.  
  54. #ifdef APOLLO
  55. #include <sys/time.h>
  56. #endif
  57.  
  58. #ifdef VMS
  59. #include <file.h>
  60. #include <rmsdef.h>
  61. #include <fab.h>
  62. #include <nam.h>
  63. extern unsigned char vms_file_written[];    /* set in rename_sans_version */
  64. #endif
  65.  
  66. #ifdef HAVE_TIMEVAL
  67. #ifdef HPUX
  68. #include <time.h>
  69. #else
  70. #include <sys/time.h>
  71. #endif
  72. #endif
  73.  
  74. #ifdef HPUX_NET
  75. #include <netio.h>
  76. #ifndef HPUX8
  77. #include <errnet.h>
  78. #endif
  79. #endif
  80.  
  81. #ifdef NULL
  82. #undef NULL
  83. #endif
  84. #include "lisp.h"
  85. #include "buffer.h"
  86. #include "window.h"
  87.  
  88. #include "filetypes.h"
  89.  
  90. #ifndef O_WRONLY
  91. #define O_WRONLY 1
  92. #endif
  93.  
  94. #define min(a, b) ((a) < (b) ? (a) : (b))
  95. #define max(a, b) ((a) > (b) ? (a) : (b))
  96.  
  97. /* Nonzero during writing of auto-save files */
  98. int auto_saving;
  99.  
  100. /* Nonzero means, when reading a filename in the minibuffer,
  101.  start out by inserting the default directory into the minibuffer. */
  102. int insert_default_directory;
  103.  
  104. /* On VMS, nonzero means write new files with record format stmlf.
  105.    Zero means use var format.  */
  106. int vms_stmlf_recfm;
  107.  
  108. Lisp_Object Qfile_error, Qfile_already_exists;
  109.  
  110. report_file_error (string, data)
  111.      char *string;
  112.      Lisp_Object data;
  113. {
  114.   Lisp_Object errstring;
  115.  
  116.   if (errno >= 0 && errno < sys_nerr)
  117.     errstring = build_string (sys_errlist[errno]);
  118.   else
  119.     errstring = build_string ("undocumented error code");
  120.  
  121.   /* System error messages are capitalized.  Downcase the initial
  122.      unless it is followed by a slash.  */
  123.   if (XSTRING (errstring)->data[1] != '/')
  124.     XSTRING (errstring)->data[0] = DOWNCASE (XSTRING (errstring)->data[0]);
  125.  
  126.   while (1)
  127.     Fsignal (Qfile_error,
  128.          Fcons (build_string (string), Fcons (errstring, data)));
  129. }
  130.  
  131. DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory,
  132.   1, 1, 0,
  133.   "Return the directory component in file name NAME.\n\
  134. Return nil if NAME does not include a directory.\n\
  135. Otherwise returns a directory spec.\n\
  136. Given a Unix syntax file name, returns a string ending in slash;\n\
  137. on VMS, perhaps instead a string ending in :, ] or >.")
  138.   (file)
  139.      Lisp_Object file;
  140. {
  141.   register unsigned char *beg;
  142.   register unsigned char *p;
  143.  
  144.   CHECK_STRING (file, 0);
  145.  
  146.   beg = XSTRING (file)->data;
  147.   p = beg + XSTRING (file)->size;
  148.  
  149.   while (p != beg && p[-1] != '/'
  150. #ifdef    AMIGA
  151.          && p[-1] != ':'
  152. #endif    /* AMIGA */
  153. #ifdef VMS
  154.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  155. #endif /* VMS */
  156.      ) p--;
  157.  
  158.   if (p == beg)
  159.     return Qnil;
  160.   return make_string (beg, p - beg);
  161. }
  162.  
  163. DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, Sfile_name_nondirectory,
  164.   1, 1, 0,
  165.   "Return file name NAME sans its directory.\n\
  166. For example, in a Unix-syntax file name,\n\
  167. this is everything after the last slash,\n\
  168. or the entire name if it contains no slash.")
  169.   (file)
  170.      Lisp_Object file;
  171. {
  172.   register unsigned char *beg, *p, *end;
  173.  
  174.   CHECK_STRING (file, 0);
  175.  
  176.   beg = XSTRING (file)->data;
  177.   end = p = beg + XSTRING (file)->size;
  178.  
  179.   while (p != beg && p[-1] != '/'
  180. #ifdef    AMIGA
  181.          && p[-1] != ':'
  182. #endif    /* AMIGA */
  183. #ifdef VMS
  184.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  185. #endif /* VMS */
  186.      ) p--;
  187.  
  188.   return make_string (p, end - p);
  189. }
  190.  
  191. char *
  192. file_name_as_directory (out, in)
  193.      char *out, *in;
  194. {
  195.   int size = strlen (in) - 1;
  196.  
  197.   strcpy (out, in);
  198.  
  199. #ifdef VMS
  200.   /* Is it already a directory string? */
  201.   if (in[size] == ':' || in[size] == ']' || in[size] == '>')
  202.     return out;
  203.   /* Is it a VMS directory file name?  If so, hack VMS syntax.  */
  204.   else if (! index (in, '/')
  205.        && ((size > 3 && ! strcmp (&in[size - 3], ".DIR"))
  206.            || (size > 3 && ! strcmp (&in[size - 3], ".dir"))
  207.            || (size > 5 && (! strncmp (&in[size - 5], ".DIR", 4)
  208.                 || ! strncmp (&in[size - 5], ".dir", 4))
  209.            && (in[size - 1] == '.' || in[size - 1] == ';')
  210.            && in[size] == '1')))
  211.     {
  212.       register char *p, *dot;
  213.       char brack;
  214.  
  215.       /* x.dir -> [.x]
  216.      dir:x.dir --> dir:[x]
  217.      dir:[x]y.dir --> dir:[x.y] */
  218.       p = in + size;
  219.       while (p != in && *p != ':' && *p != '>' && *p != ']') p--;
  220.       if (p != in)
  221.     {
  222.       strncpy (out, in, p - in);
  223.       out[p - in] = '\0';
  224.       if (*p == ':')
  225.         {
  226.           brack = ']';
  227.           strcat (out, ":[");
  228.         }
  229.       else
  230.         {
  231.           brack = *p;
  232.           strcat (out, ".");
  233.         }
  234.       p++;
  235.     }
  236.       else
  237.     {
  238.       brack = ']';
  239.       strcpy (out, "[.");
  240.     }
  241.       dot = (char *) index (p, '.');
  242.       if (dot)
  243.     {
  244.       /* blindly remove any extension */
  245.       size = strlen (out) + (dot - p);
  246.       strncat (out, p, dot - p);
  247.     }
  248.       else
  249.     {
  250.       strcat (out, p);
  251.       size = strlen (out);
  252.     }
  253.       out[size++] = brack;
  254.       out[size] = '\0';
  255.     }
  256. #else /* not VMS */
  257. #ifdef    AMIGA
  258.   /* AmigaDOS syntax, append slash if the last char isn't a ':' or '/' */
  259.   if (out[size] != '/' && out[size] != ':' && size != 0)
  260.     strcat (out, "/");
  261. #else    /* not AMIGA */
  262.   /* For Unix syntax, Append a slash if necessary */
  263.   if (out[size] != '/')
  264.     strcat (out, "/");
  265. #endif /* not AMIGA */
  266. #endif /* not VMS */
  267.   return out;
  268. }
  269.  
  270. DEFUN ("file-name-as-directory", Ffile_name_as_directory,
  271.        Sfile_name_as_directory, 1, 1, 0,
  272.   "Return a string representing file FILENAME interpreted as a directory.\n\
  273. This string can be used as the value of default-directory\n\
  274. or passed as second argument to expand-file-name.\n\
  275. For a Unix-syntax file name, just appends a slash.\n\
  276. On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc.")
  277.   (file)
  278.      Lisp_Object file;
  279. {
  280.   char *buf;
  281.  
  282.   CHECK_STRING (file, 0);
  283.   if (NULL (file))
  284.     return Qnil;
  285.   buf = (char *) alloca (XSTRING (file)->size + 10);
  286.   return build_string (file_name_as_directory (buf, XSTRING (file)->data));
  287. }
  288.  
  289. /*
  290.  * Convert from directory name to filename.
  291.  * On VMS:
  292.  *       xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
  293.  *       xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
  294.  * On UNIX, it's simple: just make sure there is a terminating /
  295.  
  296.  * Value is nonzero if the string output is different from the input.
  297.  */
  298.  
  299. directory_file_name (src, dst)
  300.      char *src, *dst;
  301. {
  302.   long slen;
  303. #ifdef VMS
  304.   long rlen;
  305.   char * ptr, * rptr;
  306.   char bracket;
  307.   struct FAB fab = cc$rms_fab;
  308.   struct NAM nam = cc$rms_nam;
  309.   char esa[NAM$C_MAXRSS];
  310. #endif /* VMS */
  311.  
  312.   slen = strlen (src) - 1;
  313. #ifdef VMS
  314.   if (! index (src, '/')
  315.       && (src[slen] == ']' || src[slen] == ':' || src[slen] == '>'))
  316.     {
  317.       /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */
  318.       fab.fab$l_fna = src;
  319.       fab.fab$b_fns = slen + 1;
  320.       fab.fab$l_nam = &nam;
  321.       fab.fab$l_fop = FAB$M_NAM;
  322.  
  323.       nam.nam$l_esa = esa;
  324.       nam.nam$b_ess = sizeof esa;
  325.       nam.nam$b_nop |= NAM$M_SYNCHK;
  326.  
  327.       /* We call SYS$PARSE to handle such things as [--] for us. */
  328.       if (SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL)
  329.     {
  330.       slen = nam.nam$b_esl - 1;
  331.       if (esa[slen] == ';' && esa[slen - 1] == '.')
  332.         slen -= 2;
  333.       esa[slen + 1] = '\0';
  334.       src = esa;
  335.     }
  336.       if (src[slen] != ']' && src[slen] != '>')
  337.     {
  338.       /* what about when we have logical_name:???? */
  339.       if (src[slen] == ':')
  340.         {            /* Xlate logical name and see what we get */
  341.           ptr = strcpy (dst, src); /* upper case for getenv */
  342.           while (*ptr)
  343.         {
  344.           if ('a' <= *ptr && *ptr <= 'z')
  345.             *ptr -= 040;
  346.           ptr++;
  347.         }
  348.           dst[slen] = 0;    /* remove colon */
  349.           if (!(src = egetenv (dst)))
  350.         return 0;
  351.           /* should we jump to the beginning of this procedure?
  352.          Good points: allows us to use logical names that xlate
  353.          to Unix names,
  354.          Bad points: can be a problem if we just translated to a device
  355.          name...
  356.          For now, I'll punt and always expect VMS names, and hope for
  357.          the best! */
  358.           slen = strlen (src) - 1;
  359.           if (src[slen] != ']' && src[slen] != '>')
  360.         { /* no recursion here! */
  361.           strcpy (dst, src);
  362.           return 0;
  363.         }
  364.         }
  365.       else
  366.         {        /* not a directory spec */
  367.           strcpy (dst, src);
  368.           return 0;
  369.         }
  370.     }
  371.       bracket = src[slen];
  372.       ptr = (char *) index (src, bracket - 2);
  373.       if (ptr == 0)
  374.     { /* no opening bracket */
  375.       strcpy (dst, src);
  376.       return 0;
  377.     }
  378.       if (!(rptr = (char *) rindex (src, '.')))
  379.     rptr = ptr;
  380.       slen = rptr - src;
  381.       strncpy (dst, src, slen);
  382.       dst[slen] = '\0';
  383.       if (*rptr == '.')
  384.     {
  385.       dst[slen++] = bracket;
  386.       dst[slen] = '\0';
  387.     }
  388.       else
  389.     {
  390.       /* If we have the top-level of a rooted directory (i.e. xx:[000000]),
  391.          then translate the device and recurse. */
  392.       if (dst[slen - 1] == ':'
  393.           && dst[slen - 2] != ':'    /* skip decnet nodes */
  394.           && strcmp(src + slen, "[000000]") == 0)
  395.         {
  396.           dst[slen - 1] = '\0';
  397.           if ((ptr = egetenv (dst))
  398.           && (rlen = strlen (ptr) - 1) > 0
  399.           && (ptr[rlen] == ']' || ptr[rlen] == '>')
  400.           && ptr[rlen - 1] == '.')
  401.         {
  402.           char * buf = (char *) alloca (strlen (ptr) + 1);
  403.           strcpy (buf, ptr);
  404.           buf[rlen - 1] = ']';
  405.           buf[rlen] = '\0';
  406.           return directory_file_name (buf, dst);
  407.         }
  408.           else
  409.         dst[slen - 1] = ':';
  410.         }
  411.       strcat (dst, "[000000]");
  412.       slen += 8;
  413.     }
  414.       rptr++;
  415.       rlen = strlen (rptr) - 1;
  416.       strncat (dst, rptr, rlen);
  417.       dst[slen + rlen] = '\0';
  418.       strcat (dst, ".DIR.1");
  419.       return 1;
  420.     }
  421. #endif /* VMS */
  422.   /* Process as Unix format: just remove any final slash.
  423.      But leave "/" unchanged; do not change it to "".  */
  424.   strcpy (dst, src);
  425.   if (slen > 0 && dst[slen] == '/')
  426.     dst[slen] = 0;
  427.   return 1;
  428. }
  429.  
  430. DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name,
  431.   1, 1, 0,
  432.   "Returns the file name of the directory named DIR.\n\
  433. This is the name of the file that holds the data for the directory DIR.\n\
  434. In Unix-syntax, this just removes the final slash.\n\
  435. On VMS, given a VMS-syntax directory name such as \"[X.Y]\",\n\
  436. returns a file name such as \"[X]Y.DIR.1\".")
  437.   (directory)
  438.      Lisp_Object directory;
  439. {
  440.   char *buf;
  441.  
  442.   CHECK_STRING (directory, 0);
  443.  
  444.   if (NULL (directory))
  445.     return Qnil;
  446. #ifdef VMS
  447.   /* 20 extra chars is insufficient for VMS, since we might perform a
  448.      logical name translation. an equivalence string can be up to 255
  449.      chars long, so grab that much extra space...  - sss */
  450.   buf = (char *) alloca (XSTRING (directory)->size + 20 + 255);
  451. #else
  452.   buf = (char *) alloca (XSTRING (directory)->size + 20);
  453. #endif
  454.   directory_file_name (XSTRING (directory)->data, buf);
  455.   return build_string (buf);
  456. }
  457.  
  458. DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0,
  459.   "Generate temporary name (string) starting with PREFIX (a string).")
  460.   (prefix)
  461.      Lisp_Object prefix;
  462. {
  463.   Lisp_Object val;
  464.   val = concat2 (prefix, build_string ("XXXXXX"));
  465.   mktemp (XSTRING (val)->data);
  466.   return val;
  467. }
  468.  
  469. DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
  470.   "Convert FILENAME to absolute, and canonicalize it.\n\
  471. Second arg DEFAULT is directory to start with if FILENAME is relative\n\
  472.  (does not start with slash); if DEFAULT is nil or missing,\n\
  473. the current buffer's value of default-directory is used.\n\
  474. Filenames containing . or .. as components are simplified;\n\
  475. initial ~ is expanded.  See also the function  substitute-in-file-name.")
  476.      (name, defalt)
  477.      Lisp_Object name, defalt;
  478. {
  479. #ifdef AMIGA
  480.   unsigned char *nm, *tilde, *newdir, *colon, *t_pos, *target;
  481.  
  482.   CHECK_STRING (name, 0);
  483.  
  484.   nm = XSTRING (name)->data;
  485.   /* Find base directory */
  486.   if (NULL (defalt))
  487.       defalt = current_buffer->directory;
  488.   CHECK_STRING (defalt, 1);
  489.   newdir = XSTRING (defalt)->data;
  490.  
  491.   /* Concat newdir w/ nm and canonicalize */
  492.   /* newdir always contains at least the device name.
  493.      It is assumed canonical */
  494.   target = (unsigned char *)alloca(strlen(nm) + strlen(newdir) + 2);
  495.   file_name_as_directory (target, newdir);
  496.   t_pos = target + strlen(target);
  497.  
  498.   while (*nm)
  499.   {
  500.       unsigned char *comp_end = nm;
  501.       int comp_len;
  502.  
  503.       /* Find next component of path (everything upto the next /) */
  504.       do comp_end++; while (comp_end[0] && comp_end[-1] != '/' && comp_end[-1] != ':');
  505.       comp_len = comp_end - nm;
  506.  
  507.       if (comp_len == 1 && nm[0] == '/' ||
  508.       nm[0] == '.' && nm[1] == '.' &&
  509.       (comp_len == 2 || comp_len == 3 && nm[2] == '/'))
  510.       {
  511.       /* Previous directory */
  512.       if (t_pos > target && t_pos[-1] != ':')
  513.       {
  514.           t_pos--; /* Back up over / */
  515.           while (t_pos > target &&
  516.              t_pos[-1] != ':' && t_pos[-1] != '/') t_pos--;
  517.       }
  518.       }
  519.       else if (comp_len == 2 && nm[0] == '.' && nm[1] == '/' ||
  520.            comp_len == 1 && nm[0] == '.') ; /* Ignore . */
  521.       else if (nm[0] == ':') /* Just keep disk name */
  522.       {
  523.       char *new_pos;
  524.  
  525.       *t_pos = 0; /* Limit search for : */
  526.       t_pos = index(target, ':');
  527.       if (t_pos) t_pos++;
  528.       else t_pos = target;
  529.       }
  530.       else if (nm[0] == '~' || index(nm, ':'))
  531.       {
  532.       char *exp_name;
  533.  
  534.       if (nm[0] == '~')
  535.           if (nm[1] == '/' || nm[1] == 0) /* Home directory */
  536.           {
  537.           newdir = (unsigned char *) egetenv ("HOME");
  538.           if (!newdir) newdir = (unsigned char *) "s:";
  539.           }
  540.           else
  541.           {
  542.           /* Handle ~ followed by user name.  */
  543.           char lastc = nm[comp_len - 1];
  544.           int len = comp_len - 1;
  545.  
  546.           if (lastc == ':' || lastc == '/') len--;
  547.  
  548.           /* ~name becomes name: */
  549.           newdir = (unsigned char *) alloca (len + 2);
  550.           bcopy((char *) nm + 1, newdir, len);
  551.           newdir[len] = ':';
  552.           newdir[len + 1] = 0;
  553.           }
  554.       else /* we have name: */
  555.       {
  556.           newdir = (char *)alloca(comp_len + 1);
  557.           bcopy(nm, newdir, comp_len);
  558.           newdir[comp_len] = 0;
  559.       }
  560.       exp_name = (char *)alloca(1024);
  561.       if (expand_path(newdir, exp_name, 1024))
  562.       {
  563.           char *colon = strchr(exp_name, ':');
  564.  
  565.           /* Detect paths with multiple colons (eg from PATH:) and
  566.          leave them alone. They create confusion. */
  567.           if (!(colon && strchr(colon + 1, ':'))) newdir = exp_name;
  568.       }
  569.       target = (unsigned char *)alloca(strlen(nm) + strlen(newdir) + 2);
  570.       file_name_as_directory (target, newdir);
  571.       t_pos = target + strlen(target);
  572.       }
  573.       else /* Copy component */
  574.       {
  575.       bcopy(nm, t_pos, comp_len);
  576.       t_pos += comp_len;
  577.       }
  578.  
  579.       nm = comp_end;
  580.   }
  581.   return make_string (target, t_pos - target);
  582. #else /* not AMIGA */
  583.   unsigned char *nm;
  584.   
  585.   register unsigned char *newdir, *p, *o;
  586.   int tlen;
  587.   unsigned char *target;
  588.   struct passwd *pw;
  589.   int lose;
  590. #ifdef VMS
  591.   unsigned char * colon = 0;
  592.   unsigned char * close = 0;
  593.   unsigned char * slash = 0;
  594.   unsigned char * brack = 0;
  595.   int lbrack = 0, rbrack = 0;
  596.   int dots = 0;
  597. #endif /* VMS */
  598.   
  599.   CHECK_STRING (name, 0);
  600.  
  601. #ifdef VMS
  602.   /* Filenames on VMS are always upper case.  */
  603.   name = Fupcase (name);
  604. #endif
  605.  
  606.   nm = XSTRING (name)->data;
  607.   
  608.   /* If nm is absolute, flush ...// and detect /./ and /../.
  609.      If no /./ or /../ we can return right away. */
  610.   if (
  611.       nm[0] == '/'
  612. #ifdef VMS
  613.       || index (nm, ':')
  614. #endif /* VMS */
  615.       )
  616.     {
  617.       p = nm;
  618.       lose = 0;
  619.       while (*p)
  620.     {
  621.       if (p[0] == '/' && p[1] == '/'
  622. #ifdef APOLLO
  623.           /* // at start of filename is meaningful on Apollo system */
  624.           && nm != p
  625. #endif /* APOLLO */
  626.           )
  627.         nm = p + 1;
  628.       if (p[0] == '/' && p[1] == '~')
  629.         nm = p + 1, lose = 1;
  630.       if (p[0] == '/' && p[1] == '.'
  631.           && (p[2] == '/' || p[2] == 0
  632.           || (p[2] == '.' && (p[3] == '/' || p[3] == 0))))
  633.         lose = 1;
  634. #ifdef VMS
  635.       if (p[0] == '\\')
  636.         lose = 1;
  637.       if (p[0] == '/') {
  638.         /* if dev:[dir]/, move nm to / */
  639.         if (!slash && p > nm && (brack || colon)) {
  640.           nm = (brack ? brack + 1 : colon + 1);
  641.           lbrack = rbrack = 0;
  642.           brack = 0;
  643.           colon = 0;
  644.         }
  645.         slash = p;
  646.       }
  647.       if (p[0] == '-')
  648. #ifndef VMS4_4
  649.         /* VMS pre V4.4,convert '-'s in filenames. */
  650.         if (lbrack == rbrack)
  651.           {
  652.         if (dots < 2)    /* this is to allow negative version numbers */
  653.           p[0] = '_';
  654.           }
  655.         else
  656. #endif /* VMS4_4 */
  657.           if (lbrack > rbrack &&
  658.           ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
  659.            (p[1] == '.' || p[1] == ']' || p[1] == '>')))
  660.         lose = 1;
  661. #ifndef VMS4_4
  662.           else
  663.         p[0] = '_';
  664. #endif /* VMS4_4 */
  665.       /* count open brackets, reset close bracket pointer */
  666.       if (p[0] == '[' || p[0] == '<')
  667.         lbrack++, brack = 0;
  668.       /* count close brackets, set close bracket pointer */
  669.       if (p[0] == ']' || p[0] == '>')
  670.         rbrack++, brack = p;
  671.       /* detect ][ or >< */
  672.       if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<'))
  673.         lose = 1;
  674.       if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~')
  675.         nm = p + 1, lose = 1;
  676.       if (p[0] == ':' && (colon || slash))
  677.         /* if dev1:[dir]dev2:, move nm to dev2: */
  678.         if (brack)
  679.           {
  680.         nm = brack + 1;
  681.         brack = 0;
  682.           }
  683.         /* if /pathname/dev:, move nm to dev: */
  684.         else if (slash)
  685.           nm = slash + 1;
  686.         /* if node::dev:, move colon following dev */
  687.         else if (colon && colon[-1] == ':')
  688.           colon = p;
  689.         /* if dev1:dev2:, move nm to dev2: */
  690.         else if (colon && colon[-1] != ':')
  691.           {
  692.         nm = colon + 1;
  693.         colon = 0;
  694.           }
  695.       if (p[0] == ':' && !colon)
  696.         {
  697.           if (p[1] == ':')
  698.         p++;
  699.           colon = p;
  700.         }
  701.       if (lbrack == rbrack)
  702.         if (p[0] == ';')
  703.           dots = 2;
  704.         else if (p[0] == '.')
  705.           dots++;
  706. #endif /* VMS */
  707.       p++;
  708.     }
  709.       if (!lose)
  710.     {
  711. #ifdef VMS
  712.       if (index (nm, '/'))
  713.         return build_string (sys_translate_unix (nm));
  714. #endif /* VMS */
  715.       if (nm == XSTRING (name)->data)
  716.         return name;
  717.       return build_string (nm);
  718.     }
  719.     }
  720.  
  721.   /* Now determine directory to start with and put it in NEWDIR.  */
  722.  
  723.   newdir = 0;
  724.  
  725.   if (nm[0] == '~')
  726.     {
  727.       if (nm[1] == '/'
  728. #ifdef VMS
  729.       || nm[1] == ':'
  730. #endif /* VMS */
  731.       || nm[1] == 0)
  732.     {
  733.       /* Handle ~ on its own.  */
  734.       newdir = (unsigned char *) egetenv ("HOME");
  735.     }
  736.       else
  737.     {
  738.       /* Handle ~ followed by user name.  */
  739.       unsigned char *user = nm + 1;
  740.       /* Find end of name.  */
  741.       unsigned char *ptr = (unsigned char *) index (user, '/');
  742.       int len = ptr ? ptr - user : strlen (user);
  743. #ifdef VMS
  744.       unsigned char *ptr1 = (unsigned char *) index (user, ':');
  745.       if (ptr1 != 0 && ptr1 - user < len)
  746.         len = ptr1 - user;
  747. #endif /* VMS */
  748.       /* Copy the user name into temp storage.  */
  749.       o = (unsigned char *) alloca (len + 1);
  750.       bcopy ((char *) user, o, len);
  751.       o[len] = 0;
  752.  
  753.       /* Look up the user name.  */
  754.       pw = (struct passwd *) getpwnam (o);
  755.       if (!pw)
  756.         error ("User \"%s\" is not known", o);
  757.       newdir = (unsigned char *) pw->pw_dir;
  758.  
  759.       /* Discard the user name from NM.  */
  760.       nm += len;
  761.     }
  762.  
  763.       /* Discard the ~ from NM.  */
  764.       nm++;
  765. #ifdef VMS
  766.       if (*nm != 0)
  767.     nm++;            /* Don't leave the slash in nm.  */
  768. #endif /* VMS */
  769.  
  770.       if (newdir == 0)
  771.     newdir = (unsigned char *) "";
  772.     }
  773.  
  774.   if (nm[0] != '/'
  775. #ifdef VMS
  776.       && !index (nm, ':')
  777. #endif /* not VMS */
  778.       && !newdir)
  779.     {
  780.       if (NULL (defalt))
  781.     defalt = current_buffer->directory;
  782.       CHECK_STRING (defalt, 1);
  783.       newdir = XSTRING (defalt)->data;
  784.     }
  785.  
  786.   if (newdir != 0)
  787.     {
  788.       /* Get rid of any slash at the end of newdir.  */
  789.       int length = strlen (newdir);
  790.       if (length > 1 && newdir[length - 1] == '/')
  791.     {
  792.       unsigned char *temp = (unsigned char *) alloca (length);
  793.       bcopy (newdir, temp, length - 1);
  794.       temp[length - 1] = 0;
  795.       newdir = temp;
  796.     }
  797.       tlen = length + 1;
  798.     }
  799.   else
  800.     tlen = 0;
  801.  
  802.   /* Now concatenate the directory and name to new space in the stack frame */
  803.  
  804.   tlen += strlen (nm) + 1;
  805.   target = (unsigned char *) alloca (tlen);
  806.   *target = 0;
  807.  
  808.   if (newdir)
  809.     {
  810. #ifndef VMS
  811.       if (nm[0] == 0 || nm[0] == '/')
  812.     strcpy (target, newdir);
  813.       else
  814. #endif
  815.       file_name_as_directory (target, newdir);
  816.     }
  817.  
  818.   strcat (target, nm);
  819. #ifdef VMS
  820.   if (index (target, '/'))
  821.     strcpy (target, sys_translate_unix (target));
  822. #endif /* VMS */
  823.  
  824.   /* Now canonicalize by removing /. and /foo/.. if they appear */
  825.  
  826.   p = target;
  827.   o = target;
  828.  
  829.   while (*p)
  830.     {
  831. #ifdef VMS
  832.       if (*p != ']' && *p != '>' && *p != '-')
  833.     {
  834.       if (*p == '\\')
  835.         p++;
  836.       *o++ = *p++;
  837.     }
  838.       else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2)
  839.     /* brackets are offset from each other by 2 */
  840.     {
  841.       p += 2;
  842.       if (*p != '.' && *p != '-' && o[-1] != '.')
  843.         /* convert [foo][bar] to [bar] */
  844.         while (o[-1] != '[' && o[-1] != '<')
  845.           o--;
  846.       else if (*p == '-' && *o != '.')
  847.         *--p = '.';
  848.     }
  849.       else if (p[0] == '-' && o[-1] == '.' &&
  850.            (p[1] == '.' || p[1] == ']' || p[1] == '>'))
  851.     /* flush .foo.- ; leave - if stopped by '[' or '<' */
  852.     {
  853.       do
  854.         o--;
  855.       while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<');
  856.       if (p[1] == '.')    /* foo.-.bar ==> bar*/
  857.         p += 2;
  858.       else if (o[-1] == '.') /* '.foo.-]' ==> ']' */
  859.         p++, o--;
  860.       /* else [foo.-] ==> [-] */
  861.     }
  862.       else
  863.     {
  864. #ifndef VMS4_4
  865.       if (*p == '-' &&
  866.           o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
  867.           p[1] != ']' && p[1] != '>' && p[1] != '.')
  868.         *p = '_';
  869. #endif /* VMS4_4 */
  870.       *o++ = *p++;
  871.     }
  872. #else /* not VMS */
  873.       if (*p != '/')
  874.      {
  875.       *o++ = *p++;
  876.     }
  877.       else if (!strncmp (p, "//", 2)
  878. #ifdef APOLLO
  879.            /* // at start of filename is meaningful in Apollo system */
  880.            && o != target
  881. #endif /* APOLLO */
  882.            )
  883.     {
  884.       o = target;
  885.       p++;
  886.     }
  887.       else if (p[0] == '/' && p[1] == '.' &&
  888.            (p[2] == '/' || p[2] == 0))
  889.     p += 2;
  890.       else if (!strncmp (p, "/..", 3)
  891.            /* `/../' is the "superroot" on certain file systems.  */
  892.            && o != target
  893.            && (p[3] == '/' || p[3] == 0))
  894.     {
  895.       while (o != target && *--o != '/')
  896.         ;
  897. #ifdef APOLLO
  898.       if (o == target + 1 && o[-1] == '/' && o[0] == '/')
  899.         ++o;
  900.       else
  901. #endif APOLLO
  902.       if (o == target && *o == '/')
  903.         ++o;
  904.       p += 3;
  905.     }
  906.       else
  907.      {
  908.       *o++ = *p++;
  909.     }
  910. #endif /* not VMS */
  911.     }
  912.  
  913.   return make_string (target, o - target);
  914. #endif /* not AMIGA */
  915. }
  916.  
  917. DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name,
  918.   Ssubstitute_in_file_name, 1, 1, 0,
  919.   "Substitute environment variables referred to in STRING.\n\
  920. A $ begins a request to substitute; the env variable name is the alphanumeric\n\
  921. characters and underscores after the $, or is surrounded by braces.\n\
  922. If a ~ appears following a /, everything through that / is discarded.\n\
  923. On VMS, $ substitution is not done; this function does little and only\n\
  924. duplicates what expand-file-name does.")
  925.   (string)
  926.      Lisp_Object string;
  927. {
  928.   unsigned char *nm;
  929.  
  930.   register unsigned char *s, *p, *o, *x, *endp;
  931.   unsigned char *target;
  932.   int total = 0;
  933.   int substituted = 0;
  934.   unsigned char *xnm;
  935.  
  936.   CHECK_STRING (string, 0);
  937.  
  938.   nm = XSTRING (string)->data;
  939.   endp = nm + XSTRING (string)->size;
  940.  
  941.   /* If /~ or // appears, discard everything through first slash. */
  942.  
  943.   for (p = nm; p != endp; p++)
  944.     {
  945. #ifdef AMIGA
  946.       if (p[0] == '~' && p != nm && p[-1] == '/')
  947.     {
  948.         nm = p;
  949.         substituted = 1;
  950.     }
  951.       else if (p[0] == ':')
  952.     {
  953.           char *p2 = p;
  954.       while (p2 > nm && p2[-1] != ':' && p2[-1] != '/') p2--;
  955.       if (p2 != nm)
  956.         {
  957.           nm = p2;
  958.           substituted = 1;
  959.         }
  960.     }
  961. #else /* not AMIGA */
  962.       if ((p[0] == '~' ||
  963. #ifdef APOLLO
  964.        /* // at start of file name is meaningful in Apollo system */
  965.        (p[0] == '/' && p - 1 != nm)
  966. #else /* not APOLLO */
  967.        p[0] == '/'
  968. #endif /* not APOLLO */
  969.        )
  970.       && p != nm &&
  971. #ifdef VMS
  972.       (p[-1] == ':' || p[-1] == ']' || p[-1] == '>' ||
  973. #endif /* VMS */
  974.       p[-1] == '/')
  975. #ifdef VMS
  976.       )
  977. #endif /* VMS */
  978.     {
  979.       nm = p;
  980.       substituted = 1;
  981.     }
  982. #endif /* not AMIGA */
  983.     }
  984.  
  985. #ifdef VMS
  986.   return build_string (nm);
  987. #else
  988.  
  989.   /* See if any variables are substituted into the string
  990.      and find the total length of their values in `total' */
  991.  
  992.   for (p = nm; p != endp;)
  993.     if (*p != '$')
  994.       p++;
  995.     else
  996.       {
  997.     p++;
  998.     if (p == endp)
  999.       goto badsubst;
  1000.     else if (*p == '$')
  1001.       {
  1002.         /* "$$" means a single "$" */
  1003.         p++;
  1004.         total -= 1;
  1005.         substituted = 1;
  1006.         continue;
  1007.       }
  1008.     else if (*p == '{')
  1009.       {
  1010.         o = ++p;
  1011.         while (p != endp && *p != '}') p++;
  1012.         if (*p != '}') goto missingclose;
  1013.         s = p;
  1014.       }
  1015.     else
  1016.       {
  1017.         o = p;
  1018.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1019.         s = p;
  1020.       }
  1021.  
  1022.     /* Copy out the variable name */
  1023.     target = (unsigned char *) alloca (s - o + 1);
  1024.     strncpy (target, o, s - o);
  1025.     target[s - o] = 0;
  1026.  
  1027.     /* Get variable value */
  1028.     o = (unsigned char *) egetenv (target);
  1029. /* The presence of this code makes vax 5.0 crash, for reasons yet unknown */
  1030. #if 0
  1031. #ifdef USG
  1032.     if (!o && !strcmp (target, "USER"))
  1033.       o = egetenv ("LOGNAME");
  1034. #endif /* USG */
  1035. #endif /* 0 */
  1036.     if (!o) goto badvar;
  1037.     total += strlen (o);
  1038.     substituted = 1;
  1039.       }
  1040.  
  1041.   if (!substituted)
  1042.     return string;
  1043.  
  1044.   /* If substitution required, recopy the string and do it */
  1045.   /* Make space in stack frame for the new copy */
  1046.   xnm = (unsigned char *) alloca (XSTRING (string)->size + total + 1);
  1047.   x = xnm;
  1048.  
  1049.   /* Copy the rest of the name through, replacing $ constructs with values */
  1050.   for (p = nm; *p;)
  1051.     if (*p != '$')
  1052.       *x++ = *p++;
  1053.     else
  1054.       {
  1055.     p++;
  1056.     if (p == endp)
  1057.       goto badsubst;
  1058.     else if (*p == '$')
  1059.       {
  1060.         *x++ = *p++;
  1061.         continue;
  1062.       }
  1063.     else if (*p == '{')
  1064.       {
  1065.         o = ++p;
  1066.         while (p != endp && *p != '}') p++;
  1067.         if (*p != '}') goto missingclose;
  1068.         s = p++;
  1069.       }
  1070.     else
  1071.       {
  1072.         o = p;
  1073.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1074.         s = p;
  1075.       }
  1076.  
  1077.     /* Copy out the variable name */
  1078.     target = (unsigned char *) alloca (s - o + 1);
  1079.     strncpy (target, o, s - o);
  1080.     target[s - o] = 0;
  1081.  
  1082.     /* Get variable value */
  1083.     o = (unsigned char *) egetenv (target);
  1084. /* The presence of this code makes vax 5.0 crash, for reasons yet unknown */
  1085. #if 0
  1086. #ifdef USG
  1087.     if (!o && !strcmp (target, "USER"))
  1088.       o = egetenv ("LOGNAME");
  1089. #endif /* USG */
  1090. #endif /* 0 */
  1091.     if (!o)
  1092.       goto badvar;
  1093.  
  1094.     strcpy (x, o);
  1095.     x += strlen (o);
  1096.       }
  1097.  
  1098.   *x = 0;
  1099.  
  1100.   /* If /~ or // appears, discard everything through first slash. */
  1101.  
  1102.   for (p = xnm; p != x; p++)
  1103.     if ((p[0] == '~' ||
  1104. #ifdef APOLLO
  1105.      /* // at start of file name is meaningful in Apollo system */
  1106.      (p[0] == '/' && p - 1 != xnm)
  1107. #else /* not APOLLO */
  1108.      p[0] == '/'
  1109. #endif /* not APOLLO */
  1110.      )
  1111.     && p != nm && p[-1] == '/')
  1112.       xnm = p;
  1113.  
  1114.   return make_string (xnm, x - xnm);
  1115.  
  1116.  badsubst:
  1117.   error ("Bad format environment-variable substitution");
  1118.  missingclose:
  1119.   error ("Missing \"}\" in environment-variable substitution");
  1120.  badvar:
  1121.   error ("Substituting nonexistent environment variable \"%s\"", target);
  1122.  
  1123.   /* NOTREACHED */
  1124. #endif /* not VMS */
  1125. }
  1126.  
  1127. Lisp_Object
  1128. expand_and_dir_to_file (filename, defdir)
  1129.      Lisp_Object filename, defdir;
  1130. {
  1131.   register Lisp_Object abspath;
  1132.  
  1133.   abspath = Fexpand_file_name (filename, defdir);
  1134. #ifdef VMS
  1135.   {
  1136.     register int c = XSTRING (abspath)->data[XSTRING (abspath)->size - 1];
  1137.     if (c == ':' || c == ']' || c == '>')
  1138.       abspath = Fdirectory_file_name (abspath);
  1139.   }
  1140. #else
  1141.   /* Remove final slash, if any (unless path is root).
  1142.      stat behaves differently depending!  */
  1143.   if (XSTRING (abspath)->size > 1
  1144.       && XSTRING (abspath)->data[XSTRING (abspath)->size - 1] == '/')
  1145.     {
  1146.       if (EQ (abspath, filename))
  1147.     abspath = Fcopy_sequence (abspath);
  1148.       XSTRING (abspath)->data[XSTRING (abspath)->size - 1] = 0;
  1149.     }
  1150. #endif
  1151.   return abspath;
  1152. }
  1153.  
  1154. barf_or_query_if_file_exists (absname, querystring, interactive)
  1155.      Lisp_Object absname;
  1156.      unsigned char *querystring;
  1157.      int interactive;
  1158. {
  1159.   register Lisp_Object tem;
  1160.   struct gcpro gcpro1;
  1161.  
  1162.   if (access (XSTRING (absname)->data, 4) >= 0)
  1163.     {
  1164.       if (! interactive)
  1165.     Fsignal (Qfile_already_exists,
  1166.          Fcons (build_string ("File already exists"),
  1167.             Fcons (absname, Qnil)));
  1168.       GCPRO1 (absname);
  1169.       tem = Fyes_or_no_p (format1 ("File %s already exists; %s anyway? ",
  1170.                    XSTRING (absname)->data, querystring));
  1171.       UNGCPRO;
  1172.       if (NULL (tem))
  1173.     Fsignal (Qfile_already_exists,
  1174.          Fcons (build_string ("File already exists"),
  1175.             Fcons (absname, Qnil)));
  1176.     }
  1177.   return;
  1178. }
  1179.  
  1180. DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4,
  1181.   "fCopy file: \nFCopy %s to file: \np",
  1182.   "Copy FILE to NEWNAME.  Both args strings.\n\
  1183. Signals a  file-already-exists  error if NEWNAME already exists,\n\
  1184. unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.\n\
  1185. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1186. This is what happens in interactive use with M-x.\n\
  1187. Fourth arg non-nil means give the new file the same last-modified time\n\
  1188. that the old one has.  (This works on only some systems.)")
  1189.   (filename, newname, ok_if_already_exists, keep_date)
  1190.      Lisp_Object filename, newname, ok_if_already_exists, keep_date;
  1191. {
  1192.   int ifd, ofd, n;
  1193.   char buf[16 * 1024];
  1194.   struct stat st;
  1195.   struct gcpro gcpro1, gcpro2;
  1196.  
  1197.   GCPRO2 (filename, newname);
  1198.   CHECK_STRING (filename, 0);
  1199.   CHECK_STRING (newname, 1);
  1200.   filename = Fexpand_file_name (filename, Qnil);
  1201.   newname = Fexpand_file_name (newname, Qnil);
  1202.   if (NULL (ok_if_already_exists)
  1203.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1204.     barf_or_query_if_file_exists (newname, "copy to it",
  1205.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1206.  
  1207.   ifd = open (XSTRING (filename)->data, 0);
  1208.   if (ifd < 0)
  1209.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1210.  
  1211. #ifdef VMS
  1212.   /* Create the copy file with the same record format as the input file */
  1213.   ofd = sys_creat (XSTRING (newname)->data, 0666, ifd);
  1214. #else
  1215.   ofd = creat (XSTRING (newname)->data, 0666);
  1216. #endif /* VMS */
  1217.   if (ofd < 0)
  1218.     {
  1219.       close (ifd);
  1220.       report_file_error ("Opening output file", Fcons (newname, Qnil));
  1221.     }
  1222.  
  1223.   while ((n = read (ifd, buf, sizeof buf)) > 0)
  1224.     if (write (ofd, buf, n) != n)
  1225.       {
  1226.     close (ifd);
  1227.     close (ofd);
  1228.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1229.       }
  1230.  
  1231.   if (fstat (ifd, &st) >= 0)
  1232.     {
  1233. #ifdef HAVE_TIMEVAL
  1234.       if (!NULL (keep_date))
  1235.     {
  1236. #ifdef USE_UTIME
  1237. /* AIX has utimes() in compatibility package, but it dies.  So use good old
  1238.    utime interface instead. */
  1239.       struct {
  1240.         time_t atime;
  1241.         time_t mtime;
  1242.       } tv;
  1243.       tv.atime = st.st_atime;
  1244.       tv.mtime = st.st_mtime;
  1245.       utime (XSTRING (newname)->data, &tv);
  1246. #else /* not USE_UTIME */
  1247.       struct timeval timevals[2];
  1248.       timevals[0].tv_sec = st.st_atime;
  1249.       timevals[1].tv_sec = st.st_mtime;
  1250.       timevals[0].tv_usec = timevals[1].tv_usec = 0;
  1251.       utimes (XSTRING (newname)->data, timevals);
  1252. #endif /* not USE_UTIME */
  1253.     }
  1254. #endif /* HAVE_TIMEVALS */
  1255.  
  1256. #ifdef APOLLO
  1257.       if (!egetenv ("USE_DOMAIN_ACLS"))
  1258. #endif
  1259.       chmod (XSTRING (newname)->data, st.st_mode & 07777);
  1260.     }
  1261.  
  1262.   close (ifd);
  1263.   if (close (ofd) < 0)
  1264.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1265.  
  1266.   UNGCPRO;
  1267.   return Qnil;
  1268. }
  1269.  
  1270. DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: ",
  1271.   "Delete specified file.  One argument, a file name string.\n\
  1272. If file has multiple names, it continues to exist with the other names.")
  1273.   (filename)
  1274.      Lisp_Object filename;
  1275. {
  1276.   CHECK_STRING (filename, 0);
  1277.   filename = Fexpand_file_name (filename, Qnil);
  1278.   if (0 > unlink (XSTRING (filename)->data))
  1279.     report_file_error ("Removing old name", Flist (1, &filename));
  1280.   return Qnil;
  1281. }
  1282.  
  1283. DEFUN ("rename-file", Frename_file, Srename_file, 2, 3,
  1284.   "fRename file: \nFRename %s to file: \np",
  1285.   "Rename FILE as NEWNAME.  Both args strings.\n\
  1286. If file has names other than FILE, it continues to have those names.\n\
  1287. Signals a  file-already-exists  error if NEWNAME already exists\n\
  1288. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1289. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1290. This is what happens in interactive use with M-x.")
  1291.   (filename, newname, ok_if_already_exists)
  1292.      Lisp_Object filename, newname, ok_if_already_exists;
  1293. {
  1294. #ifdef NO_ARG_ARRAY
  1295.   Lisp_Object args[2];
  1296. #endif
  1297.   struct gcpro gcpro1, gcpro2;
  1298.  
  1299.   GCPRO2 (filename, newname);
  1300.   CHECK_STRING (filename, 0);
  1301.   CHECK_STRING (newname, 1);
  1302.   filename = Fexpand_file_name (filename, Qnil);
  1303.   newname = Fexpand_file_name (newname, Qnil);
  1304.   if (NULL (ok_if_already_exists)
  1305.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1306.     barf_or_query_if_file_exists (newname, "rename to it",
  1307.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1308. #ifndef BSD4_1
  1309.   if (0 > rename (XSTRING (filename)->data, XSTRING (newname)->data))
  1310. #else
  1311.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data)
  1312.       || 0 > unlink (XSTRING (filename)->data))
  1313. #endif
  1314.     {
  1315.       if (errno == EXDEV)
  1316.     {
  1317.       Fcopy_file (filename, newname, ok_if_already_exists, Qt);
  1318.       Fdelete_file (filename);
  1319.     }
  1320.       else
  1321. #ifdef NO_ARG_ARRAY
  1322.     {
  1323.       args[0] = filename;
  1324.       args[1] = newname;
  1325.       report_file_error ("Renaming", Flist (2, args));
  1326.     }
  1327. #else
  1328.     report_file_error ("Renaming", Flist (2, &filename));
  1329. #endif
  1330.     }
  1331.   UNGCPRO;
  1332.   return Qnil;
  1333. }
  1334.  
  1335. DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3,
  1336.   "fAdd name to file: \nFName to add to %s: \np",
  1337.   "Give FILE additional name NEWNAME.  Both args strings.\n\
  1338. Signals a  file-already-exists  error if NEWNAME already exists\n\
  1339. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1340. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1341. This is what happens in interactive use with M-x.")
  1342.   (filename, newname, ok_if_already_exists)
  1343.      Lisp_Object filename, newname, ok_if_already_exists;
  1344. {
  1345. #ifdef NO_ARG_ARRAY
  1346.   Lisp_Object args[2];
  1347. #endif
  1348.   struct gcpro gcpro1, gcpro2;
  1349.  
  1350.   GCPRO2 (filename, newname);
  1351.   CHECK_STRING (filename, 0);
  1352.   CHECK_STRING (newname, 1);
  1353.   filename = Fexpand_file_name (filename, Qnil);
  1354.   newname = Fexpand_file_name (newname, Qnil);
  1355.   if (NULL (ok_if_already_exists)
  1356.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1357.     barf_or_query_if_file_exists (newname, "make it a new name",
  1358.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1359.   unlink (XSTRING (newname)->data);
  1360.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data))
  1361.     {
  1362. #ifdef NO_ARG_ARRAY
  1363.       args[0] = filename;
  1364.       args[1] = newname;
  1365.       report_file_error ("Adding new name", Flist (2, args));
  1366. #else
  1367.       report_file_error ("Adding new name", Flist (2, &filename));
  1368. #endif
  1369.     }
  1370.  
  1371.   UNGCPRO;
  1372.   return Qnil;
  1373. }
  1374.  
  1375. #ifdef S_IFLNK
  1376. DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3,
  1377.   "sMake symbolic link to file: \nFMake symbolic link to file %s: \np",
  1378.   "Make a symbolic link to TARGET, named LINKNAME.  Both args strings.\n\
  1379. There is no completion for LINKNAME, because it is read simply as a string;\n\
  1380. this is to enable you to make a link to a relative file name.\n\n\
  1381. Signals a  file-already-exists  error if LINKNAME already exists\n\
  1382. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1383. A number as third arg means request confirmation if LINKNAME already exists.\n\
  1384. This happens for interactive use with M-x.")
  1385.   (filename, newname, ok_if_already_exists)
  1386.      Lisp_Object filename, newname, ok_if_already_exists;
  1387. {
  1388. #ifdef NO_ARG_ARRAY
  1389.   Lisp_Object args[2];
  1390. #endif
  1391.   struct gcpro gcpro1, gcpro2;
  1392.  
  1393.   GCPRO2 (filename, newname);
  1394.   CHECK_STRING (filename, 0);
  1395.   CHECK_STRING (newname, 1);
  1396. #if 0 /* This made it impossible to make a link to a relative name.  */
  1397.   filename = Fexpand_file_name (filename, Qnil);
  1398. #endif
  1399.   newname = Fexpand_file_name (newname, Qnil);
  1400.   if (NULL (ok_if_already_exists)
  1401.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1402.     barf_or_query_if_file_exists (newname, "make it a link",
  1403.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1404.   if (0 > symlink (XSTRING (filename)->data, XSTRING (newname)->data))
  1405.     {
  1406.       int failure = 1;
  1407.       /* If we failed because the link name already exists,
  1408.      try deleting it.  */
  1409.       if (errno == EEXIST)
  1410.     {
  1411.       unlink (XSTRING (newname)->data);
  1412.       failure = 0 > symlink (XSTRING (filename)->data,
  1413.                  XSTRING (newname)->data);
  1414.     }
  1415.       /* If we have not started to win, report the error.  */
  1416.       if (failure)
  1417.     {
  1418. #ifdef NO_ARG_ARRAY
  1419.       args[0] = filename;
  1420.       args[1] = newname;
  1421.       report_file_error ("Making symbolic link", Flist (2, args));
  1422. #else
  1423.       report_file_error ("Making symbolic link", Flist (2, &filename));
  1424. #endif
  1425.     }
  1426.     }
  1427.   UNGCPRO;
  1428.   return Qnil;
  1429. }
  1430. #endif /* S_IFLNK */
  1431.  
  1432. #ifdef VMS
  1433.  
  1434. DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name,
  1435.        2, 2,
  1436.        "sDefine logical name: \nsDefine logical name %s as: ",
  1437.        "Define the job-wide logical name NAME to have the value STRING.\n\
  1438. If STRING is nil or a null string, the logical name NAME is deleted.")
  1439.   (varname, string)
  1440.      Lisp_Object varname;
  1441.      Lisp_Object string;
  1442. {
  1443.   CHECK_STRING (varname, 0);
  1444.   if (NULL (string))
  1445.     delete_logical_name (XSTRING (varname)->data);
  1446.   else
  1447.     {
  1448.       CHECK_STRING (string, 1);
  1449.  
  1450.       if (XSTRING (string)->size == 0)
  1451.         delete_logical_name (XSTRING (varname)->data);
  1452.       else
  1453.         define_logical_name (XSTRING (varname)->data, XSTRING (string)->data);
  1454.     }
  1455.  
  1456.   return string;
  1457. }
  1458. #endif /* VMS */
  1459.  
  1460. #ifdef HPUX_NET
  1461.  
  1462. DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0,
  1463.        "Open a network connection to PATH using LOGIN as the login string.")
  1464.      (path, login)
  1465.      Lisp_Object path, login;
  1466. {
  1467.   int netresult;
  1468.   
  1469.   CHECK_STRING (path, 0);
  1470.   CHECK_STRING (login, 0);  
  1471.   
  1472.   netresult = netunam (XSTRING (path)->data, XSTRING (login)->data);
  1473.  
  1474.   if (netresult == -1)
  1475.     return Qnil;
  1476.   else
  1477.     return Qt;
  1478. }
  1479. #endif /* HPUX_NET */
  1480.  
  1481. DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p,
  1482.        1, 1, 0,
  1483.        "Return t if file FILENAME specifies an absolute path name.")
  1484.      (filename)
  1485.      Lisp_Object filename;
  1486. {
  1487.   unsigned char *ptr;
  1488.  
  1489.   CHECK_STRING (filename, 0);
  1490.   ptr = XSTRING (filename)->data;
  1491. #ifdef    AMIGA
  1492.   /* An absolute filename has a non-leading ':' in it */
  1493.   if (*ptr != ':')
  1494.       while (*ptr)
  1495.       if (*ptr++ == ':') return Qt;
  1496.   return Qnil;
  1497. #else    /* not AMIGA */
  1498.   if (*ptr == '/' || *ptr == '~'
  1499. #ifdef VMS
  1500. /* ??? This criterion is probably wrong for '<'.  */
  1501.       || index (ptr, ':') || index (ptr, '<')
  1502.       || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']'))
  1503.       && ptr[1] != '.')
  1504. #endif /* VMS */
  1505.       )
  1506.     return Qt;
  1507.   else
  1508.     return Qnil;
  1509. #endif /* not AMIGA */
  1510. }
  1511.  
  1512. DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0,
  1513.   "Return t if file FILENAME exists.  (This does not mean you can read it.)\n\
  1514. See also file-readable-p and file-attributes.")
  1515.   (filename)
  1516.      Lisp_Object filename;
  1517. {
  1518.   Lisp_Object abspath;
  1519.   struct stat sb;
  1520.  
  1521.   CHECK_STRING (filename, 0);
  1522.   abspath = Fexpand_file_name (filename, Qnil);
  1523. #ifdef S_IFLNK
  1524.   return (lstat (XSTRING (abspath)->data, &sb) >= 0) ? Qt : Qnil;
  1525. #else
  1526.   return (stat (XSTRING (abspath)->data, &sb) >= 0) ? Qt : Qnil;
  1527. #endif
  1528. }
  1529.  
  1530. DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0,
  1531.   "Return t if file FILENAME exists and you can read it.\n\
  1532. See also file-exists-p and file-attributes.")
  1533.   (filename)
  1534.      Lisp_Object filename;
  1535. {
  1536.   Lisp_Object abspath;
  1537.  
  1538.   CHECK_STRING (filename, 0);
  1539.   abspath = Fexpand_file_name (filename, Qnil);
  1540.   return (access (XSTRING (abspath)->data, 4) >= 0) ? Qt : Qnil;
  1541. }
  1542.  
  1543. DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0,
  1544.   "If file FILENAME is the name of a symbolic link\n\
  1545. returns the name of the file to which it is linked.\n\
  1546. Otherwise returns NIL.")
  1547.   (filename)
  1548.      Lisp_Object filename;
  1549. {
  1550. #ifdef S_IFLNK
  1551.   char *buf;
  1552.   int bufsize;
  1553.   int valsize;
  1554.   Lisp_Object val;
  1555.  
  1556.   CHECK_STRING (filename, 0);
  1557.   filename = Fexpand_file_name (filename, Qnil);
  1558.  
  1559.   bufsize = 100;
  1560.   while (1)
  1561.     {
  1562.       buf = (char *) xmalloc (bufsize);
  1563.       bzero (buf, bufsize);
  1564.       valsize = readlink (XSTRING (filename)->data, buf, bufsize);
  1565.       if (valsize < bufsize) break;
  1566.       /* Buffer was not long enough */
  1567.       free (buf);
  1568.       bufsize *= 2;
  1569.     }
  1570.   if (valsize == -1)
  1571.     {
  1572.       free (buf);
  1573.       return Qnil;
  1574.     }
  1575.   val = make_string (buf, valsize);
  1576.   free (buf);
  1577.   return val;
  1578. #else /* not S_IFLNK */
  1579.   return Qnil;
  1580. #endif /* not S_IFLNK */
  1581. }
  1582.  
  1583. /* Having this before file-symlink-p mysteriously caused it to be forgotten
  1584.    on the RT/PC.  */
  1585. DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
  1586.   "Return t if file FILENAME can be written or created by you.")
  1587.   (filename)
  1588.      Lisp_Object filename;
  1589. {
  1590.   Lisp_Object abspath, dir;
  1591.  
  1592.   CHECK_STRING (filename, 0);
  1593.   abspath = Fexpand_file_name (filename, Qnil);
  1594.   if (access (XSTRING (abspath)->data, 0) >= 0)
  1595.     return (access (XSTRING (abspath)->data, 2) >= 0) ? Qt : Qnil;
  1596.   dir = Ffile_name_directory (abspath);
  1597. #ifdef VMS
  1598.   if (!NULL (dir))
  1599.     dir = Fdirectory_file_name (dir);
  1600. #endif /* VMS */
  1601.   return (access (!NULL (dir) ? (char *) XSTRING (dir)->data : "", 2) >= 0
  1602.       ? Qt : Qnil);
  1603. }
  1604.  
  1605. DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0,
  1606.   "Return t if file FILENAME is the name of a directory as a file.\n\
  1607. A directory name spec may be given instead; then the value is t\n\
  1608. if the directory so specified exists and really is a directory.")
  1609.   (filename)
  1610.      Lisp_Object filename;
  1611. {
  1612.   register Lisp_Object abspath;
  1613.   struct stat st;
  1614.  
  1615.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  1616.  
  1617.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1618.     return Qnil;
  1619.   return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
  1620. }
  1621.  
  1622. DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0,
  1623.   "Return mode bits of FILE, as an integer.")
  1624.   (filename)
  1625.      Lisp_Object filename;
  1626. {
  1627.   Lisp_Object abspath;
  1628.   struct stat st;
  1629.  
  1630.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  1631.  
  1632.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1633.     return Qnil;
  1634.   return make_number (st.st_mode & 07777);
  1635. }
  1636.  
  1637. DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0,
  1638.   "Set mode bits of FILE to MODE (an integer).\n\
  1639. Only the 12 low bits of MODE are used.")
  1640.   (filename, mode)
  1641.      Lisp_Object filename, mode;
  1642. {
  1643.   Lisp_Object abspath;
  1644.  
  1645.   abspath = Fexpand_file_name (filename, current_buffer->directory);
  1646.   CHECK_NUMBER (mode, 1);
  1647.  
  1648. #ifndef APOLLO
  1649.   if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  1650.     report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  1651. #else /* APOLLO */
  1652.   if (!egetenv ("USE_DOMAIN_ACLS"))
  1653.     {
  1654.       struct stat st;
  1655.       struct timeval tvp[2];
  1656.  
  1657.       /* chmod on apollo also change the file's modtime; need to save the
  1658.      modtime and then restore it. */
  1659.       if (stat (XSTRING (abspath)->data, &st) < 0)
  1660.     {
  1661.       report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  1662.       return (Qnil);
  1663.     }
  1664.  
  1665.       if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  1666.      report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  1667.  
  1668.       /* reset the old accessed and modified times.  */
  1669.       tvp[0].tv_sec = st.st_atime + 1; /* +1 due to an Apollo roundoff bug */
  1670.       tvp[0].tv_usec = 0;
  1671.       tvp[1].tv_sec = st.st_mtime + 1; /* +1 due to an Apollo roundoff bug */
  1672.       tvp[1].tv_usec = 0;
  1673.  
  1674.       if (utimes (XSTRING (abspath)->data, tvp) < 0)
  1675.      report_file_error ("Doing utimes", Fcons (abspath, Qnil));
  1676.     }
  1677. #endif /* APOLLO */
  1678.  
  1679.   return Qnil;
  1680. }
  1681.  
  1682. DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0,
  1683.   "Return t if file FILE1 is newer than file FILE2.\n\
  1684. If FILE1 does not exist, the answer is nil;\n\
  1685. otherwise, if FILE2 does not exist, the answer is t.")
  1686.   (file1, file2)
  1687.      Lisp_Object file1, file2;
  1688. {
  1689.   Lisp_Object abspath;
  1690.   struct stat st;
  1691.   int mtime1;
  1692.  
  1693.   CHECK_STRING (file1, 0);
  1694.   CHECK_STRING (file2, 0);
  1695.  
  1696.   abspath = expand_and_dir_to_file (file1, current_buffer->directory);
  1697.  
  1698.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1699.     return Qnil;
  1700.  
  1701.   mtime1 = st.st_mtime;
  1702.  
  1703.   abspath = expand_and_dir_to_file (file2, current_buffer->directory);
  1704.  
  1705.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1706.     return Qt;
  1707.  
  1708.   return (mtime1 > st.st_mtime) ? Qt : Qnil;
  1709. }
  1710.  
  1711. close_file_unwind (fd)
  1712.      Lisp_Object fd;
  1713. {
  1714.   close (XFASTINT (fd));
  1715. }
  1716.  
  1717. DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
  1718.   1, 2, 0,
  1719.   "Insert contents of file FILENAME after point.\n\
  1720. Returns list of absolute pathname and length of data inserted.\n\
  1721. If second argument VISIT is non-nil, the buffer's visited filename\n\
  1722. and last save file modtime are set, and it is marked unmodified.\n\
  1723. If visiting and the file does not exist, visiting is completed\n\
  1724. before the error is signaled.")
  1725.   (filename, visit)
  1726.      Lisp_Object filename, visit;
  1727. {
  1728.   struct stat st;
  1729.   register int fd;
  1730.   register int inserted = 0;
  1731.   register int i = 0;
  1732.   int count = specpdl_ptr - specpdl;
  1733.   struct gcpro gcpro1;
  1734.  
  1735.   GCPRO1 (filename);
  1736.   if (!NULL (current_buffer->read_only))
  1737.     Fbarf_if_buffer_read_only();
  1738.  
  1739.   CHECK_STRING (filename, 0);
  1740.   filename = Fexpand_file_name (filename, Qnil);
  1741.  
  1742.   fd = -1;
  1743.  
  1744. #ifndef APOLLO
  1745.   if (stat (XSTRING (filename)->data, &st) < 0
  1746. #if defined (AIX) && defined (S_ISMPX)
  1747.       /* Don't let emacs open /dev/pts, as it causes kernel confusion.  */
  1748.       || S_ISMPX (st.st_mode)
  1749. #endif
  1750.       || (fd = open (XSTRING (filename)->data, 0)) < 0)
  1751. #else
  1752.   if ((fd = open (XSTRING (filename)->data, 0)) < 0
  1753.       || fstat (fd, &st) < 0)
  1754. #endif /* not APOLLO */
  1755.     {
  1756.       if (fd >= 0) close (fd);
  1757.       if (NULL (visit))
  1758.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1759.       st.st_mtime = -1;
  1760.       goto notfound;
  1761.     }
  1762.  
  1763.   record_unwind_protect (close_file_unwind, make_number (fd));
  1764.  
  1765. #ifdef VMS
  1766.   /* VAXCRTL adds implied carriage control to certain record types.  */
  1767.   if (st.st_fab_rfm == FAB$C_FIX
  1768.       && (st.st_fab_rat & (FAB$M_FTN | FAB$M_CR) != 0))
  1769.     st.st_size += st.st_size / st.st_fab_mrs;
  1770. #endif
  1771.  
  1772.   /* Supposedly happens on VMS.  */
  1773.   if (st.st_size < 0)
  1774.     error ("File size is negative");
  1775.   {
  1776.     register Lisp_Object temp;
  1777.  
  1778.     /* Make sure point-max won't overflow after this insertion.  */
  1779.     XSET (temp, Lisp_Int, st.st_size + Z);
  1780.     if (st.st_size + Z != XINT (temp))
  1781.       error ("maximum buffer size exceeded");
  1782.   }
  1783.  
  1784.   if (NULL (visit))
  1785.     prepare_to_modify_buffer ();
  1786.  
  1787.   move_gap (point);
  1788.   if (GAP_SIZE < st.st_size)
  1789.     make_gap (st.st_size - GAP_SIZE);
  1790.     
  1791.   while (1)
  1792.     {
  1793.       int try = min (st.st_size - inserted, 64 << 10);
  1794.       int this = read (fd, &FETCH_CHAR (point + inserted - 1) + 1, try);
  1795.  
  1796.       if (this <= 0)
  1797.     {
  1798.       i = this;
  1799.       break;
  1800.     }
  1801.  
  1802.       GPT += this;
  1803.       GAP_SIZE -= this;
  1804.       ZV += this;
  1805.       Z += this;
  1806.       inserted += this;
  1807.     }
  1808.  
  1809.   if (inserted > 0)
  1810.     MODIFF++;
  1811.   record_insert (point, inserted);
  1812.  
  1813.   close (fd);
  1814.  
  1815.   /* Discard the unwind protect */
  1816.   specpdl_ptr = specpdl + count;
  1817.  
  1818.   if (i < 0)
  1819.     error ("IO error reading %s: %s",
  1820.        XSTRING (filename)->data, err_str (errno));
  1821.  
  1822.  notfound:
  1823.  
  1824.   if (!NULL (visit))
  1825.     {
  1826.       current_buffer->undo_list = Qnil;
  1827. #ifdef APOLLO
  1828.       stat (XSTRING (filename)->data, &st);
  1829. #endif
  1830.       current_buffer->modtime = st.st_mtime;
  1831.       current_buffer->save_modified = MODIFF;
  1832.       current_buffer->auto_save_modified = MODIFF;
  1833.       XFASTINT (current_buffer->save_length) = Z - BEG;
  1834. #ifdef CLASH_DETECTION
  1835.       if (!NULL (current_buffer->filename))
  1836.     unlock_file (current_buffer->filename);
  1837.       unlock_file (filename);
  1838. #endif /* CLASH_DETECTION */
  1839.       current_buffer->filename = filename;
  1840.       /* If visiting nonexistent file, return nil.  */
  1841.       if (st.st_mtime == -1)
  1842.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1843.     }
  1844.  
  1845.   UNGCPRO;
  1846.   return Fcons (filename, Fcons (make_number (inserted), Qnil));
  1847. }
  1848.  
  1849. DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 5,
  1850.   "r\nFWrite region to file: ",
  1851.   "Write current region into specified file.\n\
  1852. When called from a program, takes three arguments:\n\
  1853. START, END and FILENAME.  START and END are buffer positions.\n\
  1854. Optional fourth argument APPEND if non-nil means\n\
  1855.   append to existing file contents (if any).\n\
  1856. Optional fifth argument VISIT if t means\n\
  1857.   set last-save-file-modtime of buffer to this file's modtime\n\
  1858.   and mark buffer not modified.\n\
  1859. If VISIT is neither t nor nil, it means do not print\n\
  1860.   the \"Wrote file\" message.")
  1861.   (start, end, filename, append, visit)
  1862.      Lisp_Object start, end, filename, append, visit;
  1863. {
  1864.   register int desc;
  1865.   int failure;
  1866.   int save_errno;
  1867.   unsigned char *fn;
  1868.   struct stat st;
  1869.   int tem;
  1870.   int count = specpdl_ptr - specpdl;
  1871. #ifdef VMS
  1872.   unsigned char *fname = 0;    /* If non-0, original filename (must rename) */
  1873. #endif /* VMS */
  1874.  
  1875.   /* Special kludge to simplify auto-saving */
  1876.   if (NULL (start))
  1877.     {
  1878.       XFASTINT (start) = BEG;
  1879.       XFASTINT (end) = Z;
  1880.     }
  1881.   else
  1882.     validate_region (&start, &end);
  1883.  
  1884.   filename = Fexpand_file_name (filename, Qnil);
  1885.   fn = XSTRING (filename)->data;
  1886.  
  1887. #ifdef CLASH_DETECTION
  1888.   if (!auto_saving)
  1889.     lock_file (filename);
  1890. #endif /* CLASH_DETECTION */
  1891.  
  1892.   desc = -1;
  1893.   if (!NULL (append))
  1894.     desc = open (fn, O_WRONLY);
  1895.  
  1896.   if (desc < 0)
  1897. #ifdef VMS
  1898.     if (auto_saving)    /* Overwrite any previous version of autosave file */
  1899.       {
  1900.     vms_truncate (fn);    /* if fn exists, truncate to zero length */
  1901.     desc = open (fn, O_RDWR);
  1902.     if (desc < 0)
  1903.       desc = creat_copy_attrs (XTYPE (current_buffer->filename) == Lisp_String
  1904.                    ? XSTRING (current_buffer->filename)->data : 0,
  1905.                    fn);
  1906.       }
  1907.     else        /* Write to temporary name and rename if no errors */
  1908.       {
  1909.     Lisp_Object temp_name;
  1910.     temp_name = Ffile_name_directory (filename);
  1911.  
  1912.     if (!NULL (temp_name))
  1913.       {
  1914.         temp_name = Fmake_temp_name (concat2 (temp_name,
  1915.                           build_string ("$$SAVE$$")));
  1916.         fname = XSTRING (filename)->data;
  1917.         fn = XSTRING (temp_name)->data;
  1918.         desc = creat_copy_attrs (fname, fn);
  1919.         if (desc < 0)
  1920.           {
  1921.         /* If we can't open the temporary file, try creating a new
  1922.            version of the original file.  VMS "creat" creates a
  1923.            new version rather than truncating an existing file. */
  1924.         fn = fname;
  1925.         fname = 0;
  1926.         desc = creat (fn, 0666);
  1927. #if 0
  1928.         if (desc < 0)
  1929.           {
  1930.             /* We can't make a new version;
  1931.                try to truncate and rewrite existing version if any.  */
  1932.             vms_truncate (fn);
  1933.             desc = open (fn, O_RDWR);
  1934.           }
  1935. #endif
  1936.           }
  1937.       }
  1938.     else
  1939.       desc = creat (fn, 0666);
  1940.       }
  1941. #else /* not VMS */
  1942.   desc = creat (fn, 0666);
  1943. #endif /* not VMS */
  1944.  
  1945.   if (desc < 0)
  1946.     {
  1947. #ifdef CLASH_DETECTION
  1948.       save_errno = errno;
  1949.       if (!auto_saving) unlock_file (filename);
  1950.       errno = save_errno;
  1951. #endif /* CLASH_DETECTION */
  1952.       report_file_error ("Opening output file", Fcons (filename, Qnil));
  1953.     }
  1954.  
  1955.   record_unwind_protect (close_file_unwind, make_number (desc));
  1956.  
  1957.   if (!NULL (append))
  1958.     if (lseek (desc, 0, 2) < 0)
  1959.       {
  1960. #ifdef CLASH_DETECTION
  1961.     if (!auto_saving) unlock_file (filename);
  1962. #endif /* CLASH_DETECTION */
  1963.     report_file_error ("Lseek error", Fcons (filename, Qnil));
  1964.       }
  1965.  
  1966. #ifdef VMS
  1967. /*
  1968.  * Kludge Warning: The VMS C RTL likes to insert carriage returns
  1969.  * if we do writes that don't end with a carriage return. Furthermore
  1970.  * it cannot handle writes of more then 16K. The modified
  1971.  * version of "sys_write" in SYSDEP.C (see comment there) copes with
  1972.  * this EXCEPT for the last record (iff it doesn't end with a carriage
  1973.  * return). This implies that if your buffer doesn't end with a carriage
  1974.  * return, you get one free... tough. However it also means that if
  1975.  * we make two calls to sys_write (a la the following code) you can
  1976.  * get one at the gap as well. The easiest way to fix this (honest)
  1977.  * is to move the gap to the next newline (or the end of the buffer).
  1978.  * Thus this change.
  1979.  *
  1980.  * Yech!
  1981.  */
  1982.   if (GPT > BEG && GPT_ADDR[-1] != '\n')
  1983.     move_gap (find_next_newline (GPT, 1));
  1984. #endif
  1985.  
  1986.   failure = 0;
  1987.   if (XINT (start) != XINT (end))
  1988.     {
  1989.       if (XINT (start) < GPT)
  1990.     {
  1991.       register int end1 = XINT (end);
  1992.       tem = XINT (start);
  1993.       failure = 0 > e_write (desc, &FETCH_CHAR (tem),
  1994.                  min (GPT, end1) - tem);
  1995.       save_errno = errno;
  1996.     }
  1997.  
  1998.       if (XINT (end) > GPT && !failure)
  1999.     {
  2000.       tem = XINT (start);
  2001.       tem = max (tem, GPT);
  2002.       failure = 0 > e_write (desc, &FETCH_CHAR (tem), XINT (end) - tem);
  2003.       save_errno = errno;
  2004.     }
  2005.     }
  2006.  
  2007. #ifndef USG
  2008. #ifndef VMS
  2009. #ifndef BSD4_1
  2010.   /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
  2011.      Disk full in NFS may be reported here.  */
  2012.   if (fsync (desc) < 0)
  2013.     failure = 1, save_errno = errno;
  2014. #endif
  2015. #endif
  2016. #endif
  2017.  
  2018. #if 0
  2019.   /* Spurious "file has changed on disk" warnings have been 
  2020.      observed on Sun 3 as well.  Maybe close changes the modtime
  2021.      with nfs as well.  */
  2022.  
  2023.   /* On VMS and APOLLO, must do the stat after the close
  2024.      since closing changes the modtime.  */
  2025. #ifndef VMS
  2026. #ifndef APOLLO
  2027.   /* Recall that #if defined does not work on VMS.  */
  2028. #define FOO
  2029.   fstat (desc, &st);
  2030. #endif
  2031. #endif
  2032. #endif /* 0 */
  2033.  
  2034.   /* NFS can report a write failure now.  */
  2035.   if (close (desc) < 0)
  2036.     failure = 1, save_errno = errno;
  2037.  
  2038. #ifdef VMS
  2039.   /* If we wrote to a temporary name and had no errors, rename to real name. */
  2040.   if (fname)
  2041.     {
  2042.       if (!failure)
  2043.     failure = (rename_sans_version (fn, fname) != 0), save_errno = errno;
  2044.       fn = vms_file_written;    /* this is filled by rename_sans_version */
  2045.     }
  2046. #endif /* VMS */
  2047.  
  2048. #ifndef FOO
  2049.   stat (fn, &st);
  2050. #endif
  2051.   /* Discard the unwind protect */
  2052.   specpdl_ptr = specpdl + count;
  2053.  
  2054. #ifdef CLASH_DETECTION
  2055.   if (!auto_saving)
  2056.     unlock_file (filename);
  2057. #endif /* CLASH_DETECTION */
  2058.  
  2059.   /* Do this before reporting IO error
  2060.      to avoid a "file has changed on disk" warning on
  2061.      next attempt to save.  */
  2062.   if (EQ (visit, Qt))
  2063.     current_buffer->modtime = st.st_mtime;
  2064.  
  2065.   if (failure)
  2066.     error ("IO error writing %s: %s", fn, err_str (save_errno));
  2067.  
  2068.   if (EQ (visit, Qt))
  2069.     {
  2070.       current_buffer->save_modified = MODIFF;
  2071.       XFASTINT (current_buffer->save_length) = Z - BEG;
  2072.       current_buffer->filename = filename;
  2073.     }
  2074.   else if (!NULL (visit))
  2075.     return Qnil;
  2076.  
  2077.   if (!auto_saving)
  2078.     message ("Wrote %s", fn);
  2079.  
  2080.   return Qnil;
  2081. }
  2082.  
  2083. int
  2084. e_write (desc, addr, len)
  2085.      int desc;
  2086.      register char *addr;
  2087.      register int len;
  2088. {
  2089.   char buf[16 * 1024];
  2090.   register char *p, *end;
  2091.  
  2092.   if (!EQ (current_buffer->selective_display, Qt))
  2093.     return write (desc, addr, len) - len;
  2094.   else
  2095.     {
  2096.       p = buf;
  2097.       end = p + sizeof buf;
  2098.       while (len--)
  2099.     {
  2100.       if (p == end)
  2101.         {
  2102.           if (write (desc, buf, sizeof buf) != sizeof buf)
  2103.         return -1;
  2104.           p = buf;
  2105.         }
  2106.       *p = *addr++;
  2107.       if (*p++ == '\015')
  2108.         p[-1] = '\n';
  2109.     }
  2110.       if (p != buf)
  2111.     if (write (desc, buf, p - buf) != p - buf)
  2112.       return -1;
  2113.     }
  2114.   return 0;
  2115. }
  2116.  
  2117. DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime,
  2118.   Sverify_visited_file_modtime, 1, 1, 0,
  2119.   "Return t if last mod time of BUF's visited file matches what BUF records.\n\
  2120. This means that the file has not been changed since it was visited or saved.")
  2121.   (buf)
  2122.      Lisp_Object buf;
  2123. {
  2124.   struct buffer *b;
  2125.   struct stat st;
  2126.  
  2127.   CHECK_BUFFER (buf, 0);
  2128.   b = XBUFFER (buf);
  2129.  
  2130.   if (XTYPE (b->filename) != Lisp_String) return Qt;
  2131.   if (b->modtime == 0) return Qt;
  2132.  
  2133.   if (stat (XSTRING (b->filename)->data, &st) < 0)
  2134.     {
  2135.       /* If the file doesn't exist now and didn't exist before,
  2136.      we say that it isn't modified, provided the error is a tame one.  */
  2137.       if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
  2138.     st.st_mtime = -1;
  2139.       else
  2140.     st.st_mtime = 0;
  2141.     }
  2142.   if (st.st_mtime == b->modtime
  2143.       /* If both are positive, accept them if they are off by one second.  */
  2144.       || (st.st_mtime > 0 && b->modtime > 0
  2145.       && (st.st_mtime == b->modtime + 1
  2146.           || st.st_mtime == b->modtime - 1)))
  2147.     return Qt;
  2148.   return Qnil;
  2149. }
  2150.  
  2151. DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime,
  2152.   Sclear_visited_file_modtime, 0, 0, 0,
  2153.   "Clear out records of last mod time of visited file.\n\
  2154. Next attempt to save will certainly not complain of a discrepancy.")
  2155.   ()
  2156. {
  2157.   current_buffer->modtime = 0;
  2158.   return Qnil;
  2159. }
  2160.  
  2161. Lisp_Object
  2162. auto_save_error ()
  2163. {
  2164.   unsigned char *name = XSTRING (current_buffer->name)->data;
  2165.  
  2166.   bell ();
  2167.   message ("Autosaving...error for %s", name);
  2168.   Fsleep_for (make_number (1));
  2169.   message ("Autosaving...error!for %s", name);
  2170.   Fsleep_for (make_number (1));
  2171.   message ("Autosaving...error for %s", name);
  2172.   Fsleep_for (make_number (1));
  2173.   return Qnil;
  2174. }
  2175.  
  2176. Lisp_Object
  2177. auto_save_1 ()
  2178. {
  2179.   return
  2180.     Fwrite_region (Qnil, Qnil,
  2181.            current_buffer->auto_save_file_name,
  2182.            Qnil, Qlambda);
  2183. }
  2184.  
  2185. DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 1, "",
  2186.   "Auto-save all buffers that need it.\n\
  2187. This is all buffers that have auto-saving enabled\n\
  2188. and are changed since last auto-saved.\n\
  2189. Auto-saving writes the buffer into a file\n\
  2190. so that your editing is not lost if the system crashes.\n\
  2191. This file is not the file you visited; that changes only when you save.\n\n\
  2192. Non-nil argument means do not print any message if successful.")
  2193.   (nomsg)
  2194.      Lisp_Object nomsg;
  2195. {
  2196.   struct buffer *old = current_buffer, *b;
  2197.   Lisp_Object tail, buf;
  2198.   int auto_saved = 0;
  2199.   int tried = 0;
  2200.   char *omessage = echo_area_contents;
  2201.   /* No GCPRO needed, because (when it matters) all Lisp_Object variables
  2202.      point to non-strings reached from Vbuffer_alist.  */
  2203.  
  2204.   auto_saving = 1;
  2205.   if (minibuf_level)
  2206.     nomsg = Qt;
  2207.  
  2208.   for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  2209.        tail = XCONS (tail)->cdr)
  2210.     {
  2211.       buf = XCONS (XCONS (tail)->car)->cdr;
  2212.       b = XBUFFER (buf);
  2213.       /* Check for auto save enabled
  2214.      and file changed since last auto save
  2215.      and file changed since last real save.  */
  2216.       if (XTYPE (b->auto_save_file_name) == Lisp_String
  2217.       && b->save_modified < BUF_MODIFF (b)
  2218.       && b->auto_save_modified < BUF_MODIFF (b))
  2219.     {
  2220.       /* If we at least consider a buffer for auto-saving,
  2221.          don't try again for a suitable time.  */
  2222.       tried++;
  2223.       if ((XFASTINT (b->save_length) * 10
  2224.            > (BUF_Z (b) - BUF_BEG (b)) * 13)
  2225.           /* A short file is likely to change a large fraction;
  2226.          spare the user annoying messages.  */
  2227.           && XFASTINT (b->save_length) > 5000
  2228.           /* These messages are frequent and annoying for `*mail*'.  */
  2229.           && !EQ (b->filename, Qnil))
  2230.         {
  2231.           /* It has shrunk too much; don't checkpoint. */
  2232.           message ("Buffer %s has shrunk a lot; not autosaving it",
  2233.                XSTRING (b->name)->data);
  2234.           Fsleep_for (make_number (1));
  2235.           continue;
  2236.         }
  2237.       set_buffer_internal (b);
  2238.       if (!auto_saved && NULL (nomsg))
  2239.         message1 ("Auto-saving...");
  2240.       internal_condition_case (auto_save_1, Qt, auto_save_error);
  2241.       auto_saved++;
  2242.       b->auto_save_modified = BUF_MODIFF (b);
  2243.       XFASTINT (current_buffer->save_length) = Z - BEG;
  2244.       set_buffer_internal (old);
  2245.     }
  2246.     }
  2247.  
  2248.   if (tried)
  2249.     record_auto_save ();
  2250.  
  2251.   if (auto_saved && NULL (nomsg))
  2252.     message1 (omessage ? omessage : "Auto-saving...done");
  2253.  
  2254.   auto_saving = 0;
  2255.   return Qnil;
  2256. }
  2257.  
  2258. DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved,
  2259.   Sset_buffer_auto_saved, 0, 0, 0,
  2260.   "Mark current buffer as auto-saved with its current text.\n\
  2261. No auto-save file will be written until the buffer changes again.")
  2262.   ()
  2263. {
  2264.   current_buffer->auto_save_modified = MODIFF;
  2265.   XFASTINT (current_buffer->save_length) = Z - BEG;
  2266.   return Qnil;
  2267. }
  2268.  
  2269. DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p,
  2270.   0, 0, 0,
  2271.   "Return t if buffer has been auto-saved since last read in or saved.")
  2272.   ()
  2273. {
  2274.   return (current_buffer->save_modified < current_buffer->auto_save_modified) ? Qt : Qnil;
  2275. }
  2276.  
  2277. /* Reading and completing file names */
  2278. extern Lisp_Object Ffile_name_completion (), Ffile_name_all_completions ();
  2279.  
  2280. DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal,
  2281.   3, 3, 0,
  2282.   "Internal subroutine for read-file-name.  Do not call this.")
  2283.   (string, dir, action)
  2284.      Lisp_Object string, dir, action;
  2285.   /* action is nil for complete, t for return list of completions,
  2286.      lambda for verify final value */
  2287. {
  2288.   Lisp_Object name, specdir, realdir, val;
  2289.   if (XSTRING (string)->size == 0)
  2290.     {
  2291.       name = string;
  2292.       realdir = dir;
  2293.       if (EQ (action, Qlambda))
  2294.     return Qnil;
  2295.     }
  2296.   else
  2297.     {
  2298.       string = Fsubstitute_in_file_name (string);
  2299.       name = Ffile_name_nondirectory (string);
  2300.       realdir = Ffile_name_directory (string);
  2301.       if (NULL (realdir))
  2302.     realdir = dir;
  2303.       else
  2304.     realdir = Fexpand_file_name (realdir, dir);
  2305.     }
  2306.  
  2307.   if (NULL (action))
  2308.     {
  2309.       specdir = Ffile_name_directory (string);
  2310.       val = Ffile_name_completion (name, realdir);
  2311.       if (XTYPE (val) != Lisp_String)
  2312.     return (val);
  2313.  
  2314.       if (!NULL (specdir))
  2315.     val = concat2 (specdir, val);
  2316. #ifndef VMS
  2317.       {
  2318.     register unsigned char *old, *new;
  2319.     register int n;
  2320.     int osize, count;
  2321.  
  2322.     osize = XSTRING (val)->size;
  2323.     /* Quote "$" as "$$" to get it past substitute-in-file-name */
  2324.     for (n = osize, count = 0, old = XSTRING (val)->data; n > 0; n--)
  2325.       if (*old++ == '$') count++;
  2326.     if (count > 0)
  2327.       {
  2328.         old = XSTRING (val)->data;
  2329.         val = Fmake_string (make_number (osize + count), make_number (0));
  2330.         new = XSTRING (val)->data;
  2331.         for (n = osize; n > 0; n--)
  2332.           if (*old != '$')
  2333.         *new++ = *old++;
  2334.           else
  2335.         {
  2336.           *new++ = '$';
  2337.           *new++ = '$';
  2338.           old++;
  2339.         }
  2340.       }
  2341.       }
  2342. #endif /* Not VMS */
  2343.       return (val);
  2344.     }
  2345.  
  2346.   if (EQ (action, Qt))
  2347.     return Ffile_name_all_completions (name, realdir);
  2348.   /* Only other case actually used is ACTION = lambda */
  2349. #ifdef VMS
  2350.   /* Supposedly this helps commands such as `cd' that read directory names,
  2351.      but can someone explain how it helps them? -- RMS */
  2352.   if (XSTRING (name)->size == 0)
  2353.     return Qt;
  2354. #endif /* VMS */
  2355.   return Ffile_exists_p (string);
  2356. }
  2357.  
  2358. DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 4, 0,
  2359.   "Read file name, prompting with PROMPT and completing in directory DIR.\n\
  2360. Value is not expanded!  You must call expand-file-name yourself.\n\
  2361. Default name to DEFAULT if user enters a null string.\n\
  2362. Fourth arg MUSTMATCH non-nil means require existing file's name.\n\
  2363.  Non-nil and non-t means also require confirmation after completion.\n\
  2364. DIR defaults to current buffer's directory default.")
  2365.   (prompt, dir, defalt, mustmatch)
  2366.      Lisp_Object prompt, dir, defalt, mustmatch;
  2367. {
  2368.   Lisp_Object val, insdef, tem;
  2369.   struct gcpro gcpro1, gcpro2;
  2370.   register char *homedir;
  2371.   int count;
  2372.  
  2373.   if (NULL (dir))
  2374.     dir = current_buffer->directory;
  2375.   if (NULL (defalt))
  2376.     defalt = current_buffer->filename;
  2377.  
  2378.   /* If dir starts with user's homedir, change that to ~. */
  2379.   homedir = (char *) egetenv ("HOME");
  2380.   if (homedir != 0
  2381.       && XTYPE (dir) == Lisp_String
  2382.       && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
  2383.       && XSTRING (dir)->data[strlen (homedir)] == '/')
  2384.     {
  2385.       dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
  2386.              XSTRING (dir)->size - strlen (homedir) + 1);
  2387.       XSTRING (dir)->data[0] = '~';
  2388.     }
  2389.  
  2390.   if (insert_default_directory)
  2391.     insdef = dir;
  2392.   else
  2393.     insdef = build_string ("");
  2394.  
  2395. #ifdef VMS
  2396.   count = specpdl_ptr - specpdl;
  2397.   specbind (intern ("completion-ignore-case"), Qt);
  2398. #endif
  2399.  
  2400.   GCPRO2 (insdef, defalt);
  2401.   val = Fcompleting_read (prompt, intern ("read-file-name-internal"),
  2402.               dir, mustmatch,
  2403.               insert_default_directory ? insdef : Qnil);
  2404.  
  2405. #ifdef VMS
  2406.   unbind_to (count);
  2407. #endif
  2408.  
  2409.   UNGCPRO;
  2410.   if (NULL (val))
  2411.     error ("No file name specified");
  2412.   tem = Fstring_equal (val, insdef);
  2413.   if (!NULL (tem) && !NULL (defalt))
  2414.     return defalt;
  2415.   return Fsubstitute_in_file_name (val);
  2416. }
  2417.  
  2418. syms_of_fileio ()
  2419. {
  2420.   Qfile_error = intern ("file-error");
  2421.   staticpro (&Qfile_error);
  2422.   Qfile_already_exists = intern("file-already-exists");
  2423.   staticpro (&Qfile_already_exists);
  2424.  
  2425.   Fput (Qfile_error, Qerror_conditions,
  2426.     Fcons (Qfile_error, Fcons (Qerror, Qnil)));
  2427.   Fput (Qfile_error, Qerror_message,
  2428.     build_string ("File error"));
  2429.  
  2430.   Fput (Qfile_already_exists, Qerror_conditions,
  2431.     Fcons (Qfile_already_exists,
  2432.            Fcons (Qfile_error, Fcons (Qerror, Qnil))));
  2433.   Fput (Qfile_already_exists, Qerror_message,
  2434.     build_string ("File already exists"));
  2435.  
  2436.   DEFVAR_BOOL ("insert-default-directory", &insert_default_directory,
  2437.     "*Non-nil means when reading a filename start with default dir in minibuffer.");
  2438.   insert_default_directory = 1;
  2439.  
  2440.   DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm,
  2441.     "*Non-nil means write new files with record format `stmlf'.\n\
  2442. nil means use format `var'.  This variable is meaningful only on VMS.");
  2443.   vms_stmlf_recfm = 0;
  2444.  
  2445.   defsubr (&Sfile_name_directory);
  2446.   defsubr (&Sfile_name_nondirectory);
  2447.   defsubr (&Sfile_name_as_directory);
  2448.   defsubr (&Sdirectory_file_name);
  2449.   defsubr (&Smake_temp_name);
  2450.   defsubr (&Sexpand_file_name);
  2451.   defsubr (&Ssubstitute_in_file_name);
  2452.   defsubr (&Scopy_file);
  2453.   defsubr (&Sdelete_file);
  2454.   defsubr (&Srename_file);
  2455.   defsubr (&Sadd_name_to_file);
  2456. #ifdef S_IFLNK
  2457.   defsubr (&Smake_symbolic_link);
  2458. #endif /* S_IFLNK */
  2459. #ifdef VMS
  2460.   defsubr (&Sdefine_logical_name);
  2461. #endif /* VMS */
  2462. #ifdef HPUX_NET
  2463.   defsubr (&Ssysnetunam);
  2464. #endif /* HPUX_NET */
  2465.   defsubr (&Sfile_name_absolute_p);
  2466.   defsubr (&Sfile_exists_p);
  2467.   defsubr (&Sfile_readable_p);
  2468.   defsubr (&Sfile_writable_p);
  2469.   defsubr (&Sfile_symlink_p);
  2470.   defsubr (&Sfile_directory_p);
  2471.   defsubr (&Sfile_modes);
  2472.   defsubr (&Sset_file_modes);
  2473.   defsubr (&Sfile_newer_than_file_p);
  2474.   defsubr (&Sinsert_file_contents);
  2475.   defsubr (&Swrite_region);
  2476.   defsubr (&Sverify_visited_file_modtime);
  2477.   defsubr (&Sclear_visited_file_modtime);
  2478.   defsubr (&Sdo_auto_save);
  2479.   defsubr (&Sset_buffer_auto_saved);
  2480.   defsubr (&Srecent_auto_save_p);
  2481.  
  2482.   defsubr (&Sread_file_name_internal);
  2483.   defsubr (&Sread_file_name);
  2484. }
  2485.