Ce cat permet comme le vrai cat d'afficher un fichier de taille infini
lutilisation du cat est simple : my_cat [-En] [filename]
Source / Exemple :
/***************************************************
Name : my_cat.c
Author: alexscott & djordje
Mail : alexscott_devil@hotmail.com
Date(create) : 02:12 07/11/2003
Description : ben un chat bien a nous
Syntax: my_cat [-En] [filename]
#include <fcntl.h>
void *malloc(int);
int my_strlen(char *str)
{
int i;
for (i = 0; str[i]; i++);
return(i);
}
int my_strlenbn(char *str)
{
int i;
for (i = 0; str[i] != '\n' && str[i]; i++);
if(str[i])
i++;
return(i);
}
int my_intlen(int len)
{
int i = 1;
int a = 10;
while(a <= len)
{
a *= 10;
i++;
}
return(i);
}
char *my_itoa(int nb)
{
char *result;
int m = my_intlen(nb);
result = malloc(m + 1);
bzero(result, m);
while(m)
{
result[m-1] = '0' + nb % 10;
nb /= 10;
m--;
}
return(result);
}
void my_putchar(char c)
{
write(1, &c, 1);
}
void my_puts(char *str)
{
write(1, str, my_strlen(str));
}
int my_putstr(char *str, int *mark)
{
int i;
static int suite = 1;
static int test = 0;
if(str)
{
if(mark[1] && (test || suite == 1))
{
for(i = 0; i < 6 - my_intlen(suite); i++)
my_putchar(' ');
my_puts(my_itoa(suite++));
my_putchar('\t');
}
test = 0;
if((mark[0] || mark[1]) && str[my_strlen(str) - 1] == '\n')
{
write(1, str, my_strlen(str) - 1);
test = 1;
if(mark[0])
my_putchar('$');
my_putchar('\n');
}
else
my_puts(str);
return(1);
}
if(test)
{
if(mark[0])
my_putchar('$');
my_putchar('\n');
}
return(0);
}
void my_error(int error)
{
if(!error)
my_puts("my_cat : no filename\n");
if(error == 1)
my_puts("my_cat : Filename error !!!\n");
my_puts("syntax: my_cat [-En] [filename]\n");
exit(2);
}
char *my_read(int file_d)
{
char *str;
int len;
str = malloc(1024 + 1);
memset(str, 0, 1024);
len = read(file_d, str, 1024);
if(len)
return(str);
return(0);
}
char *my_line(const int file_d)
{
int i;
int fin;
int nb;
static char *str;
char *result;
fin = 1;
if(!str || !my_strlen(str))
{
str = malloc(1024 + 1);
fin = read(file_d, str, 1024);
}
if(!fin)
return(0);
nb = my_strlenbn(str);
result = malloc(nb + 1);
bzero(result, nb);
i = 0;
for(; i < nb; i++, str++)
result[i] = str[0];
return(result);
}
int main(int argc,char **argv)
{
int i = 1;
int file_d;
int *mark;
mark = malloc(2 + 1);
bzero(mark, 2);
if(argv[1][0] == '-')//creer pour pouvoir a la rigueur mettre dotr options
{
for(i = 1; i < my_strlen(argv[1]); i++)
{
if(argv[1][i] == 'E')
mark[0] = 1;
else if(argv[1][i] == 'n')
mark[1] = 1;
else
my_error(0);
}
i = 2;
}
if(!argv[i])
{
while(42)
my_putstr(my_read(1), mark);
exit(2);//trip car sert a rien
}
file_d = open(argv[i], O_RDONLY);
if(file_d != -1)
{
if(mark[0] || mark[1])
{
while(my_putstr(my_line(file_d), mark));
close(file_d);
}
else
{
while(my_putstr(my_read(file_d), mark));
close(file_d);
}
}
else
{
close(file_d);
my_error(1);
}
free(mark);
return(0);
}
Conclusion :
Voila deja correction de quelques problemes
et rajout de la gestion de my_cat sans arguments
des options E et n (numerotations de lignes et $ a la fin de chaque ligne
par contre le
my_cat -En
est pas encore top
car si on fait
my_cat -En >> file
ben ..... ca marche pas top.....
voila une belle MAJ non ???
(a fait un moment ke je devais la mettre car cela fait un moment qu'elle est faite)
... d'un seul caractère : "\0"
if(str != "aaaaaaaaaaaaaa")
Pourquoi pas if (str) ou if (str != NULL), c'est quand même plus parlant...
ou alors, si ça te fait flipper d'utiliser des pointeurs nulls, on peut utiliser une chaine d'un seul caractère : "