LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do i invoke the ls command within a C code (https://www.linuxquestions.org/questions/programming-9/how-do-i-invoke-the-ls-command-within-a-c-code-420671/)

Josh_ 03-01-2006 12:26 PM

How do i invoke the ls command within a C code
 
Hey everyone!!

What i'm just trying to do is to invoke the ls command, like when we use: getpid() ..the output shows the PID number.
Now i'd like to do the same thing but instead of using getpid() use something to execute the ls command (current directory) and ls / command ( root directory)

Incomplete code:

#include <stdio.h>

int main() {

char x[100];

//char list [1000]; -> ls stuff? i think this is
wrong


while(1) {

printf("Command: ");
fgets( x, 100, stdin);

if(strcmp(x, "pids\n") == 0 )
printf("My pid is %d and the ppid is %d, \n"
, getpid(), getppid());

//now i'd like to do the same thing but for ls and
// ls /

else if(strcmp(x, "lists\n") == 0)
printf( /*execute ls command*/);

else if (strcmp(x, "root\n") == 0)
printf( /*execute ls / command( root directory)*/);

}
return 0;
}

any suggestion?
Thks [[]]

Jaqui 03-01-2006 12:30 PM

What you want is to use a system call.
the GNU GCC docs describe how, but state there are functions to do almost everything in gcc already.

Josh_ 03-01-2006 12:39 PM

doesn't say what i wanna know :S

getls() , does not exists lol what should i use?

by the way how do i get out of the while loop?
Like in java we use: System.exit(0);

in C what do we use?

Thk you so much!

jlliagre 03-01-2006 12:42 PM

Code:

FILE *fp=popen("ls -l", "r");
Beware from security issues if you make the command user modifiable.

Jaqui 03-01-2006 12:47 PM

Quote:

#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>

int rc;

rc = syscall(SYS_ls, );

if (rc == -1)
fprintf(stderr, "ls failed, errno = %d\n", errno);

return 0;
a partial listing of the code to use ls from c.

the system call when finished returns control to the calling program, return is the end of function signal, 0 is the standard no error indicator.

tonyfreeman 03-01-2006 12:49 PM

ls.c source code
 
I thought I'd add this just in case: Download the source code for the ls command and study how it is implemented. It is in the "coreutils" package provided by the GNU software group.

http://ftp.gnu.org/pub/gnu/coreutils/

-- Tony

Josh_ 03-01-2006 01:01 PM

ok thks.

Last question how do we get out of a while loop?

if i use: exit(0); it gives me error ! :S so what do we use in C code?

paulsm4 03-01-2006 01:15 PM

Hi, John -

I was about to suggest "man popen", when I got sidetracked ... and you got about six other replies.

For whatever it's worth, here's a code sample that illustrates "popen":
Code:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char *
trim (char * s)
{
  int i = strlen(s);
  while (--i >= 0)
  {
    if (!isspace (s[i]))
    {
      s[i+1] = '\0';
      break;
    }
  }
  return s;
}

int
main (int argc, char *argv[])
{
  char cmd[] = "ls -l /tmp/xxx*";

  FILE *fp = popen (cmd, "r");
  if (fp == NULL)
  {
    perror ("popen failed!\n");
    return 1;
  }

  char buff[1024];
  while (fgets (buff, sizeof buff, fp) != NULL)
  {
    printf ("Next line: %s...\n", trim (buff));
  }

  pclose (fp);
  return 0;
}

Quote:

cc -Wall -g -o xxx xxx.c
./xxx
Code:

Next line: -rwxr-xr-x  1 paulsm users 9793 2006-03-01 10:55 /tmp/xxx...
Next line: -rw-r--r--  1 paulsm users  547 2006-03-01 10:55 /tmp/xxx.c...



jschiwal 03-01-2006 01:37 PM

Reading the contents of a directory is similar to reading the contents of a file. A directory is a special file containing a list of inode number / name pairs.
Look through the <sys/types.h> and <dirent.h> headers. From
There is an opaque type named DIR that is similar to the FILE type.

Excerpt from Linux Programming by Example by Arnold Robbins:
Code:

int
process(char *dir)
{
    DIR *dp*;
    struct dirent *ent;

    if ((dp = opendir(dir)) == NULL) {
      fprintf(stderr, "%s: %s: cannot open for reading: %s\n",
        myname, dir, strerror(errno));
        return 1;
    }

    erro = 0;   
    while ((ent = readdir(dp)) != NULL)
        printf("%8ld %s\n", ent->d_ino, ent->dname);
    if (errno != 0) {
        fprintf(stderr, "%s: %s: reading directory entries: %s\n",
              myname, dir, strerror(errno));
    return 1;
    }

    if (closedir(dp) != 0) {
        fprintf(stderr, "%s: %s: closedir: %s\n",
            myname, dir, strerror(errno));
        return 1;
    }

    return 0;
}

The readdir() functions reads an entry from the directory. If you are at the end of the directory, the function returns null which is what your while function tests for.
Your example System.exit(0); is for improperly bailing out of a program if used in a while loop.

The touch pad on my laptop is a bit touchy today. The code got rearranged a few times while typing. I hope it is correct now.


All times are GMT -5. The time now is 11:27 AM.