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 lstat_utf8(const gchar *s, struct stat *st)
205 if (!s) return FALSE;
206 sl = path_from_utf8(s);
207 ret = (lstat(sl, st) == 0);
213 gint isname(const gchar *s)
217 return stat_utf8(s, &st);
220 gint isfile(const gchar *s)
224 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
227 gint isdir(const gchar *s)
231 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
234 gint islink(const gchar *s)
238 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode));
241 gint64 filesize(const gchar *s)
245 if (!stat_utf8(s, &st)) return 0;
246 return (gint)st.st_size;
249 time_t filetime(const gchar *s)
253 if (!stat_utf8(s, &st)) return 0;
257 gint filetime_set(const gchar *s, time_t tval)
266 ut.actime = ut.modtime = tval;
268 sl = path_from_utf8(s);
269 ret = (utime(sl, &ut) == 0);
276 gint access_file(const gchar *s, int mode)
281 if (!s) return FALSE;
283 sl = path_from_utf8(s);
284 ret = (access(sl, mode) == 0);
290 gint unlink_file(const gchar *s)
295 if (!s) return FALSE;
297 sl = path_from_utf8(s);
298 ret = (unlink(sl) == 0);
304 gint symlink_utf8(const gchar *source, const gchar *target)
310 if (!source || !target) return FALSE;
312 sl = path_from_utf8(source);
313 tl = path_from_utf8(target);
315 ret = (symlink(sl, tl) == 0);
323 gint mkdir_utf8(const gchar *s, int mode)
328 if (!s) return FALSE;
330 sl = path_from_utf8(s);
331 ret = (mkdir(sl, mode) == 0);
336 gint rmdir_utf8(const gchar *s)
341 if (!s) return FALSE;
343 sl = path_from_utf8(s);
344 ret = (rmdir(sl) == 0);
350 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
356 if (!s || !t) return FALSE;
358 sl = path_from_utf8(s);
359 tl = path_from_utf8(t);
361 if (stat(sl, &st) == 0)
367 /* set the dest file attributes to that of source (ignoring errors) */
369 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
370 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
372 tb.actime = st.st_atime;
373 tb.modtime = st.st_mtime;
374 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
383 /* paths are in filesystem encoding */
384 static gint hard_linked(const gchar *a, const gchar *b)
389 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
391 return (sta.st_dev == stb.st_dev &&
392 sta.st_ino == stb.st_ino);
395 gint copy_file(const gchar *s, const gchar *t)
403 sl = path_from_utf8(s);
404 tl = path_from_utf8(t);
406 if (hard_linked(sl, tl))
413 fi = fopen(sl, "rb");
416 fo = fopen(tl, "wb");
427 if (!fi || !fo) return FALSE;
429 while((b = fread(buf, sizeof(char), 4096, fi)) && b != 0)
431 if (fwrite(buf, sizeof(char), b, fo) != b)
442 copy_file_attributes(s, t, TRUE, TRUE);
447 gint move_file(const gchar *s, const gchar *t)
452 if (!s || !t) return FALSE;
454 sl = path_from_utf8(s);
455 tl = path_from_utf8(t);
456 if (rename(sl, tl) < 0)
458 /* this may have failed because moving a file across filesystems
459 was attempted, so try copy and delete instead */
464 /* err, now we can't delete the source file so return FALSE */
479 gint rename_file(const gchar *s, const gchar *t)
484 if (!s || !t) return FALSE;
486 sl = path_from_utf8(s);
487 tl = path_from_utf8(t);
488 ret = (rename(sl, tl) == 0);
495 gchar *get_current_dir(void)
500 pathl = g_get_current_dir();
501 path8 = path_to_utf8(pathl);
507 gint path_list(const gchar *path, GList **files, GList **dirs)
511 struct stat ent_sbuf;
512 GList *f_list = NULL;
513 GList *d_list = NULL;
516 if (!path) return FALSE;
518 pathl = path_from_utf8(path);
528 if (pathl[0] == '/' && pathl[1] == '\0')
531 pathl = g_strdup("");
534 while ((dir = readdir(dp)) != NULL)
536 gchar *name = dir->d_name;
537 gchar *filepath = g_strconcat(pathl, "/", name, NULL);
539 if (stat(filepath, &ent_sbuf) >= 0)
544 name8 = path_to_utf8(name);
545 path8 = g_strconcat(path, "/", name8, NULL);
548 if (dirs && S_ISDIR(ent_sbuf.st_mode) &&
549 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) )
551 d_list = g_list_prepend(d_list, path8);
554 else if (files && S_ISREG(ent_sbuf.st_mode))
556 f_list = g_list_prepend(f_list, path8);
569 if (dirs) *dirs = g_list_reverse(d_list);
570 if (files) *files = g_list_reverse(f_list);
575 void path_list_free(GList *list)
577 g_list_foreach(list, (GFunc)g_free, NULL);
581 GList *path_list_copy(GList *list)
583 GList *new_list = NULL;
594 new_list = g_list_prepend(new_list, g_strdup(path));
597 return g_list_reverse(new_list);
600 long checksum_simple(const gchar *path)
607 path8 = path_from_utf8(path);
608 f = fopen(path8, "r");
612 while((c = fgetc(f)) != EOF)
622 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
628 if (!divider) divider = "";
630 unique = g_strconcat(path, ext, NULL);
631 while (isname(unique))
636 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
640 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
654 gchar *unique_filename_simple(const gchar *path)
660 if (!path) return NULL;
662 name = filename_from_path(path);
663 if (!name) return NULL;
665 ext = extension_from_path(name);
669 unique = unique_filename(path, NULL, "_", TRUE);
675 base = remove_extension_from_path(path);
676 unique = unique_filename(base, ext, "_", TRUE);
683 const gchar *filename_from_path(const gchar *path)
687 if (!path) return NULL;
689 base = strrchr(path, '/');
690 if (base) return base + 1;
695 gchar *remove_level_from_path(const gchar *path)
698 const gchar *ptr = path;
701 if (!path) return NULL;
703 p = strlen(path) - 1;
704 if (p < 0) return NULL;
705 while(ptr[p] != '/' && p > 0) p--;
706 if (p == 0 && ptr[p] == '/') p++;
707 new_path = g_strndup(path, (guint)p);
711 gchar *concat_dir_and_file(const gchar *base, const gchar *name)
713 if (!base || !name) return NULL;
715 if (strcmp(base, "/") == 0) return g_strconcat(base, name, NULL);
717 return g_strconcat(base, "/", name, NULL);
720 const gchar *extension_from_path(const gchar *path)
722 if (!path) return NULL;
723 return strrchr(path, '.');
726 gint file_extension_match(const gchar *path, const gchar *ext)
731 if (!path) return FALSE;
732 if (!ext) return TRUE;
737 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
740 gchar *remove_extension_from_path(const gchar *path)
743 const gchar *ptr = path;
746 if (!path) return NULL;
747 if (strlen(path) < 2) return g_strdup(path);
749 p = strlen(path) - 1;
750 while(ptr[p] != '.' && p > 0) p--;
751 if (p == 0) p = strlen(path) - 1;
752 new_path = g_strndup(path, (guint)p);
756 void parse_out_relatives(gchar *path)
764 while (path[s] != '\0')
766 if (path[s] == '/' && path[s+1] == '.' && (path[s+2] == '/' || path[s+2] == '\0') )
770 else if (path[s] == '/' && path[s+1] == '.' && path[s+2] == '.' && (path[s+3] == '/' || path[s+3] == '\0') )
774 while (path[t] != '/' && t > 0) t--;
778 if (s != t) path[t] = path[s];
783 if (t == 0 && path[t] == '/') t++;
784 if (t > 1 && path[t-1] == '/') t--;
788 gint file_in_path(const gchar *name)
795 if (!name) return FALSE;
796 path = g_strdup(getenv("PATH"));
797 if (!path) return FALSE;
798 namel = path_from_utf8(name);
802 while (p < l && !ret)
806 while (path[e] != ':' && path[e] != '\0') e++;
809 f = g_strconcat(path + p, "/", namel, NULL);
810 if (isfile(f)) ret = TRUE;