Exemple #1
0
void
tail_reading_all(int N, FILE *stream)
{
  Line **line, *l;
  int i, last, first;

  if (!N)
    return;

  line = malloc(N * sizeof(Line *));

  if (!line)
  {
    fprintf(stderr, "Error in tail_reading_all\n");
    exit(EXIT_FAILURE);
  }

  for (i = 0; i < N; ++i)
    line[i] = NULL;

  last = 0;

  while ((l = io_readline(stream)))
  {
    if (line[last])
      line_free(line[last]); /* we could just reuse old buffers */

    line[last] = l;
    last = (last + 1) % N;
  }

  last = (N + last - 1) % N;

  first = (last + 1) % N;

  if (!line[first])
    first = 0;

  for (i = first; i != last; i = (i + 1) % N)
  {
    fwrite(line[i]->buf, 1, line[i]->size, stdout);
    fprintf(stdout, "\n");
  }

  if (N && line[last])
  {
    fwrite(line[last]->buf, 1, line[last]->size, stdout);
    fprintf(stdout, "\n");
  }

  for (i = 0; i < N; i++)
  {
    if (line[i])
      line_free(line[i]);
    else
      break;
  }

  free(line);
}
Exemple #2
0
void
block_update(struct block *block)
{
	struct properties *props = &block->updated_props;
	char buf[2048] = { 0 };
	int nr;

	/* Read a single line for persistent block, everything otherwise */
	if (block->interval == INTER_PERSIST) {
		nr = io_readline(block->out, buf, sizeof(buf));
		if (nr < 0) {
			berror(block, "failed to read a line");
			return mark_as_failed(block, "failed to read");
		} else if (nr == 0) {
			berror(block, "pipe closed");
			return mark_as_failed(block, "pipe closed");
		}
	} else {
		/* Note: read(2) returns 0 for end-of-pipe */
		if (read(block->out, buf, sizeof(buf) - 1) == -1) {
			berrorx(block, "read stdout");
			return mark_as_failed(block, strerror(errno));
		}
	}

	/* Reset the defaults and merge the output */
	memcpy(props, &block->default_props, sizeof(struct properties));

	if (block->format == FORMAT_JSON)
		block_update_json(block, buf);
	else
		block_update_plain_text(block, buf);

	if (*FULL_TEXT(block) && *LABEL(block)) {
		static const size_t size = sizeof(props->full_text);
		char concat[size];
		snprintf(concat, size, "%s %s", LABEL(block), FULL_TEXT(block));
		strcpy(props->full_text, concat);
	}

	bdebug(block, "updated successfully");
}
Exemple #3
0
io_http_res* http_get(const char* url, int timeout) {
  io_http_req req;
  io_http_res* res = malloc(sizeof(io_http_res));
  parse_url(url, &req);
  printf("http.host = %s, http.port = %d, http.path = %s, http.query = %s\n",
      req.host, req.port, req.path, req.query_string);

  int sockfd = io_tcp_connect(req.host, req.port);
  if (sockfd < 0) return NULL;

  // request buff
  char buf[MAX_IO_BUFFER];
  // poll
  struct pollfd pfds[1];
  int maxpfds = 1, nready = 0, n;

  pfds[0].fd = sockfd;
  pfds[0].events = POLLWRNORM;

  for (;;) {
    bzero(buf, sizeof(buf));
    nready = poll(pfds, maxpfds, timeout);
    if (nready < 0) {
      fprintf(stderr, "%s:%d http poll error\n", __FILE__, __LINE__);
      close(sockfd);
      return NULL;
    } else if (nready == 0) {
      fprintf(stderr, "%s:%d http poll timeout\n", __FILE__, __LINE__);
      close(sockfd);
      return NULL;
    }

    if (pfds[0].revents & POLLWRNORM) {
      // write
      // printf("sizeof char[] = %ld\n", sizeof(buf));
      if (req.query_string[0] == '\0')
        snprintf(buf, sizeof(buf), "GET %s HTTP/1.1\r\n", req.path);
      else
        snprintf(buf, sizeof(buf), "GET %s?%s HTTP/1.1\r\n", req.path, req.query_string);

      snprintf(buf + strlen(buf), sizeof(buf), "host: %s\r\n\r\n", req.host);
      printf("request:\n%s\n", buf);
      if (io_writen(sockfd, buf, strlen(buf)) < 0) {
        fprintf(stderr, "%s:%d http send request error: %s\n", __FILE__, __LINE__, buf);
        close(sockfd);
        return NULL;
      }
      pfds[0].events = POLLRDNORM;
    }

    if (pfds[0].revents & POLLRDNORM) {
      // read
      if (io_readline(sockfd, buf, sizeof(buf)) < 0) {
        fprintf(stdout, "%s:%d http recv message error\n", __FILE__, __LINE__);
        close(sockfd);
        return NULL;
      }
      printf("response status line:\n%s\n", buf);
      sscanf(buf, "%s %d %s", res->version, &res->status_code, res->status_message);

      // io_readn(sockfd, buf, 1024);
      // printf("%s\n", buf);
      if (res->status_code == 200) {
        res->content_length = 0;
        while (buf[0] != '\r' && buf[1] != '\n') {
          io_readline(sockfd, buf, sizeof(buf));
          fprintf(stdout, "%s\n", buf);

          // Content-Length
          if (14 == start_with(buf, "Content-Length")) {
            res->content_length = atoi(buf + 15);
            res->content = malloc(res->content_length);
          }
        }
        if (res->content_length <= 0) {
          res->content_length = 8192;
          res->content = malloc(res->content_length);
        }
        n = io_readn(sockfd, res->content, res->content_length);
        res->content[n] = '\0';
        close(sockfd);
        return res;
      }
      break;
    }
  }
  close(sockfd);
  return NULL;
}
/**
 * Get info from a "url" file -- a media stream file.
 * This should really get more metainfo, but I'll leave that
 * to later.
 *
 * @param filename .url file to process
 * @param pmp3 MP3FILE structure that must be filled
 * @returns TRUE if file should be added to db, FALSE otherwise
 */
int scan_get_urlinfo(char *filename, MP3FILE *pmp3) {
    IOHANDLE hfile;
    char *head, *tail;
    char linebuffer[256];
    uint32_t len;

    DPRINTF(E_DBG,L_SCAN,"Getting URL file info\n");

    if(!(hfile = io_new())) {
        DPRINTF(E_LOG,L_SCAN,"Can't create file handle\n");
        return FALSE;
    }
    
    if(!io_open(hfile,"file://%U?ascii=1",filename)) {
        DPRINTF(E_WARN,L_SCAN,"Could not open %s for reading: %s\n",filename,
            io_errstr(hfile));
        io_dispose(hfile);
        return FALSE;
    }

    io_buffer(hfile);
    len = sizeof(linebuffer);
    if(!io_readline(hfile,(unsigned char *)linebuffer,&len)) {
        DPRINTF(E_WARN,L_SCAN,"Error reading from file %s: %s",filename,io_errstr(hfile));
        io_close(hfile);
        io_dispose(hfile);
        return FALSE;
    }

    while((linebuffer[strlen(linebuffer)-1] == '\n') ||
          (linebuffer[strlen(linebuffer)-1] == '\r')) {
        linebuffer[strlen(linebuffer)-1] = '\0';
    }

    head=linebuffer;
    tail=strchr(head,',');
    if(!tail) {
        DPRINTF(E_LOG,L_SCAN,"Badly formatted .url file - must be bitrate,descr,url\n");
        io_close(hfile);
        io_dispose(hfile);
        return FALSE;
    }

    pmp3->bitrate=atoi(head);
    head=++tail;
    tail=strchr(head,',');
    if(!tail) {
        DPRINTF(E_LOG,L_SCAN,"Badly formatted .url file - must be bitrate,descr,url\n");
        io_close(hfile);
        io_dispose(hfile);
        return FALSE;
    }

    *tail++='\0';

    pmp3->title=strdup(head);
    pmp3->url=strdup(tail);
    
    io_close(hfile);
    io_dispose(hfile);

    DPRINTF(E_DBG,L_SCAN,"  Title:    %s\n",pmp3->title);
    DPRINTF(E_DBG,L_SCAN,"  Bitrate:  %d\n",pmp3->bitrate);
    DPRINTF(E_DBG,L_SCAN,"  URL:      %s\n",pmp3->url);

    return TRUE;
}