system call as variabe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rudra

    #1

    system call as variabe

    i have written a code::
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    int sys;
    int i686;
    sys=system("/bin/uname -m");
    //printf ("%d",sys);
    if (sys==i686){
    printf("the syst. 32 bit");
    }
    else
    printf("64but") ;
    }
    where i want the output of "system("/bin/uname -m")" as the variable
    sys.....to find the machine structure.this procedure is not quite
    working. can you people suggest me some way?
  • Richard Heathfield

    #2
    Re: system call as variabe

    rudra said:
    i have written a code::
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    int sys;
    int i686;
    sys=system("/bin/uname -m");
    //printf ("%d",sys);
    if (sys==i686){
    Since the defined-but-not-initialised i686 int object doesn't have a value,
    how can this comparison be meaningful? I think what you're really trying
    to do is find out whether the output of uname is the string "i686", but
    the return value of the system() function won't tell you this.

    One possibility for achieving your objective is to redirect the output of
    the command to a file, and then open and read the file. This would keep
    the code reasonably portable. An alternative that you might wish to
    explore if you're into system-specific stuff is a pipe (and
    comp.unix.progr ammer knows all about pipes).

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Keith Thompson

      #3
      Re: system call as variabe

      Richard Heathfield <rjh@see.sig.in validwrites:
      rudra said:
      >i have written a code::
      > #include <stdio.h>
      > #include <stdlib.h>
      > int main()
      > {
      > int sys;
      > int i686;
      > sys=system("/bin/uname -m");
      > //printf ("%d",sys);
      > if (sys==i686){
      >
      Since the defined-but-not-initialised i686 int object doesn't have a value,
      how can this comparison be meaningful? I think what you're really trying
      to do is find out whether the output of uname is the string "i686", but
      the return value of the system() function won't tell you this.
      >
      One possibility for achieving your objective is to redirect the output of
      the command to a file, and then open and read the file. This would keep
      the code reasonably portable. An alternative that you might wish to
      explore if you're into system-specific stuff is a pipe (and
      comp.unix.progr ammer knows all about pipes).
      Portability is a tricky thing. The call system("/bin/uname -m") is
      portable in the sense that it uses a function defined by the C
      standard, but it will fail unless the string "/bin/uname -m" happens
      to be meaningful as a command.

      As it happens, any system on which "/bin/uname -m" is a valid command
      will have other methods to achieve the same result, using non-portable
      functions. <OT>Using a pipe should work, but invoking the underlying
      uname() function directly is cleaner and really no less portable.</OT>

      Let me repeat the excellent advice to post to comp.unix.progr ammer.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Richard Heathfield

        #4
        Re: system call as variabe

        Keith Thompson said:
        Richard Heathfield <rjh@see.sig.in validwrites:
        <snip>
        >>
        >One possibility for achieving your objective is to redirect the output
        >of the command to a file, and then open and read the file. This would
        >keep the code reasonably portable. An alternative that you might wish to
        >explore if you're into system-specific stuff is a pipe (and
        >comp.unix.prog rammer knows all about pipes).
        >
        Portability is a tricky thing. The call system("/bin/uname -m") is
        portable in the sense that it uses a function defined by the C
        standard, but it will fail unless the string "/bin/uname -m" happens
        to be meaningful as a command.
        Yes, which is why I said "reasonably portable" rather than "maximally
        portable". One obvious improvement is to have the command string read in
        at runtime, allowing the user to provide a command that is relevant to the
        host system. This presupposes, alas, that the user knows what he's doing,
        and also trusts that he is not a malicious exploiter.

        <snip>
        Let me repeat the excellent advice to post to comp.unix.progr ammer.
        By all means.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Antoninus Twink

          #5
          Re: system call as variabe

          On 9 Jul 2008 at 21:50, Keith Thompson wrote:
          As it happens, any system on which "/bin/uname -m" is a valid command
          will have other methods to achieve the same result, using non-portable
          functions. Using a pipe should work, but invoking the underlying
          uname() function directly is cleaner and really no less portable.
          Cleaner and safer.

          Here's a simple example:

          #include <stdio.h>
          #include <sys/utsname.h>

          int main(void)
          {
          struct utsname buf;
          if(uname(&buf)= =0)
          printf("sysname : %s\nnodename: %s\nrelease: %s\nversion: %s\nmachine: %s\n",
          buf.sysname, buf.nodename, buf.release, buf.version, buf.machine);
          return 0;
          }

          Comment

          • Antoninus Twink

            #6
            Re: system call as variabe

            On 9 Jul 2008 at 22:00, Richard Heathfield wrote:
            One obvious improvement is to have the command string read in at
            runtime, allowing the user to provide a command that is relevant to
            the host system.
            An improvement, eh? Let's all pray we never have to rely on the security
            of your software. Do you also like your programs to run with root
            privileges for good measure?

            Comment

            Working...