1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
void my_putchar(char c)
{
write(1, &c, 1);
}
void my_putstr(char *str)
{
for (; *str != 0; str++)
my_putchar(*str);
}
void cat(char *fn)
{
int nb;
int fd;
char *buffer;
fd = open(fn, O_RDONLY);
buffer = (char *) malloc(sizeof(char) * 512);
while ((nb = read(fd, buffer, 512)) > 0)
write(1, buffer, nb);
if (nb == (-1))
{
my_putstr("cat: ");
my_putstr(fn);
my_putstr(": Operation not permitted\n");
}
close(fd);
}
void cat_alone()
{
char buffer[512];
int nb;
while ((nb = read(0, buffer, 512)) > 0)
write (1, buffer, nb);
}
int main(int ac, char **argv)
{
if (ac < 2)
cat_alone();
else
{
ac--;
argv++;
while (ac-- > 0)
if (*argv[0] == '-')
cat_alone();
else
cat(*argv++);
system("PAUSE");
return 0;
} |
Partager