I'm using Linux (Ubuntu 12.04). I'm trying to know how 'find' command works. My question is, how can I run and test the C files for 'find' which I downloaded from the GNU findutils ?
Asked
Active
Viewed 1,296 times
2 Answers
0
treatDirectory(dir) {
open directory dir
for each entry in dir
if the entry is a match
print it
if the entry is a directory
call treatDirectory(entry)
}
call treatDirectory(dir) for each directory given as parameter.
-
1
-
Will it? In a “proper” language, we have RAII or something similar, automatically closing the directory object at the end of the function. But, yes, that is one of the many things left as an exercise. – Mar 14 '13 at 18:16
-
Closing a file descriptor when the associated object goes out of scope is horrible design. Sometimes the file descriptor needs to remain opened. The handle to a file descriptor is not the programming language object, it's an integer, and that never goes out of scope. By the way RAII as a term is mostly unique to C++ (the concept does generalize). – Gilles 'SO- stop being evil' Mar 14 '13 at 23:20
-
Keeping a file descriptor open when the program no longer has any reference to it and therefore cannot use it anymore for anything, including closing it, serves what purpose exactly? (I did explicitly say “or something similar” to not limit the scope to C++. Ruby has a very nice design for this with
File.open
taking a block, for example.) – Christopher Creutzig Mar 15 '13 at 09:39 -
You're confusing file descriptors with file handles. A file descriptor is represented by an integer, not by a reference, it doesn't follow any language scope. Having block-scoped file handles (like
with-open-file
in Lisp,File.open(…) do …
in Ruby,with open(…) …
in Python, etc.) is good. But sometimes you need dynamically-scoped file descriptors, and then the library must not close the file under your feet. Also, closing a file open for writing may fail, so the time of closing must be carefully controlled: you can't let an asynchronous GC close a file when it's open for writing. – Gilles 'SO- stop being evil' Mar 15 '13 at 17:12 -
Seriously? I wasn't talking about file descriptors in the first place, I was talking about an object holding it. You said “closing a file descriptor when the associated object goes out of scope is horrible design.” And this type of problem is exactly why I think asynchronous GC is less useful than real destructors with well-known lifetime. It's not just about memory and file descriptors ... But let's not hijack this comment area for a discussion, ok? – Christopher Creutzig Mar 15 '13 at 18:56
0
More of a question for StackOverflow but I've got this code lying about.
To find the right way to go about it, analyzing the source code for find
will yield a m much more proper lesson. You don't really have to have it run to figure out what it's doing, you can read each line and google what you don't understand. But stepping through it with a debugger might help. If you have problems compiling it, take it to SO or whatever other resources you have to help you with build environments.
#include <dirent.h>
int doSomethingWithTheFileSystem()
{
DIR *directory;
struct dirent *whatDoesEPstandfor; //seriously, you terse C bastards just HATE new guys don't you?
int count = 0;
char filename[50]; //This is going to bite you in the ass when you have a filename larger than 50. Seriously, don't do this.
FILE* file;
directory = opendir (".");
if(directory != NULL)
{
while((whatDoesEPstandfor = readdir(directory)) != NULL )
{
printf("Looking at: %s\n",whatDoesEPstandfor->d_name );
if(strstr(whatDoesEPstandfor->d_name,"thatThingYouAreLookingFor") != NULL)
{
printf("Found it at: %s\n",whatDoesEPstandfor->d_name);
}
}
closedir (directory);
}
}

Philip
- 141
how 'find' command works.
? What are you trying to do? – Mar 14 '13 at 17:35find
do something you are missing? Other? – vonbrand Mar 14 '13 at 23:45