Bonsoir,

Je veux faire un programme qui récupère dans un char *result ce que l'on read en entrée standard, je veux que le programme tourne en boucle et renvoie 0 lorsqu'il a pu read entièrement ce que je lui donne (jusqu'au '\n').

Je n'ai pas le droit a la fonction realloc qui m'aurait simplifie la vie, donc je pense avoir trouve une astuce mais j'ai un problème de bus error, sans vraiment comprendre pourquoi.

En gros je dis que tmp = result et result = tmp + buffer en essayant de free et malloc comme il faut... Voici mon code qui ne fait rien d'extraordinaire:

.main.c
.my_get_args.h
.my_get_args.c


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include "my_get_args.h"
 
int     my_strlen(char *str)
{
  int   i;
 
  i = 0;
  while (str[i])
    i++;
  return (i);
}
 
void    my_putstr(char *str)
{
  write(1,str,my_strlen(str));
}
 
int             main()
{
  t_test        args;
 
  args.result = malloc(1 *sizeof(*args.result));
  args.result[0] = '\0';
  while (my_get_args(&args) != 0)
      my_putstr(args.result);
  return (0);
}

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef         __MY_GET_ARGS_H__
# define        __MY_GET_ARGS_H__
 
# define BUFF_SIZE 1
 
typedef struct  s_test
{
  char          *result;
  char          *tmp;
}               t_test;
 
void            my_strcpy(char *(*tmp), char *(*result), int *i);
void            my_strcpnext(char *(*result), char *buffer, int *debut, int *i);
int             my_get_args(t_test *ptr);
void            my_putstr(char *str);
int             my_strlen(char *str);
 
#endif          /*__MY_GET_ARGS__ */

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <stdlib.h>
#include <unistd.h>
#include "my_get_args.h"
 
void            my_strcpy(char *(*tmp), char *(*result), int *i)
{
  while (*result[*i])
    {
      *tmp[*i] = *result[*i];
      *i = *i + 1;
    }
  *tmp[*i] = '\0';
}
 
void            my_strcpnext(char *(*result), char *buffer, int *debut, int *i)
{
  *result[*i] = buffer[*debut];
  *i = *i + 1;
  *debut = *debut + 1;
}
 
int             my_get_args(t_test *ptr)
{
  char          buffer[BUFF_SIZE];
  int           fin;
  int           debut;
  int           i;
 
  debut = 0;
  if (debut == 0)
    fin = read(0, buffer, (BUFF_SIZE + 1));
  buffer[BUFF_SIZE] = '\0'
  i = 0;
  ptr->tmp = malloc(((my_strlen(ptr->result)) + 1) * sizeof(*(ptr->tmp)));
  ptr->tmp[0] = '\0';
  if (ptr->result[0])
    my_strcpy(&(ptr->tmp), &(ptr->result), &i);
  free(ptr->result);
  ptr->result = malloc((my_strlen(ptr->tmp) + my_strlen(buffer) + 1) * sizeof(*(ptr->result)));
  i = 0;
  if (ptr->tmp[0])
    my_strcpy(&(ptr->result), &(ptr->tmp), &i);
  while (debut < fin && buffer[debut] != '\n')
    my_strcpnext(&(ptr->result), buffer, &debut, &i);
  ptr->result[i] = '\0';
  free(ptr->tmp);
  if (buffer[debut] == '\n')
    return (0);
  else
    return (1);
}