Solved

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

Posted on 2004-04-19
8
3,089 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
Comment Utility
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
Comment Utility
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_
Comment Utility
man exec :P
0
 
LVL 2

Expert Comment

by:astrand
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now