--- /home/arthur/dired.c 2020-10-13 13:06:07.517648492 +0200 +++ src/dired.c 2020-10-14 16:18:03.750214680 +0200 @@ -20,6 +20,7 @@ #include +#include #include #ifdef HAVE_PWD_H @@ -165,7 +166,7 @@ Lisp_Object directory_files_internal (Lisp_Object directory, Lisp_Object full, Lisp_Object match, Lisp_Object nosort, bool attrs, - Lisp_Object id_format) + Lisp_Object id_format, Lisp_Object count_to_return) { if (!NILP (match)) CHECK_STRING (match); @@ -224,6 +225,18 @@ case_table = BVAR (&buffer_defaults, case_canon_table); #endif + bool only_length = false; + ptrdiff_t ind = 0, last = 0; + if(!NILP(count_to_return) && FIXNATP(count_to_return)) + { + last = XFIXNAT(count_to_return); + + if(!last){ + last = 1; + only_length = true; + } + } + /* Read directory entries and accumulate them into LIST. */ Lisp_Object list = Qnil; for (struct dirent *dp; (dp = read_dirent (d, directory)); ) @@ -267,7 +280,22 @@ else finalname = name; - list = Fcons (attrs ? Fcons (finalname, fileattrs) : finalname, list); + if (only_length) + { + ind++; + } + else + { + if (!NILP(count_to_return) && FIXNATP(count_to_return)) + { + if (ind == last) + break; + ind ++; + } + + list = Fcons (attrs ? Fcons (finalname, fileattrs) : + finalname, list); + } } closedir (d); @@ -279,16 +307,19 @@ /* Discard the unwind protect. */ specpdl_ptr = specpdl + count; + if (only_length) + return make_fixnum(ind); + if (NILP (nosort)) list = Fsort (Fnreverse (list), attrs ? Qfile_attributes_lessp : Qstring_lessp); - + (void) directory_volatile; return list; } -DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 4, 0, +DEFUN ("directory-files", Fdirectory_files, Sdirectory_files, 1, 5, 0, doc: /* Return a list of names of files in DIRECTORY. There are three optional arguments: If FULL is non-nil, return absolute file names. Otherwise return names @@ -296,9 +327,14 @@ If MATCH is non-nil, mention only file names that match the regexp MATCH. If NOSORT is non-nil, the list is not sorted--its order is unpredictable. Otherwise, the list returned is sorted with `string-lessp'. - NOSORT is useful if you plan to sort the result yourself. */) + NOSORT is useful if you plan to sort the result yourself. +If COUNT is non-nil, the function will return max (COUNT,length) + files, where length means number of files in directory. Order + in which files are returned is not guaranteed and is file system and + OS dependent. COUNT has to be an integral number in interval + [1,COUNT]. */) (Lisp_Object directory, Lisp_Object full, Lisp_Object match, - Lisp_Object nosort) + Lisp_Object nosort, Lisp_Object count) { directory = Fexpand_file_name (directory, Qnil); @@ -306,14 +342,15 @@ call the corresponding file name handler. */ Lisp_Object handler = Ffind_file_name_handler (directory, Qdirectory_files); if (!NILP (handler)) - return call5 (handler, Qdirectory_files, directory, - full, match, nosort); + return call6 (handler, Qdirectory_files, directory, + full, match, nosort, count); - return directory_files_internal (directory, full, match, nosort, false, Qnil); + return directory_files_internal (directory, full, match, nosort, + false, Qnil, count); } DEFUN ("directory-files-and-attributes", Fdirectory_files_and_attributes, - Sdirectory_files_and_attributes, 1, 5, 0, + Sdirectory_files_and_attributes, 1, 6, 0, doc: /* Return a list of names of files and their attributes in DIRECTORY. Value is a list of the form: @@ -331,9 +368,14 @@ ID-FORMAT specifies the preferred format of attributes uid and gid, see `file-attributes' for further documentation. On MS-Windows, performance depends on `w32-get-true-file-attributes', -which see. */) - (Lisp_Object directory, Lisp_Object full, Lisp_Object match, - Lisp_Object nosort, Lisp_Object id_format) +which see. +If COUNT is non-nil, the function will return max (COUNT,length) + files, where length means number of files in directory. Order + in which files are returned is not guaranteed and is file system and + OS dependent. COUNT has to be an integral number in interval + [1,COUNT]. */) +(Lisp_Object directory, Lisp_Object full, Lisp_Object match, + Lisp_Object nosort, Lisp_Object id_format, Lisp_Object count) { directory = Fexpand_file_name (directory, Qnil); @@ -342,11 +384,11 @@ Lisp_Object handler = Ffind_file_name_handler (directory, Qdirectory_files_and_attributes); if (!NILP (handler)) - return call6 (handler, Qdirectory_files_and_attributes, - directory, full, match, nosort, id_format); + return call7 (handler, Qdirectory_files_and_attributes, + directory, full, match, nosort, id_format, count); return directory_files_internal (directory, full, match, nosort, - true, id_format); + true, id_format, count); } @@ -929,7 +971,7 @@ struct stat s; /* An array to hold the mode string generated by filemodestring, - including its terminating space and null byte. */ + including its terminating space and NUL byte. */ char modes[sizeof "-rwxr-xr-x "]; char *uname = NULL, *gname = NULL;