Solved

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

Posted on 2004-04-19
8
3,132 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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