bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Explain how to access data returned from dir_readdir


From: Andrew Eggenberger
Subject: [PATCH] Explain how to access data returned from dir_readdir
Date: Thu, 22 Apr 2021 22:13:59 -0500

I wrote this patch for the website after struggling with dir_readdir. Hopefully this will help the next person who needs it (probably me in a few months).

Andrew Eggenberger

---
 hurd/interface/fs/19.mdwn | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/hurd/interface/fs/19.mdwn b/hurd/interface/fs/19.mdwn
index 86625d44..9ef9f8a4 100644
--- a/hurd/interface/fs/19.mdwn
+++ b/hurd/interface/fs/19.mdwn
@@ -27,3 +27,29 @@ returning an array of struct directs in `data`.  The number of entries
 successfully read is returned in `amount`.  If `entry` is bigger than the index
 of the last entry, then 0 is returned in `amount`.  If `bufsize` is nonzero,
 never return more than `bufsize` bytes of data regardless.
+
+Although the value returned in `data` is described as an "array of struct
+directs" in fs.defs, typical array access (direct[4], etc.) will only work to
+access the individual dirents in `data` if they all happen to be the size of
+struct direct (an alias of struct dirent in glibc). This is unlikely because
+the `d_name` member of struct dirent is a flexible `char` array to account for
+the variability of filename lengths. One way to access each member in turn is
+demonstrated below.
+
+  data_t dirents;
+  struct dirent* d;
+  mach_msg_type_number_t dataCnt = 0;
+  int amt = 0, i;
+  
+  err = dir_readdir(dirport, &dirents, &dataCnt, 0, -1, 0, &amt);
+  if (err != KERN_SUCCESS)
+    error(1, 0, "dir_readdir failed.\n");
+
+  while (i++ < amt){
+    d = (struct dirent*)dirents;
+
+    /** ... do things with d ... **/
+
+    dirents += d->d_reclen;
+
+  }
-- 
2.31.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]