Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / io.c
blobf4aa8785fb3c667641cdd48f3b3e665738f45b24
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "atf-c/dynstr.h"
40 #include "atf-c/error.h"
41 #include "atf-c/fs.h"
42 #include "atf-c/io.h"
43 #include "atf-c/sanity.h"
45 atf_error_t
46 atf_io_readline(int fd, atf_dynstr_t *dest, bool *eof)
48 char ch;
49 ssize_t cnt;
50 atf_error_t err;
52 while ((cnt = read(fd, &ch, sizeof(ch))) == sizeof(ch) &&
53 ch != '\n') {
54 err = atf_dynstr_append_fmt(dest, "%c", ch);
55 if (atf_is_error(err))
56 goto out;
59 if (cnt == -1) {
60 err = atf_libc_error(errno, "Failed to read line from file "
61 "descriptor %d", fd);
62 goto out;
65 *eof = (cnt == 0);
66 err = atf_no_error();
68 out:
69 return err;
72 atf_error_t
73 atf_io_write_ap(int fd, const char *fmt, va_list ap)
75 atf_error_t err;
76 atf_dynstr_t str;
78 err = atf_dynstr_init_ap(&str, fmt, ap);
79 if (!atf_is_error(err)) {
80 ssize_t cnt = write(fd, atf_dynstr_cstring(&str),
81 atf_dynstr_length(&str));
83 if (cnt == -1)
84 err = atf_libc_error(errno, "Failed to write '%s' to file "
85 "descriptor %d", atf_dynstr_cstring(&str),
86 fd);
87 else {
88 INV(cnt >= 0);
89 INV((size_t)cnt == atf_dynstr_length(&str));
90 err = atf_no_error();
93 atf_dynstr_fini(&str);
96 return err;
99 atf_error_t
100 atf_io_write_fmt(int fd, const char *fmt, ...)
102 atf_error_t err;
103 va_list ap;
105 va_start(ap, fmt);
106 err = atf_io_write_ap(fd, fmt, ap);
107 va_end(ap);
109 return err;
112 atf_error_t
113 atf_io_cmp(int *result, const atf_fs_path_t *p1, const atf_fs_path_t *p2)
115 int fd1, fd2, res;
116 const char *path1, *path2;
117 atf_error_t err;
119 err = atf_no_error();
120 res = 1;
121 path1 = atf_fs_path_cstring(p1);
122 path2 = atf_fs_path_cstring(p2);
124 fd1 = open(path1, O_RDONLY);
125 if (fd1 == -1) {
126 err = atf_libc_error(errno, "Cannot open file: %s", path1);
127 goto out_fd1;
130 fd2 = open(path2, O_RDONLY);
131 if (fd2 == -1) {
132 err = atf_libc_error(errno, "Cannot open file: %s", path2);
133 goto out_fd2;
136 for (;;) {
137 ssize_t r1, r2;
138 char buf1[512], buf2[512];
140 r1 = read(fd1, buf1, sizeof(buf1));
141 if (r1 < 0) {
142 err = atf_libc_error(errno, "Cannot read file: %s", path1);
143 break;
146 r2 = read(fd2, buf2, sizeof(buf2));
147 if (r2 < 0) {
148 err = atf_libc_error(errno, "Cannot read file: %s", path2);
149 break;
152 if ((r1 == 0) && (r2 == 0)) {
153 res = 0;
154 break;
157 if ((r1 != r2) || (memcmp(buf1, buf2, r1) != 0)) {
158 break;
162 close(fd2);
164 out_fd2:
165 close(fd1);
167 out_fd1:
169 if (err == atf_no_error())
170 *result = res;
172 return err;