Van wrote:
> 
> Greetings:
> 
> Working on a shell based application that does an insert when a shell is started,
> and, needs to update that record on exit.
> 
> You can't set an environment value back to the parent process, so would a fork()
> make the most sense?
> 
> This currently works in a bash shell .profile implementation.
> MYSQL_REC_NO=`date '+%Y%m%d_%H%M%S'`
> 
> I want to do this in a c application, instead.
> setenv("MYSQL_REC_NO",THE_DATE, 1); only works within the program and
> MYSQL_REC_NO dies on return from main.
> 
> Anyone doing something similar?
> 
> Regards,
> Van

Greetings:

No need to reply.  Just closing this discussion and providing what might save
someone else some time if ever faced with a similar situation.  Of course, if
any Windows programmer wants to port a static version of this I wouldn't be
offended to not have to do the work myself.  >:)

Since the shell app requires a shell and is only useful to a shell session, it
doesn't make much sense to create a shell from a shell that only needs to record
when the parent shell has exited.  The following implementation does the job
without incurring more overhead by a fork() than adding an environment variable:

In .profile add:
`~/btimeshell -s -K$TICKET_NO` 
trap "~/btimeshell -s -K$TICKET_NO --CALL_TYPE='Log Out'" 0

The first call sets the environment variable, and, the second call uses the
environment variable when it receives sig 0 {pretty much undefined, but, seems
to be SIG_EXIT, or something similar}.

Main calls:
my_init();
load_defaults ("my", groups, &argc, &argv);
/* among many other things along with processing */

Relevant structure info for load_defaults:
struct option long_options[] =
{
/* some stuff */
        {"CALL_TYPE",   optional_argument, NULL, 'T'},
        {"TICKET_NO",  optional_argument, NULL, 'K'},
/* more stuff */
        {0,0,0,0}
};

And, the while call:
        while ((opt = getopt_long (argc, argv, "T::U::u::C::B::K::p::vhsrS",
                                        long_options, &option_index)) != EOF)

Which grabs the TICKET_NO variable if present in the environment in:
                        case 'K':
                        /* Set TICKET_NO */
                                TICKET_NO = optarg;
                                setenv("TICKET_NO", TICKET_NO, 1);
                                TICKET_NO = ticket_no();
                                strncpy(syscmd, "export TICKET_NO=", sizeof(syscmd));
                                strncat (syscmd, TICKET_NO, sizeof(syscmd));
                                system (syscmd);
                                printf("%s\n", syscmd);
                                break;

The above printf call when enclosed within a `{some command}` construct exports
the TICKET_NO variable to the shell so a fork isn't necessary.  It's the only
variable necessary for the trap 0 call in .profile to pair up with the initial
insert that assigned the TICKET_NO variable.

The ticket_no() function follows:
char *ticket_no()
/* Set the Ticket Number,  Return formatted */
{
struct tm *datetime;    /* For current time */
time_t  t;                                      
static char ticket_no[15+1];

        if (getenv("TICKET_NO") != NULL &&
                        strlen(getenv("TICKET_NO")) == 15)
        /* For passing an existing TICKET_NO, unless it's been monkeyed with */
        {
                strncpy (ticket_no, getenv("TICKET_NO"), sizeof(ticket_no));
        } else {
          /*    Get the current date and format it for the Ticket # */
          tzset();
          time(&t);
          datetime = localtime(&t);
                if ( t == -1) {
                        perror("Time is munged");
                        exit(EXIT_FAILURE);
                }               
                strftime(ticket_no, sizeof(ticket_no), "%Y%m%d_%H%M%S", datetime);
        }

        return ticket_no;
}

--
Regards,
Van
-- 
=================================================================
Linux rocks!!!   http://www.dedserius.com/
=================================================================

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to