LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2006, 01:26 PM   #1
Josh_
LQ Newbie
 
Registered: Mar 2006
Posts: 3

Rep: Reputation: 0
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 [[]]
 
Old 03-01-2006, 01:30 PM   #2
Jaqui
Member
 
Registered: Jan 2006
Location: Vancouver BC
Distribution: LFS, SLak, Gentoo, Debian
Posts: 291

Rep: Reputation: 36
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.
 
Old 03-01-2006, 01:39 PM   #3
Josh_
LQ Newbie
 
Registered: Mar 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
Old 03-01-2006, 01:42 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Code:
FILE *fp=popen("ls -l", "r");
Beware from security issues if you make the command user modifiable.
 
Old 03-01-2006, 01:47 PM   #5
Jaqui
Member
 
Registered: Jan 2006
Location: Vancouver BC
Distribution: LFS, SLak, Gentoo, Debian
Posts: 291

Rep: Reputation: 36
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.
 
Old 03-01-2006, 01:49 PM   #6
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196

Rep: Reputation: 30
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
 
Old 03-01-2006, 02:01 PM   #7
Josh_
LQ Newbie
 
Registered: Mar 2006
Posts: 3

Original Poster
Rep: Reputation: 0
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?

Last edited by Josh_; 03-01-2006 at 02:05 PM.
 
Old 03-01-2006, 02:15 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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...

Last edited by paulsm4; 03-01-2006 at 02:17 PM.
 
Old 03-01-2006, 02:37 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
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.

Last edited by jschiwal; 03-01-2006 at 02:45 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
where to get source code for netstat command Madhusudhan Linux - Software 1 02-09-2006 02:48 AM
execute command from c code alaios Programming 3 06-06-2005 06:26 AM
error when using a system command to invoke convert nodyl Programming 4 08-25-2004 11:00 PM
how to invoke a linux command inside a java code ? kusum Linux - Software 2 11-23-2003 02:19 PM
Command for getting assembly code aizkorri Programming 1 06-18-2002 06:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration