From a6cdfdfff38584cadec40b6d6f2df2651e0b34b8 Mon Sep 17 00:00:00 2001 From: John Ata Date: Mon, 17 Oct 2016 14:47:02 -0400 Subject: Fix nftw when called with FTW_CHDIR and FTW_DEPTH Change directory back to the parent before processing the directory (after the contents have already been processed). Signed-off-by: John Ata --- libc/misc/ftw/ftw.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'libc/misc') diff --git a/libc/misc/ftw/ftw.c b/libc/misc/ftw/ftw.c index 05ea0fa66..c6241ed90 100644 --- a/libc/misc/ftw/ftw.c +++ b/libc/misc/ftw/ftw.c @@ -558,19 +558,14 @@ fail: --data->ftw.level; data->ftw.base = previous_base; - /* Finally, if we process depth-first report the directory. */ - if (result == 0 && (data->flags & FTW_DEPTH)) - result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw); - - if (old_dir - && (data->flags & FTW_CHDIR) + if ((data->flags & FTW_CHDIR) && (result == 0 || ((data->flags & FTW_ACTIONRETVAL) && (result != -1 && result != FTW_STOP)))) { /* Change back to the parent directory. */ int done = 0; - if (old_dir->stream != NULL) + if (old_dir && old_dir->stream != NULL) if (__fchdir (dirfd (old_dir->stream)) == 0) done = 1; @@ -587,6 +582,10 @@ fail: } } + /* Finally, if we process depth-first report the directory. */ + if (result == 0 && (data->flags & FTW_DEPTH)) + result = (*data->func) (data->dirbuf, st, FTW_DP, &data->ftw); + return result; } -- cgit v1.2.3