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