27 #include <system_helper.h>
30 #include <mach-o/dyld.h>
38 #include <sys/utsname.h>
46 using namespace basis;
53 #define LOG(to_print) printf("%s\n", astring(to_print).s())
60 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
61 astring application_configuration::get_cmdline_from_proc()
63 FUNCDEF(
"get_cmdline_from_proc");
64 static astring __check_once_app_path;
66 if (__check_once_app_path.
length())
return __check_once_app_path;
69 __check_once_app_path = query_for_process_info();
70 return __check_once_app_path;
74 a_sprintf cmds_filename(
"/proc/%d/cmdline", process_id());
75 FILE *cmds_file = fopen(cmds_filename.
s(),
"r");
77 LOG(
"failed to open our process's command line file.\n");
82 char *filebuff =
new char[size + 1];
83 ssize_t chars_read =
getline((
char **)&filebuff, &size, cmds_file);
86 if (!chars_read ||
negative(chars_read)) {
87 LOG(
"failed to get any characters from our process's cmdline file.\n");
92 __check_once_app_path = filebuff;
96 for (
int i = __check_once_app_path.
length() - 1; i >= 0; i--) {
97 if (__check_once_app_path[i] ==
'"') __check_once_app_path.
zap(i, i);
101 filename testing(__check_once_app_path);
110 system((
astring(
"which ") + __check_once_app_path +
" >" + temp_filename).s());
111 FILE *which_file = fopen(temp_filename.
s(),
"r");
113 LOG(
"failed to open the temporary output from which.\n");
118 filebuff =
new char[size + 1];
119 chars_read =
getline((
char **)&filebuff, &size, which_file);
122 if (!chars_read ||
negative(chars_read)) {
123 LOG(
"failed to get any characters from the which cmd output.\n");
127 __check_once_app_path = filebuff;
129 __check_once_app_path.
zap(__check_once_app_path.
end(), __check_once_app_path.
end());
133 return __check_once_app_path;
137 astring application_configuration::query_for_process_info()
139 FUNCDEF(
"query_for_process_info");
144 a_sprintf tmpfile(
"/tmp/proc_name_check_%d_%d.txt", process_id(),
147 a_sprintf cmd(
"ps -o args=\"\" %d >%s", process_id(),
150 a_sprintf cmd(
"ps h -O \"args\" %d >%s", process_id(),
154 int sysret = system(cmd.
s());
156 LOG(
"failed to run ps command to get process info");
160 FILE *output = fopen(tmpfile.
s(),
"r");
162 LOG(
"failed to open the ps output file");
170 while (size_read > 0) {
171 const char to_check = buff[size_read - 1];
172 if ( !to_check || (to_check ==
'\r') || (to_check ==
'\n')
173 || (to_check ==
'\t') )
180 LOG(
"could not read output of process list");
188 #define SET_BOGUS_NAME(error) { \
192 unlink(tmpfile.s()); \
194 astring home_dir = environment::get("HOME"); \
195 to_return = home_dir + "/failed_to_determine.exe"; \
198 astring application_configuration::application_name()
205 _NSGetExecutablePath(buffer, &buffsize);
206 to_return = (
char *)buffer;
207 #elif defined(__UNIX__) || defined(__GNU_WINDOWS__)
208 to_return = get_cmdline_from_proc();
218 #pragma error("hmmm: no means of finding app name is implemented.")
224 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
228 #pragma error("hmmm: need process id implementation for this OS!")
229 basis::un_int application_configuration::process_id() {
return 0; }
232 astring application_configuration::current_directory()
244 #pragma error("hmmm: need support for current directory on this OS.")
251 const char *application_configuration::software_product_name()
253 #ifdef GLOBAL_PRODUCT_NAME
254 return GLOBAL_PRODUCT_NAME;
260 astring application_configuration::application_directory()
267 utsname kernel_parms;
268 uname(&kernel_parms);
269 to_return =
version(kernel_parms.release);
278 #pragma error("hmmm: need version info for this OS!")
287 astring application_configuration::application_configuration_file()
290 return cfg_file.
raw();
300 const astring &application_configuration::DEFAULT_VIRTUAL_UNIX_ROOT()
313 if (static_root_holder().length()) {
314 return static_root_holder();
318 static_root_holder() =
astring(
"/");
319 return static_root_holder();
326 astring virtual_root = FEISTY_MEOW_VIRTUAL_UNIX_ROOT;
329 return DEFAULT_VIRTUAL_UNIX_ROOT();
331 static_root_holder() = virtual_root;
332 return static_root_holder();
343 astring application_configuration::get_logging_directory()
353 astring log_dir = read_item(LOGGING_FOLDER_NAME());
359 ini_configurator::RETURN_ONLY,
360 ini_configurator::APPLICATION_DIRECTORY);
361 ini.
store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
365 log_dir = parser_bits::substitute_env_vars(log_dir);
372 bool okay = directory::recursive_create(log_dir);
374 LOG(
astring(
"failed to create logging directory: ") + log_dir);
389 {
return get_logging_directory() +
"/" +
base_name; }
393 filename ini_name = application_configuration_file();
395 ini_configurator::APPLICATION_DIRECTORY);
396 astring to_return = ini.
load(GLOBAL_SECTION_NAME(), key_name,
"");
400 to_return = parser_bits::substitute_env_vars(to_return);
#define SET_BOGUS_NAME(error)
a_sprintf is a specialization of astring that provides printf style support.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
void reset()
clears out the contents string.
int end() const
returns the index of the last (non-null) character in the string.
int length() const
Returns the current length of the string.
static astring TMP()
provides a single place to get the temporary directory.
Defines installation-specific locations in the file system.
bool store(const basis::astring §ion, const basis::astring &entry, const basis::astring &to_store)
a synonym for put.
basis::astring load(const basis::astring §ion, const basis::astring &entry, const basis::astring &default_value)
a synonym for get that implements the auto-store behavior.
Supports a configurator-based interface on text initialization files.
Provides operations commonly needed on file names.
bool exists() const
returns true if the file exists.
bool had_directory() const
returns true if the name that we were given had a non-empty directory.
const basis::astring & raw() const
returns the astring that we're holding onto for the path.
filename dirname() const
returns the directory for the filename.
a platform-independent way to acquire random numbers in a specific range.
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
Holds a file's version identifier.
char * getline(register filepointer *filep)
char * base_name(register char *file)
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
The guards collection helps in testing preconditions and reporting errors.
unsigned int un_int
Abbreviated name for unsigned integers.
bool negative(const type &a)
negative returns true if "a" is less than zero.
const int KILOBYTE
Number of bytes in a kilobyte.
const int MAXIMUM_COMMAND_LINE
const char * PATH_CONFIGURATION_FILENAME()
A platform independent way to obtain the timestamp of a file.
An extension to floating point primitives providing approximate equality.
A dynamic container class that holds any kind of object via pointers.
bool is_eol(char to_check)
#define STATIC_STRING(str)
Statically defines a string for the rest of the program's life.
#define SAFE_STATIC(type, func_name, parms)
Statically defines a singleton object whose scope is the program's lifetime.
Support for unicode builds.
Aids in achievement of platform independence.