Exemplo n.º 1
0
/**
 * main execution loop
 */
int main(int argc, char **argv) {
    char *buf = NULL;               /* Input buffer */
    int lineLen;                    /* Length of buffer */
    char **bufargs;                 /* malloc, may cause segfaults */
    struct sigaction sa;
    int quit = FALSE;

    /* set up fg and bg cmds */
    globalCmd[FG] = NULL;
    globalCmd[BG] = NULL;

    /* Handle SIGINT signals */
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = sigint_recieved;
    sa.sa_flags = (int) 0x10000000;    /* SA_RESTART, not available in ANSI */
    sigaction(SIGINT, &sa, 0);

    /* Handle script argument */
    handle_args(argc, argv);

    while (!quit) {

        /* print prompt */
        reap_command(&globalCmd[BG]);
        print_prompt();

        /* get line */
        lineLen = get_line(STDIN_FILENO, &buf);

        /* Check if error in reading line */
        if (lineLen < 0) {
            printf("error reading line\r\n");
            exit(ERR_READ);
        }

        /* Check if EOF (no chars in buffer) */
        if (lineLen == 0) {
            free(buf);
            break;
        }

        /* if nothing as read or it's a comment, do nothing */
        if (lineLen == 1 || buf[0] == '#') {
            free(buf);
            continue;
        }

        /* Split the string into args */
        bufargs = split_string(buf);

        /* Process the args */
        quit = process_buf(bufargs);

        /* free memory */
        free(bufargs);
        free(buf);
    }

    return (ERR_EOF);
}
Exemplo n.º 2
0
static int		read_file(int const fd, char **line, char **test, int c)
{
	int		byte_read;
	char	buf[BUFF_SIZE + 1];
	int		char_left;

	if ((char_left = 0) == 0 && process_buf(&char_left, test, &c, line))
		return (1);
	while ((byte_read = read(fd, buf, BUFF_SIZE)))
	{
		if ((buf[byte_read] = 0) == 0 && byte_read == -1)
			return (-1);
		char_left = process_line(byte_read, buf, line, c);
		c += byte_read;
		if (char_left > 0)
		{
			if (!(*test))
				ft_memdel((void **)test);
			(*test) = ft_strnew(char_left);
			ft_strncpy((*test), buf + (byte_read - char_left), char_left);
		}
		else if (char_left == 0)
			ft_memdel((void **)test);
		if (char_left != -1)
			return (1);
	}
	return (0);
}
static int process_client(int fd)
{
    int done = 0;
    size_t total_size = 0;
    char buf[512];

    while (1) {
        ssize_t count;

        count = read(fd, buf, sizeof(buf));
        if (count < 0) {
            if (errno != EAGAIN) {
                ALOGW("read errno=%d", errno);
                done = 1;
            }
            return done;
        } else if (count == 0) {
            done = 1;
            return done;
        }

        total_size += count;
        if (total_size >= 3) {
            process_buf(buf, total_size);
            done = 1;
            return done;
        }
    }
}
Exemplo n.º 4
0
bool Ublox::encode(char c)
{
    buf[pos] = c;
    pos++;

    if(c == '\n') //linefeed
    {
        bool ret = process_buf();
        memset(buf, '\0', 120);
        pos = 0;
        return ret;
    }

    if(pos >= 120) //avoid a buffer overrun
    {
        memset(buf, '\0', 120);
        pos = 0;
    }
    return false;
}
Exemplo n.º 5
0
void tint2env_read_socket(void)
{
	int client_fd;
	ssize_t num_read;
	char buf[SOCKET_BUF_SIZE];

	/* sfd is non-blocking */
	client_fd = accept(sfd, NULL, NULL);
	if (client_fd == -1) {
		fprintf(stderr, "info: tint2 button was not used\n");
		return;
	}
	while ((num_read = read(client_fd, buf, SOCKET_BUF_SIZE)) > 0) {
		buf[num_read] = '\0';
		process_buf(buf);
	}
	if (num_read == -1)
		die("read");
	if (close(client_fd) == -1)
		warn("close");
}