You are not logged in. Login Now
 0-10          
 
Author Message
herasleftnut
Funky Unix code Mark Unseen   Oct 4 03:34 UTC 2006

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.
cross
response 1 of 10: Mark Unseen   Oct 4 04:41 UTC 2006

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.
herasleftnut
response 2 of 10: Mark Unseen   Oct 4 12:52 UTC 2006

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.
herasleftnut
response 3 of 10: Mark Unseen   Oct 4 13:12 UTC 2006

Never mind, i just did

ls who

at the shell prompt.
mcnally
response 4 of 10: Mark Unseen   Oct 4 14:01 UTC 2006

 Actually, it'd be more like:
    ls ';who'
cross
response 5 of 10: Mark Unseen   Oct 4 19:08 UTC 2006

What Mike said.  In fact, it's *exactly* like ls ';who' (note the quotes).
herasleftnut
response 6 of 10: Mark Unseen   Oct 5 00:47 UTC 2006

And I just found this wonderful function called setproctitle().
herasleftnut
response 7 of 10: Mark Unseen   Oct 5 01:31 UTC 2006

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.

cross
response 8 of 10: Mark Unseen   Oct 5 03:26 UTC 2006

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.
herasleftnut
response 9 of 10: Mark Unseen   Oct 6 00:29 UTC 2006

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"
cross
response 10 of 10: Mark Unseen   Oct 6 17:56 UTC 2006

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.
 0-10          
Response Not Possible: You are Not Logged In
 

- Backtalk version 1.3.30 - Copyright 1996-2006, Jan Wolter and Steve Weiss