Exemple #1
0
void	read_lemin(t_li *li)
{
	int		ret;
	char	*line;
	int		retour;
	int		nbline;

	nbline = 0;
	retour = 0;
	while ((ret = get_next_line(STDIN_FILENO, &line)) > 0)
	{
		nbline++;
		retour = lineparser(li, line) + lineparser2(li, line);
		if (retour == 0)
		{
			if (li->opts['v'])
				ft_printf("{red}Error %d line: %d [%s] {eoc}\n",
				retour, nbline, line);
			break ;
		}
		addstdin(li, line);
		free(line);
		line = NULL;
	}
	check_lemin(li);
	li->usedwires = (t_wire **)malloc(sizeof(t_wire) * li->ant);
}
Exemple #2
0
int main (int argc, char *argv[])
{
  int opt, trace;
  char *pn;
	FILE *f;
	char buf[PIPE_BUF];
	char *l;
  struct ic *ic;
	int s;

	trace = 0;
	while ((opt = getopt(argc, argv, "th")) != -1) {
		switch (opt) {
			case 't':
				trace = 1;
				break;
			default: /* '?' */
				fprintf(stderr, "Usage: %s [-t] pipename\n", basename(argv[0]));
				fprintf(stderr, "\t-t - Trace\n");
				exit(EXIT_FAILURE);
		}
	}
	if (optind >= argc) {
		fprintf(stderr, "Pipename expected after options\n");
		exit(EXIT_FAILURE);
	}

  pn = argv[optind];

  /* ------------------------------------------------------ */

	if ((f = fopen(pn, "rw+")) == NULL)
		err(EXIT_FAILURE, "fopen(%s)", pn);

	signal(SIGALRM, alarmhandler);
	alarm(1);

	signal(SIGUSR1, openhandler);

  if (mkdir(PATH, 0755) == -1 && errno != EEXIST)
    err(EXIT_FAILURE, "mkdir(%s)", PATH);

  ic = ICcreate(FILES, sizeof(struct m), CACHE_SIZE, PATH);
	assert(ic);

	while (1) {

		if ((l = fgets(buf, PIPE_BUF, f))) {
			struct m *m;

      buf[strlen(buf)-1] = '\0';

			if (trace) fprintf(stdout, "line: %s\n", buf);

			if ((m = lineparser(buf)) == NULL) continue;
			if (trace) printm(stdout, (char *)m);

		  s = ICadd(ic, hash(m->id), (char *)m);
			assert(s == IC_OK);

      if (trace) ICprintcache(stderr, ic, 1, printm);

		} else if (l == NULL && errno == EINTR) {
      time_t t = time(NULL);

			if (sigflush) {

				if (trace) fprintf(stdout, "signal: %d %s", sigflush, ctime(&t));

        s = ICflushall(ic);
			  assert(s == IC_OK);

				sigflush = 0;
				signal(SIGALRM, alarmhandler);
				alarm(1);
			}

			if (sigopen) {

				if (trace) fprintf(stdout, "signal: %d %s", sigopen, ctime(&t));

        s = ICdrop(ic);
			  assert(s == IC_OK);

        ic = ICcreate(FILES, sizeof(struct m), CACHE_SIZE, PATH);
			  assert(ic);

				sigopen = 0;
				signal(SIGUSR1, openhandler);
			}
		} else {
			break; /* while (1) */
		}
	}

  s = ICdrop(ic);
	assert(s == IC_OK);

	return 0;
}