Date: Fri, 23 Dec 2005 00:04:42 EST-10EDT,10,-1,0,7200,3,-1,0,7200,3600 Subject: [UnixOS2_Archive] No. 675 ************************************************** Thursday 22 December 2005 Number 675 ************************************************** Subjects for today 1 Re: Code to find configuration files in %ETC% : Yuri Dario" 2 Re: Code to find configuration files in %ETC% : Stefan.Neis at t-online.de **= Email 1 ==========================** Date: Wed, 21 Dec 2005 16:00:12 +0100 (CET) From: "Yuri Dario" Subject: Re: Code to find configuration files in %ETC% Hi Paul, >#ifdef __INNOTEK_LIBC__ >const char *env_path; (line 83) >env_path=getenv("ETC"); >#undef ns_g_conffile >#define ns_g_conffile env_path >#endif you should consider getenv() could return NULL, but I think standard OS/2 installations will always define ETC. Inside an header will make env_path to be created before reaching main(), and probably this is possible only for C++ code (I think C compiler doesn't allow it). BTW a better solution is to #define ns_g_conffile my_ns_g_conffile() and write such function in some os2 specific file. This kind of approach works most of the times, and the compiler will directly complain if the statement syntax is not correct (e.g. when string is concatenad in source code). Bye, Yuri Dario /* * member of TeamOS/2 - Italy * http://www.os2power.com/yuri * http://www.teamos2.it */ **= Email 2 ==========================** Date: Wed, 21 Dec 2005 16:19:35 +0100 From: Stefan.Neis at t-online.de Subject: Re: Code to find configuration files in %ETC% Hi, > #ifdef __INNOTEK_LIBC__ > const char *env_path; (line 83) > env_path=getenv("ETC"); > #undef ns_g_conffile > #define ns_g_conffile env_path > #endif > Anyone have any ideas why the same code works in a function and not in a > header? Well, "statements" e.g. assignments are illegal outside of functions, all that is allowed are declarations - at most you can do a declaration with an initialization like: const char *env_path=getenv("ETC"); IIRC, C (as opposed to C++) is relatively strict as to what kind of initialization is allowed, I don't think calling a function will work. In general I'd recommend to declare the variable as "extern" an initialize it at the very beginning of "main", i.e. have #ifdef __INNOTEK_LIBC__ extern const char *env_path; #undef ns_g_conffile #define ns_g_conffile env_path #endif in global.h and in the c-file containing main, do an additional const char *env_path (which tells the compiler: "This is the module where I want memory for the global variable allocated") and at the of main do the assignment of the value (env_path=getenv("ETC");). Or if your code might allow changing ETC during runtime, having #define ns_g_conffile (env_path=getenv("ETC)) and not doing the intialization at the start of main might be worth trying. HTH, Stefan