Bonjour,
j'ai un petit problème mon minishell segfault et je ne comprends pas pourquoi. Je l'ai lancé sous valgrind et il me donne
Process terminating with default action of signal 11 (SIGSEGV)
==15010== Access not within mapped region at address 0x8
==15010== at 0x401FD3: prompt (cmd.c:175)
==15010== by 0x401255: main (main.c:51)
==15010== If you believe this happened as a result of a stack
==15010== overflow in your program's main thread (unlikely but
==15010== possible), you can try to increase the size of the
==15010== main thread stack using the --main-stacksize= flag.
==15010== The main thread stack size used in this run was 8388608.
voici le code
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
char     *launch_current(char *current, char **env)
{
  {
    char  *pwd; // char* qui va contenir le chemin du dossier courant
    char  *tmp; // char* temporaire
    char  *tmp1; //char* temporaire
    int   i;
 
    i = 0;
    if (current == NULL)
      my_putstr_error("erreur current is NULL.\n");
    else
      {
        tmp = malloc(my_strlen(current) * sizeof(*tmp));
        tmp1 = malloc(my_strlen(current) * sizeof(*tmp1));
        pwd = malloc(500 * sizeof(char));
        if (tmp == NULL || tmp1 == NULL || pwd == NULL)
          my_putstr_error("Error malloc tmp, tmp1 or pwd is NULL.\n");
        else
          {
            pwd = *my_getenv("PWD", env); // my_getenv("PWD",env) retourne le chemin du dossier actuel
            my_strcpy(tmp, pwd); // copie la valeur de pwd dans tmp
            while (i < 4)
              i++;
            i = 0;
            while (tmp[i] != '\0')
              {
                // je supprime PWD= 
                if (tmp[i] == 'P' && tmp[i + 1] =='W' && tmp[i + 2] == 'D')
                  {
                    tmp[i] = '\n';
                    tmp[i + 1] = '\n';
                    tmp[i + 2] = '\n';
                    tmp[i + 3] = '\n';
                  }
                i++;
              }
            del_char(tmp, '\n'); //je supprime les \n que j'ai inséré plus haut
            strcat(tmp, "/"); // j'ajoute un / dans tmp  
            strcat(tmp, current); // j'ajoute la valeur de current dans tmp
          }
      }
    return (tmp);
  }
}
 
 
 
int    prompt(t_env *env2, t_list *env)
{ 
  char  *cd; //pour la commande cd
  char  *cd1; 
  char  *tmp; //variable temporaire
  int   erno; //stocke la valeur de retour de execute1
  char  *error; // contiens le nom de la comme qui a echoué.
  char  *tmp1;
 
  error = 0;
  erno = 0;
  if (env2->buffer == NULL)
    my_putstr_error("errreur malloc");
  my_putstr("$-->");
  memset(&env2->buffer[0], 0, 4096);
  if (read(0, env2->buffer, 4095) == 0)
    {
      my_putchar('\n');
      return (0);
    }
  else
    {
      cd = malloc(my_strlen(env2->buffer) * sizeof(char));
      cd1 = malloc(my_strlen(env2->buffer) * sizeof(char));
      tmp = malloc(my_strlen(env2->buffer) * sizeof(char));
      tmp1 = malloc(my_strlen(env2->buffer) * sizeof(char));
      error = malloc(my_strlen(env2->buffer) * sizeof(char));
      if (cd == NULL || tmp1 == NULL || error == NULL || cd1 == NULL || tmp == NULL)
        my_putstr_error("error\n");
      else
        {
          if (my_strcmp(env2->buffer, "exit\n") == 0)
            return (0);
          else
            {
              if (env2->buffer[0] == '.' && env2->buffer[1] =='/')
                {
                  my_strncpy(tmp, env2->buffer, 2); // je copie les 2 premiers caractères de env2->buffer et les stockes dans tmp
                  printf("tmp%s\n", tmp);
                  del_str(env2->buffer, tmp);
                  printf("env2->buffer %s\n", env2->buffer);
                  if (env2->buffer == NULL)
                    my_putstr_error("error env2->buffer is NULL");
                  /* else */
                  /*   { */
                  my_strcpy(error, env2->buffer);
  env2->buffer = launch_current(
env2->buffer,
env->environ);
                  erno = execute1(env2, env, error);
                  if (erno == 65280)
                    {
                      if (env2->buffer[my_strlen(env2->buffer) - 1] == '\n')
                        env2->buffer[my_strlen(env2->buffer) - 1] = 0;
                    }
                }
              my_strncpy(cd,env2->buffer,2);
              my_strcpy(cd1, env2->buffer);
              del_str(cd1, cd);
              strcpy(tmp, cd1);
              if (strncmp(cd, "cd\n", 2) == 0)
                my_cd(tmp);
              else if (my_strncmp(env2->buffer, "setenv\n", 5) == 0)
                {
                  env = add_firsttab(env, env2->environ);
                  setenv(env, env2->buffer);
                    }
                  else
                    loop(env2);
            }
        }
    }
  /* } */
  return (42);
}
merci et bonne journée