2 * (SLIK) SimpLIstic sKin functions
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
21 #include <sys/param.h>
26 #include <gtk/gtk.h> /* for locale warning dialog */
28 #include "ui_fileops.h"
30 #include "ui_utildlg.h" /* for locale warning dialog */
33 *-----------------------------------------------------------------------------
34 * generic file information and manipulation routines (public)
35 *-----------------------------------------------------------------------------
38 /* file sorting method (case) */
39 gint file_sort_case_sensitive = FALSE;
42 void print_term(const gchar *text_utf8)
46 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
47 printf((text_l) ? text_l : text_utf8);
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\n", PACKAGE);
96 g_string_append(string, "If your filenames are not encoded in utf-8, try setting\n");
97 g_string_append(string, "the environment variable G_BROKEN_FILENAMES=1\n");
98 g_string_append_printf(string, "It appears G_BROKEN_FILENAMES is %s%s\n\n",
99 (bf) ? "set to " : "not set.", (bf) ? bf : "");
100 g_string_append_printf(string, "The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n", (lc) ? lc : "undefined");
101 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
104 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
105 string = g_string_append(string, "\nPreferred encoding appears to be UTF-8, however the file:\n");
106 g_string_append_printf(string, "\"%s\"\n%s encoded in valid UTF-8.\n",
107 (name) ? name : "[name not displayable]",
108 (g_utf8_validate(path, -1, NULL)) ? "is": "is NOT");
112 gd = generic_dialog_new("Filename encoding locale mismatch",
113 PACKAGE, "locale warning", NULL, TRUE, NULL, NULL);
114 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
116 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
117 "Filename encoding locale mismatch", string->str);
119 gtk_widget_show(gd->dialog);
121 g_string_free(string, TRUE);
124 gchar *path_to_utf8(const gchar *path)
127 GError *error = NULL;
129 if (!path) return NULL;
131 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
134 printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
136 encoding_dialog(path);
140 /* just let it through, but bad things may happen */
141 utf8 = g_strdup(path);
147 gchar *path_from_utf8(const gchar *utf8)
150 GError *error = NULL;
152 if (!utf8) return NULL;
154 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
157 printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
162 /* if invalid UTF-8, text probaby still in original form, so just copy it */
163 path = g_strdup(utf8);
169 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
170 const gchar *homedir(void)
172 static gchar *home = NULL;
176 home = path_to_utf8(getenv("HOME"));
180 struct passwd *pw = getpwuid(getuid());
181 if (pw) home = path_to_utf8(pw->pw_dir);
187 gint stat_utf8(const gchar *s, struct stat *st)
192 if (!s) return FALSE;
193 sl = path_from_utf8(s);
194 ret = (stat(sl, st) == 0);
200 gint isname(const gchar *s)
204 return stat_utf8(s, &st);
207 gint isfile(const gchar *s)
211 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
214 gint isdir(const gchar *s)
218 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
221 gint64 filesize(const gchar *s)
225 if (!stat_utf8(s, &st)) return 0;
226 return (gint)st.st_size;
229 time_t filetime(const gchar *s)
233 if (!stat_utf8(s, &st)) return 0;
237 gint filetime_set(const gchar *s, time_t tval)
246 ut.actime = ut.modtime = tval;
248 sl = path_from_utf8(s);
249 ret = (utime(sl, &ut) == 0);
256 gint access_file(const gchar *s, int mode)
261 if (!s) return FALSE;
263 sl = path_from_utf8(s);
264 ret = (access(sl, mode) == 0);
270 gint unlink_file(const gchar *s)
275 if (!s) return FALSE;
277 sl = path_from_utf8(s);
278 ret = (unlink(sl) == 0);
284 gint symlink_utf8(const gchar *source, const gchar *target)
290 if (!source || !target) return FALSE;
292 sl = path_from_utf8(source);
293 tl = path_from_utf8(target);
295 ret = (symlink(sl, tl) == 0);
303 gint mkdir_utf8(const gchar *s, int mode)
308 if (!s) return FALSE;
310 sl = path_from_utf8(s);
311 ret = (mkdir(sl, mode) == 0);
316 gint rmdir_utf8(const gchar *s)
321 if (!s) return FALSE;
323 sl = path_from_utf8(s);
324 ret = (rmdir(sl) == 0);
330 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
336 if (!s || !t) return FALSE;
338 sl = path_from_utf8(s);
339 tl = path_from_utf8(t);
341 if (stat(sl, &st) == 0)
347 /* set the dest file attributes to that of source (ignoring errors) */
349 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
350 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
352 tb.actime = st.st_atime;
353 tb.modtime = st.st_mtime;
354 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
363 /* paths are in filesystem encoding */
364 static gint hard_linked(const gchar *a, const gchar *b)
369 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
371 return (sta.st_dev == stb.st_dev &&
372 sta.st_ino == stb.st_ino);
375 gint copy_file(const gchar *s, const gchar *t)
383 sl = path_from_utf8(s);
384 tl = path_from_utf8(t);
386 if (hard_linked(sl, tl))
393 fi = fopen(sl, "rb");
396 fo = fopen(tl, "wb");
407 if (!fi || !fo) return FALSE;
409 while((b = fread(buf, sizeof(char), 4096, fi)) && b != 0)
411 if (fwrite(buf, sizeof(char), b, fo) != b)
422 copy_file_attributes(s, t, TRUE, TRUE);
427 gint move_file(const gchar *s, const gchar *t)
432 if (!s || !t) return FALSE;
434 sl = path_from_utf8(s);
435 tl = path_from_utf8(t);
436 if (rename(sl, tl) < 0)
438 /* this may have failed because moving a file across filesystems
439 was attempted, so try copy and delete instead */
444 /* err, now we can't delete the source file so return FALSE */
459 gint rename_file(const gchar *s, const gchar *t)
464 if (!s || !t) return FALSE;
466 sl = path_from_utf8(s);
467 tl = path_from_utf8(t);
468 ret = (rename(sl, tl) == 0);
475 gchar *get_current_dir(void)
480 pathl = g_get_current_dir();
481 path8 = path_to_utf8(pathl);
487 gint path_list(const gchar *path, GList **files, GList **dirs)
491 struct stat ent_sbuf;
492 GList *f_list = NULL;
493 GList *d_list = NULL;
496 if (!path) return FALSE;
498 pathl = path_from_utf8(path);
508 if (pathl[0] == '/' && pathl[1] == '\0')
511 pathl = g_strdup("");
514 while ((dir = readdir(dp)) != NULL)
516 /* skip removed files */
519 gchar *name = dir->d_name;
520 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
521 if (stat(filepath, &ent_sbuf) >= 0)
526 name8 = path_to_utf8(name);
527 path8 = g_strconcat(path, "/", name8, NULL);
530 if (dirs && S_ISDIR(ent_sbuf.st_mode) &&
531 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) )
533 d_list = g_list_prepend(d_list, path8);
536 else if (files && S_ISREG(ent_sbuf.st_mode))
538 f_list = g_list_prepend(f_list, path8);
550 if (dirs) *dirs = g_list_reverse(d_list);
551 if (files) *files = g_list_reverse(f_list);
556 void path_list_free(GList *list)
558 g_list_foreach(list, (GFunc)g_free, NULL);
562 GList *path_list_copy(GList *list)
564 GList *new_list = NULL;
575 new_list = g_list_prepend(new_list, g_strdup(path));
578 return g_list_reverse(new_list);
581 long checksum_simple(const gchar *path)
588 path8 = path_from_utf8(path);
589 f = fopen(path8, "r");
593 while((c = fgetc(f)) != EOF)
603 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
609 if (!divider) divider = "";
611 unique = g_strconcat(path, ext, NULL);
612 while (isname(unique))
617 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
621 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
635 gchar *unique_filename_simple(const gchar *path)
641 if (!path) return NULL;
643 name = filename_from_path(path);
644 if (!name) return NULL;
646 ext = extension_from_path(name);
650 unique = unique_filename(path, NULL, "_", TRUE);
656 base = remove_extension_from_path(path);
657 unique = unique_filename(base, ext, "_", TRUE);
664 const gchar *filename_from_path(const gchar *path)
668 if (!path) return NULL;
670 base = strrchr(path, '/');
671 if (base) return base + 1;
676 gchar *remove_level_from_path(const gchar *path)
679 const gchar *ptr = path;
682 if (!path) return NULL;
684 p = strlen(path) - 1;
685 if (p < 0) return NULL;
686 while(ptr[p] != '/' && p > 0) p--;
687 if (p == 0 && ptr[p] == '/') p++;
688 new_path = g_strndup(path, (guint)p);
692 gchar *concat_dir_and_file(const gchar *base, const gchar *name)
694 if (!base || !name) return NULL;
696 if (strcmp(base, "/") == 0) return g_strconcat(base, name, NULL);
698 return g_strconcat(base, "/", name, NULL);
701 const gchar *extension_from_path(const gchar *path)
703 if (!path) return NULL;
704 return strrchr(path, '.');
707 gint file_extension_match(const gchar *path, const gchar *ext)
712 if (!path) return FALSE;
713 if (!ext) return TRUE;
718 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
721 gchar *remove_extension_from_path(const gchar *path)
724 const gchar *ptr = path;
727 if (!path) return NULL;
728 if (strlen(path) < 2) return g_strdup(path);
730 p = strlen(path) - 1;
731 while(ptr[p] != '.' && p > 0) p--;
732 if (p == 0) p = strlen(path) - 1;
733 new_path = g_strndup(path, (guint)p);
737 void parse_out_relatives(gchar *path)
745 while (path[s] != '\0')
747 if (path[s] == '/' && path[s+1] == '.' && (path[s+2] == '/' || path[s+2] == '\0') )
751 else if (path[s] == '/' && path[s+1] == '.' && path[s+2] == '.' && (path[s+3] == '/' || path[s+3] == '\0') )
755 while (path[t] != '/' && t > 0) t--;
759 if (s != t) path[t] = path[s];
764 if (t == 0 && path[t] == '/') t++;
765 if (t > 1 && path[t-1] == '/') t--;
769 gint file_in_path(const gchar *name)
776 if (!name) return FALSE;
777 path = g_strdup(getenv("PATH"));
778 if (!path) return FALSE;
779 namel = path_from_utf8(name);
783 while (p < l && !ret)
787 while (path[e] != ':' && path[e] != '\0') e++;
790 f = g_strconcat(path + p, "/", namel, NULL);
791 if (isfile(f)) ret = TRUE;