home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / editors / mntemacs.zoo / src / dired.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  12.8 KB  |  478 lines

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