2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
22 #include <sys/param.h>
27 #include <gtk/gtk.h> /* for locale warning dialog */
30 #include "ui_fileops.h"
32 #include "ui_utildlg.h" /* for locale warning dialog */
35 *-----------------------------------------------------------------------------
36 * generic file information and manipulation routines (public)
37 *-----------------------------------------------------------------------------
42 void print_term(const gchar *text_utf8)
46 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
47 fputs((text_l) ? text_l : text_utf8, stdout);
51 static void encoding_dialog(const gchar *path);
53 static gint encoding_dialog_idle(gpointer data)
57 encoding_dialog(path);
63 static gint encoding_dialog_delay(gpointer data)
65 g_idle_add(encoding_dialog_idle, data);
70 static void encoding_dialog(const gchar *path)
72 static gint warned_user = FALSE;
78 /* check that gtk is initialized (loop is level > 0) */
79 if (gtk_main_level() == 0)
81 /* gtk not initialized */
82 gtk_init_add(encoding_dialog_delay, g_strdup(path));
86 if (warned_user) return;
90 bf = getenv("G_BROKEN_FILENAMES");
93 string = g_string_new("");
94 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
95 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
96 g_string_append(string, "\n");
97 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
99 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
101 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
102 g_string_append(string, "\n");
103 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
104 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
107 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
108 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
109 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
111 if (g_utf8_validate(path, -1, NULL))
112 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
114 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
115 g_string_append(string, "\n");
119 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
120 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL);
121 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
123 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
124 _("Filename encoding locale mismatch"), string->str);
126 gtk_widget_show(gd->dialog);
128 g_string_free(string, TRUE);
131 gchar *path_to_utf8(const gchar *path)
134 GError *error = NULL;
136 if (!path) return NULL;
138 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
141 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
143 encoding_dialog(path);
147 /* just let it through, but bad things may happen */
148 utf8 = g_strdup(path);
154 gchar *path_from_utf8(const gchar *utf8)
157 GError *error = NULL;
159 if (!utf8) return NULL;
161 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
164 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
169 /* if invalid UTF-8, text probaby still in original form, so just copy it */
170 path = g_strdup(utf8);
176 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
177 const gchar *homedir(void)
179 static gchar *home = NULL;
183 home = path_to_utf8(getenv("HOME"));
187 struct passwd *pw = getpwuid(getuid());
188 if (pw) home = path_to_utf8(pw->pw_dir);
194 gint stat_utf8(const gchar *s, struct stat *st)
199 if (!s) return FALSE;
200 sl = path_from_utf8(s);
201 ret = (stat(sl, st) == 0);
207 gint lstat_utf8(const gchar *s, struct stat *st)
212 if (!s) return FALSE;
213 sl = path_from_utf8(s);
214 ret = (lstat(sl, st) == 0);
220 gint isname(const gchar *s)
224 return stat_utf8(s, &st);
227 gint isfile(const gchar *s)
231 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
234 gint isdir(const gchar *s)
238 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
241 gint islink(const gchar *s)
245 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode));
248 gint64 filesize(const gchar *s)
252 if (!stat_utf8(s, &st)) return 0;
256 time_t filetime(const gchar *s)
260 if (!stat_utf8(s, &st)) return 0;
264 gint filetime_set(const gchar *s, time_t tval)
273 ut.actime = ut.modtime = tval;
275 sl = path_from_utf8(s);
276 ret = (utime(sl, &ut) == 0);
283 gint access_file(const gchar *s, int mode)
288 if (!s) return FALSE;
290 sl = path_from_utf8(s);
291 ret = (access(sl, mode) == 0);
297 gint unlink_file(const gchar *s)
302 if (!s) return FALSE;
304 sl = path_from_utf8(s);
305 ret = (unlink(sl) == 0);
311 gint symlink_utf8(const gchar *source, const gchar *target)
317 if (!source || !target) return FALSE;
319 sl = path_from_utf8(source);
320 tl = path_from_utf8(target);
322 ret = (symlink(sl, tl) == 0);
330 gint mkdir_utf8(const gchar *s, int mode)
335 if (!s) return FALSE;
337 sl = path_from_utf8(s);
338 ret = (mkdir(sl, mode) == 0);
343 gint rmdir_utf8(const gchar *s)
348 if (!s) return FALSE;
350 sl = path_from_utf8(s);
351 ret = (rmdir(sl) == 0);
357 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
363 if (!s || !t) return FALSE;
365 sl = path_from_utf8(s);
366 tl = path_from_utf8(t);
368 if (stat(sl, &st) == 0)
374 /* set the dest file attributes to that of source (ignoring errors) */
376 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
377 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
379 tb.actime = st.st_atime;
380 tb.modtime = st.st_mtime;
381 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
390 /* paths are in filesystem encoding */
391 static gint hard_linked(const gchar *a, const gchar *b)
396 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
398 return (sta.st_dev == stb.st_dev &&
399 sta.st_ino == stb.st_ino);
402 gint copy_file(const gchar *s, const gchar *t)
410 sl = path_from_utf8(s);
411 tl = path_from_utf8(t);
413 if (hard_linked(sl, tl))
420 fi = fopen(sl, "rb");
423 fo = fopen(tl, "wb");
434 if (!fi || !fo) return FALSE;
436 while ((b = fread(buf, sizeof(char), sizeof(buf), fi)) && b != 0)
438 if (fwrite(buf, sizeof(char), b, fo) != b)
449 copy_file_attributes(s, t, TRUE, TRUE);
454 gint move_file(const gchar *s, const gchar *t)
459 if (!s || !t) return FALSE;
461 sl = path_from_utf8(s);
462 tl = path_from_utf8(t);
463 if (rename(sl, tl) < 0)
465 /* this may have failed because moving a file across filesystems
466 was attempted, so try copy and delete instead */
471 /* err, now we can't delete the source file so return FALSE */
486 gint rename_file(const gchar *s, const gchar *t)
491 if (!s || !t) return FALSE;
493 sl = path_from_utf8(s);
494 tl = path_from_utf8(t);
495 ret = (rename(sl, tl) == 0);
502 gchar *get_current_dir(void)
507 pathl = g_get_current_dir();
508 path8 = path_to_utf8(pathl);
514 void string_list_free(GList *list)
516 g_list_foreach(list, (GFunc)g_free, NULL);
520 GList *string_list_copy(GList *list)
522 GList *new_list = NULL;
533 new_list = g_list_prepend(new_list, g_strdup(path));
536 return g_list_reverse(new_list);
539 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
545 if (!divider) divider = "";
547 unique = g_strconcat(path, ext, NULL);
548 while (isname(unique))
553 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
557 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
571 gchar *unique_filename_simple(const gchar *path)
577 if (!path) return NULL;
579 name = filename_from_path(path);
580 if (!name) return NULL;
582 ext = extension_from_path(name);
586 unique = unique_filename(path, NULL, "_", TRUE);
592 base = remove_extension_from_path(path);
593 unique = unique_filename(base, ext, "_", TRUE);
600 const gchar *filename_from_path(const gchar *path)
604 if (!path) return NULL;
606 base = strrchr(path, G_DIR_SEPARATOR);
607 if (base) return base + 1;
612 gchar *remove_level_from_path(const gchar *path)
616 if (!path) return NULL;
620 if (path[p] == G_DIR_SEPARATOR) n = p;
625 return g_strndup(path, (gsize) n);
628 const gchar *extension_from_path(const gchar *path)
630 if (!path) return NULL;
631 return strrchr(path, '.');
634 gint file_extension_match(const gchar *path, const gchar *ext)
639 if (!path) return FALSE;
640 if (!ext) return TRUE;
646 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
649 gchar *remove_extension_from_path(const gchar *path)
653 if (!path) return NULL;
657 if (path[p] == '.') n = p;
662 return g_strndup(path, (gsize) n);
665 void parse_out_relatives(gchar *path)
673 while (path[s] != '\0')
675 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
677 /* /. occurence, let's see more */
680 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
682 /* /./ or /., just skip this part */
686 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
688 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
691 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
696 if (s != t) path[t] = path[s];
701 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
702 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
706 gint file_in_path(const gchar *name)
713 if (!name) return FALSE;
714 path = g_strdup(getenv("PATH"));
715 if (!path) return FALSE;
716 namel = path_from_utf8(name);
720 while (p < l && !ret)
724 while (path[e] != ':' && path[e] != '\0') e++;
727 f = g_build_filename(path + p, namel, NULL);
728 if (isfile(f)) ret = TRUE;