C - Linked list next node is null

Viewed 403

I'm trying to obtain the next value of my struct by using a linked list but the next value is null.

I know that I'm not doing this the right way, hope you can help me to solve my problem.

Input file:

new_employee Hasselhoff David PDG F-13000 1 
new_employee Anderson Pamela DDR F-31000 2 
new_employee LeeNolin Gena DDR F-94270 3
new_employee Charvet David DPR F-54000 4

The code:

char    *my_strcat(char *s1, char *s2)
{
  char  *tmp;
  int   i;
  int   j;

  i = -1;
  j = 0;
  tmp = malloc(sizeof(char) * (strlen(s1) + strlen(s2)) + 1);
  while (s1[++i])
    tmp[j++] = s1[i];
  i = -1;
  while (s2[++i])
    tmp[j++] = s2[i];
  tmp[j] = '\0';
  return (tmp);
}

char    *read_func()
{
  char *str;
  char line[256];

  str = malloc(sizeof(char) * 256);
  while (fgets(line, sizeof(line), stdin))
    str = my_strcat(str, line);
  return (str);
}

void            fill_struct(t_emp *s_emp)
{
  char          *read;

  read = read_func();
  if (strstr(read, "new_employee"))
    {
      while (s_emp->next != NULL)
        s_emp = s_emp->next;
      s_emp->next = malloc(sizeof(t_emp));
      strdup(strtok(read, " "));
      s_emp->name = strdup(strtok(NULL, " "));
      s_emp->forename = strdup(strtok(NULL, " "));
      s_emp->job = strdup(strtok(NULL, " "));
      s_emp->zipcode = strdup(strtok(NULL, " "));
      s_emp->id = atoi(strtok(NULL, " "));
      s_emp = s_emp->next;
    }
}

void    print_emp(t_emp *s_emp)
{
  while (s_emp != NULL)
    {
      printf("******************************\n");
      printf("%s %s\n", s_emp->forename, s_emp->name);
      printf("position: %s\n", s_emp->job);
      printf("city: %s\n", s_emp->zipcode);
      s_emp = s_emp->next;
    }
}

main function:

int     main()
{
  t_emp s_emp;

  if (!isatty(STDIN_FILENO))
    {
      fill_struct(&s_emp);
      print_emp(&s_emp);
    }
  else
    show_help();
}

output:

******************************
David Hasselhoff
position: PDG
city: F-13000
******************************
(null) (null)
position: (null)
city: (null)

desired output:

******************************
David Hasselhoff
position: PDG
city: F-13000
******************************
Pamela Anderson
position: DDR
city: F-31000

Thanks!

1 Answers
Related