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 ?

  • Do you know how to program in C? – parsifal Mar 14 '13 at 17:27
  • How do you mean how 'find' command works.? What are you trying to do? – MichaelT Mar 14 '13 at 17:35
  • Actually I'm trying to write a code that mimics 'find' from findutils package. I got a look at the find.c from the package files, but when I try to run that code using codeblocks I'm unable to compile it. – Northstar Mar 14 '13 at 17:40
  • @parsifal Yes. I code in C sometimes. – Northstar Mar 14 '13 at 17:52
  • @Northstar what are you trying to do to mimic find? Would you consider saying "use perl and File::Find" (find2perl is a wonderful quick script tool) to do it for you to be an appropriate answer? Or is the problem the "using codeblocks I'm unable to compile it."? – MichaelT Mar 14 '13 at 17:55
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
    This will quickly run out of file descriptors .... – Ingo Mar 14 '13 at 18:11
  • 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. – Christopher Creutzig 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 9: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

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);

Not the answer you're looking for? Browse other questions tagged or ask your own question.