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