<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hello,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I am trying to use fts to traverse through the current directory without recursion and i either get a segfault of the following error when I call fts_children()</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
fts_children: Undefined error: 0.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Here's my code snippet below:<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
main</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
{</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
&nbsp;&nbsp;&nbsp; if(argc == 1)</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PrintFileNames(&quot;.&quot;);</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
}<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<span>void PrintFileNames(char* dir )<br>
</span>
<div>{<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp;FTS *filetree = NULL;<br>
</div>
<div>&nbsp; &nbsp;FTSENT *file &nbsp;= NULL;<br>
</div>
<div>&nbsp; &nbsp;FTSENT *child = NULL;<br>
</div>
<div>&nbsp; &nbsp;char *path[] = {dir, NULL};<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp;filetree = fts_open(path, FTS_PHYSICAL, &amp;compare);<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp;if(filetree == NULL)<br>
</div>
<div>&nbsp; &nbsp; &nbsp; ErrorMessage(&quot;fts_open failed&quot;);<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp;else<br>
</div>
<div>&nbsp; &nbsp;{<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; while((file = fts_read(filetree)) != NULL)<br>
</div>
<div>&nbsp; &nbsp; &nbsp; {<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;child = fts_children(filetree, 0);<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(child == NULL)<br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ErrorMessage(&quot;fts_children&quot;);<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;%s\n&quot;,child-&gt;fts_name);<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; }<br>
</div>
<div>&nbsp; &nbsp;}<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp;if(errno != 0)<br>
</div>
<div>&nbsp; &nbsp; &nbsp; ErrorMessage(&quot;fts_read failed&quot;);<br>
</div>
<div><br>
</div>
<div>&nbsp; &nbsp;if( fts_close(filetree) &lt; 0)<br>
</div>
<div>&nbsp; &nbsp; &nbsp; ErrorMessage(&quot;fts_close&quot;);<br>
</div>
<span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
}</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Can anyone shed some light on what's going on?</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks.<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> cs631apue-bounces@lists.stevens.edu &lt;cs631apue-bounces@lists.stevens.edu&gt; on behalf of Jan Schaumann &lt;jschauma@stevens.edu&gt;<br>
<b>Sent:</b> Sunday, October 6, 2019 4:35 PM<br>
<b>To:</b> cs631apue@lists.stevens.edu &lt;cs631apue@lists.stevens.edu&gt;<br>
<b>Subject:</b> Re: [cs631apue] About FTS</font>
<div>&nbsp;</div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Yuxi Wang &lt;ywang286@stevens.edu&gt; wrote:<br>
&nbsp;<br>
&gt; 1.To use these fields to reference any files represented by<br>
&gt; other FTSENT structures will require that the path buffer be modified<br>
&gt; using the information contained in that FTSENT structure's fts_pathlen<br>
&gt; field.&nbsp; Any such modifications should be undone before further calls to<br>
&gt; fts_read() are attempted.&nbsp; The fts_name field is always NUL-terminated.<br>
<br>
I'm honestly struggling to come up with a useful<br>
example, but the point is that you cannot manipulate<br>
the buffers (e.g., via strcat(3) etc.) in e.g., your<br>
comparison function, nor can you rely on the buffer to<br>
be NULL terminated for a previously returned element.<br>
<br>
That means that you can't keep a pointer to the<br>
element returned and subsequently call e.g. strlen(3)<br>
on _that_ element's fts_path field.<br>
<br>
Let's say you want to write a program that will print<br>
the path within the parent directory:<br>
<br>
$ pwd<br>
/tmp<br>
$ ls<br>
file<br>
$ printParentPath file<br>
tmp/file<br>
$ printParentPath /var/tmp/subdir/file<br>
subdir/file<br>
<br>
This might require you to keep track of the parent<br>
struct.&nbsp; You might try to<br>
<br>
- get strlen of current entry accpath, n<br>
- get strlen of parent entry accpath, m<br>
- malloc a buffer of size n &#43; 1 &#43; m<br>
- sprintf parent, '/', current to buffer<br>
<br>
But the parent entry's accpath may not be<br>
NULL-terminated, thus strlen(3) giving you a bogus<br>
result.<br>
<br>
&gt; 2.What does the fts_accpath represent for? I tracked<br>
&gt; the FTS structure and found it always equal<br>
&gt; fts_name.<br>
<br>
fts_accpath will be different depending on whether<br>
you've specified FTS_NOCHDIR or not.&nbsp; With<br>
FTS_NOCHDIR, fts_accpath will look like fts_path,<br>
without, fts_accpath will look like fts_name.<br>
<br>
I haven't verified, but I could imagine that results<br>
will also differ based on whether you're following<br>
symlinks and the hierarchy includes symlinks to<br>
directories whereby directory traversal may encounter<br>
the same subdir via multiple paths.<br>
<br>
-Jan<br>
_______________________________________________<br>
cs631apue mailing list<br>
cs631apue@lists.stevens.edu<br>
<a href="https://lists.stevens.edu/mailman/listinfo/cs631apue">https://lists.stevens.edu/mailman/listinfo/cs631apue</a><br>
</div>
</span></font></div>
</body>
</html>