#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct emp {
	char *name;
	char *forename;
	char *job;
	char *zipcode;
	int id;
	struct emp *next;
} t_emp;

//For strdup is not a standard
char *strdup(const char *s){
	char *ret = malloc(strlen(s)+1);
	if(ret)
		strcpy(ret, s);
	return ret;
}

t_emp *read_func(FILE *fp){
	static const char *delim = " \t\n";
	char line[256];

	while (fgets(line, sizeof(line), fp)){
		char *str = strtok(line, delim);
		if(!str || strcmp(str, "new_employee") != 0)
			continue;
		t_emp *emp = malloc(sizeof(*emp));//check omitted
		emp->name = strdup(strtok(NULL, delim));//check omitted
		emp->forename = strdup(strtok(NULL, delim));
		emp->job = strdup(strtok(NULL, delim));
		emp->zipcode = strdup(strtok(NULL, delim));
		emp->id = atoi(strtok(NULL, delim));
		emp->next = NULL;
		return emp;
	}
	return NULL;
}

void fill_struct(t_emp *s_emp){
	FILE *fp = stdin;
	while(NULL!= (s_emp->next = read_func(fp)))
		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;
	}
}

int	 main(void){
	t_emp s_emp = { .next = NULL };//list holder, this is dummy.

	fill_struct(&s_emp);
	print_emp(s_emp.next);
	//release list
}