clang format
This commit is contained in:
parent
443ce0501f
commit
4574d13e55
1 changed files with 77 additions and 83 deletions
|
@ -2,18 +2,17 @@
|
|||
// The Sorcerer’s Apprentice
|
||||
//
|
||||
// To use this program, compile it with dynamically linked libmagic, as mirrored
|
||||
// at https://github.com/threatstack/libmagic. You may install it with apt-get, yum or brew.
|
||||
// Refer to the Makefile for further reference.
|
||||
// at https://github.com/threatstack/libmagic. You may install it with apt-get,
|
||||
// yum or brew. Refer to the Makefile for further reference.
|
||||
//
|
||||
// This program is designed to run interactively as a backend daemon to the GenMagic library,
|
||||
// and follows the command line pattern:
|
||||
// This program is designed to run interactively as a backend daemon to the
|
||||
// GenMagic library, and follows the command line pattern:
|
||||
//
|
||||
// $ apprentice --database-file <file> --database-default
|
||||
//
|
||||
// Where each argument either refers to a compiled or uncompiled magic database, or the default
|
||||
// database. They will be loaded in the sequence that they were specified. Note that you must
|
||||
// specify at least one database.
|
||||
// Erlang Term
|
||||
// Where each argument either refers to a compiled or uncompiled magic database,
|
||||
// or the default database. They will be loaded in the sequence that they were
|
||||
// specified. Note that you must specify at least one database. Erlang Term
|
||||
//
|
||||
// -- main: send atom ready
|
||||
// enter loop
|
||||
|
@ -25,18 +24,18 @@
|
|||
// error: {:error, :badarg} | {:error, {errno, String.t()}}
|
||||
// {:stop, _} -> exit(ERROR_OK) -> exit 0
|
||||
//
|
||||
#include <ei.h>
|
||||
#include <errno.h>
|
||||
#include <getopt.h>
|
||||
#include <libgen.h>
|
||||
#include <magic.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <ei.h>
|
||||
#include <magic.h>
|
||||
#define USAGE "[--database-file <path/to/magic.mgc> | --database-default, ...]"
|
||||
#define DELIMITER "\t"
|
||||
|
||||
|
@ -89,7 +88,8 @@ int main (int argc, char **argv) {
|
|||
setup_system();
|
||||
|
||||
ei_x_buff ok_buf;
|
||||
if (ei_x_new_with_version(&ok_buf) || ei_x_encode_atom(&ok_buf, "ready")) return 5;
|
||||
if (ei_x_new_with_version(&ok_buf) || ei_x_encode_atom(&ok_buf, "ready"))
|
||||
return 5;
|
||||
write_cmd(ok_buf.buff, ok_buf.index);
|
||||
if (ei_x_free(&ok_buf) != 0)
|
||||
exit(ERROR_EI);
|
||||
|
@ -112,7 +112,8 @@ int process_command(byte *buf) {
|
|||
exit(ERROR_BAD_TERM);
|
||||
|
||||
// Initialize result
|
||||
if (ei_x_new_with_version(&result) || ei_x_encode_tuple_header(&result, 2)) exit(ERROR_EI);
|
||||
if (ei_x_new_with_version(&result) || ei_x_encode_tuple_header(&result, 2))
|
||||
exit(ERROR_EI);
|
||||
|
||||
if (ei_decode_tuple_header(buf, &index, &arity) != 0) {
|
||||
error(&result, "badarg");
|
||||
|
@ -161,22 +162,20 @@ int process_command(byte *buf) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void setup_environment() {
|
||||
opterr = 0;
|
||||
}
|
||||
void setup_environment() { opterr = 0; }
|
||||
|
||||
void setup_options(int argc, char **argv) {
|
||||
const char *option_string = "f:";
|
||||
static struct option long_options[] = {
|
||||
{"database-file", required_argument, 0, 'f'},
|
||||
{"database-default", no_argument, 0, 'd'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
{0, 0, 0, 0}};
|
||||
|
||||
int option_character;
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
option_character = getopt_long(argc, argv, option_string, long_options, &option_index);
|
||||
option_character =
|
||||
getopt_long(argc, argv, option_string, long_options, &option_index);
|
||||
if (-1 == option_character) {
|
||||
break;
|
||||
}
|
||||
|
@ -245,9 +244,6 @@ magic_t magic_setup(int flags) {
|
|||
}
|
||||
while (current_database) {
|
||||
magic_load(magic, current_database->path);
|
||||
if (isatty(STDERR_FILENO)) {
|
||||
fprintf(stderr, ANSI_RESET);
|
||||
}
|
||||
current_database = current_database->next;
|
||||
}
|
||||
return magic;
|
||||
|
@ -276,7 +272,8 @@ void process_file(char *path, ei_x_buff *result) {
|
|||
ei_x_encode_tuple_header(result, 2);
|
||||
long errlon = (long)mime_encoding_errno;
|
||||
ei_x_encode_long(result, errlon);
|
||||
ei_x_encode_binary(result, mime_encoding_error, strlen(mime_encoding_error));
|
||||
ei_x_encode_binary(result, mime_encoding_error,
|
||||
strlen(mime_encoding_error));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -296,14 +293,14 @@ void process_file(char *path, ei_x_buff *result) {
|
|||
ei_x_encode_atom(result, "ok");
|
||||
ei_x_encode_tuple_header(result, 3);
|
||||
ei_x_encode_binary(result, mime_type_result, strlen(mime_type_result));
|
||||
ei_x_encode_binary(result, mime_encoding_result, strlen(mime_encoding_result));
|
||||
ei_x_encode_binary(result, mime_encoding_result,
|
||||
strlen(mime_encoding_result));
|
||||
ei_x_encode_binary(result, type_name_result, strlen(type_name_result));
|
||||
return;
|
||||
}
|
||||
|
||||
// From https://erlang.org/doc/tutorial/erl_interface.html
|
||||
int read_exact(byte *buf, int len)
|
||||
{
|
||||
int read_exact(byte *buf, int len) {
|
||||
int i, got = 0;
|
||||
|
||||
do {
|
||||
|
@ -316,8 +313,7 @@ int read_exact(byte *buf, int len)
|
|||
return (len);
|
||||
}
|
||||
|
||||
int write_exact(byte *buf, int len)
|
||||
{
|
||||
int write_exact(byte *buf, int len) {
|
||||
int i, wrote = 0;
|
||||
|
||||
do {
|
||||
|
@ -329,8 +325,7 @@ int write_exact(byte *buf, int len)
|
|||
return (len);
|
||||
}
|
||||
|
||||
int read_cmd(byte *buf)
|
||||
{
|
||||
int read_cmd(byte *buf) {
|
||||
int len;
|
||||
|
||||
if (read_exact(buf, 2) != 2)
|
||||
|
@ -339,8 +334,7 @@ int read_cmd(byte *buf)
|
|||
return read_exact(buf, len);
|
||||
}
|
||||
|
||||
int write_cmd(byte *buf, int len)
|
||||
{
|
||||
int write_cmd(byte *buf, int len) {
|
||||
byte li;
|
||||
|
||||
li = (len >> 8) & 0xff;
|
||||
|
|
Loading…
Reference in a new issue