Operating System - HP-UX
1755716 Members
2851 Online
108837 Solutions
New Discussion юеВ

Re: Accessing the directory listing into a string directly

 
Umesh_1
Occasional Contributor

Accessing the directory listing into a string directly

Hi All,

I want to list the files and directories in a directory using the system call
system("ls ") in "c" language.
I can redirect the output to a file. But again I need to read it into a string.

Can some one please help me if there is any way that I can access the output into a string directly without using file.

Please reply me for any queries.

Thanks in advance
Umesh
Simple
7 REPLIES 7
Peter Kloetgen
Esteemed Contributor

Re: Accessing the directory listing into a string directly

Hi Umesh,

you could use command substitution:

var=`ls /directory_to_list`

or

var=$(ls /directory_to_list)

if you use posix- or korn- shell. The first syntax works also for bourne- shells.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
A. Clay Stephenson
Acclaimed Contributor

Re: Accessing the directory listing into a string directly

Hi:

First of all, it would be extremely dangerous to attempt to load an entire directory into a string. I suppose what you mean is how to avoid the temporary file. Use the popen() function.

char s_cmd[256],s_tmp[512],*p = NULL;
char *my_dir = "/tmp/";
FILE *f = NULL;
int cc = 0;

extern int errno;

(void) sprintf(s_cmd,"ls %s",my_dir);
f = popen(s_cmd,"r");
if (f != NULL)
{
s[0] = '\0';
p = fgets(s_tmp,(int) sizeof(s_tmp),f);
while (p != NULL)
{
printf("%s",s_tmp);
p = fgets(s_tmp,(int) sizeof(s_tmp),f);
}
cc = pclose(f);
}
else cc = (errno != 0) ? errno : -1;

That should do it for you, Clay

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Accessing the directory listing into a string directly

Hi again:

Ooops...

Change
s[0] = '\0';
to
s_tmp[0] = '\0';

Actually, you could leave the statement out; it's just that I tend to initialize everything. The other area of concern is to make sure that the s_cmd variable is big enough to hold your largest directory name and that s_tmp is sized big enough to hold the largest filename plus a Linefeed.
If it ain't broke, I can fix that.
harry d brown jr
Honored Contributor

Re: Accessing the directory listing into a string directly

Clay,

SHAME on you for initializing variables!

Isn't it funny how a lot of today's programmers leave so much to "chance"? Like when they HARDCODE array length's, thinking that no one will ever need more than X rows. Or they decide to abuse malloc to get memory, without really thinking about other elegant solutions to the problem. Or the compiler will initialize it for me....

live free or die
harry
Live Free or Die
Rodney Hills
Honored Contributor

Re: Accessing the directory listing into a string directly

I guess that is why perl is becoming such a favorite tool of choice for administrators over developing "c" programs.

-- Rod Hills
There be dragons...
Steven Gillard_2
Honored Contributor

Re: Accessing the directory listing into a string directly

The best way is to use the C library routines opendir / readdir / closedir. See the appropriate man pages for details.

Regards,
Steve
Carlos Fernandez Riera
Honored Contributor

Re: Accessing the directory listing into a string directly

The correct way is use of opendir/readdir.

See man, there are some example.
unsupported