Writing c code for who | sort | lp


 
Thread Tools Search this Thread
Top Forums Programming Writing c code for who | sort | lp
# 1  
Old 01-18-2003
Bug Writing c code for who | sort | lp

Hi,

Can any one please let me know the code for implementing
who | sort | lp

Basically I want to implent pipe here. The program can use the existing who, sort and lp


Also please let me know How many processes are created in Unix when the following commands are executed?
$ ls | more
$ ls > more

Thanks in Advance
Yeheya Ansari
# 2  
Old 01-18-2003
system("who|sort|lp"); will do it. But why would you do this? Just type:
who|sort|lp
at the shell prompt.

"ls| more" is two processes. "ls > more" is just one process. But they don't do the same thing. The second one sends the output to a file called "more".
# 3  
Old 01-19-2003
Thanks for the Reply.

What u suggest is just calling the command from C. As written earlier, I want to implenet "PIPE" in C, using pipe command and forking different processes.

Can u please suggest the solution for that.

Also if u know some document on Advanced Unix Programming, pls refer it to me.

Regards,
Yeheya
# 4  
Old 01-19-2003
If you want to do it the hard way, that is your choice I guess. But I'm certainly not going to code that for you. I do hope that you have read our rules about homework. I will point out that that if you use system(), you will indeed be implementing pipes and forking processes.

You seem to know most of the required system calls. The only other one you'll need is exec(). I suggest that you start with just "who | lp". Once you get that working, add in the sort.

I suggest "Advanced Programming in the UNIX Environment" by Richard Stevens. I think it's the best book on the subject that was ever written.
# 5  
Old 01-20-2003
To implement "who|sort|lp" command feature using a C code you might have to work with Standard C Library Functions like :
getutent ( ) / getutline ( ) , setutent ( ) , endutent ( ) , qsort ( ) and some basics of terminal I/O.
See man pages for more information.
# 6  
Old 01-20-2003
Well, I have some time to kill...so
Code:
#include <unistd.h>

main()
{
        int pipe1[2], pipe2[2];
        pipe(pipe1);

        if(fork()) {
                pipe(pipe2);
                if(fork()) {
                        close(0);
                        dup(pipe2[0]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execl("/usr/bin/sort", "sort", NULL);
                } else {
                        close(0);
                        dup(pipe1[0]);
                        close(1);
                        dup(pipe2[1]);
                        close(pipe1[0]);
                        close(pipe1[1]);
                        close(pipe2[0]);
                        close(pipe2[1]);
                        execl("/usr/bin/sed", "sed", "s/tty/TTY/g", NULL);
                }
        } else {
                close(1);
                dup(pipe1[1]);
                close(pipe1[0]);
                close(pipe1[1]);
                execl("/usr/bin/who", "who", NULL);
        }
        exit(0);
}

The system that I'm on doesn't have lp so I use a sed to get to 3 processes...
# 7  
Old 01-29-2003
I was just wondering that had the requirement been like this :
who | sort | lp 2> log.dat
then how would we modify the code you have written.Is it possible that the purpose can be attended without forking more processes.

How can we do ">" (redirection) of the output to a file using exec utility.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help writing code

Hi, i need a string that take from a log file just two words (ngwan0) and his related ip address and then execute 2 commands in a shell. Any ideas where to start? Thanks (3 Replies)
Discussion started by: Board27
3 Replies

2. Shell Programming and Scripting

Writing a UNIX script from LOG to provide return code.

Folks - Firstly, I do apologize that my first post here is a question. I am quite familiar with UNIX since our application is running on it. We are trying to automate a few things on our end and I am challenged with a task in hand that requires UNIX scripting. I am totally a newbie in UNIX... (4 Replies)
Discussion started by: sk72
4 Replies

3. Shell Programming and Scripting

Sort error code 512

Hi Friends, I am back with small issue which i tried thinking but could not get much!!!! issue here is..i m using kind of extraction query using perl code in that i do sort with few number of csv files. each of them will be around 50 to 100 M size. sometime i get following error message... (1 Reply)
Discussion started by: Shahul
1 Replies

4. UNIX for Dummies Questions & Answers

sed remove two headers; writing more elegant code

Hi there, I have two questions. First, I was wondering how to use sed to remove two header lines or two tail lines. Here I just do the same operation twice...I'm sure there is a better way. Second, and more importantly, is there a better way to have these operations use files other than... (5 Replies)
Discussion started by: mikey11415
5 Replies

5. Programming

Vi question for writing source code

Hi guys, I'm modifying an old f77 code using vi. I'm modifying the code remotely from my windows machine using xming using vi. I'm using tabs to move past the first 6 columns of the code and to keep my loops and if statements neat, but when I hit the tab key, vi displays a big red block which is... (7 Replies)
Discussion started by: rks171
7 Replies

6. Homework & Coursework Questions

Writing Code

1. The problem statement, all variables and given/known data: Write a script that will perform the grep command on a user defined file and a user defined pattern 2. Relevant commands, code, scripts, algorithms: must use grep command (that is the only piece of information that was given) ... (2 Replies)
Discussion started by: akjim101
2 Replies

7. Shell Programming and Scripting

script or piece of code where the data returned by a stored procedure you are writing

hi fndz. Can you please help me with the code if I call a stored procedure from my shell script and stored procedure returns a cursor, cursor output should be saved to a file (3 Replies)
Discussion started by: enigma_83
3 Replies

8. Shell Programming and Scripting

Help in writing the code

hi i am studying unix with c language ,and completely new to this .I have question about how to pass the control from one program file to another.My first program has the main process and it should go into the second program where i can create six processes with also the usage of pipes to... (3 Replies)
Discussion started by: soumya_v7
3 Replies

9. Shell Programming and Scripting

writing code!!!

suppose by writing this code "$ /sbin/ifconfig eth0| grep 'inet addr:' " i've got this output "inet addr:192.168.2.1 Mask:255.255.255.0" now if i want to see the output like this "IP address: 192.168.2.1 Subnet Mask: 255.255.255.0" what is the code should be? can anyone... (3 Replies)
Discussion started by: shelly
3 Replies

10. Shell Programming and Scripting

conditional writing of sql code

Hello again... I have a request from another department to list for them all the columns and tables we use in this certain database. I have spooled the oracle stored procedured into 1 file. I need a way to write out parts of that file. The criteria is to to start the block to be written when... (0 Replies)
Discussion started by: kburrows
0 Replies
Login or Register to Ask a Question