parameter trouble - Minix

This is a discussion on parameter trouble - Minix ; hi this may be a basic c question but ill ask it here anyway: the manpage of execve gives me the following signature int execve(const char *name, char *const argv[], char *const envp[]) what exactly is the difference between "const ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: parameter trouble

  1. parameter trouble

    hi
    this may be a basic c question but ill ask it here anyway:
    the manpage of execve gives me the following signature

    int execve(const char *name, char *const argv[], char *const envp[])

    what exactly is the difference between "const char *name" and "char
    *const name[]"?

    would someone give me an example of how to call this?

    thanx,

    martin


  2. Re: parameter trouble


    sancho1980 wrote:
    > hi
    > this may be a basic c question but ill ask it here anyway:
    > the manpage of execve gives me the following signature
    >
    > int execve(const char *name, char *const argv[], char *const envp[])
    >
    > what exactly is the difference between "const char *name" and "char
    > *const name[]"?
    >
    > would someone give me an example of how to call this?
    >
    > thanx,
    >
    > martin


    ps: i was just trying to do the exercise in the minix book at the end
    of chapter 1; my shell doesnt work; this is what it looks like:

    void main(void)
    {
    char command[20], parameters[20];
    while (1)
    {
    printf("Enter command: ");
    gets(command);
    printf("Enter parameters: ");
    gets(parameters);
    if (command[0] == 0)
    break;
    if (fork() != 0)
    waitpid(-1, NULL, 0);
    else
    execve(command, parameters, 0);
    }
    }

    No matter what I enter, no command seems to be executed...

    btw: i execute minix in vmware player; is there any way for me to
    exchange files between my windows environment and minix in vmware so i
    dont always have to copy by hand when i'm posting something?

    thanx,

    martin


  3. Re: parameter trouble

    Hello,

    const *ptr pointer to a constant object,
    *const ptr constant pointer to a non-constant object,
    ptr[] the same as above.

    Now in combination,

    *ptr[] constant pointer to a non-constant pointer,
    *const ptr[] constant pointer to a constant pointer.

    Another way of saying this is:

    ptr[] an array,
    *ptr[] an array of pointers,
    *const ptr[] an array of constant pointers.

    In summary, an array is the same as a constant pointer to the first
    element of the array. If the 'const' keyword precedes the , then
    the object pointed-to is constant. When the 'const' keyword follows
    the '*', the pointer itself is constant.

    Kind regards.
    José Juan Mendoza Rodríguez

    let me=josejuanmr in
    let privacy=iespana in
    let net=es in
    me@privacy.net


  4. Re: parameter trouble


    Hi Martin,

    "sancho1980" writes:

    > sancho1980 wrote:
    > > hi
    > > this may be a basic c question but ill ask it here anyway:
    > > the manpage of execve gives me the following signature
    > >
    > > int execve(const char *name, char *const argv[], char *const envp[])
    > >
    > > what exactly is the difference between "const char *name" and "char
    > > *const name[]"?
    > >
    > > would someone give me an example of how to call this?
    > >
    > > thanx,
    > >
    > > martin

    >
    > ps: i was just trying to do the exercise in the minix book at the end
    > of chapter 1; my shell doesnt work; this is what it looks like:
    >
    > void main(void)
    > {
    > char command[20], parameters[20];
    > while (1)
    > {
    > printf("Enter command: ");
    > gets(command);
    > printf("Enter parameters: ");
    > gets(parameters);


    Don't use fix size buffers. Don't use gets(). Just try to do it right
    from the beginning and don't fall into bad habits. gets() is
    evil. (Exercise: Why is gets() evil? Hint: Search comp.std.c for
    discussion of gets() :-).


    > if (command[0] == 0)
    > break;
    > if (fork() != 0)
    > waitpid(-1, NULL, 0);
    > else
    > execve(command, parameters, 0);


    Check out the difference between execve() and execvp() (and all other
    members of that family). I'm almost certain, what you want is
    execvp() and I'm almost sure, your 'parameters' should be called
    'parameter'. :-).

    > }
    > }
    >
    > No matter what I enter, no command seems to be executed...


    It helps to check the exit status of exec*() and perhaps to print the
    system error encoded in errno. perror() comes handy here.

    And BTW: 'man -k' is a shell command you should learn if you intend to
    learn Unix programming. In your case: 'man -k error' and 'man -k exec'
    and don't forget to read the 'SEE ALSO' section in the man pages.

    More tips: Try to get the exec() call right without fork() and with
    constant parameters first. Then wrap the if...fork() around that. Then
    move the constant parameters to lokal variables out of the
    if...fork(). Then and only then do the input procedure which reads
    input from the user and assigns it to the variables which are passed
    to exec(). You'll find that you'll have to handle 2 variable size
    structures: The input string and the char*[] (an array of strings!)
    passed to exec(). To avoid execessive strdup()-ing and freeing, a
    useful approach would be to read the input string into a really large
    fix size buffer (longe than any reasonable input line, say 1k?) with
    an appropriate function (which can honor the size restriction) and
    abort on overflow. Then cut that into pieces by inserting '\0' at the
    appropriate places and recording the char*'s that point to beginnings
    of those strings (the future argv[] of the programs to be exec()ed)
    into a char*[]. Again abort on overflow of that last array ('too many
    parameters'). Now pass all that to some exec()-function.

    Developing by generalizing a constant (working) sample has often
    proved to be a useful approach for me.

    The program with constant parameters will be really short. The
    difficult part for you, IMHO, is to process the input line into
    command and parameters.

    > btw: i execute minix in vmware player; is there any way for me to
    > exchange files between my windows environment and minix in vmware so i
    > dont always have to copy by hand when i'm posting something?


    Perhaps mcopy and doswrite? I don't know how far rsync and ssh are in
    Minix 3.

    Regards -- Markus


  5. Re: parameter trouble

    Jose Juan Mendoza Rodriguez writes:

    > In summary, an array is the same as a constant pointer to the first
    > element of the array.


    When calling a procedure and in function prototypes you're right. But
    not always (forgive me pointing that out for completeness):

    int* i;

    and

    int j [29];

    are different. You can see the difference when taking sizeof(i) and
    sizeof(j) (one is sizeof(void*), the other 29*size(int)).

    Arrays and parameters are really different object types in the language
    definition, but "arrays decay to pointers" when passed in a function
    call.

    That 'int k[]' is allowed a synonym for int* k in the parameter list
    declaration of a function, always seemed an historic accident to
    me. You can even write

    int f( int k[100] )

    which suggests an array of 100 ints is passed. But no: sizeof(k) is
    sizeof(void*) which is somehow different from the experience you get
    with variables (as opposed to parameters here). The [n] in function
    prototypes ought to be forbidden, really. [] (without constant) also
    suggest the wrong thing, but I assume that

    int f( char* c[] )

    is just a bit more readable than

    int f( char** c )

    Regards -- Markus.



  6. Re: parameter trouble

    sancho1980 wrote:

    > btw: i execute minix in vmware player; is there any way for me to
    > exchange files between my windows environment and minix in vmware so i
    > dont always have to copy by hand when i'm posting something?


    One easy way is to enable networking for your Minix-on-VMWare system
    and enable the ftp service, then you can copy files between your
    Windows host and your Minix guest using ftp. Or, with networking
    enabled you can log onto your Minix system remotely. I use SecureCRT
    on a Windows host to connect to both virtual and real Minix
    installations remotely so I can cut and paste in the terminal window.

    Although Minix is not a VMWare-approved guest OS with an official set
    of VMWare tools, a program that implements much of the functionality
    of the VMWare tools was posted for Minix 2, before Minix 3 was
    released. It is available in my contributed software directory at
    http://minix1.woodhull.com/pub/contrib/vmw-01.tar.Z. I tested it on
    Minix 3.1.0 and 3.1.1 on VMWare Workstation and I expect it is also
    OK with Minix 3.1.2+ on VMWare Player, although I haven't tested
    this.

    I found the most useful part of the vmw tool to be its ability to
    write to and read from the VMWare host's clipboard from a Minix
    guest. A description can be found at
    http://minix1.woodhull.com/pub/contrib/vmw-01.txt.

    Here is an extract from that file:

    ------------------------
    NAME
    vmw - VMware Command Line Tools

    SYNOPSIS
    vmw [-q] [-C|-c|-p|-r|-t|-v|-V]

    OPTIONS
    -C toggle 'copy & paste' feature
    -c copy to host's clipboard
    -p print product name
    -q be quiet (return exit status)
    -r print screen size
    -t set time and date
    -v paste from host's clipboard
    -V print version (default)
    ------------------------

    This might also be on the official minix3.org site somewhere.

    There is another tool for Minix on VMWare on my site which is of
    potential interest. This one is not immediately useful for Minix 3, as
    it patches the console driver, and, of course, drivers are the part of
    Minix 3 that are most radically changes since Minix 2. The patch
    allows one to write the current Minix console screen to the host
    clipboard by pressing the F4 key. Maybe someone would like to
    experiment with porting this one? The package is at
    http://minix1.woodhull.com/pub/contrib/vmw-p01.tar.Z.

    - Al


+ Reply to Thread