blob: d298e3e20d2e15850b46152ff2e87070faa5245d [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001SYNOPSIS
2 #include <files.h>
3
Zesstrad59c3892019-11-28 20:53:39 +01004 mixed * get_dir(string str)
5 mixed * get_dir(string str, int mask)
MG Mud User88f12472016-06-24 23:31:02 +02006
Zesstra715ec202025-07-09 22:18:31 +02007DESCRIPTION
8 This function takes a path as argument and returns an array of file
9 names and attributes in that directory.
MG Mud User88f12472016-06-24 23:31:02 +020010
Zesstra715ec202025-07-09 22:18:31 +020011 Returns 0 if the directory to search in does not exist.
MG Mud User88f12472016-06-24 23:31:02 +020012
Zesstra715ec202025-07-09 22:18:31 +020013 The filename part of the path may contain '*' or '?' as wildcards:
14 every '*' matches an arbitrary amount of characters (or just itself),
15 a '?' matches any character. Thus get_dir("/path/*") would return an
16 alphabetically sorted array of all files in directory "/path", or
17 just ({ "/path/*" }) if this file happens to exist.
MG Mud User88f12472016-06-24 23:31:02 +020018
Zesstra715ec202025-07-09 22:18:31 +020019 To query the content of a directory, use the directory name with a
20 trailing '/' or '/.', for example get_dir("/path/."). Use the
21 directory name as it is to get information about the directory itself.
MG Mud User88f12472016-06-24 23:31:02 +020022
Zesstra715ec202025-07-09 22:18:31 +020023 The optional second argument mask can be used to get
24 information about the specified files.
MG Mud User88f12472016-06-24 23:31:02 +020025
Zesstra715ec202025-07-09 22:18:31 +020026 GETDIR_EMPTY (0x00) get_dir returns an empty array (not very
27 useful).
28 GETDIR_NAMES (0x01) put the alphabetically sorted file names into
29 the returned array.
30 GETDIR_SIZES (0x02) put the file sizes unsorted into the returned
31 array. directories have size FSIZE_DIR (-2).
32 GETDIR_DATES (0x04) put the file modification dates (in seconds
33 since 01/01/1970) unsorted into the
34 returned array.
35 GETDIR_ACCESS (0x40) put the file access dates unsorted into
36 the returned array.
37 GETDIR_MODES (0x80) put the unix file modes unsorted into
38 the returned array.
MG Mud User88f12472016-06-24 23:31:02 +020039
Zesstra715ec202025-07-09 22:18:31 +020040 GETDIR_ALL (0xDF) Return all.
MG Mud User88f12472016-06-24 23:31:02 +020041
Zesstra715ec202025-07-09 22:18:31 +020042 GETDIR_PATH (0x10) if this mask bit is set, the filenames with
43 the full path will be returned
44 (GETDIR_NAMES is implied).
45 GETDIR_UNSORTED (0x20) if this mask bit is set, the result of will
46 _not_ be sorted.
MG Mud User88f12472016-06-24 23:31:02 +020047
Zesstra715ec202025-07-09 22:18:31 +020048 Note: You should use GETDIR_NAMES|GETDIR_UNSORTED to get the entries
49 in the same order as with GETDIR_SIZES and GETDIR_DATES.
MG Mud User88f12472016-06-24 23:31:02 +020050
Zesstra715ec202025-07-09 22:18:31 +020051 The values of mask can be added together.
MG Mud User88f12472016-06-24 23:31:02 +020052
Zesstra715ec202025-07-09 22:18:31 +020053NOTES
54 The path argument to this efun is processed by valid_read() in the
55 mudlib master before being used. The mudlib may normalize this path
56 (e.g. strip leading or trailing "/") and this may lead to expected
57 results (e.g. get_dir("/dir/", ...) not returning the contents
58 of /dir/).
59 COMPAT mode: GETDIR_PATH will return the paths without leading /.
Zesstrad59c3892019-11-28 20:53:39 +010060
Zesstra715ec202025-07-09 22:18:31 +020061EXAMPLES
62 function returns
MG Mud User88f12472016-06-24 23:31:02 +020063 -------------------------------------------------------------------
Zesstra715ec202025-07-09 22:18:31 +020064 get_dir("/obj/.") all files contained in directory /obj.
65 get_dir("/obj/") the same as get_dir("/obj/")
MG Mud User88f12472016-06-24 23:31:02 +020066
Zesstra715ec202025-07-09 22:18:31 +020067 get_dir("/obj/sword.c") ({ "sword.c" }) if /obj/sword.c
68 exists (it may be a file or a
69 directory), otherwise ({ }) if
70 /obj is a directory,
71 otherwise 0.
MG Mud User88f12472016-06-24 23:31:02 +020072
Zesstra715ec202025-07-09 22:18:31 +020073 get_dir("/obj/*") ({ "*" }) if * exists.
74 otherwise and normally an
75 alphabetically sorted array with all
76 names of files and directories in
77 /obj if /obj is a directory,
78 otherwise 0.
MG Mud User88f12472016-06-24 23:31:02 +020079
Zesstra715ec202025-07-09 22:18:31 +020080 get_dir("/obj/sword.c", GETDIR_SIZES) ({ <size of /obj/sword.c> })
81 if that file exists.
82 get_dir("/obj/.", GETDIR_NAMES) the same as get_dir("/obj/.").
83 get_dir("/obj/.", GETDIR_SIZES) an array with the sizes of the files
84 in /obj, not sorted by names.
85 get_dir("/obj/.", GETDIR_NAMES|GETDIR_SIZES|GETDIR_DATES) or shorter
86 get_dir("/obj/.", GETDIR_ALL) an one-dimensional array that
87 contains for each file in /obj its
88 name, its size and its modification
89 date, sorted by names, for example
Zesstrad59c3892019-11-28 20:53:39 +010090 ({
Zesstra715ec202025-07-09 22:18:31 +020091 "axe.c" , 927, 994539583,
92 "sword.c", 1283, 998153903,
Zesstrad59c3892019-11-28 20:53:39 +010093 }).
MG Mud User88f12472016-06-24 23:31:02 +020094
Zesstra715ec202025-07-09 22:18:31 +020095 get_dir("/obj/sword.c", GETDIR_NAMES|GETDIR_PATH)
96 ({ "/obj/sword.c" }) if applicable.
97 get_dir("/obj/sword.c", GETDIR_PATH) Short form of the same query.
98
MG Mud User88f12472016-06-24 23:31:02 +020099
100 transpose_array(({ get_dir(str, GETDIR_NAMES|GETDIR_UNSORTED)
101 , get_dir(str, GETDIR_SIZES)
102 , get_dir(str, GETDIR_DATES) }));
Zesstra715ec202025-07-09 22:18:31 +0200103 This returns an array of arrays, with filename, size and
104 filetime as elements, not sorted by names, for example
Zesstrad59c3892019-11-28 20:53:39 +0100105 ({
Zesstra715ec202025-07-09 22:18:31 +0200106 ({ "sword.c", 1283, 998153903 }),
107 ({ "axe.c" , 927, 994539583 }),
Zesstrad59c3892019-11-28 20:53:39 +0100108 }).
109
Zesstrad59c3892019-11-28 20:53:39 +0100110
Zesstra715ec202025-07-09 22:18:31 +0200111HISTORY
112 LDMud 3.2.9 added GETDIR_PATH.
113 LDMud 3.2.11/3.3.648 added GETDIR_ACCESS and GETDIR_MODES.
114
115SEE ALSO
Zesstrad59c3892019-11-28 20:53:39 +0100116 mkdir(E), rmdir(E), file_size(E), write_file(E), write_bytes(E),
117 read_file(E), read_bytes(E)