471,850 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,850 software developers and data experts.

"ls -a | wc -l " in c

i am trying to implement the unix command ls -l | wc -l
in c...( using pipes and excev)
its not working.... can anyone help me here ???
thanks in advance
rahul
#include<stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 int main()
5 {
6 int a[2] , test ;
7 pipe(a);
8 close(0);
9 dup2(a[0],0);
10 if (fork() == 0)
11 {
12 close(1) ;
13 dup2(a[1] ,1);
14 close(a[1]);
15 close(a[0]);
16 close(a[0]);
17 execlp("/bin/ls" , "ls" , "-a" , NULL );
18 }
19 else
20 {
21
22 wait (NULL);
23 execlp("/usr/bin/wc" , "wc" , "-l" );
24 }
25 }

Sep 25 '06 #1
2 6480
"ra*******************@gmail.com" <ra*******************@gmail.comwrites:
i am trying to implement the unix command ls -l | wc -l
in c...( using pipes and excev)
its not working.... can anyone help me here ???
pipes and execv are extensions, not part of standard C. Try
comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 25 '06 #2
ra*******************@gmail.com wrote:
i am trying to implement the unix command ls -l | wc -l
in c...( using pipes and excev)
its not working.... can anyone help me here ???
thanks in advance
rahul
#include<stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 int main()
5 {
6 int a[2] , test ;
7 pipe(a);
8 close(0);
9 dup2(a[0],0);
10 if (fork() == 0)
11 {
12 close(1) ;
13 dup2(a[1] ,1);
14 close(a[1]);
15 close(a[0]);
16 close(a[0]);
17 execlp("/bin/ls" , "ls" , "-a" , NULL );
18 }
19 else
20 {
21
22 wait (NULL);
23 execlp("/usr/bin/wc" , "wc" , "-l" );
24 }
25 }
1. There is no use to wait(NULL)
2. You should add code to check if your system calls do not fail
3. You forgot NULL at the end of the execlp of wc

This works

#include<stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int a[2] , pid;
pipe(a);
if ((pid=fork()) == 0)
{
dup2(a[1] ,1);
close(a[1]);
close(a[0]);
execlp("/bin/ls" , "ls" , "-a" , NULL );
perror("/bin/ls");
}
else if (pid>0)
{
dup2(a[0],0);
close(a[1]);
close(a[0]);
execlp("/usr/bin/wc" , "wc" , "-l", NULL ); /* add NULL
!! */
perror("/usr/bin/wc");
} else
perror("fork");
}

Adding the perror lines quickly reveiled
/usr/bin/wc: Bad address

Hubble.

Sep 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Paulo da Silva | last post: by
5 posts views Thread by Didier C | last post: by
7 posts views Thread by Xah Lee | last post: by
1 post views Thread by kurt.krueckeberg | last post: by
26 posts views Thread by Michel Rouzic | last post: by
3 posts views Thread by Ioannis Vranos | last post: by
3 posts views Thread by =?iso-8859-1?q?KLEIN_St=E9phane?= | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.