Find the answer to your Linux question:
Results 1 to 2 of 2

Thread: using switches

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    using switches

    i have made ls-l command work only when in the command line i put
    ./ls * to run my program.i want to run it every time without typing *.this is because the program wants as arguments a file and when i put * takes all files and directories and using stat prints all information for them.
    i tried in my program :argv[i]="*"; but it not seems to can i do that?
    therefore i want to pass some switches like -l.when the user types -l i also tried argv[1]="-l" but it doesn't work.i also tried glob but i don;t really know how it works.
    any suggestions appreciated

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Täby, Sweden
    argv[i]="*" doesn't work because the wildcard isn't expanded by your program, it's expanded by the shell right before it starts your program.
    The way the ordinary ls does this is that when it stat()'s a file, it checks if it is a directory (if(S_ISDIR(statvar.st_mode)) {}), and if it is, it lists all files in it recursively using opendir and readdir. The default argument to ls is just ".", referring to the current directory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts