How do I export a environmental variable to a C program?

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

    How do I export a environmental variable to a C program?

    Hi all ,

    I want to set a env variable in a C program? . Not through shell env
    variables. Any help?

    Thanks
    Vijay
  • Antoninus Twink

    #2
    Re: How do I export a environmental variable to a C program?

    On 9 May 2008 at 17:26, iavian wrote:
    I want to set a env variable in a C program? . Not through shell env
    variables. Any help?
    What do you mean "not through shell env variables"?

    If you're on a POSIX system, check out setenv() and putenv().

    Comment

    • dj3vande@csclub.uwaterloo.ca.invalid

      #3
      Re: How do I export a environmental variable to a C program?

      In article <5eca7cf1-8080-4625-b2c1-383808d530b7@b1 g2000hsg.google groups.com>,
      iavian <vijay@iavian.c omwrote:
      >Hi all ,
      >
      >I want to set a env variable in a C program? . Not through shell env
      >variables. Any help?
      How an environment variable gets set is beyond the scope of the C
      language.
      They're typically assigned through an OS-defined mechanism when the
      process is created. A newsgroup whose scope includes how to start
      processes on your OS may be able to provide more specific guidance.

      Many C implementations also provide extensions that allow environment
      variables to be set from within the program. Your implementation' s
      documentation may shed some light on that.


      dave

      --
      Dave Vandervies dj3vande at eskimo dot com
      He'll be buried face down, nine edge first. Unlike most people of whom
      that is said, in his case, it's a compliment.
      --Richard Bos, on John Backus, in comp.lang.c

      Comment

      • Walter Roberson

        #4
        Re: How do I export a environmental variable to a C program?

        In article <5eca7cf1-8080-4625-b2c1-383808d530b7@b1 g2000hsg.google groups.com>,
        iavian <vijay@iavian.c omwrote:
        >I want to set a env variable in a C program? . Not through shell env
        >variables. Any help?
        C does not provide any mechanism to set environment variables.

        It is common for OSes to provide a setenv() routine in their libraries.

        Note that when OSes do so provide, often setenv() only affects
        the environment of the running program and any programs that it
        invokes after that point, such as by system() or by OS routines
        such as exec*() . Unix-like systems often have *no* mechanism
        that would allow a program to change the environment variables of
        other programs that are already running.

        --
        "All is vanity." -- Ecclesiastes

        Comment

        • Jens Thoms Toerring

          #5
          Re: How do I export a environmental variable to a C program?

          iavian <vijay@iavian.c omwrote:
          I want to set a env variable in a C program? . Not through shell env
          variables. Any help?
          This is not a C question since this only depends on how your
          shell handles things. But, that out of the way, you simply
          can't do that (at least with no shell I ever heard of). Your
          C program is a process started by your shell and such a child-
          process can never change anything in its "parent" process.

          The only thing you can do is to have the the program output
          something the shell then explicitely executes, like (e.g.
          using bash):

          ---- env_cmd.c -----

          #include <stdio.h>

          int main( void )
          {
          puts( "export X=AAA" );
          return 0;
          }

          --------------------

          and then do e.g.
          gcc -o env_cmd env_cmd.c
          `./env_cmd` # note the backticks
          echo $X
          AAA
          Regards, Jens
          --
          \ Jens Thoms Toerring ___ jt@toerring.de
          \______________ ____________ http://toerring.de

          Comment

          • Keith Thompson

            #6
            Re: How do I export a environmental variable to a C program?

            jt@toerring.de (Jens Thoms Toerring) writes:
            iavian <vijay@iavian.c omwrote:
            >I want to set a env variable in a C program? . Not through shell env
            >variables. Any help?
            >
            This is not a C question since this only depends on how your
            shell handles things. But, that out of the way, you simply
            can't do that (at least with no shell I ever heard of). Your
            C program is a process started by your shell and such a child-
            process can never change anything in its "parent" process.
            The OP didn't seem to be asking about propagating the setting to the
            parent process (assuming that "parent process" is meaningful). As far
            as I can tell, the POSIX setenv() function (not standard C) would
            satisfy the requirement he stated. Whether it would satisfy his
            actual requirement is another question.

            <OT>In Unix-like systems, there are various ways for a process to set
            one of its own environment variables in response to information from a
            child process. You demonstrated one of them in part of your article
            that I ruthlessly snipped; other ways are possible.
            comp.unix.progr ammer would be a better place to discuss it -- after
            checking any FAQ lists, since this kind of question comes up
            frequently.</OT>

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

            Comment

            • iavian

              #7
              Re: How do I export a environmental variable to a C program?

              Thanks for all who replied ..

              I am writing a function for postgres in C which connect to Oracle
              db .. postgres accepts a shared object to be loaded . So, when the .so
              is loaded inside postgres , .so could find oracle environments.. I am
              not sure what should be the best way to do it ..

              whenever a row is inserted in postgres , a trigger will call this C
              function which connects to oracle and inserts the record in Oracle db

              Any pointers?

              My problem is
              On May 9, 2:52 pm, Keith Thompson <ks...@mib.orgw rote:
              j...@toerring.d e (Jens Thoms Toerring) writes:
              >
              iavian <vi...@iavian.c omwrote:
              I want to set a env variable in a C program? . Not through shell env
              variables. Any help?
              >
              This is not a C question since this only depends on how your
              shell handles things. But, that out of the way, you simply
              can't do that (at least with no shell I ever heard of). Your
              C program is a process started by your shell and such a child-
              process can never change anything in its "parent" process.
              >
              The OP didn't seem to be asking about propagating the setting to the
              parent process (assuming that "parent process" is meaningful). As far
              as I can tell, the POSIX setenv() function (not standard C) would
              satisfy the requirement he stated. Whether it would satisfy his
              actual requirement is another question.
              >
              <OT>In Unix-like systems, there are various ways for a process to set
              one of its own environment variables in response to information from a
              child process. You demonstrated one of them in part of your article
              that I ruthlessly snipped; other ways are possible.
              comp.unix.progr ammer would be a better place to discuss it -- after
              checking any FAQ lists, since this kind of question comes up
              frequently.</OT>
              >
              --
              Keith Thompson (The_Other_Keit h) <ks...@mib.or g>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • CBFalconer

                #8
                Re: How do I export a environmental variable to a C program?

                iavian wrote:
                >
                I am writing a function for postgres in C which connect to Oracle
                db .. postgres accepts a shared object to be loaded . So, when the
                .so is loaded inside postgres , .so could find oracle environments..
                I am not sure what should be the best way to do it ..
                >
                whenever a row is inserted in postgres , a trigger will call this C
                function which connects to oracle and inserts the record in Oracle db
                Top-posting has lost all connection to previous traffic in this
                thread. It was totally unnecessary.

                Please do not top-post. Your answer belongs after (or intermixed
                with) the quoted material to which you reply, after snipping all
                irrelevant material. See the following links:

                <http://www.catb.org/~esr/faqs/smart-questions.html>
                <http://www.caliburn.nl/topposting.html >
                <http://www.netmeister. org/news/learn2quote.htm l>
                <http://cfaj.freeshell. org/google/ (taming google)
                <http://members.fortune city.com/nnqweb/ (newusers)

                --
                [mail]: Chuck F (cbfalconer at maineline dot net)
                [page]: <http://cbfalconer.home .att.net>
                Try the download section.


                ** Posted from http://www.teranews.com **

                Comment

                • Keith Thompson

                  #9
                  Re: How do I export a environmental variable to a C program?

                  iavian <vijay@iavian.c omwrites:
                  I am writing a function for postgres in C which connect to Oracle
                  db .. postgres accepts a shared object to be loaded . So, when the .so
                  is loaded inside postgres , .so could find oracle environments.. I am
                  not sure what should be the best way to do it ..
                  >
                  whenever a row is inserted in postgres , a trigger will call this C
                  function which connects to oracle and inserts the record in Oracle db
                  >
                  Any pointers?
                  I think you've already been advised to ask in comp.unix.progr ammer (I
                  assume you're using a Unix-like system). That's the best advice
                  you're going to get here.

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

                  Comment

                  • Jack Klein

                    #10
                    Re: How do I export a environmental variable to a C program?

                    On Fri, 9 May 2008 10:26:48 -0700 (PDT), iavian <vijay@iavian.c om>
                    wrote in comp.lang.c:
                    Hi all ,
                    >
                    I want to set a env variable in a C program? . Not through shell env
                    variables. Any help?
                    The standard C library does not provide any functions to set
                    environment variables, just one to search for and read them.

                    All of today's most popular operating systems provided their own
                    system-specific extensions for doing this. You need to ask in a group
                    that supports your particular compiler/OS combination for details.

                    --
                    Jack Klein
                    Home: http://JK-Technology.Com
                    FAQs for
                    comp.lang.c http://c-faq.com/
                    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                    alt.comp.lang.l earn.c-c++

                    Comment

                    Working...