Main Page | Modules | Data Structures | File List | Data Fields

env.c

00001 /*
00002  * Copyright (c) 2005, 2006 by KoanLogic s.r.l. - All rights reserved.
00003  */
00004 
00005 static const char rcsid[] =
00006     "$Id: env.c,v 1.2 2006/11/27 14:11:13 tho Exp $";
00007 
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 
00011 #include <string.h>
00012 #include <stdlib.h>
00013 #include <stdio.h>
00014 
00015 #include <toolbox/env.h>
00016 #include <toolbox/carpal.h>
00017 #include <toolbox/misc.h>
00018 
00040 int u_env_init (const char *prefix, const char *cfile)
00041 {
00042     enum { BUFSZ = 1024 };
00043     struct stat sb;
00044     char line[BUFSZ], pcmd[BUFSZ], *val;
00045     FILE *pi = NULL;
00046 
00047     dbg_return_if (cfile == NULL || prefix == NULL, ~0);
00048 
00049     /* if 'cfile' does not exist bail out */
00050     warn_err_sifm (stat(cfile, &sb) == -1, "%s", cfile);
00051 
00052     snprintf(pcmd, BUFSZ, ". %s 2>/dev/null && env", cfile);
00053 
00054     dbg_err_if ((pi = popen(pcmd, "r")) == NULL);
00055 
00056     while(fgets(line, BUFSZ-1, pi))
00057     {
00058         if(strncmp(line, prefix, strlen(prefix)) == 0)
00059         {
00060             line[strlen(line)-1] = 0;
00061             val = strchr(line, '=');
00062             if(!val)
00063                 continue; /* should never happen... */
00064             *val++ = 0;
00065             /* line is the name and val the value */
00066             dbg_err_if(setenv(line, val, 1));
00067         }
00068     }
00069 
00070     pclose(pi);
00071     return 0;
00072 
00073 err:
00074     U_PCLOSE(pi);
00075     return ~0;
00076 }
00077 
00088 const char *u_env_var (const char *name)
00089 {
00090     return getenv(name);
00091 }
00092 

←Products
© 2005-2006 - KoanLogic S.r.l. - All rights reserved