This is the popular bash command cat
written in C.
I have no idea what this line does:
if (argc==1) return cat(stdin), 0;
I've never seen anything like it before. Here's the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void cat(FILE *f)
{
int c;
while (EOF != (c = getc(f)))
{
if (EOF == putchar(c))
{
perror("cat");
return;
}
}
if (ferror(f))
{
perror("cat");
return;
}
}
int main(int argc, char **argv)
{
if (argc==1) return cat(stdin), 0;
for(int i=1; i<argc; i++)
{
if (!strcmp("-", argv[i]))
{
cat(stdin);
continue;
}
FILE *f = fopen(argv[i], "r");
if (!f)
{
perror("cat");
continue;
}
cat(f);
fclose(f);
}
return 0;
}
What does the line if (argc==1) return cat(stdin), 0;
do?