xref: /freebsd/contrib/atf/atf-c++/utils.cpp (revision 61e21613)
1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 // 1. Redistributions of source code must retain the above copyright
8 //    notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 //    notice, this list of conditions and the following disclaimer in the
11 //    documentation and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "atf-c++/utils.hpp"
27 
28 extern "C" {
29 #include "atf-c/utils.h"
30 }
31 
32 #include <cstdlib>
33 #include <iostream>
34 
35 void
36 atf::utils::cat_file(const std::string& path, const std::string& prefix)
37 {
38     atf_utils_cat_file(path.c_str(), prefix.c_str());
39 }
40 
41 void
42 atf::utils::copy_file(const std::string& source, const std::string& destination)
43 {
44     atf_utils_copy_file(source.c_str(), destination.c_str());
45 }
46 
47 bool
48 atf::utils::compare_file(const std::string& path, const std::string& contents)
49 {
50     return atf_utils_compare_file(path.c_str(), contents.c_str());
51 }
52 
53 void
54 atf::utils::create_file(const std::string& path, const std::string& contents)
55 {
56     atf_utils_create_file(path.c_str(), "%s", contents.c_str());
57 }
58 
59 bool
60 atf::utils::file_exists(const std::string& path)
61 {
62     return atf_utils_file_exists(path.c_str());
63 }
64 
65 pid_t
66 atf::utils::fork(void)
67 {
68     std::cout.flush();
69     std::cerr.flush();
70     return atf_utils_fork();
71 }
72 
73 void
74 atf::utils::reset_resultsfile(void)
75 {
76 
77     atf_utils_reset_resultsfile();
78 }
79 
80 bool
81 atf::utils::grep_file(const std::string& regex, const std::string& path)
82 {
83     return atf_utils_grep_file("%s", path.c_str(), regex.c_str());
84 }
85 
86 bool
87 atf::utils::grep_string(const std::string& regex, const std::string& str)
88 {
89     return atf_utils_grep_string("%s", str.c_str(), regex.c_str());
90 }
91 
92 void
93 atf::utils::redirect(const int fd, const std::string& path)
94 {
95     if (fd == STDOUT_FILENO)
96         std::cout.flush();
97     else if (fd == STDERR_FILENO)
98         std::cerr.flush();
99     atf_utils_redirect(fd, path.c_str());
100 }
101 
102 void
103 atf::utils::wait(const pid_t pid, const int exitstatus,
104                  const std::string& expout, const std::string& experr)
105 {
106     atf_utils_wait(pid, exitstatus, expout.c_str(), experr.c_str());
107 }
108