implement the command ls -la |grep <arg> |wc using c language

hi i want to ask how can i implement a program using c language that will emulate a command of the form
 ls -la |grep <arg> |wc, where arg is the argument passed to the program at command line.(one argument only)
the output of the program must match with the output of the shell when you type this command at the prompt. i have to create my own .c source file, as well as the makefile which must produce a binary(i.e. executable file) named lgw when make is typed.
note that i should not use the "system" function in my code.
rania12345Asked:
Who is Participating?
 
sunnycoderConnect With a Mentor Commented:
homework question ?

man scandir
man opendir
man readdir
man stat
man fstat
man strstr

Let us see what have you done so far !!
0
 
da99rmdCommented:
Why c, just make a shell script instead like:
#!/bin/bash
if [ $# -eq 2 ]; then
   ls -la $1 | grep $2 | wc
else
echo "Usage: $0 (dir) (what to grep for)"
fi
# End script

/Rob
0
 
mac_Commented:
man exec :P
0
 
astrandCommented:
Here's some code to give you a start. You need to implement error checking, word count and byte count yourself.

<code edited by sunnycoder, Page Editor>
0
 
timbauerConnect With a Mentor Commented:
I am not totally sure what your constraints are.
If you must use C there are two fundamental approaches (that I know of).

sunny, showed you the necessary system calls to implement "ls".
"wc" is trivial.
"grep" is a bit harder.

I suspect your teacher wants you to use those existing programs, but
start them yourself.
 You will need to fork() until you have 3 processes (2 forks).
 You will then need to connect up those processes with pipes.
 Finally, you must "dup2" the standard io streams to the pipes.
 After that each process calls their repsective "exec" function.

Here are some useful functions/topics to check into.
man fork
man pipe
man dup2
man exec

The idea is pretty easy, but little details can catch you if you don't
tread carefully. Read the pages carefully (especially fork), and develop incrementally.
You really want to know what fork does, what exactly it copies and what it does not.

Cheers,
 - Tim
0
All Courses

From novice to tech pro — start learning today.