blob: db2d43be101c1da00c97ec67ad2d13be46e8aa38 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include <cabinet/magic.hpp>
#include <fcntl.h>
#include <cstdlib>
#include <iostream>
#include <print>
using flags = cab::magic::flags;
auto main(int argc, char ** argv) -> int
{
if (argc != 2)
{
std::println(std::cerr, "Expected a filename");
return EXIT_FAILURE;
}
auto magic = cab::magic::open(flags::follow_symlinks | flags::mime_type | flags::forward_errors);
if (!magic)
{
std::println(std::cerr, "Failed to initialize libmagic: {}", magic.error().message());
return EXIT_FAILURE;
}
auto fd = open(argv[1], O_RDONLY);
auto mime_type = magic->process(fd);
if (!mime_type)
{
std::println(std::cerr, "Failed to determine file type: {}", mime_type.error());
return EXIT_FAILURE;
}
std::println("{}: {}", argv[1], mime_type.value());
}
|