简体   繁体   English

自定义shell glob问题

[英]Custom shell glob problem

I have to write a shell program in c that doesn't use the system() function. 我必须在c中编写一个不使用system()函数的shell程序 One of the features is that we have to be able to use wild cards. 其中一个功能是我们必须能够使用外卡。 I can't seem to find a good example of how to use glob or this fnmatch functions that I have been running into so I have been messing around and so far I have a some what working blog feature (depending on how I have arranged my code). 我似乎无法找到一个很好的例子,说明如何使用glob或我遇到的这个fnmatch函数,所以我一直在搞乱,到目前为止我有一些工作博客功能(取决于我如何安排我的码)。

If I have a glob variable declared as a global then the function partially works. 如果我将glob变量声明为全局变量,那么函数部分工作。 However any command afterwards produces in error. 但是之后的任何命令都会产生错误。 example: 例:

ls *.c
produce correct results
ls -l //no glob required
null passed through

so I tried making it a local variable. 所以我试着把它变成局部变量。 This is my code right now: 这是我现在的代码:

int runCommand(commandStruct * command1) {
if(!globbing)
    execvp(command1->cmd_path, command1->argv);
else{
    glob_t globbuf;
    printf("globChar: %s\n", globChar);
    glob(globChar, GLOB_DOOFFS, NULL, &globbuf);
    //printf("globbuf.gl_pathv[0]: %s\n", &globbuf.gl_pathv[0]);
    execvp(command1->cmd_path, &globbuf.gl_pathv[0]);
    //globfree(&globbuf);
    globbing = 0;
}
return 1;

} }

When doing this with the globbuf as a local, it produces a null for globbuf.gl_path[0]. 当使用globbuf作为本地执行此操作时,它会为globbuf.gl_path [0]生成null。 Can't seem to figure out why. 似乎无法弄清楚为什么。 Anyone with a knowledge of how glob works know what might be the cause? 任何了解glob工作原理的人都知道原因可能是什么? Can post more code if necessary but this is where the problem lies. 可以在必要时发布更多代码,但这就是问题所在。

You are asking for GLOB_DOOFFS but you did not specify any number in globbuf.gl_offs saying how many slots to reserve. 您要求GLOB_DOOFFS但您没有在globbuf.gl_offs指定任何数字来说明要保留多少个插槽。

Presumably as a global variable it gets initialized to 0. 大概作为一个全局变量,它被初始化为0。

Also this: &globbuf.gl_pathv[0] can simply be globbuf.gl_pathv . 另外这个: &globbuf.gl_pathv[0]可以简单地是globbuf.gl_pathv

And don't forget to run globfree(globbuf) . 并且不要忘记运行globfree(globbuf)

I suggest running your program under valgrind because it probably has a number of memory leaks, and/or access to uninitialized memory. 我建议在valgrind下运行你的程序,因为它可能有许多内存泄漏,和/或访问未初始化的内存。

this works for me: 这对我有用:

...
glob_t glob_buffer;
const char * pattern = "/tmp/*";
int i;
int match_count;


glob( pattern , 0 , NULL , &glob_buffer ); 
match_count = glob_buffer.gl_pathc;
printf("Number of mathces: %d \n", match_count);

for (i=0; i < match_count; i++) 
    printf("match[%d] = %s \n",i,glob_buffer.gl_pathv[i]);

globfree( &glob_buffer );
...

Observe that the execvp function expects the argument list to end with a NULL pointer, ie I think it will be the easiest to create your own char ** argv copy with all the elements from the glob_buffer.gl_pathv[] and a NULL pointer at the end. 观察到execvp函数期望参数列表以NULL指针结束,即我认为最简单的方法是使用glob_buffer.gl_pathv[]所有元素和一个NULL指针创建自己的char ** argv副本。结束。

If you don't have to use * style wildcards I've always found it simpler to use opendir(), readdir() and strcasestr(). 如果你不必使用* style通配符,我总是发现使用opendir(),readdir()和strcasestr()更简单。 opendir() opens a directory (can be ".") like a file, readdir() reads an entry from it, returns NULL at the end. opendir()像文件一样打开一个目录(可以是“。”),readdir()从中读取一个条目,最后返回NULL。 So use it like 所以就像使用它一样

struct dirent *de = NULL;
DIR *dirp = opendir(".");
while ((de = readdir(dirp)) != NULL) {
  if ((strcasestr(de->d_name,".jpg") != NULL) {
  // do something with your JPEG
  }
}

Just remember to closedir() what you opendir(). 只记得关闭()你的opendir()。 A struct dirent has the d_type field if you want to use it, most files are type DT_REG (not dirs, pipes, symlinks, sockets, etc.). 如果你想使用它,struct dirent有d_type字段,大多数文件是DT_REG类型(不是dirs,管道,符号链接,套接字等)。

It doesn't make a list like glob does, the directory is the list, you just use criteria to control what you select from it. 它没有像glob这样的列表,目录是列表,你只需使用条件来控制你从中选择的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM