Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Create new environment for process.
0003  *
0004  * @file newenv.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <errno.h>
0038 #include <memory.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 
0043 #include "userlog.h"
0044 
0045 
0046 /*---------------------------Externs------------------------------------*/
0047 /*---------------------------Macros-------------------------------------*/
0048 /*---------------------------Enums--------------------------------------*/
0049 /*---------------------------Typedefs-----------------------------------*/
0050 /*---------------------------Globals------------------------------------*/
0051 /*---------------------------Statics------------------------------------*/
0052 /*---------------------------Prototypes---------------------------------*/
0053 
0054 /**
0055  * Strip whitespace from string.
0056  * @param pp
0057  */
0058 exprivate char * strip_whitespace(char *pp)
0059 {
0060     while (' '==pp[0] || '\t'==pp[0])
0061     {
0062         pp++;
0063     }
0064     
0065     return pp;
0066 }
0067 
0068 /**
0069  * Build environment
0070  * @param p_pm
0071  * @return 
0072  */
0073 expublic int ndrx_load_new_env(char *file)
0074 {
0075     int ret=EXSUCCEED;
0076     FILE *f;
0077     char line [8192];
0078     char *p;
0079     char *e;
0080     long line_no=0;
0081     int len;
0082     
0083     /* Just open the file, and load new env.... */
0084     
0085     if (NULL==(f=NDRX_FOPEN(file, "r")))
0086     {
0087         NDRX_LOG(log_error, "Failed to open environment override file [%s]:%s",
0088                             file, strerror(errno));
0089         EXFAIL_OUT(ret);
0090     }
0091    
0092     while ( fgets ( line, sizeof(line), f ) != NULL ) /* read a line */
0093     {
0094         p = line;
0095         line_no++;
0096         
0097         /* strip trailing newline */
0098         len = strlen(p);
0099         if (len > 0 && '\n'==p[len-1])
0100         {
0101             p[len-1]=EXEOS;
0102         }
0103         
0104         NDRX_LOG(log_debug, "%d:env over: [%s]", line_no, line);
0105         
0106         
0107         /* strip leading white space */
0108         p = strip_whitespace(p);
0109         
0110         /* Check isn't it comment */
0111         if ('#' == line[0] || EXEOS == line[0])
0112         {
0113             continue;
0114         }
0115         
0116         if (0==strncmp(line, "export ", 7) || 0==strncmp(line, "export\t", 7))
0117         {
0118             p+=7;
0119         }
0120         
0121         /* strip leading white space (one more time) */
0122         p = strip_whitespace(p);
0123         
0124         /* now find equal sign... */
0125         e = strchr(p, '=');
0126         if (NULL==e)
0127         {
0128             NDRX_LOG(log_error, "Error on line %d: No equal "
0129                     "sign found in [%s]", line_no, p);
0130             EXFAIL_OUT(ret);
0131         }
0132         
0133         /* we are OK, we have key & value */
0134         *e=EXEOS;
0135         e++; /* << This is value and p is key */
0136         NDRX_LOG(log_debug, "Key: [%s], value: [%s]", p, e);
0137         
0138         if (EXSUCCEED!=setenv(p, e, EXTRUE))
0139         {
0140             NDRX_LOG(log_error, "Failed to set env: %s", strerror(errno));
0141             EXFAIL_OUT(ret);
0142         }
0143     }
0144         
0145 out:
0146 
0147     if (NULL!=f)
0148         NDRX_FCLOSE(f);
0149 
0150     return ret;
0151 }
0152 /* vim: set ts=4 sw=4 et smartindent: */