📄 fileio.c
字号:
#else if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data) || 0 > unlink (XSTRING (filename)->data))#endif { if (errno == EXDEV) { Fcopy_file (filename, newname, ok_if_already_exists, Qt); Fdelete_file (filename); } else#ifdef NO_ARG_ARRAY { args[0] = filename; args[1] = newname; report_file_error ("Renaming", Flist (2, args)); }#else report_file_error ("Renaming", Flist (2, &filename));#endif } UNGCPRO; return Qnil;}DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3, "fAdd name to file: \nFName to add to %s: \np", "Give FILE additional name NEWNAME. Both args strings.\n\Signals a file-already-exists error if NEWNAME already exists\n\unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\A number as third arg means request confirmation if NEWNAME already exists.\n\This is what happens in interactive use with M-x.") (filename, newname, ok_if_already_exists) Lisp_Object filename, newname, ok_if_already_exists;{#ifdef NO_ARG_ARRAY Lisp_Object args[2];#endif struct gcpro gcpro1, gcpro2; GCPRO2 (filename, newname); CHECK_STRING (filename, 0); CHECK_STRING (newname, 1); filename = Fexpand_file_name (filename, Qnil); newname = Fexpand_file_name (newname, Qnil); if (NULL (ok_if_already_exists) || XTYPE (ok_if_already_exists) == Lisp_Int) barf_or_query_if_file_exists (newname, "make it a new name", XTYPE (ok_if_already_exists) == Lisp_Int); unlink (XSTRING (newname)->data); if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data)) {#ifdef NO_ARG_ARRAY args[0] = filename; args[1] = newname; report_file_error ("Adding new name", Flist (2, args));#else report_file_error ("Adding new name", Flist (2, &filename));#endif } UNGCPRO; return Qnil;}#ifdef S_IFLNKDEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3, "FMake symbolic link to file: \nFMake symbolic link to file %s: \np", "Make a symbolic link to FILENAME, named LINKNAME. Both args strings.\n\Signals a file-already-exists error if NEWNAME already exists\n\unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\A number as third arg means request confirmation if NEWNAME already exists.\n\This happens for interactive use with M-x.") (filename, newname, ok_if_already_exists) Lisp_Object filename, newname, ok_if_already_exists;{#ifdef NO_ARG_ARRAY Lisp_Object args[2];#endif struct gcpro gcpro1, gcpro2; GCPRO2 (filename, newname); CHECK_STRING (filename, 0); CHECK_STRING (newname, 1); filename = Fexpand_file_name (filename, Qnil); newname = Fexpand_file_name (newname, Qnil); if (NULL (ok_if_already_exists) || XTYPE (ok_if_already_exists) == Lisp_Int) barf_or_query_if_file_exists (newname, "make it a link", XTYPE (ok_if_already_exists) == Lisp_Int); if (0 > symlink (XSTRING (filename)->data, XSTRING (newname)->data)) {#ifdef NO_ARG_ARRAY args[0] = filename; args[1] = newname; report_file_error ("Making symbolic link", Flist (2, args));#else report_file_error ("Making symbolic link", Flist (2, &filename));#endif } UNGCPRO; return Qnil;}#endif /* S_IFLNK */#ifdef VMSDEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name, 2, 2, "sDefine logical name: \nsDefine logical name %s as: ", "Define the job-wide logical name NAME to have the value STRING.\n\If STRING is nil or a null string, the logical name NAME is deleted.") (varname, string) Lisp_Object varname; Lisp_Object string;{ CHECK_STRING (varname, 0); if (NULL (string)) delete_logical_name (XSTRING (varname)->data); else { CHECK_STRING (string, 1); if (XSTRING (string)->size == 0) delete_logical_name (XSTRING (varname)->data); else define_logical_name (XSTRING (varname)->data, XSTRING (string)->data); } return string;}#endif /* VMS */#ifdef HPUX_NETDEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0, "Open a network connection to PATH using LOGIN as the login string.") (path, login) Lisp_Object path, login;{ int netresult; CHECK_STRING (path, 0); CHECK_STRING (login, 0); netresult = netunam (XSTRING (path)->data, XSTRING (login)->data); if (netresult == -1) return Qnil; else return Qt;}#endif /* HPUX_NET */DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p, 1, 1, 0, "Return t if file FILENAME specifies an absolute path name.") (filename) Lisp_Object filename;{ unsigned char *ptr; CHECK_STRING (filename, 0); ptr = XSTRING (filename)->data; if (*ptr == '/' || *ptr == '~'#ifdef VMS/* ??? This criterion is probably wrong for '<'. */ || index (ptr, ':') || index (ptr, '<') || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']')) && ptr[1] != '.')#endif /* VMS */ ) return Qt; else return Qnil;}DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0, "Return t if file FILENAME exists. (This does not mean you can read it.)\n\See also file-readable-p and file-attributes.") (filename) Lisp_Object filename;{ Lisp_Object abspath; CHECK_STRING (filename, 0); abspath = Fexpand_file_name (filename, Qnil); return (access (XSTRING (abspath)->data, 0) >= 0) ? Qt : Qnil;}DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0, "Return t if file FILENAME exists and you can read it.\n\See also file-exists-p and file-attributes.") (filename) Lisp_Object filename;{ Lisp_Object abspath; CHECK_STRING (filename, 0); abspath = Fexpand_file_name (filename, Qnil); return (access (XSTRING (abspath)->data, 4) >= 0) ? Qt : Qnil;}DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0, "If file FILENAME is the name of a symbolic link\n\returns the name of the file to which it is linked.\n\Otherwise returns NIL.") (filename) Lisp_Object filename;{#ifdef S_IFLNK char *buf; int bufsize; int valsize; Lisp_Object val; CHECK_STRING (filename, 0); filename = Fexpand_file_name (filename, Qnil); bufsize = 100; while (1) { buf = (char *) xmalloc (bufsize); bzero (buf, bufsize); valsize = readlink (XSTRING (filename)->data, buf, bufsize); if (valsize < bufsize) break; /* Buffer was not long enough */ free (buf); bufsize *= 2; } if (valsize == -1) { free (buf); return Qnil; } val = make_string (buf, valsize); free (buf); return val;#else /* not S_IFLNK */ return Qnil;#endif /* not S_IFLNK */}/* Having this before file-symlink-p mysteriously caused it to be forgotten on the RT/PC. */DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0, "Return t if file FILENAME can be written or created by you.") (filename) Lisp_Object filename;{ Lisp_Object abspath, dir; CHECK_STRING (filename, 0); abspath = Fexpand_file_name (filename, Qnil); if (access (XSTRING (abspath)->data, 0) >= 0) return (access (XSTRING (abspath)->data, 2) >= 0) ? Qt : Qnil; dir = Ffile_name_directory (abspath);#ifdef VMS if (!NULL (dir)) dir = Fdirectory_file_name (dir);#endif /* VMS */ return (access (!NULL (dir) ? (char *) XSTRING (dir)->data : "", 2) >= 0 ? Qt : Qnil);}DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0, "Return t if file FILENAME is the name of a directory as a file.\n\A directory name spec may be given instead; then the value is t\n\if the directory so specified exists and really is a directory.") (filename) Lisp_Object filename;{ register Lisp_Object abspath; struct stat st; abspath = expand_and_dir_to_file (filename, current_buffer->directory); if (stat (XSTRING (abspath)->data, &st) < 0) return Qnil; return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;}DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0, "Return mode bits of FILE, as an integer.") (filename) Lisp_Object filename;{ Lisp_Object abspath; struct stat st; abspath = expand_and_dir_to_file (filename, current_buffer->directory); if (stat (XSTRING (abspath)->data, &st) < 0) return Qnil; return make_number (st.st_mode & 07777);}DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0, "Set mode bits of FILE to MODE (an integer).\n\Only the 12 low bits of MODE are used.") (filename, mode) Lisp_Object filename, mode;{ Lisp_Object abspath; abspath = Fexpand_file_name (filename, current_buffer->directory); CHECK_NUMBER (mode, 1);#ifndef APOLLO if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0) report_file_error ("Doing chmod", Fcons (abspath, Qnil));#else /* APOLLO */ if (!egetenv ("USE_DOMAIN_ACLS")) { struct stat st; struct timeval tvp[2]; /* chmod on apollo also change the file's modtime; need to save the modtime and then restore it. */ if (stat (XSTRING (abspath)->data, &st) < 0) { report_file_error ("Doing chmod", Fcons (abspath, Qnil)); return (Qnil); } if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0) report_file_error ("Doing chmod", Fcons (abspath, Qnil)); /* reset the old accessed and modified times. */ tvp[0].tv_sec = st.st_atime + 1; /* +1 due to an Apollo roundoff bug */ tvp[0].tv_usec = 0; tvp[1].tv_sec = st.st_mtime + 1; /* +1 due to an Apollo roundoff bug */ tvp[1].tv_usec = 0; if (utimes (XSTRING (abspath)->data, tvp) < 0) report_file_error ("Doing utimes", Fcons (abspath, Qnil)); }#endif /* APOLLO */ return Qnil;}DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0, "Return t if file FILE1 is newer than file FILE2.\n\If FILE1 does not exist, the answer is nil;\n\otherwise, if FILE2 does not exist, the answer is t.") (file1, file2) Lisp_Object file1, file2;{ Lisp_Object abspath; struct stat st; int mtime1; CHECK_STRING (file1, 0); CHECK_STRING (file2, 0); abspath = expand_and_dir_to_file (file1, current_buffer->directory); if (stat (XSTRING (abspath)->data, &st) < 0) return Qnil; mtime1 = st.st_mtime; abspath = expand_and_dir_to_file (file2, current_buffer->directory); if (stat (XSTRING (abspath)->data, &st) < 0) return Qt; return (mtime1 > st.st_mtime) ? Qt : Qnil;}close_file_unwind (fd) Lisp_Object fd;{ close (XFASTINT (fd));}DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents, 1, 2, 0, "Insert contents of file FILENAME after point.\n\Returns list of absolute pathname and length of data inserted.\n\If second argument VISIT is non-nil, the buffer's visited filename\n\and last save file modtime are set, and it is marked unmodified.\n\If visiting and the file does not exist, visiting is completed\n\before the error is signaled.") (filename, visit) Lisp_Object filename, visit;{ struct stat st; register int fd; register int inserted = 0; register int i = 0; int count = specpdl_ptr - specpdl; struct gcpro gcpro1; GCPRO1 (filename); if (!NULL (current_buffer->read_only)) Fbarf_if_buffer_read_only(); CHECK_STRING (filename, 0); filename = Fexpand_file_name (filename, Qnil); fd = -1;#ifndef APOLLO if (stat (XSTRING (filename)->data, &st) < 0 || (fd = open (XSTRING (filename)->data, 0)) < 0)#else if ((fd = open (XSTRING (filename)->data, 0)) < 0 || fstat (fd, &st) < 0)#endif /* not APOLLO */ { if (fd >= 0) close (fd); if (NULL (visit)) report_file_error ("Opening input file", Fcons (filename, Qnil)); st.st_mtime = -1; goto notfound; } record_unwind_protect (close_file_unwind, make_number (fd)); /* Supposedly happens on VMS. */ if (st.st_size < 0) error ("File size is negative"); { register Lisp_Object temp; /* Make sure point-max won't overflow after this insertion. */ XSET (temp, Lisp_Int, st.st_size + Z); if (st.st_size + Z != XINT (temp)) error ("maximum buffer size exceeded"); } if (NULL (visit)) prepare_to_modify_buffer (); move_gap (point); if (GAP_SIZE < st.st_size) make_gap ((int)st.st_size - GAP_SIZE); while (1) { int try = min (st.st_size - inserted, 64 << 10); int this = read (fd, &FETCH_CHAR (point + inserted - 1) + 1, try); if (this <= 0) { i = this; break; } GPT += this; GAP_SIZE -= this; ZV += this; Z += this; inserted += this; } if (inserted > 0) MODIFF++; record_insert (point, inserted); close (fd); /* Discard the unwind protect */ specpdl_ptr = specpdl + count; if (i < 0) error ("IO error reading %s: %s", XSTRING (filename)->data, err_str (errno)); notfound: if (!NULL (visit)) { current_buffer->undo_list = Qnil;#ifdef APOLLO stat (XSTRING (filename)->data, &st);#endif current_buffer->modtime = st.st_mtime; current_buffer->save_modified = MODIFF; current_buffer->auto_save_modified = MODIFF; XFASTINT (current_buffer->save_length) = Z - BEG;#ifdef CLASH_DETECTION if (!NULL (current_buffer->filename)) unlock_file (current_buffer->filename); unlock_file (filename);#endif /* CLASH_DETECTION */ current_buffer->filename = filename; /* If visiting nonexistent file, return nil. */ if (st.st_mtime == -1) report_file_error ("Opening input file", Fcons (filename, Qnil)); } UNGCPRO; return Fcons (filename, Fcons (make_number (inserted), Qnil));}DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 5, "r\nFWrite region to file: ", "Write current region into specified file.\n\When called from a program, takes three arguments:\n\START, END and FILENAME. START and END are buffer positions.\n\Optional fourth argument APPEND if non-nil means\n\ append to existing file contents (if any).\n\Optional fifth argument VISIT if t means\n\ set last-save-file-modtime of buffer to this file's modtime\n\ and mark buffer not modified.\n\If VISIT is neither t nor nil, it means do not print\n\ the \"Wrote file\" message.") (start, end, filename, append, visit) Lisp_Object start, end, filename, append, visit;{ register int desc; int failure; int save_errno; unsigned char *fn; struct stat st; int tem; int count = specpdl_ptr - specpdl;#ifdef VMS unsigned char *fname = 0; /* If non-0, original filename (must rename) */#endif /* VMS */ /* Special kludge to simplify auto-saving */ if (NULL (start)) { XFASTINT (start) = BEG; XFASTINT (end) = Z; } else validate_region (&start, &end); filename = Fexpand_file_name (filename, Qnil); fn = XSTRING (filename)->data;#ifdef CLASH_DETECTION if (!auto_saving) lock_file (filename);#endif /* CLASH_DETECTION */ desc = -1; if (!NULL (append)) desc = open (fn, O_WRONLY); if (desc < 0)#ifdef VMS if (auto_saving) /* Overwrite any previous version of autosave file */ { vms_truncate (fn); /* if fn exists, truncate to zero length */ desc = open (fn, O_RDWR); if (desc < 0) desc = creat_copy_attrs (XTYPE (current_buffer->filename) == Lisp_String ? XSTRING (current_buffer->filename)->data : 0, fn); } else /* Write to temporary name and rename if no errors */ { Lisp_Object temp_name; temp_name = Ffile_name_directory (filename); if (!NULL (temp_name)) { temp_name = Fmake_temp_name (concat2 (temp_name, build_string ("$$SAVE$$"))); fname = XSTRING (filename)->data; fn = XSTRING (temp_name)->data; desc = creat_copy_attrs (fname, fn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -