Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-04-19
8
Medium Priority
?
3,237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 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