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.