home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / dired.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-13  |  14.2 KB  |  512 lines

  1. /* Lisp functions for making directory listings.
  2.    Copyright (C) 1985, 1986, 1992 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 2, 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. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24.  
  25. #include "config.h"
  26.  
  27. #ifdef SYSV_SYSTEM_DIR
  28.  
  29. #include <dirent.h>
  30. #define DIRENTRY struct dirent
  31. #define NAMLEN(p) strlen (p->d_name)
  32.  
  33. #else
  34.  
  35. #ifdef NONSYSTEM_DIR_LIBRARY
  36. #include "ndir.h"
  37. #else /* not NONSYSTEM_DIR_LIBRARY */
  38. #include <sys/dir.h>
  39. #endif /* not NONSYSTEM_DIR_LIBRARY */
  40.  
  41. #define DIRENTRY struct direct
  42. #define NAMLEN(p) p->d_namlen
  43.  
  44. extern DIR *opendir ();
  45. extern struct direct *readdir ();
  46.  
  47. #endif
  48.  
  49. #include "lisp.h"
  50. #include "buffer.h"
  51. #include "commands.h"
  52.  
  53. #include "regex.h"
  54.  
  55. #define min(a, b) ((a) < (b) ? (a) : (b))
  56.  
  57. /* if system does not have symbolic links, it does not have lstat.
  58.    In that case, use ordinary stat instead.  */
  59.  
  60. #ifndef S_IFLNK
  61. #define lstat stat
  62. #endif
  63.  
  64. Lisp_Object Vcompletion_ignored_extensions;
  65.  
  66. Lisp_Object Qcompletion_ignore_case;
  67.  
  68. DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 5, 0,
  69.   "Return a list of names of files in DIRECTORY.\n\
  70. There are four optional arguments:\n\
  71. If FULL is non-nil, absolute pathnames of the files are returned.\n\
  72. If MATCH is non-nil, only pathnames containing that regexp are returned.\n\
  73. If NOSORT is non-nil, the list is not sorted--its order is unpredictable.\n\
  74.  NOSORT is useful if you plan to sort the result yourself.\n\
  75. If FILES-ONLY is the symbol t, then only the \"files\" in the directory\n\
  76.  will be returned; subdirectories will be excluded.  If FILES-ONLY is not\n\
  77.  nil and not t, then only the subdirectories will be returned.  Otherwise,\n\
  78.  if FILES-ONLY is nil (the default) then both files and subdirectories will\n\
  79.  be returned.")
  80.   (dirname, full, match, nosort, files_only)
  81.      Lisp_Object dirname, full, match, nosort, files_only;
  82. {
  83.   DIR *d;
  84.   char slashfilename[MAXNAMLEN+2];
  85.   char *filename = slashfilename;
  86.   int length;
  87.   Lisp_Object list, name, tail_cons;
  88.   Lisp_Object name_as_dir;
  89.   struct stat st;
  90.   char statbuf [MAXNAMLEN+2];
  91.   char *statbuf_tail;
  92.  
  93.   /* In search.c */
  94.   extern struct re_pattern_buffer searchbuf;
  95.  
  96.   if (!NILP (match))
  97.     {
  98.       CHECK_STRING (match, 3);
  99.       /* Compile it now so we don't get an error after opendir */
  100. #ifdef VMS
  101.       compile_pattern (match, &searchbuf,
  102.                buffer_defaults.downcase_table->contents);
  103. #else
  104.       compile_pattern (match, &searchbuf, 0);
  105. #endif
  106.     }
  107.  
  108.   dirname = Fexpand_file_name (dirname, Qnil);
  109.   if (!(d = opendir ((char *)
  110.              XSTRING (Fdirectory_file_name (dirname))->data)))
  111.     report_file_error ("Opening directory", Fcons (dirname, Qnil));
  112.  
  113.   name_as_dir = Ffile_name_as_directory (dirname);
  114.   memcpy (statbuf, ((char *) XSTRING (name_as_dir)->data),
  115.       XSTRING (name_as_dir)->size);
  116.   statbuf_tail = statbuf + XSTRING (name_as_dir)->size;
  117.  
  118.   list = tail_cons = Qnil;
  119.   length = XSTRING (dirname)->size;
  120. #ifndef VMS
  121.   if (length == 0   ||  XSTRING (dirname)->data[length - 1] != '/')
  122.     *filename++ = '/';
  123. #endif /* VMS */
  124.  
  125.   /* Loop reading blocks */
  126.   while (1)
  127.     {
  128.       DIRENTRY *dp = readdir (d);
  129.       int len;
  130.  
  131.       if (!dp) break;
  132.       len = NAMLEN (dp);
  133.       if (dp->d_ino)
  134.     {
  135.       strncpy (filename, dp->d_name, len);
  136.       filename[len] = 0;
  137.       if (NILP (match) ||
  138.           (0 <= re_search (&searchbuf, filename, len, 0, len, 0)))
  139.         {
  140.           if (!NILP (files_only))
  141.         {
  142.           int dir_p;
  143.  
  144.           memcpy (statbuf_tail, filename, len);
  145.           statbuf_tail [len] = 0;
  146.  
  147.           if (stat (statbuf, &st) < 0)
  148.             dir_p = 0;
  149.           else
  150.             dir_p = ((st.st_mode & S_IFMT) == S_IFDIR);
  151.  
  152.           if ((files_only == Qt) && dir_p)
  153.             continue;
  154.           else if ((files_only != Qt) && !dir_p)
  155.             continue;
  156.         }
  157.  
  158.           if (!NILP (full))
  159.         name = concat2 (dirname, build_string (slashfilename));
  160.           else
  161.         name = build_string (filename);
  162.  
  163.           if (NILP (tail_cons))
  164.         list = tail_cons = Fcons (name, Qnil);
  165.           else
  166.         {
  167.           XCONS (tail_cons)->cdr = Fcons (name, Qnil);
  168.           tail_cons = XCONS (tail_cons)->cdr;
  169.         }
  170.         }
  171.     }
  172.     }
  173.   closedir (d);
  174.   if (!NILP (nosort))
  175.     return list;
  176.   return Fsort (Fnreverse (list), Qstring_lessp);
  177. }
  178.  
  179. static Lisp_Object file_name_completion ();
  180.  
  181. DEFUN ("file-name-completion", Ffile_name_completion, Sfile_name_completion,
  182.   2, 2, 0,
  183.   "Complete file name FILE in directory DIR.\n\
  184. Returns the longest string\n\
  185. common to all filenames in DIR that start with FILE.\n\
  186. If there is only one and FILE matches it exactly, returns t.\n\
  187. Returns nil if DIR contains no name starting with FILE.")
  188.   (file, dirname)
  189.      Lisp_Object file, dirname;
  190. {
  191.   /* Don't waste time trying to complete a null string.
  192.      Besides, this case happens when user is being asked for
  193.      a directory name and has supplied one ending in a /.
  194.      We would not want to add anything in that case
  195.      even if there are some unique characters in that directory.  */
  196.   if (STRINGP (file) && XSTRING (file)->size == 0)
  197.     return file;
  198.   return file_name_completion (file, dirname, 0, 0);
  199. }
  200.  
  201. DEFUN ("file-name-all-completions", Ffile_name_all_completions,
  202.   Sfile_name_all_completions, 2, 2, 0,
  203.   "Return a list of all completions of file name FILE in directory DIR.\n\
  204. These are all file names in directory DIR which begin with FILE.")
  205.   (file, dirname)
  206.      Lisp_Object file, dirname;
  207. {
  208.   return file_name_completion (file, dirname, 1, 0);
  209. }
  210.  
  211. #ifdef VMS
  212.  
  213. DEFUN ("file-name-all-versions", Ffile_name_all_versions,
  214.   Sfile_name_all_versions, 2, 2, 0,
  215.   "Return a list of all versions of file name FILE in directory DIR.")
  216.   (file, dirname)
  217.      Lisp_Object file, dirname;
  218. {
  219.   return file_name_completion (file, dirname, 1, 1);
  220. }
  221.  
  222. #endif /* VMS */
  223.  
  224. static int file_name_completion_stat (Lisp_Object, DIRENTRY *, struct stat *);
  225.  
  226. static Lisp_Object
  227. file_name_completion (file, dirname, all_flag, ver_flag)
  228.      Lisp_Object file, dirname;
  229.      int all_flag, ver_flag;
  230. {
  231.   DIR *d;
  232.   int bestmatchsize, skip;
  233.   register int compare, matchsize;
  234.   unsigned char *p1, *p2;
  235.   int matchcount = 0;
  236.   Lisp_Object bestmatch, tem, elt, name;
  237.   struct stat st;
  238.   int directoryp;
  239.   int passcount;
  240.   int count = specpdl_ptr - specpdl;
  241. #ifdef VMS
  242.   extern DIRENTRY * readdirver ();
  243.  
  244.   DIRENTRY *((* readfunc) ());
  245.  
  246.   /* Filename completion on VMS ignores case, since VMS filesys does.  */
  247.   specbind (Qcompletion_ignore_case, Qt);
  248.  
  249.   readfunc = readdir;
  250.   if (ver_flag)
  251.     readfunc = readdirver;
  252.   file = Fupcase (file);
  253. #endif /* VMS */
  254.  
  255.   dirname = Fexpand_file_name (dirname, Qnil);
  256.   bestmatch = Qnil;
  257.  
  258.   /* With passcount = 0, ignore files that end in an ignored extension.
  259.      If nothing found then try again with passcount = 1, don't ignore them.
  260.      If looking for all completions, start with passcount = 1,
  261.      so always take even the ignored ones.
  262.  
  263.      ** It would not actually be helpful to the user to ignore any possible
  264.      completions when making a list of them.**  */
  265.  
  266.   for (passcount = !!all_flag; NILP (bestmatch) && passcount < 2; passcount++)
  267.     {
  268.       if (!(d = opendir ((char *)
  269.              XSTRING (Fdirectory_file_name (dirname))->data)))
  270.     report_file_error ("Opening directory", Fcons (dirname, Qnil));
  271.  
  272.       /* Loop reading blocks */
  273.       /* (att3b compiler bug requires do a null comparison this way) */
  274.       while (1)
  275.     {
  276.       DIRENTRY *dp;
  277.       int len;
  278.  
  279. #ifdef VMS
  280.       dp = (*readfunc) (d);
  281. #else
  282.       dp = readdir (d);
  283. #endif
  284.       if (!dp) break;
  285.  
  286.       len = NAMLEN (dp);
  287.  
  288.       if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
  289.         goto quit;
  290.       if (!dp->d_ino
  291.           || len < XSTRING (file)->size
  292.           || 0 <= scmp (dp->d_name,
  293.                 (char *) XSTRING (file)->data,
  294.                 XSTRING (file)->size))
  295.         continue;
  296.  
  297.           if (file_name_completion_stat (dirname, dp, &st) < 0)
  298.             continue;
  299.  
  300.           directoryp = ((st.st_mode & S_IFMT) == S_IFDIR);
  301.       tem = Qnil;
  302.           if (!directoryp)
  303.             {
  304.           /* Compare extensions-to-be-ignored against end of this file name */
  305.           /* if name is not an exact match against specified string */
  306.           if (!passcount && len > XSTRING (file)->size)
  307.         /* and exit this for loop if a match is found */
  308.         for (tem = Vcompletion_ignored_extensions;
  309.              CONSP (tem); tem = XCONS (tem)->cdr)
  310.           {
  311.             elt = XCONS (tem)->car;
  312.             if (!STRINGP (elt)) continue;
  313.             skip = len - XSTRING (elt)->size;
  314.             if (skip < 0) continue;
  315.  
  316.             if (0 <= scmp (dp->d_name + skip,
  317.                    (char *) XSTRING (elt)->data,
  318.                    XSTRING (elt)->size))
  319.               continue;
  320.             break;
  321.           }
  322.         }
  323.  
  324.       /* Unless an ignored-extensions match was found,
  325.              process this name as a completion */
  326.       if (passcount || !CONSP (tem))
  327.         {
  328.           /* Update computation of how much all possible completions match */
  329.  
  330.           matchcount++;
  331.  
  332.           if (all_flag || NILP (bestmatch))
  333.         {
  334.           /* This is a possible completion */
  335.           if (directoryp)
  336.             {
  337.               /* This completion is a directory; make it end with '/' */
  338.               name = Ffile_name_as_directory (make_string (dp->d_name, len));
  339.             }
  340.           else
  341.             name = make_string (dp->d_name, len);
  342.           if (all_flag)
  343.             {
  344.               bestmatch = Fcons (name, bestmatch);
  345.             }
  346.           else
  347.             {
  348.               bestmatch = name;
  349.               bestmatchsize = XSTRING (name)->size;
  350.             }
  351.         }
  352.           else
  353.         {
  354.           compare = min (bestmatchsize, len);
  355.           p1 = XSTRING (bestmatch)->data;
  356.           p2 = (unsigned char *) dp->d_name;
  357.           for (matchsize = 0; matchsize < compare; matchsize++)
  358.             if (p1[matchsize] != p2[matchsize]) break;
  359.           /* If this dirname all matches,
  360.              see if implicit following slash does too.  */
  361.           if (directoryp  &&
  362.               compare == matchsize &&
  363.               bestmatchsize > matchsize &&
  364.               p1[matchsize] == '/')
  365.             matchsize++;
  366.           bestmatchsize = min (matchsize, bestmatchsize);
  367.         }
  368.         }
  369.     }
  370.       closedir (d);
  371.     }
  372.  
  373.   unbind_to (count, Qnil);
  374.  
  375.   if (all_flag || NILP (bestmatch))
  376.     return bestmatch;
  377.   if (matchcount == 1 && bestmatchsize == XSTRING (file)->size)
  378.     return Qt;
  379.   return Fsubstring (bestmatch, make_number (0), make_number (bestmatchsize));
  380.  quit:
  381.   if (d) closedir (d);
  382.   Vquit_flag = Qnil;
  383.   return Fsignal (Qquit, Qnil);
  384. }
  385.  
  386. static int
  387. file_name_completion_stat (dirname, dp, st_addr)
  388.      Lisp_Object dirname;
  389.      DIRENTRY *dp;
  390.      struct stat *st_addr;
  391. {
  392.   int len = NAMLEN (dp);
  393.   int pos = XSTRING (dirname)->size;
  394.   char *fullname = (char *) alloca (len + pos + 2);
  395.  
  396.   memcpy (fullname, XSTRING (dirname)->data, pos);
  397. #ifndef VMS
  398.   if (fullname[pos - 1] != '/')
  399.     fullname[pos++] = '/';
  400. #endif
  401.  
  402.   memcpy (fullname + pos, dp->d_name, len);
  403.   fullname[pos + len] = 0;
  404.  
  405.   return stat (fullname, st_addr);
  406. }
  407.  
  408. static Lisp_Object
  409. make_time (time)
  410.      int time;
  411. {
  412.   return Fcons (make_number (time >> 16),
  413.         Fcons (make_number (time & 0177777), Qnil));
  414. }
  415.  
  416.  
  417. extern void filemodestring (struct stat *, char *);
  418.  
  419. DEFUN ("file-attributes", Ffile_attributes, Sfile_attributes, 1, 1, 0,
  420.   "Return a list of attributes of file FILENAME.\n\
  421. Value is nil if specified file cannot be opened.\n\
  422. Otherwise, list elements are:\n\
  423.  0. t for directory, string (name linked to) for symbolic link, or nil.\n\
  424.  1. Number of links to file.\n\
  425.  2. File uid.\n\
  426.  3. File gid.\n\
  427.  4. Last access time, as a list of two integers.\n\
  428.   First integer has high-order 16 bits of time, second has low 16 bits.\n\
  429.  5. Last modification time, likewise.\n\
  430.  6. Last status change time, likewise.\n\
  431.  7. Size in bytes.\n\
  432.  8. File modes, as a string of ten letters or dashes as in ls -l.\n\
  433.  9. t iff file's gid would change if file were deleted and recreated.\n\
  434. 10. inode number.\n\
  435. \n\
  436. If file does not exists, returns nil.")
  437.   (filename)
  438.      Lisp_Object filename;
  439. {
  440.   Lisp_Object values[11];
  441.   Lisp_Object dirname;
  442.   struct stat s;
  443.   struct stat sdir;
  444.   char modes[10];
  445.  
  446.   filename = Fexpand_file_name (filename, Qnil);
  447.   if (lstat ((char *)XSTRING (filename)->data, &s) < 0)
  448.     return Qnil;
  449.  
  450.   switch (s.st_mode & S_IFMT)
  451.     {
  452.     default:
  453.       values[0] = Qnil; break;
  454.     case S_IFDIR:
  455.       values[0] = Qt; break;
  456. #ifdef S_IFLNK
  457.     case S_IFLNK:
  458.       values[0] = Ffile_symlink_p (filename); break;
  459. #endif
  460.     }
  461.   values[1] = make_number (s.st_nlink);
  462.   values[2] = make_number (s.st_uid);
  463.   values[3] = make_number (s.st_gid);
  464.   values[4] = make_time (s.st_atime);
  465.   values[5] = make_time (s.st_mtime);
  466.   values[6] = make_time (s.st_ctime);
  467.   /* perhaps we should set this to most-positive-fixnum if it is too large? */
  468.   values[7] = make_number (s.st_size);
  469.   filemodestring (&s, modes);
  470.   values[8] = make_string (modes, 10);
  471. #ifdef BSD4_3 /* Gross kludge to avoid lack of "#if defined(...)" in VMS */
  472. #define BSD4_2 /* A new meaning to the term `backwards compatability' */
  473. #endif
  474. #ifdef BSD4_2            /* file gid will be dir gid */
  475.   dirname = Ffile_name_directory (filename);
  476.   if (dirname != Qnil && stat ((char *)XSTRING (dirname)->data, &sdir) == 0)
  477.     values[9] = (sdir.st_gid != s.st_gid) ? Qt : Qnil;
  478.   else                    /* if we can't tell, assume worst */
  479.     values[9] = Qt;
  480. #else                    /* file gid will be egid */
  481.   values[9] = (s.st_gid != getegid ()) ? Qt : Qnil;
  482. #endif    /* BSD4_2 (or BSD4_3) */
  483. #ifdef BSD4_3
  484. #undef BSD4_2 /* ok, you can look again without throwing up */
  485. #endif
  486.   values[10] = make_number (s.st_ino);
  487.   return Flist (11, values);
  488. }
  489.  
  490. void
  491. syms_of_dired ()
  492. {
  493.   defsubr (&Sdirectory_files);
  494.   defsubr (&Sfile_name_completion);
  495. #ifdef VMS
  496.   defsubr (&Sfile_name_all_versions);
  497. #endif /* VMS */
  498.   defsubr (&Sfile_name_all_completions);
  499.   defsubr (&Sfile_attributes);
  500.  
  501. #ifdef VMS
  502.   Qcompletion_ignore_case = intern ("completion-ignore-case");
  503.   staticpro (&Qcompletion_ignore_case);
  504. #endif /* VMS */
  505.  
  506.   DEFVAR_LISP ("completion-ignored-extensions", &Vcompletion_ignored_extensions,
  507.     "*Completion ignores filenames ending in any string in this list.\n\
  508. This variable does not affect lists of possible completions,\n\
  509. but does affect the commands that actually do completions.");
  510.   Vcompletion_ignored_extensions = Qnil;
  511. }
  512.