now depending on libmagic
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
CC := gcc
|
||||
CFLAGS := -Wall -Wextra -O3 -flto=auto
|
||||
CFLAGS := -Wall -Wextra -O3 -flto=auto -lmagic
|
||||
CURSES := $(shell pkg-config --libs ncursesw)
|
||||
CFLAGS_DEBUG := $(CFLAGS) -ggdb
|
||||
CFLAGS_PROFILE := $(CFLAGS) -pg
|
||||
|
||||
+29
-40
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <magic.h>
|
||||
|
||||
#include "backend.h"
|
||||
#include "defines.h"
|
||||
@@ -12,6 +13,8 @@ static FILE *ueberzug = NULL;
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
char previewd;
|
||||
magic_t cookie_type;
|
||||
magic_t cookie_description;
|
||||
|
||||
char* text(file *f);
|
||||
void images_print(char *file_name);
|
||||
@@ -19,24 +22,11 @@ void images_clear();
|
||||
char* generic(file *f);
|
||||
|
||||
char* get_mimetype(file *f){
|
||||
static const char *cmd_str = "file --mime-type -b";
|
||||
char *cmd = parse_cmd(cmd_str, f);
|
||||
char *mime = (char*)magic_file(cookie_type, f->file_name);
|
||||
|
||||
FILE *cmd_open = popen(cmd, "r");
|
||||
char *line = NULL;
|
||||
size_t size = 0;
|
||||
|
||||
free(cmd);
|
||||
if (getline(&line, &size, cmd_open) != -1){
|
||||
pclose(cmd_open);
|
||||
return line;
|
||||
} else {
|
||||
pclose(cmd_open);
|
||||
return "unknown";
|
||||
}
|
||||
return mime;
|
||||
}
|
||||
char* preview_file(file *f){
|
||||
/* this calls "file" on path */
|
||||
|
||||
char *file_buffer;
|
||||
|
||||
@@ -46,19 +36,23 @@ char* preview_file(file *f){
|
||||
images_clear();
|
||||
#endif
|
||||
|
||||
if (strstr(mime, "text")) {
|
||||
file_buffer = text(f);
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
} else if (strstr(mime, "image")) {
|
||||
file_buffer = generic(f);
|
||||
images_print(f->file_name);
|
||||
previewd = 1;
|
||||
#endif
|
||||
} else {
|
||||
if (mime != NULL) {
|
||||
if (strstr(mime, "text")) {
|
||||
file_buffer = text(f);
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
} else if (strstr(mime, "image")) {
|
||||
file_buffer = generic(f);
|
||||
images_print(f->file_name);
|
||||
previewd = 1;
|
||||
#endif
|
||||
} else {
|
||||
file_buffer = generic(f);
|
||||
}
|
||||
} else {
|
||||
file_buffer = generic(f);
|
||||
}
|
||||
|
||||
free(mime);
|
||||
/*free(mime); seems like magic_file handles the free too*/
|
||||
return file_buffer;
|
||||
|
||||
}
|
||||
@@ -80,21 +74,11 @@ char* text(file *f){
|
||||
}
|
||||
}
|
||||
char* generic(file *f){
|
||||
static const char *cmd_str = "file";
|
||||
char *cmd = parse_cmd(cmd_str, f);
|
||||
char *mime = (char*)magic_file(cookie_description, f->file_name);
|
||||
char *buffer = malloc(strlen(mime)+1);
|
||||
memcpy(buffer, mime, strlen(mime)+1);
|
||||
return buffer;
|
||||
|
||||
FILE *cmd_open = popen(cmd, "r");
|
||||
char *line = NULL;
|
||||
size_t size = 0;
|
||||
|
||||
free(cmd);
|
||||
if (getline(&line, &size, cmd_open) != -1) {
|
||||
pclose(cmd_open);
|
||||
return line;
|
||||
} else {
|
||||
pclose(cmd_open);
|
||||
return "failed executing shell command \"file\"";
|
||||
}
|
||||
}
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
void images_clear(){
|
||||
@@ -119,13 +103,18 @@ void images_print(char *file_name){
|
||||
free(path);
|
||||
}
|
||||
void ueberzug_init(){
|
||||
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW == 2
|
||||
ueberzug = popen("ueberzug layer -s ", "w");
|
||||
#elif SETTINGS_UEBERZUG_IMAGE_PREVIEW == 1
|
||||
ueberzug = popen("ueberzug layer -s --no-cache ", "w");
|
||||
#endif
|
||||
}
|
||||
void file_preview_init(){
|
||||
cookie_type = magic_open(MAGIC_MIME_TYPE);
|
||||
cookie_description = magic_open(MAGIC_NONE);
|
||||
magic_load(cookie_type, NULL);
|
||||
magic_load(cookie_description, NULL);
|
||||
}
|
||||
void ueberzug_close(){
|
||||
images_clear();
|
||||
pclose(ueberzug);
|
||||
|
||||
+2
-1
@@ -7,7 +7,8 @@
|
||||
char* preview_file(file *f);
|
||||
char* get_mimetype(file *f);
|
||||
void images_clear();
|
||||
void ueberzug_init();
|
||||
void file_preview_init();
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
void ueberzug_init();
|
||||
void ueberzug_close();
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user