|
|
The following is supposed to simulate
$ls;who
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(void) {
pid_t pid;
unsigned char *fname =";who";
if( (pid=fork()) < 0 ) {
fprintf(stderr, "fork error \n");
} else if (pid == 0) {
execl("/bin/ls","ls",fname,(char *)0);
}
exit(0);
}
However, when I run the code, I get
$gcc exc.c -o exc
$./exc
$ls: ;who: No such file or directory
The question is can I pass a string like ";pwho" without having to use
additional args on execl() ?
10 responses total.
I think you're mixing ideas. "ls;who" is a something the shell does.
execl() is a library function that wraps a system call; it does no
interpretation of commands. So it has just passes ";who" as the 1st
argument to ls, which then takes over and tries to list that file (which
doesn't exist). I think what want is something more like the following:
/*
* Simulate some shell stuff.
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void
spawn(char *path, char *argv0)
{
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
execl(path, argv0, NULL);
perror(path);
_exit(EXIT_FAILURE);
} else
wait(NULL);
}
int
main(void)
{
spawn("/bin/ls", "ls");
spawn("/usr/bin/who", "who");
return(EXIT_SUCCESS);
}
Note: I wrap up all the fork()/exec()/wait() stuff in a helper function.
Let's see if I'm understannding this correctly. My original code would be equivalent to say like: ls -who vs something like ls -al.
Never mind, i just did ls who at the shell prompt.
Actually, it'd be more like:
ls ';who'
What Mike said. In fact, it's *exactly* like ls ';who' (note the quotes).
And I just found this wonderful function called setproctitle().
And you want to hear something really horrible? Going off topic, it took me 7 months to clue in on the Quicksort algorithm. I'm thinking I go too hung up on the mathematics or funky coding.
Ack! Sorting is the type of thing that you shouldn't know, but that you should've forgotten. (Well, I guess it's unfair to say that you shouldn't know it - you should at least know it well enough to understand it.) But in general, you're better off using a library to do your sorting for you instead of rolling your own.
Okay, i was thinking more about the shell thingy at Labor Ready. Anyhow, how when I go like $sh -c "echo \"test\" " test it doesn't print "test"
Because you're at the shell, and you're invoking the shell as a program to do something for you. The shell that you're invoking (think of it was the 2nd instance of the shell, if you like) is interpreting the double quotes that you've escaped. So, "echo \"test\" " is handled by the 1st shell (the one you're interacting with) and passes the string ``echo "test" '' to the 2nd shell. The second shell invokes ``echo'' with the string ``test'' (minus quotes, which it interpreted) as the first argument.
Response not possible - You must register and login before posting.
|
|
- Backtalk version 1.3.30 - Copyright 1996-2006, Jan Wolter and Steve Weiss