No Next Item No Next Conference Can't Favor Can't Forget Item List Conference Home Entrance    Help
View Responses


Grex Systems Item 50: Funky Unix code
Entered by herasleftnut on Wed Oct 4 03:34:45 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.



#1 of 10 by cross on Wed Oct 4 04:41:14 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.


#2 of 10 by herasleftnut on Wed Oct 4 12:52:05 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.


#3 of 10 by herasleftnut on Wed Oct 4 13:12:14 2006:

Never mind, i just did

ls who

at the shell prompt.


#4 of 10 by mcnally on Wed Oct 4 14:01:58 2006:

 Actually, it'd be more like:
    ls ';who'


#5 of 10 by cross on Wed Oct 4 19:08:14 2006:

What Mike said.  In fact, it's *exactly* like ls ';who' (note the quotes).


#6 of 10 by herasleftnut on Thu Oct 5 00:47:36 2006:

And I just found this wonderful function called setproctitle().


#7 of 10 by herasleftnut on Thu Oct 5 01:31:25 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.



#8 of 10 by cross on Thu Oct 5 03:26:04 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.


#9 of 10 by herasleftnut on Fri Oct 6 00:29:21 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"


#10 of 10 by cross on Fri Oct 6 17:56:53 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.

Response not possible - You must register and login before posting.

No Next Item No Next Conference Can't Favor Can't Forget Item List Conference Home Entrance    Help

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