Powered by Invision Power Board


  Reply to this topicStart new topicStart Poll

> Writing "find" code in C
SQ4LIFE
Posted: Nov 4 2003, 12:26 AM
Quote Post


Newbie
*

Group: Members
Posts: 3
Member No.: 129
Joined: 4-November 03



Writing "find" code
I've got a project that writing "find" code in solaris.

That is finding a file in current and sub directories.

While I am looking for some example on the book for two days, but I have no idea where to start.

I think I need to use recursion, but is it true?

I am still a Unix beginner and the teacher and book didn't give me enough info...

Please tell me the way of structure and concept.

Thanks in advance.

SQ
PMEmail Poster
Top
fishsponge
Posted: Nov 4 2003, 12:38 AM
Quote Post


Administrator
********

Group: Admin
Posts: 679
Member No.: 1
Joined: 13-February 03



NOTE: I have moved this topic into the Programming forum, as it is not suited to the General forum at all... please read the forum rules, and think before you post :D

While it is a *lot* to ask someone else to write the code for you, i think i can try to give you an idea of the general structure...

I'm going to assume you want to be able to search for a file called "fred", for example.

1. List contents of current directory.
2. Search (grep?) results for string "fred".
3. If not present, then list contents of first directory.
4. Search results for string "fred".
5. if not present, then list contents of first directory within this directory.
6. keep looping...

recursion would probably be a good thing to use. You could then call your find() method on the first directory, and then call it again on the next directory, and the next, and the next etc...

If you are really adventurous, you could try looking for the source code for the linux "find" command... to find files in linux named fred, you would use the following command:
CODE
hobbs@turnip:~/binit$ find . -name fred
./fred
./test/fred
hobbs@turnip:~/binit$

so you would need to look through the source code for the bit that gets executed when the "-name" parameter is supplied.

HTH.
PMEmail PosterUsers WebsiteICQAOLYahooMSN
Top
SQ4LIFE
Posted: Nov 4 2003, 04:06 AM
Quote Post


Newbie
*

Group: Members
Posts: 3
Member No.: 129
Joined: 4-November 03



Sorry about breaking the forum rule. I will try look at next time.

Anyway, how can I find the source code for the reference?

I've been looking around for a while, but never found. Is it a secret or something?

Sorry about dumb questions...

And thanks for your help.
PMEmail Poster
Top
fishsponge
Posted: Nov 4 2003, 10:03 AM
Quote Post


Administrator
********

Group: Admin
Posts: 679
Member No.: 1
Joined: 13-February 03



s'ok... :D

1. go here: http://www.gnu.org/prep/ftp.html
2. Select a mirror near you
3. go into "findutils" directory
4. download "findutils-4.1.tar.gz"
5. gunzip and untar it into a directory on your machine
6. When it's unpacked, go to the "findutils-4.1/find/" directory, and you will find the C source code for the find command.

PLEASE NOTE: This software is released under the GNU Public License (GPL), so please be careful with what you do with it. To find out about the GPL, click here: http://www.gnu.org/copyleft/gpl.html
PMEmail PosterUsers WebsiteICQAOLYahooMSN
Top
SQ4LIFE
Posted: Nov 4 2003, 11:10 AM
Quote Post


Newbie
*

Group: Members
Posts: 3
Member No.: 129
Joined: 4-November 03



Thanks for the info. I will be careful about it.
PMEmail Poster
Top
xplicit^
Posted: Dec 8 2003, 04:41 PM
Quote Post


Newbie
*

Group: Members
Posts: 2
Member No.: 153
Joined: 8-December 03



I assume it works very similar to the command 'dir' (has the same function as ls) in unix, although I used to use 'ls' for unix and 'dir' for MS-Dos.


and here the Code for 'dir' .. maybe it can help you:



CODE

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

main(int argc, char*argv[])

{
  DIR *pDir;
  struct dirent *p;
  struct stat fileStat;
  char buffer[1024];

if( argc != 2 )

 {
       perror( "Please enter a parameter" );
       exit(1);
  }

if( (pDir=opendir(argv[1])) == 0)

   {
       perror( "Couldn't read the directory" );
       exit(1);
    }

while( (p=readdir(pDir)) != 0)
  {
     strcpy(buffer, argv[1]);
     strcat(buffer, "/");
     strcat(buffer, p->d_name);
     if( !stat(buffer, &fileStat) )
       {
          if( S_ISDIR(fileStat.st_mode) )
            {
                printf("%s/n", p->d_name);
              }
         }
         else
               perror( " Couldn't read the file" );
    }
exit(0);

}



I hope it could help you a bit. It's just to get your inspiration working :)
Actually many scripts like that have a similar basic principle.

Cheers,

xplicit^
PMEmail Poster
Top
fishsponge
Posted: Dec 8 2003, 06:35 PM
Quote Post


Administrator
********

Group: Admin
Posts: 679
Member No.: 1
Joined: 13-February 03



i think you are slightly confused i'm afraid...

the 'dir' command is *only* for DOS, and simply lists the contents of the current working directory. Using 'dir' with the '/s' option does allow searching, although the code sample you pasted doesn't contain this.

The 'cd' command is used in both unix *and* DOS, and serves pretty much the same purpose in both. It will change the current directory that you are working in. The 'cd' command will in no way whatsoever give you a directory listing, or allow you to search.
PMEmail PosterUsers WebsiteICQAOLYahooMSN
Top
xplicit^
Posted: Dec 8 2003, 06:53 PM
Quote Post


Newbie
*

Group: Members
Posts: 2
Member No.: 153
Joined: 8-December 03



i have already edited my post. Sorry mate, it must be my fever. I meant 'ls -la' :)

it doesn't allow searching, of course: but it does look for files in a dir. I think to find something his script/programm should look for files and choose :)
PMEmail Poster
Top
nrutter
Posted: Dec 18 2004, 08:58 AM
Quote Post


Newbie
*

Group: Members
Posts: 7
Member No.: 721
Joined: 17-December 04



The examples section here
may help
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming/Languages | Next Newest »

Topic Options Reply to this topicStart new topicStart Poll

 



[ Script Execution time: 0.1646 ]   [ 12 queries used ]   [ GZIP Enabled ]




Partners: Cambridge Plus :: <Link Available> :: Stainless Steel Footswitch :: <Link Available>
Unix Man Pages / Linux Man Pages :: HiFi Forum :: SIP VoIP Phone & Provider Reviews :: UNIX/Linux Forum Archives

More info on advertising on Unix/Linux Forum