d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Piping In C
Add Reply New Topic New Poll
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Dec 3 2014 03:16pm
So my assignment is to write a program which uses a pipe to pipe the output of the "ls" process to another process which sorts and displays this output. The sorting criteria is to sort by file extension (no extension comes first) and then by file name.

Here is what i have currently:
Code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAXLINE 4096 /* max line length */

void err_sys (const char* message);

int main(void)
{
int n;
int ls[2];
pid_t pid;
char line[MAXLINE];

if (pipe(ls) < 0)
err_sys("pipe error");
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
else if (pid > 0)
{ /* parent */
close(ls[0]);
write(ls[1], "hello world\n", 12);
}
else
{ /* child */
close(ls[1]);
n = read(ls[0], line, MAXLINE);
write(STDOUT_FILENO, line, n);
}
exit(0);
}

void err_sys (const char* message)
{
printf ("%s\n", message);
exit (1);
}


would it be best to create another function and call it within the else block to do the sorting?
also is everything that i have now good so far? the ls is going to change a bit, i think based on the description of the assignment the names should be coming from user input and not defined like hello world is.
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Dec 3 2014 03:22pm
Derp i forgot ls is a command to list the directory
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll