4 #define _XOPEN_SOURCE 700
5 #define _FILE_OFFSET_BITS 64
14 #include <sys/types.h>
35 const size_t len = strlen(path);
39 if (len > 1 && path[1] !=
'/') {
40 while (path[idx] && path[idx] !=
'/') {
44 user = g_strndup(path + 1, idx - 1);
50 if (home_path == NULL) {
51 return g_strdup(path);
54 rpath = g_build_filename(home_path, path + idx, NULL);
57 rpath = g_strdup(path);
66 if (uri == NULL || strlen(uri) == 0) {
70 GString* command = g_string_new(
"xdg-open ");
71 char* tmp = g_shell_quote(uri);
73 g_string_append(command, tmp);
77 bool res = g_spawn_command_line_async(command->str, &error);
83 g_string_free(command, TRUE);
90 if (user == NULL || g_strcmp0(user, g_get_user_name()) == 0) {
91 const char* homedir = g_getenv(
"HOME");
92 return g_strdup(homedir ? homedir : g_get_home_dir());
97 struct passwd* result;
98 #ifdef _SC_GETPW_R_SIZE_MAX
99 int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
107 char* buffer = g_malloc0(
sizeof(
char) * bufsize);
109 getpwnam_r(user, &pwd, buffer, bufsize, &result);
110 if (result == NULL) {
115 char* dir = g_strdup(pwd.pw_dir);
123 static const char* VARS[] = {
130 static const char* DEFAULTS[] = {
134 "/usr/local/share/:/usr/share",
139 return g_strdup(g_get_user_data_dir());
141 return g_strdup(g_get_user_config_dir());
145 const char* tmp = g_getenv(VARS[path]);
146 if (tmp == NULL || !g_strcmp0(tmp,
"")) {
147 return g_strdup(DEFAULTS[path]);
149 return g_strdup(tmp);
159 if (patharray == NULL || !g_strcmp0(patharray,
"")) {
164 char** paths = g_strsplit(patharray,
":", 0);
165 for (
unsigned int i = 0; paths[i] !=
'\0'; ++i) {
178 if (fixed_path == NULL || mode == NULL) {
182 FILE* fp = fopen(fixed_path, mode);
233 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
242 char* line = fgetln(file, &size);
247 char* copy = strndup(line, size);
253 g_strdelimit(copy,
"\n\r",
'\0');
267 if (getline(&line, &size, file) == -1) {
275 g_strdelimit(line,
"\n\r",
'\0');
304 const off_t curpos = ftello(file);
309 fseeko(file, 0, SEEK_END);
310 const off_t size = ftello(file) - curpos;
311 fseeko(file, curpos, SEEK_SET);
314 char* content = malloc(1);
319 if ((uintmax_t)size >= (uintmax_t)SIZE_MAX) {
324 char* buffer = malloc(size + 1);
329 size_t read = fread(buffer, size, 1, file);
350 for(i = 0; i < strlen(line); i++) {
351 if (isspace(line[i]) != 0) {
378 void* tmp = realloc(*ptr, size);
409 fprintf(stderr,
"warning: ");
412 fprintf(stderr,
"error: ");
415 fprintf(stderr,
"info: ");
418 fprintf(stderr,
"debug: (%s:%d) ",
function, line);
425 va_start(ap, format);
426 vfprintf(stderr, format, ap);
429 fprintf(stderr,
"\n");