Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does zsh expand globs?

I have a C program that displays it's command-line by iterating through the argv variable.

#include <stdio.h>

int main(int argc, char *argv[]){
    int i = 0;
    printf("----------\n");
    for(i = 0; i < argc; i++)
        printf("%s\n", argv[i]);
    return 0;
}

I invoked the program in a folder containing a large C++ source tree like this:

./a.out **/*.h

The output:

zsh: argument list too long: ./a.out

However, programs like ls and grep work without any problems when invoked using the **/*.h glob in the same folder. Why does zsh fail when invoking my program? How does zsh go about expanding wildcards?

Edit: I'm using zsh on cygwin.

like image 300
Agnel Kurian Avatar asked Aug 05 '09 10:08

Agnel Kurian


2 Answers

Is the application you tried to run a Windows app (including mingw) or a Cygwin app?

ARG_MAX defines how long the command line can be. Usually it's set by the OS, and all applications are limited to it, but on Cygwin applications compiled for Cygwin can use a larger buffer than Windows apps - see this message for an example discussion.

If you don't necessarily require all the files as args at the same time, you can use xargs to partition the filenames to blocks that fit in ARG_MAX:

echo **/*.h | xargs ./a.out
like image 169
orip Avatar answered Sep 30 '22 15:09

orip


I compiled you code and executed it in the current directory in a similar way and it worked fine without any error messages. Also I came upon this message posted by someone suggesting that the error message "argument list too long" does not exist in the souce code for zsh so might actually be an OS issue : http://www.zsh.org/mla/workers/1996/msg00060.html

like image 24
Himanshu Avatar answered Sep 30 '22 15:09

Himanshu