Solved

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

Posted on 2004-04-19
8
3,123 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:rania12345
8 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 10858622
homework question ?

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

Let us see what have you done so far !!
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 10866987
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
 
LVL 3

Expert Comment

by:mac_
ID: 10882830
man exec :P
0
 
LVL 2

Expert Comment

by:astrand
ID: 10893087
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
 
LVL 2

Assisted Solution

by:timbauer
timbauer earned 250 total points
ID: 10939192
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question