ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)*/);
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 196
Rep:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.