4 #if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
5 #define _XOPEN_SOURCE 700
7 #define _FILE_OFFSET_BITS 64
16 #include <sys/types.h>
23 #include <glib/gi18n-lib.h>
41 const size_t len = strlen(path);
45 if (len > 1 && path[1] !=
'/') {
46 while (path[idx] && path[idx] !=
'/') {
50 user = g_strndup(path + 1, idx - 1);
56 if (home_path == NULL) {
57 return g_strdup(path);
60 rpath = g_build_filename(home_path, path + idx, NULL);
63 rpath = g_strdup(path);
72 if (uri == NULL || strlen(uri) == 0) {
76 GString* command = g_string_new(
"xdg-open ");
77 char* tmp = g_shell_quote(uri);
79 g_string_append(command, tmp);
83 bool res = g_spawn_command_line_async(command->str, &error);
89 g_string_free(command, TRUE);
96 if (user == NULL || g_strcmp0(user, g_get_user_name()) == 0) {
97 const char* homedir = g_getenv(
"HOME");
98 return g_strdup(homedir ? homedir : g_get_home_dir());
103 struct passwd* result;
104 #ifdef _SC_GETPW_R_SIZE_MAX
105 int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
113 char* buffer = g_malloc0(
sizeof(
char) * bufsize);
115 getpwnam_r(user, &pwd, buffer, bufsize, &result);
116 if (result == NULL) {
121 char* dir = g_strdup(pwd.pw_dir);
129 static const char* VARS[] = {
136 static const char* DEFAULTS[] = {
140 "/usr/local/share/:/usr/share",
145 return g_strdup(g_get_user_data_dir());
147 return g_strdup(g_get_user_config_dir());
151 const char* tmp = g_getenv(VARS[path]);
152 if (tmp == NULL || !g_strcmp0(tmp,
"")) {
153 return g_strdup(DEFAULTS[path]);
155 return g_strdup(tmp);
165 if (patharray == NULL || !g_strcmp0(patharray,
"")) {
170 char** paths = g_strsplit(patharray,
":", 0);
171 for (
size_t i = 0; paths[i] != NULL; ++i) {
184 if (fixed_path == NULL || mode == NULL) {
188 FILE* fp = fopen(fixed_path, mode);
239 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
248 char* line = fgetln(file, &size);
253 char* copy = strndup(line, size);
259 g_strdelimit(copy,
"\n\r",
'\0');
273 if (getline(&line, &size, file) == -1) {
281 g_strdelimit(line,
"\n\r",
'\0');
310 const off_t curpos = ftello(file);
315 fseeko(file, 0, SEEK_END);
316 const off_t size = ftello(file) - curpos;
317 fseeko(file, curpos, SEEK_SET);
320 char* content = malloc(1);
325 if ((uintmax_t)size >= (uintmax_t)SIZE_MAX) {
330 char* buffer = malloc(size + 1);
335 size_t read = fread(buffer, size, 1, file);
356 for(i = 0; i < strlen(line); i++) {
357 if (isspace(line[i]) != 0) {
384 void* tmp = realloc(*ptr, size);
405 if (level < debug_level) {
412 fprintf(stderr,
"warning: ");
415 fprintf(stderr,
"error: ");
418 fprintf(stderr,
"info: ");
421 fprintf(stderr,
"debug: (%s:%d) ",
function, line);
428 va_start(ap, format);
429 vfprintf(stderr, format, ap);
432 fprintf(stderr,
"\n");
454 if ((keyval >=
'!' && keyval <=
'/')
455 || (keyval >=
':' && keyval <=
'@')
456 || (keyval >=
'[' && keyval <=
'`')
457 || (keyval >=
'{' && keyval <=
'~')
459 *state |= GDK_SHIFT_MASK;
469 GString* str = g_string_new(
"");
470 while (*value !=
'\0') {
471 const char c = *value++;
472 if (strchr(
"\\ \t\"\'", c) != NULL) {
473 g_string_append_c(str,
'\\');
475 g_string_append_c(str, c);
478 return g_string_free(str, FALSE);
484 if (session == NULL || argument_list == NULL) {
490 if (cmd == NULL || strlen(cmd) == 0) {
491 girara_debug(
"exec-command is empty, executing directly.");
496 bool dont_append_first_space = cmd == NULL;
497 GString* command = g_string_new(cmd ? cmd :
"");
501 if (dont_append_first_space ==
false) {
502 g_string_append_c(command,
' ');
504 dont_append_first_space =
false;
505 char* tmp = g_shell_quote(value);
506 g_string_append(command, tmp);
510 GError* error = NULL;
512 gboolean ret = g_spawn_command_line_async(command->str, &error);
519 g_string_free(command, TRUE);