Home > Archive > Unix Programming > February 2007 > Finding the path of a directory
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Finding the path of a directory
|
|
| iwasinnihon 2007-02-03, 4:12 am |
| I am writing a program that needs to traverse through a directory and
list all files and directories and the files in the subdirectories.
In other words when I type
newprog test
It would print out all of the files and directories in the folder
test. As it reaches each subdirectory in test it needs to print out
all of the files and directories in that subdirectory and so forth.
Everything I have tried I end up with an error. Does anyone have any
simple ideas on how to do this?
| |
| Logan Shaw 2007-02-03, 4:12 am |
| iwasinnihon wrote:
> I am writing a program that needs to traverse through a directory and
> list all files and directories and the files in the subdirectories.
>
> In other words when I type
>
> newprog test
>
> It would print out all of the files and directories in the folder
> test. As it reaches each subdirectory in test it needs to print out
> all of the files and directories in that subdirectory and so forth.
>
> Everything I have tried I end up with an error. Does anyone have any
> simple ideas on how to do this?
1. Use recursion.
2. Avoid using chdir() -- it's tricky to reverse its effects.
3. Post the code you tried. It's hard to help someone when all
the information you have is that they encountered "an error".
You could also look at the source for the "find" command, but I
recommend against that, since it probably uses a bunch of clever
tricks to improve performance, and what you want is simplicity,
at least until you get something working and are ready to move
on to the more complex task of optimizing the snot out of it, if
indeed you ever need to do that.
- Logan
| |
| Måns Rullgård 2007-02-03, 8:06 am |
| Logan Shaw <lshaw-usenet@austin.rr.com> writes:
> iwasinnihon wrote:
>
> 1. Use recursion.
> 2. Avoid using chdir() -- it's tricky to reverse its effects.
> 3. Post the code you tried. It's hard to help someone when all
> the information you have is that they encountered "an error".
4. Check if your system has one of the ftw() or nftw() functions.
> You could also look at the source for the "find" command, but I
5. Check if the "find" command can do what you need as-is.
--
Måns Rullgård
mru@inprovide.com
| |
| William Ahern 2007-02-03, 7:06 pm |
| On Sat, 03 Feb 2007 13:41:23 +0000, Måns Rullgård wrote:
> Logan Shaw <lshaw-usenet@austin.rr.com> writes:
>
>
> 4. Check if your system has one of the ftw() or nftw() functions.
>
Or fts_open() and fts_read(). ftw() is probably more prevalent, but some
systems only have the BSD fts functions.
| |
|
| On Feb 3, 10:37 am, "iwasinnihon" <iwasinni...@hotmail.com> wrote:
> I am writing a program that needs to traverse through a directory and
> list all files and directories and the files in the subdirectories.
>
> In other words when I type
>
> newprog test
>
> It would print out all of the files and directories in the folder
> test. As it reaches each subdirectory in test it needs to print out
> all of the files and directories in that subdirectory and so forth.
>
> Everything I have tried I end up with an error. Does anyone have any
> simple ideas on how to do this?
Hai,
Here is a code i have written to search a file in your home directory.
Alter it so that it displays all the file rather one that is
specified.
Any doubts solicited
Bye,
========================================
=================================
#include <stdio.h>
#include "../../guru.h"
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
int found = 0;
unsigned short report = 0;
unsigned short rel = 0;
char *ignore = "";
void searchIn(char *node, char *fname)
{
char *path;
DIR *dp;
struct dirent *dir;
struct stat buf;
if ((dp = opendir (node)) == NULL)
{
if (report)
printf("can't open %s: %s\n", node, strerror (errno));
return;
}
chdir (node);
while ((dir = readdir (dp)) != NULL)
{
stat (dir->d_name, &buf);
if (S_ISDIR (buf.st_mode))
{
if (strcmp (".", dir->d_name) == 0 ||
strcmp ("..", dir->d_name) == 0 ||
strcmp (ignore, dir->d_name) == 0)
continue;
searchIn (dir->d_name, fname);
}
if ((strcmp (dir->d_name, fname)) == 0)
{
path = (char *) get_current_dir_name ();
if (rel)
path = strstr (path, (char *) getenv ("LOGNAME"));
printf ("%s", path);
printf ("/\033[1m%s\033[0m \n", dir->d_name);
found++;
path = NULL;
if (!(found % 23)) waitForKey();
}
}
chdir ("..");
closedir (dp);
}
main(int argc, char *argv[])
{
double start_time = clock();
double end_time = 0;
double timetaken = 0.0;
unsigned int i;
int opt=1;
extern int optind;
extern char *optarg;
void help (char *);
char *home = (char *) getenv ("HOME");
if (argc <= 1) help (argv[0]);
while (opt)
switch (getopt (argc, argv, "i:rhp"))
{
case 'i' : ignore = optarg; break;
case 'r' : rel = 1; break;
case 'h' : help (argv[0]); break;
case 'p' : report = 1; break;
case -1 : opt = 0; break;
default : printf("Try \033[1m-h\033[m for help\n");exit(0);
}
for (i = 1; i < argc; i++)
{
//if (i == j || i == j1) continue;
searchIn (home, argv[i]);
}
end_time = clock();
timetaken = (double) (end_time - start_time) / CLOCKS_PER_SEC;
if (found)
{
printf ("%d file%s found", found, (found > 1) ? "s" : "");
printf (" in %f sec or %f mS\n", timetaken, timetaken * 1000);
}
else
printf("\033[5m\033#6NOT FOUND\033[0m\n");
exit (0);
}
void help (char *argv)
{
printf ("\n\033[4mSearches for files in home directory.\033[m\n\n");
up:printf ("Usage: %s [\033[1m-irph \033[m] <file_1>", argv);
printf (" [<file_2>...]\n");
printf ("Options: \033[1m-i\033[m <dir>\tignore directory. \n");
printf ("\t \033[1m-r \033[m\t\tShow relative path (from home D");
printf ("irectory).\n\t \033[1m-p\033[m\t\tReport about Permissi");
printf ("on denied dir.\n\t \033[1m-h\033[m\t\tDisplay Help.\n\n");
exit(0);
}
========================================
========================================
====
| |
| Christopher Layne 2007-02-05, 7:05 pm |
| guru wrote:
> Hai,
> Here is a code i have written to search a file in your home directory.
> Alter it so that it displays all the file rather one that is
> specified.
> Any doubts solicited
> Bye,
> #include "../../guru.h"
doubt.
> chdir (node);
doubt.
> if (!(found % 23)) waitForKey();
huh? Unix applications do not typically like to inflict interactiveness on the
user. If they want paging they will pipe through "more" or "less". Do not
assume they want paging by default.
Also do not presume they want random ANSI codes in their output. This is not
DOS.
| |
|
| On Feb 6, 5:40 am, Christopher Layne <cla...@com.anodized> wrote:
> guru wrote:
>
> doubt.
>
>
> doubt.
>
>
> huh? Unix applications do not typically like to inflict interactiveness on the
> user. If they want paging they will pipe through "more" or "less". Do not
> assume they want paging by default.
>
> Also do not presume they want random ANSI codes in their output. This is not
> DOS.
it is pure ANSI C code but coded on UNIX. Regarding interactive I have
changed it as option. Regarding chdir(node), it is necessary. Thanks
for your reply.
|
|
|
|
|