unix `ls' command in C program

This is a discussion on unix `ls' command in C program within the C Programming forums, part of the General Programming Boards category; Hi all, I want to run C program as follow: ./commands filepath -l Code: #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    unix `ls' command in C program

    Hi all,

    I want to run C program as follow:

    ./commands filepath -l

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    char cmdline[] = "ls "; 
    strcat(cmdline,argv[1]); // to accept filepath
    strcat(cmdline,argv[2]); // to accept -l
    system(cmdline);
    return 0;
    }
    first problem:
    I recieved "Segmentation fault (core dumped)" for ./commands filepath
    second problem:
    When I passed `l': ./commands filepath -l
    I recived:
    ls: filepath/stuff-l: No such file or directory
    Segmentation fault (core dumped)
    So, it accepted filepath/stuff-l as one string

    Thankx in advance
    Last edited by Moony; 06-28-2006 at 09:49 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Make cmdline a big enough array to be able to concatenate to it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
       char cmdline[100]; 
       if ( argc > 2 )
       {
          sprintf(cmdline, "ls %s %s", argv[1], argv[2]);
          system(cmdline);
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 06-28-2006 at 09:59 AM. Reason: Added code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    However, I need to know what is wrong in my code. I tried to make the "cmdline" big enough but failed.
    Is the problem with strcat() function?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
       char cmdline[100] = "ls "; 
       strcat(cmdline,argv[1]); // to accept filepath
       strcat(cmdline," ");
       strcat(cmdline,argv[2]); // to accept -l
       printf("cmdline = \"%s\"\n", cmdline); /* debug */
       system(cmdline);
       return 0;
    }
    
    /* my output
    H:\test>test filepath -l
    cmdline = "ls filepath -l"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. C Program to calculate the FILE Size Sytem in Unix
    By anwar_pat in forum C Programming
    Replies: 6
    Last Post: 02-23-2006, 10:17 AM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21