28 #include <system_helper.h>
31 #include <mach-o/dyld.h>
39 #include <sys/utsname.h>
47 using namespace basis;
54 #define LOG(to_print) printf("%s\n", astring(to_print).s())
61 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
62 astring application_configuration::get_cmdline_from_proc()
64 FUNCDEF(
"get_cmdline_from_proc");
65 static astring __check_once_app_path;
67 if (__check_once_app_path.
length())
return __check_once_app_path;
70 __check_once_app_path = query_for_process_info();
71 return __check_once_app_path;
75 a_sprintf cmds_filename(
"/proc/%d/cmdline", process_id());
76 FILE *cmds_file = fopen(cmds_filename.
s(),
"r");
78 LOG(
"failed to open our process's command line file.\n");
83 char *filebuff =
new char[size + 1];
84 ssize_t chars_read =
getline((
char **)&filebuff, &size, cmds_file);
87 if (!chars_read ||
negative(chars_read)) {
88 LOG(
"failed to get any characters from our process's cmdline file.\n");
93 __check_once_app_path = filebuff;
97 for (
int i = __check_once_app_path.
length() - 1; i >= 0; i--) {
98 if (__check_once_app_path[i] ==
'"') __check_once_app_path.
zap(i, i);
102 filename testing(__check_once_app_path);
111 system((
astring(
"which ") + __check_once_app_path +
" >" + temp_filename).s());
112 FILE *which_file = fopen(temp_filename.
s(),
"r");
114 LOG(
"failed to open the temporary output from which.\n");
119 filebuff =
new char[size + 1];
120 chars_read =
getline((
char **)&filebuff, &size, which_file);
123 if (!chars_read ||
negative(chars_read)) {
124 LOG(
"failed to get any characters from the which cmd output.\n");
128 __check_once_app_path = filebuff;
130 __check_once_app_path.
zap(__check_once_app_path.
end(), __check_once_app_path.
end());
134 return __check_once_app_path;
138 astring application_configuration::query_for_process_info()
140 FUNCDEF(
"query_for_process_info");
145 a_sprintf tmpfile(
"/tmp/proc_name_check_%d_%d.txt", process_id(),
148 a_sprintf cmd(
"ps -o args=\"\" %d >%s", process_id(),
151 a_sprintf cmd(
"ps h -O \"args\" %d >%s", process_id(),
155 int sysret = system(cmd.
s());
157 LOG(
"failed to run ps command to get process info");
161 FILE *output = fopen(tmpfile.
s(),
"r");
163 LOG(
"failed to open the ps output file");
171 while (size_read > 0) {
172 const char to_check = buff[size_read - 1];
173 if ( !to_check || (to_check ==
'\r') || (to_check ==
'\n')
174 || (to_check ==
'\t') )
181 LOG(
"could not read output of process list");
189 #define SET_BOGUS_NAME(error) { \
193 unlink(tmpfile.s()); \
195 astring home_dir = environment::get("HOME"); \
196 to_return = home_dir + "/failed_to_determine.exe"; \
199 astring application_configuration::application_name()
206 _NSGetExecutablePath(buffer, &buffsize);
207 to_return = (
char *)buffer;
208 #elif defined(__UNIX__) || defined(__GNU_WINDOWS__)
209 to_return = get_cmdline_from_proc();
219 #pragma error("hmmm: no means of finding app name is implemented.")
225 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
229 #pragma error("hmmm: need process id implementation for this OS!")
230 basis::un_int application_configuration::process_id() {
return 0; }
233 astring application_configuration::current_directory()
245 #pragma error("hmmm: need support for current directory on this OS.")
252 const char *application_configuration::software_product_name()
254 #ifdef GLOBAL_PRODUCT_NAME
255 return GLOBAL_PRODUCT_NAME;
261 astring application_configuration::application_directory()
268 utsname kernel_parms;
269 uname(&kernel_parms);
270 to_return =
version(kernel_parms.release);
279 #pragma error("hmmm: need version info for this OS!")
288 astring application_configuration::application_configuration_file()
291 return cfg_file.
raw();
301 const astring &application_configuration::DEFAULT_VIRTUAL_UNIX_ROOT()
314 if (static_root_holder().length()) {
315 return static_root_holder();
319 static_root_holder() =
astring(
"/");
320 return static_root_holder();
327 astring virtual_root = FEISTY_MEOW_VIRTUAL_UNIX_ROOT;
330 return DEFAULT_VIRTUAL_UNIX_ROOT();
332 static_root_holder() = virtual_root;
333 return static_root_holder();
344 astring application_configuration::get_logging_directory()
354 astring log_dir = read_item(LOGGING_FOLDER_NAME());
360 ini_configurator::RETURN_ONLY,
361 ini_configurator::APPLICATION_DIRECTORY);
362 ini.
store(GLOBAL_SECTION_NAME(), LOGGING_FOLDER_NAME(), def_log);
366 log_dir = parser_bits::substitute_env_vars(log_dir);
373 bool okay = directory::recursive_create(log_dir);
375 LOG(
astring(
"failed to create logging directory: ") + log_dir);
390 {
return get_logging_directory() +
"/" +
base_name; }
394 filename ini_name = application_configuration_file();
396 ini_configurator::APPLICATION_DIRECTORY);
397 astring to_return = ini.
load(GLOBAL_SECTION_NAME(), key_name,
"");
401 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(filepointer *filep)
char * base_name(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.