libmagic,magic_descriptor始终返回null

如何解决libmagic,magic_descriptor始终返回null

在Centos 8上运行。

完整代码如下。这是编译和执行。嗯,是的,我以root用户身份运行,所以权限应该没有问题。

我只想获得与从“文件”命令获得的输出相同的输出,并且该代码基于来自文件5.33源代码rpm的源代码。哪个应该是最新的和最大的。

[root@ws ~]# gcc a3magic.c -lmagic
[root@ws ~]# ./a.out 
set fd = 0
before 'open' errno = 0
fd = 3
fd errno = 0
    Success
magicfile = /etc/magic:/usr/share/misc/magic
after getpath errno = 2
after load errno = 2
cont    offset  type    opcode  mask    value   desc
magic_check igot=0
magic_check should be okay.
after magic_check errno = 0
input flags = 0
mcookie is okay i guess
after magic_open errno = 0
desript (null)
errno = 0
    Success
cooklabel = (null)

我一直在拔头发。测试文件存在,它只是一个文本文件。在我看来,对magic_ *例程的所有调用均有效,也就是说,我认为我检查了所有返回代码,并且设置例程的返回代码都很好。我假设描述例程的手册页是正确的。

但是返回的描述和magic_file返回的都是null。无奈之下我打印出了errno,但是我完全不相信会设置errno。 (手册页没有说将设置errno,只是说错误会返回空值。我一直在抓住稻草。)

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <magic.h>
# include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
# include <errno.h>


//------------------------------------------------------------------------------
struct magic_set * load(const char *magicfile,int flags)
{
struct magic_set *magic = magic_open(flags);
const char *e;

if (magic == NULL) {
        printf("Can't create magic");
        return NULL;
}
if (magic_load(magic,magicfile) == -1) {
        printf("%s",magic_error(magic));
        magic_close(magic);
        return NULL;
}
if ((e = magic_error(magic)) != NULL)
        printf("%s\n",e);
return magic;
}
//------------------------------------------------------------------------------
int main(void)
{
magic_t mcookie;
int igot;
int opflags = O_RDONLY ;
int flags = 0;
int posixly = 0;
int action = 0;
int e =0;
struct magic_set *msetptr = NULL;


int fd=0;
printf("set fd = %d\n",fd );
//const char *testfile = (char *)"/root/bin/twatch";
const char *testfile = (char *)"/root/URL";
printf("before 'open' errno = %d\n",errno );
fd = open(testfile,opflags );
printf("fd = %d\n",fd );
printf("fd errno = %d\n",errno );
printf("\t%s\n",strerror(errno) );


#ifdef S_IFLNK
posixly = getenv("POSIXLY_CORRECT") != NULL;
flags |=  posixly ? MAGIC_SYMLINK : 0;
#endif

//--------------------------------------
const char *magicfile = NULL;
if (magicfile == NULL)
    magicfile = magic_getpath(magicfile,action);
printf("magicfile = %s\n",magicfile );
printf("after getpath errno = %d\n",errno );

msetptr = load(magicfile,flags);
if( msetptr == NULL )
    printf("no mset ptr\n");
printf("after load errno = %d\n",errno );
//--------------------------------------


//  this looks okay
igot = magic_check(msetptr,magicfile);
printf("magic_check igot=%d\n",igot);
if( igot == 0 ) printf("magic_check should be okay.\n");
if( igot < 0 ) printf("magic_check is bad.\n");
errno = 0;
printf("after magic_check errno = %d\n",errno );
//--------------------------------------

printf("input flags = %d\n",flags );
mcookie = magic_open(flags|MAGIC_CHECK);
//mcookie = magic_open(flags);
if (mcookie == NULL) {
        printf("Can't create magic\n");
        return 1;
        }
printf("mcookie is okay i guess\n");
printf("after magic_open errno = %d\n",errno );
//--------------------------------------


//  this one spits out a lot.
//  so this works
//igot = magic_list( msetptr,magicfile );
//  this has a legit lookinhg value
//printf("magicfile = %s\n",magicfile );
//--------------------------------------

const char *sptr;
sptr = magic_descriptor(mcookie,fd);
printf("desript %s\n",sptr );
printf("errno = %d\n",strerror(errno) );

//--------------------------------------
const char *cooklabel;
cooklabel = magic_file( mcookie,testfile );
printf("cooklabel = %s\n",cooklabel );

//--------------------------------------
return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?