}
/**
- * Read a buffer and check its content for a pattern.
+ * Read a buffer and compare its contents to a string, ignoring case.
*
- * \param fd The file descriptor to receive from.
- * \param pattern The expected pattern.
- * \param bufsize The size of the internal buffer.
- *
- * This function tries to read at most \a bufsize bytes from the non-blocking
- * file descriptor \a fd. If at least \p strlen(\a pattern) bytes have been
- * received, the beginning of the received buffer is compared with \a pattern,
- * ignoring case.
+ * \param fd The file descriptor to read from.
+ * \param expectation The expected string to compare to.
*
- * \return Positive if \a pattern was received, negative on errors, zero if no data
- * was available to read.
+ * The given file descriptor is expected to be in non-blocking mode. The string
+ * comparison is performed using strncasecmp(3).
*
- * \sa \ref read_nonblock(), \sa strncasecmp(3).
+ * \return Zero if no data was available, positive if a buffer was read whose
+ * contents compare as equal to the expected string, negative otherwise.
+ * Possible errors: (a) not enough data was read, (b) the buffer contents
+ * compared as non-equal, (c) a read error occurred. In the first two cases,
+ * -E_READ_PATTERN is returned. In the read error case the (negative) return
+ * value of the underlying call to \ref read_nonblock() is returned.
*/
-int read_pattern(int fd, const char *pattern, size_t bufsize)
+int read_and_compare(int fd, const char *expectation)
{
- size_t n, len;
- char *buf = alloc(bufsize + 1);
- int ret = read_nonblock(fd, buf, bufsize, &n);
+ size_t n, len = strlen(expectation);
+ char *buf = alloc(len + 1);
+ int ret = read_nonblock(fd, buf, len, &n);
- buf[n] = '\0';
if (ret < 0)
goto out;
+ buf[n] = '\0';
ret = 0;
if (n == 0)
goto out;
ret = -E_READ_PATTERN;
- len = strlen(pattern);
if (n < len)
goto out;
- if (strncasecmp(buf, pattern, len) != 0)
+ if (strncasecmp(buf, expectation, len) != 0)
goto out;
ret = 1;
out:
- if (ret < 0) {
- PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
- PARA_NOTICE_LOG("recvd %zu bytes: %s\n", n, buf);
- }
free(buf);
return ret;
}
void valid_fd_012(void);
int readv_nonblock(int fd, struct iovec *iov, int iovcnt, size_t *num_bytes);
int read_nonblock(int fd, void *buf, size_t sz, size_t *num_bytes);
-int read_pattern(int fd, const char *pattern, size_t bufsize);
+int read_and_compare(int fd, const char *expectation);
int xwrite(int fd, const char *buf, size_t len);
int xwritev(int fd, struct iovec *iov, int iovcnt);
int for_each_file_in_dir(const char *dirname,