differentiate between file and pipe.
This commit is contained in:
parent
7108aef1b3
commit
4a9ab4f9f3
3
makefile
3
makefile
|
@ -28,3 +28,6 @@ build: $(SRC)
|
||||||
|
|
||||||
format: $(SRC)
|
format: $(SRC)
|
||||||
$(FORMATTER) $(SRC)
|
$(FORMATTER) $(SRC)
|
||||||
|
|
||||||
|
test: $(OUTPUT)
|
||||||
|
/bin/echo -e "123\n45 67" | ./$(OUTPUT)
|
||||||
|
|
17
wc.c
17
wc.c
|
@ -1,10 +1,23 @@
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h> // read, isatty
|
||||||
|
|
||||||
int main(){
|
// STDIN_FILENO
|
||||||
|
int process_fp(FILE* fp)
|
||||||
|
{
|
||||||
char buffer[1];
|
char buffer[1];
|
||||||
while (read(0, buffer, sizeof(buffer)) > 0) {
|
while (read(0, buffer, sizeof(buffer)) > 0) {
|
||||||
printf("%c", buffer[0]);
|
printf("%c", buffer[0]);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (!isatty(STDIN_FILENO)) { // check if stdin is coming from terminal or pipeline
|
||||||
|
return process_fp(STDIN_FILENO);
|
||||||
|
} else if(argc > 1){
|
||||||
|
printf("To do");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user