Talking to *n*x

Discussion in 'C Programming' started by robbie carlton, Jun 1, 2004.

  1. I understand that c was originally designed to write and communicate
    with the unix operating system. Until recently I ignored this, as I
    was mostly polaying with opengl. I am now interested in interacting
    more directly with the operating system (actually linux) and I was
    wondering if anyone has any advice/urls. Specifically, I've found the
    system() function, which almost does what I want. But, while I could
    do something like

    system("cd /me/home/somedir");
    system("mkdir somesubdir");

    which is cool. But what if I want to get information /from/ the OS?
    say I just want to get the output of a ls command. Do I have to do it
    all with system() calls and environment variables? If so, I may as
    well write a shell script.
    Anyhoo, ta.
     
    robbie carlton, Jun 1, 2004
    #1
    1. Advertising

  2. robbie carlton

    -berlin.de Guest

    robbie carlton <> wrote:
    > I understand that c was originally designed to write and communicate
    > with the unix operating system. Until recently I ignored this, as I
    > was mostly polaying with opengl. I am now interested in interacting
    > more directly with the operating system (actually linux) and I was
    > wondering if anyone has any advice/urls. Specifically, I've found the
    > system() function, which almost does what I want. But, while I could
    > do something like


    > system("cd /me/home/somedir");
    > system("mkdir somesubdir");


    > which is cool. But what if I want to get information /from/ the OS?
    > say I just want to get the output of a ls command. Do I have to do it
    > all with system() calls and environment variables? If so, I may as
    > well write a shell script.


    With anything beyond system() you leave the realm of standard C.
    So better ask this kind of questions in an appropriate newsgroup
    like comp.unix.programmer or comp.os.linux.development.apps.

    <OT>
    Have a look at the popen(3) function, but opendir(3), readdir(3)
    and closedir(3) may also be interesting for you.
    </OT>
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jun 1, 2004
    #2
    1. Advertising

  3. robbie carlton

    Case Guest

    [OT] Re: Talking to *n*x

    robbie carlton wrote:
    > I understand that c was originally designed to write and communicate
    > with the unix operating system. Until recently I ignored this, as I
    > was mostly polaying with opengl. I am now interested in interacting
    > more directly with the operating system (actually linux) and I was
    > wondering if anyone has any advice/urls. Specifically, I've found the
    > system() function, which almost does what I want. But, while I could
    > do something like
    >
    > system("cd /me/home/somedir");
    > system("mkdir somesubdir");
    >
    > which is cool. But what if I want to get information /from/ the OS?
    > say I just want to get the output of a ls command. Do I have to do it
    > all with system() calls and environment variables? If so, I may as
    > well write a shell script.
    > Anyhoo, ta.


    <OT>
    There's a thick book, "Advanced Programming in the UNIX Environment"
    written by the late Richard Stevens about this subject. The function
    system() is interesting, but in many cases not the (most efficient)
    way to go.

    All the best,

    Case
    </OT>
     
    Case, Jun 1, 2004
    #3
  4. robbie carlton

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    robbie carlton wrote:

    > I understand that c was originally designed to write and communicate
    > with the unix operating system. Until recently I ignored this, as I
    > was mostly polaying with opengl. I am now interested in interacting
    > more directly with the operating system (actually linux) and I was
    > wondering if anyone has any advice/urls. Specifically, I've found the
    > system() function, which almost does what I want. But, while I could
    > do something like
    >
    > system("cd /me/home/somedir");
    > system("mkdir somesubdir");


    You realize, of course, that these two system() calls are independant of
    each other, and the second call will /not/ create
    /me/home/somedir/somesubdir ?

    [snip]
    - --

    Lew Pitcher, IT Consultant, Enterprise Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFAvIZhagVFX4UWr64RAhEjAJ9g8g/YsDkHi2CV1P77EoCf445pSwCgubLw
    b1GMXvOo4fLz02gM5EWxS8A=
    =qaMa
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Jun 1, 2004
    #4
  5. (robbie carlton) writes:

    > I understand that c was originally designed to write and communicate
    > with the unix operating system. Until recently I ignored this, as I
    > was mostly polaying with opengl. I am now interested in interacting
    > more directly with the operating system (actually linux) and I was
    > wondering if anyone has any advice/urls.


    The newsgroup comp.unix.programmer has advice and URLs (e.g. their FAQ
    will be interesting for you). However, Unix and the system-specific
    parts of its interface to C are off-topic here, so please ask in
    comp.unix.programmer.

    > Specifically, I've found the system() function, which almost does what
    > I want. But, while I could do something like
    >
    > system("cd /me/home/somedir");
    > system("mkdir somesubdir");


    The first function call has no effect on the calling program under Unix.
    The second is likely to work, but very inefficient.

    > which is cool. But what if I want to get information /from/ the OS?
    > say I just want to get the output of a ls command. Do I have to do it
    > all with system() calls and environment variables?


    No. Please ask in comp.unix.programmer for details.

    Martin


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
     
    Martin Dickopp, Jun 1, 2004
    #5
  6. robbie carlton

    SM Ryan Guest

    # system("cd /me/home/somedir");

    This doesn't do what you perhaps you think it does.

    # which is cool. But what if I want to get information /from/ the OS?

    You can construct commands that write to temporary files, and then
    read the temporary files.

    cd /tmp
    cat <<':eof' >t.c
    #include <stdio.h>
    #include <stdlib.h>
    int main(int N,char **P) {
    printf("--------------A\n");
    system("uname > xyz");
    printf("--------------B\n");
    FILE *f = fopen("xyz","r"); int c;
    printf("--------------C\n");
    while ((c=fgetc(f))!=EOF) fputc(c,stdout);
    printf("--------------D\n");
    system("rm xyz");
    return 0;
    }
    :eof
    cc t.c
    a.out
    --------------A
    --------------B
    --------------C
    Darwin
    --------------D

    By keeping the commands passed to system() in a per-OS include, you can
    keep to ANSI C and still do system specific work.

    # say I just want to get the output of a ls command. Do I have to do it
    # all with system() calls and environment variables? If so, I may as
    # well write a shell script.

    It's not either C or scripting. Anything you can do in a script, you can
    do in C. The question is where is it easiest to do it, and often it's a
    combination of C and scripting.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    OOOOOOOOOO! NAVY SEALS!
     
    SM Ryan, Jun 1, 2004
    #6
  7. Re: [OT] Talking to *n*x

    "robbie carlton" <> wrote in message
    news:...
    > I understand that c was originally designed to write and communicate
    > with the unix operating system. Until recently I ignored this, as I
    > was mostly polaying with opengl. I am now interested in interacting
    > more directly with the operating system (actually linux) and I was
    > wondering if anyone has any advice/urls. Specifically, I've found the
    > system() function, which almost does what I want. But, while I could
    > do something like
    >
    > system("cd /me/home/somedir");
    > system("mkdir somesubdir");
    >
    > which is cool. But what if I want to get information /from/ the OS?
    > say I just want to get the output of a ls command. Do I have to do it
    > all with system() calls and environment variables? If so, I may as
    > well write a shell script.


    You've missed one fundamental thing - most of the commandline tools like ls,
    cd and mkdir are written in C and are simply wrappers for C functions. Why
    call an external program to do something which you can do yourself in pure C
    code? Try `man 2 mkdir` for example.

    This is all off topic of course. But I can't help but point out that if
    you're running system() on `cd` and `mkdir`, you're generally doing
    something wrong.

    > Anyhoo, ta.
     
    Kieran Simkin, Jun 1, 2004
    #7
  8. in comp.lang.c i read:

    ># what if I want to get information /from/ the OS?


    typically an implementation and/or o/s provides functions and libraries by
    which you can gain that information -- and it's really no less portable
    than doing things via system(), in that what you pass to system and what
    happens is undefined (by the c standard), and so is using an implementation
    provided function. in fact the implementation function may be better,
    e.g., posix/sus defines the behavior of uname() but the behavior of calling
    system() remains undefined!

    > system("uname > xyz");


    this is the tricky bit -- not all o/s' do redirection, or do but not with
    this syntax. a per-o/s set of includes still presumes it can all be done
    in the same sorts of ways, probably with a single call to system. and
    that's not a given. (also consider what happens if two such programs are
    run at nearly the same time -- reading from a truncated file doesn't
    provide the answer you wanted.) instead write an abstract interface for
    the *purpose*, then for each o/s on which your program should run you write
    the necessary code -- i.e., you `port it' -- including using whatever build
    management tools are available to `use' the proper chunk of code.

    > while ((c=fgetc(f))!=EOF) fputc(c,stdout);
    > printf("--------------D\n");


    > system("rm xyz");


    as an aside: the file should be closed first. on some systems (unix-ish)
    it doesn't matter, but on other systems that use the same sort of syntax
    (windows with pwb tools) it does (the rm will fail). i suggest fclose(f);
    remove("xyz"); instead.

    --
    a signature
     
    those who know me have no need of my name, Jun 1, 2004
    #8
  9. robbie carlton

    Dan Pop Guest

    Re: [OT] Re: Talking to *n*x

    In <40bc7cb8$0$33920$4all.nl> Case <> writes:

    >There's a thick book, "Advanced Programming in the UNIX Environment"
    >written by the late Richard Stevens about this subject. The function
    >system() is interesting, but in many cases not the (most efficient)
    >way to go.


    These days, unless you have system() in the performance critical path,
    its efficiency is a non-issue.

    fangorn:~/tmp 21> cat test.c
    #include <stdlib.h>

    int main(void)
    {
    system("date");
    return 0;
    }
    fangorn:~/tmp 22> gcc test.c
    fangorn:~/tmp 23> time ./a.out
    Wed Jun 2 18:18:04 CEST 2004
    0.000u 0.000s 0:00.01 0.0% 0+0k 0+0io 515pf+0w

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 2, 2004
    #9
  10. robbie carlton

    nobody Guest

    Re: [OT] Re: Talking to *n*x

    "Dan Pop" <> wrote in message
    news:c9kuna$sfh$...
    > In <40bc7cb8$0$33920$4all.nl> Case <> writes:
    >
    > >There's a thick book, "Advanced Programming in the UNIX Environment"
    > >written by the late Richard Stevens about this subject. The function
    > >system() is interesting, but in many cases not the (most efficient)
    > >way to go.

    >
    > These days, unless you have system() in the performance critical path,
    > its efficiency is a non-issue.
    >

    [snip]

    He didn't necessarilly mean execution-time efficiency:

    7.20.4.5 The system function
    Synopsis
    1 #include <stdlib.h>
    int system(const char *string);
    Description
    2 If string is a null pointer, the system function determines whether the
    host
    environment has a command processor. If string is not a null pointer, the
    system
    function passes the string pointed to by string to that command processor to
    be
    executed in a manner which the implementation shall document; this might
    then cause the

    ^^^^^^^^^^^^^^^^^^^^
    program calling system to behave in a non-conforming manner or to terminate.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     
    nobody, Jun 7, 2004
    #10
  11. robbie carlton

    Dan Pop Guest

    Re: [OT] Re: Talking to *n*x

    In <x2_wc.98757$> "nobody" <> writes:

    >"Dan Pop" <> wrote in message
    >news:c9kuna$sfh$...
    >> In <40bc7cb8$0$33920$4all.nl> Case <> writes:
    >>
    >> >There's a thick book, "Advanced Programming in the UNIX Environment"
    >> >written by the late Richard Stevens about this subject. The function
    >> >system() is interesting, but in many cases not the (most efficient)
    >> >way to go. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    >> ^^^^^^^^^
    >> These days, unless you have system() in the performance critical path,
    >> its efficiency is a non-issue.
    >>

    >[snip]
    >
    >He didn't necessarilly mean execution-time efficiency:


    About what other kind of efficiency could he be talking??? The text you
    quote below has exactly zilch to do with efficiency issues.

    >7.20.4.5 The system function
    >Synopsis
    >1 #include <stdlib.h>
    >int system(const char *string);
    >Description
    >2 If string is a null pointer, the system function determines whether the
    >host
    >environment has a command processor. If string is not a null pointer, the
    >system
    >function passes the string pointed to by string to that command processor to
    >be
    >executed in a manner which the implementation shall document; this might
    >then cause the
    >
    >^^^^^^^^^^^^^^^^^^^^
    >program calling system to behave in a non-conforming manner or to terminate.
    >^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


    You're reading impaired again. Compare the text you have underlined with
    "but in many cases not the (most efficient) way to go" and explain any
    connection you can find. Keep in mind that the context is Unix.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 8, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    1
    Views:
    771
    Jonathan Bromley
    Sep 1, 2003
  2. =?Utf-8?B?bWVyZGFhZA==?=

    MY ASP.NET stopped talking to my IIS!!

    =?Utf-8?B?bWVyZGFhZA==?=, Mar 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    442
    Steve C. Orr [MVP, MCSD]
    Mar 5, 2004
  3. Brent
    Replies:
    6
    Views:
    409
    einzteen
    Jul 7, 2006
  4. =?Utf-8?B?SmFzb24gQ2h1?=

    java socket write error -> talking with asp.net

    =?Utf-8?B?SmFzb24gQ2h1?=, Nov 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    3,191
    mdinant
    Apr 4, 2008
  5. Seebs

    [META] Talking about talking about C.

    Seebs, Oct 23, 2010, in forum: C Programming
    Replies:
    113
    Views:
    2,336
    Tim Rentsch
    Nov 24, 2010
Loading...

Share This Page