Skip to content

Commit 8dcc0a8

Browse files
author
Martin D. Weinberg
committed
Clean up the unique_ptr using a custom deleter
1 parent 6acbe73 commit 8dcc0a8

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

exputil/getmd5sum.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@
55
#include <memory>
66
#include <algorithm>
77

8+
// Define the custom deleter
9+
struct PcloseDeleter {
10+
void operator()(FILE* f) const {
11+
if (f) pclose(f);
12+
}
13+
};
14+
815
std::string get_md5sum(const std::string& filename)
916
{
17+
// Check if md5sum is available on the system
18+
if (std::system("which md5sum > /dev/null 2>&1") != 0) {
19+
throw std::runtime_error("md5sum command not found. Please ensure it is installed and in your PATH.");
20+
}
21+
1022
// Command to execute: md5sum <filename>
1123
std::string command = "md5sum " + filename;
1224
std::array<char, 128> buffer;
1325
std::string result = "";
1426

1527
// Use popen to execute the command and read its output
1628
// "r" mode opens the pipe for reading
17-
std::unique_ptr<FILE, decltype(&pclose)>
18-
pipe(popen(command.c_str(), "r"), pclose);
29+
std::unique_ptr<FILE, PcloseDeleter> pipe(popen(command.c_str(), "r"));
1930
if (!pipe) {
2031
throw std::runtime_error("popen() failed!");
2132
}

0 commit comments

Comments
 (0)