- head | dir
- 09 Dec 2014 04:22:22 pm
- Last edited by AliceIsDead on 13 Dec 2014 10:30:15 am; edited 1 time in total
I have this code:
Code:
Imagine the program is called head.
If I write this in the command prompt:
"head | dir"
It displays dir but instead of showing the path at the bottom and the blinking _, it lets you write whatever you want and only goes back to normal if you press Ctr+Z (EOF) or Ctr+C.
So, from what I know about pipes, the command does this:
head > tmp file
dir < tmp file
delete tmp
So, head's output is send to the tmp file... only what is head's output?
Then that output is send to dir as input.
So, what is exactly being sent as input to dir?
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_LIN 80
#define SIGN '-'
void Fatal_Errorl(int num_error, char *string)
{
switch(num_error)
{
case 1:
fprintf(stderr, "Sintax:\n\nhead [-n] [File]\n\n");
break;
case 2: fprintf(stderr,"Imp. to open file '%s'\n", string);
break;
}
exit(num_error);
}
main(int argc, char *argv[])
{
FILE *fp = stdin;
char s[MAX_LIN+1];
int i=0;
int n_lins = 10;
switch(argc)
{
case 1: break;
case 2: if (argv[1][0] ==SIGN)
n_lins = atoi(argv[1]+1);
else
if (!(fp=fopen(argv[1],"r")))
Fatal_Error(2, argv[1]);
break;
case 3: if (argv[1][0]!=SIGN)
Fatal_Error(2,argv[2]);
else
{ n_lins = atoi(argv[1]+1);
if (!(fp=fopen(argv[2],"r")))
Fatal_Error(2,argv[2]);
}
break;
default: Fatal_Error(1,"");
}
while (fgets(s,MAX_LIN+1,fp)!=NULL && i++<n_lins)
printf(s);
fclose(fp);
}
Imagine the program is called head.
If I write this in the command prompt:
"head | dir"
It displays dir but instead of showing the path at the bottom and the blinking _, it lets you write whatever you want and only goes back to normal if you press Ctr+Z (EOF) or Ctr+C.
So, from what I know about pipes, the command does this:
head > tmp file
dir < tmp file
delete tmp
So, head's output is send to the tmp file... only what is head's output?
Then that output is send to dir as input.
So, what is exactly being sent as input to dir?