-/**
- * Ensure that file descriptors 0, 1, and 2 are valid.
- *
- * Common approach that opens /dev/null until it gets a file descriptor greater
- * than two.
- *
- * \sa okir's Black Hats Manual.
- */
-void valid_fd_012(void)
-{
- while (1) {
- int fd = open("/dev/null", O_RDWR);
- if (fd < 0)
- exit(EXIT_FAILURE);
- if (fd > 2) {
- close(fd);
- break;
- }
- }
-}
-
-/**
- * Get the own hostname.
- *
- * \return A dynammically allocated string containing the hostname.
- *
- * \sa uname(2).
- */
-__malloc char *para_hostname(void)
-{
- struct utsname u;
-
- uname(&u);
- return para_strdup(u.nodename);
-}
-
-/**
- * Used to distinguish between read-only and read-write mode.
- *
- * \sa for_each_line(), for_each_line_ro().
- */
-enum for_each_line_modes{
- /** Activate read-only mode. */
- LINE_MODE_RO,
- /** Activate read-write mode. */
- LINE_MODE_RW
-};
-
-static int for_each_complete_line(enum for_each_line_modes mode, char *buf,
- size_t size, line_handler_t *line_handler, void *private_data)
-{
- char *start = buf, *end;
- int ret, i, num_lines = 0;
-
-// PARA_NOTICE_LOG("buf: %s\n", buf);
- while (start < buf + size) {
- char *next_null;
- char *next_cr;
-
- next_cr = memchr(start, '\n', buf + size - start);
- next_null = memchr(start, '\0', buf + size - start);
- if (!next_cr && !next_null)
- break;
- if (next_cr && next_null) {
- end = next_cr < next_null? next_cr : next_null;
- } else if (next_null) {
- end = next_null;
- } else
- end = next_cr;
- num_lines++;
- if (!line_handler) {
- start = ++end;
- continue;
- }
- if (mode == LINE_MODE_RO) {
- size_t s = end - start;
- char *b = para_malloc(s + 1);
- memcpy(b, start, s);
- b[s] = '\0';
-// PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
- ret = line_handler(b, private_data);
- free(b);
- } else {
- *end = '\0';
- ret = line_handler(start, private_data);
- }
- if (ret < 0)
- return ret;
- start = ++end;
- }
- if (!line_handler || mode == LINE_MODE_RO)
- return num_lines;
- i = buf + size - start;
- if (i && i != size)
- memmove(buf, start, i);
- return i;
-}
-
-/**
- * Call a custom function for each complete line.
- *
- * \param buf The buffer containing data seperated by newlines.
- * \param size The number of bytes in \a buf.
- * \param line_handler The custom function.
- * \param private_data Pointer passed to \a line_handler.
- *
- * If \p line_handler is \p NULL, the function returns the number of complete
- * lines in \p buf. Otherwise, \p line_handler is called for each complete
- * line in \p buf. The first argument to \p line_handler is the current line,
- * and \p private_data is passed as the second argument. The function returns
- * if \p line_handler returns a negative value or no more lines are in the
- * buffer. The rest of the buffer (last chunk containing an incomplete line)
- * is moved to the beginning of the buffer.
- *
- * \return If \p line_handler is not \p NULL, this function returns the number
- * of bytes not handled to \p line_handler on success, or the negative return
- * value of the \p line_handler on errors.
- *
- * \sa for_each_line_ro().
- */
-int for_each_line(char *buf, size_t size, line_handler_t *line_handler,
- void *private_data)