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 */
29 #include "ui_fileops.h"
31 #include "ui_utildlg.h" /* for locale warning dialog */
34 *-----------------------------------------------------------------------------
35 * generic file information and manipulation routines (public)
36 *-----------------------------------------------------------------------------
39 /* file sorting method (case) */
40 gint file_sort_case_sensitive = FALSE;
43 void print_term(const gchar *text_utf8)
47 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
48 printf((text_l) ? text_l : text_utf8);
52 static void encoding_dialog(const gchar *path);
54 static gint encoding_dialog_idle(gpointer data)
58 encoding_dialog(path);
64 static gint encoding_dialog_delay(gpointer data)
66 g_idle_add(encoding_dialog_idle, data);
71 static void encoding_dialog(const gchar *path)
73 static gint warned_user = FALSE;
79 /* check that gtk is initialized (loop is level > 0) */
80 if (gtk_main_level() == 0)
82 /* gtk not initialized */
83 gtk_init_add(encoding_dialog_delay, g_strdup(path));
87 if (warned_user) return;
91 bf = getenv("G_BROKEN_FILENAMES");
94 string = g_string_new("");
95 g_string_append(string, "One or more filenames are not encoded with the preferred locale character set.\n");
96 g_string_append_printf(string, "Operations on, and display of these files with %s may not succeed.\n\n", PACKAGE);
97 g_string_append(string, "If your filenames are not encoded in utf-8, try setting\n");
98 g_string_append(string, "the environment variable G_BROKEN_FILENAMES=1\n");
99 g_string_append_printf(string, "It appears G_BROKEN_FILENAMES is %s%s\n\n",
100 (bf) ? "set to " : "not set.", (bf) ? bf : "");
101 g_string_append_printf(string, "The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n", (lc) ? lc : "undefined");
102 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
105 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
106 string = g_string_append(string, "\nPreferred encoding appears to be UTF-8, however the file:\n");
107 g_string_append_printf(string, "\"%s\"\n%s encoded in valid UTF-8.\n",
108 (name) ? name : "[name not displayable]",
109 (g_utf8_validate(path, -1, NULL)) ? "is": "is NOT");
113 gd = generic_dialog_new("Filename encoding locale mismatch",
114 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL);
115 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
117 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
118 "Filename encoding locale mismatch", string->str);
120 gtk_widget_show(gd->dialog);
122 g_string_free(string, TRUE);
125 gchar *path_to_utf8(const gchar *path)
128 GError *error = NULL;
130 if (!path) return NULL;
132 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
135 printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
137 encoding_dialog(path);
141 /* just let it through, but bad things may happen */
142 utf8 = g_strdup(path);
148 gchar *path_from_utf8(const gchar *utf8)
151 GError *error = NULL;
153 if (!utf8) return NULL;
155 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
158 printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
163 /* if invalid UTF-8, text probaby still in original form, so just copy it */
164 path = g_strdup(utf8);
170 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
171 const gchar *homedir(void)
173 static gchar *home = NULL;
177 home = path_to_utf8(getenv("HOME"));
181 struct passwd *pw = getpwuid(getuid());
182 if (pw) home = path_to_utf8(pw->pw_dir);
188 gint stat_utf8(const gchar *s, struct stat *st)
193 if (!s) return FALSE;
194 sl = path_from_utf8(s);
195 ret = (stat(sl, st) == 0);
201 gint lstat_utf8(const gchar *s, struct stat *st)
206 if (!s) return FALSE;
207 sl = path_from_utf8(s);
208 ret = (lstat(sl, st) == 0);
214 gint isname(const gchar *s)
218 return stat_utf8(s, &st);
221 gint isfile(const gchar *s)
225 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
228 gint isdir(const gchar *s)
232 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
235 gint islink(const gchar *s)
239 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode));
242 gint64 filesize(const gchar *s)
246 if (!stat_utf8(s, &st)) return 0;
247 return (gint)st.st_size;
250 time_t filetime(const gchar *s)
254 if (!stat_utf8(s, &st)) return 0;
258 gint filetime_set(const gchar *s, time_t tval)
267 ut.actime = ut.modtime = tval;
269 sl = path_from_utf8(s);
270 ret = (utime(sl, &ut) == 0);
277 gint access_file(const gchar *s, int mode)
282 if (!s) return FALSE;
284 sl = path_from_utf8(s);
285 ret = (access(sl, mode) == 0);
291 gint unlink_file(const gchar *s)
296 if (!s) return FALSE;
298 sl = path_from_utf8(s);
299 ret = (unlink(sl) == 0);
305 gint symlink_utf8(const gchar *source, const gchar *target)
311 if (!source || !target) return FALSE;
313 sl = path_from_utf8(source);
314 tl = path_from_utf8(target);
316 ret = (symlink(sl, tl) == 0);
324 gint mkdir_utf8(const gchar *s, int mode)
329 if (!s) return FALSE;
331 sl = path_from_utf8(s);
332 ret = (mkdir(sl, mode) == 0);
337 gint rmdir_utf8(const gchar *s)
342 if (!s) return FALSE;
344 sl = path_from_utf8(s);
345 ret = (rmdir(sl) == 0);
351 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
357 if (!s || !t) return FALSE;
359 sl = path_from_utf8(s);
360 tl = path_from_utf8(t);
362 if (stat(sl, &st) == 0)
368 /* set the dest file attributes to that of source (ignoring errors) */
370 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
371 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
373 tb.actime = st.st_atime;
374 tb.modtime = st.st_mtime;
375 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
384 /* paths are in filesystem encoding */
385 static gint hard_linked(const gchar *a, const gchar *b)
390 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
392 return (sta.st_dev == stb.st_dev &&
393 sta.st_ino == stb.st_ino);
396 gint copy_file(const gchar *s, const gchar *t)
404 sl = path_from_utf8(s);
405 tl = path_from_utf8(t);
407 if (hard_linked(sl, tl))
414 fi = fopen(sl, "rb");
417 fo = fopen(tl, "wb");
428 if (!fi || !fo) return FALSE;
430 while((b = fread(buf, sizeof(char), 4096, fi)) && b != 0)
432 if (fwrite(buf, sizeof(char), b, fo) != b)
443 copy_file_attributes(s, t, TRUE, TRUE);
448 gint move_file(const gchar *s, const gchar *t)
453 if (!s || !t) return FALSE;
455 sl = path_from_utf8(s);
456 tl = path_from_utf8(t);
457 if (rename(sl, tl) < 0)
459 /* this may have failed because moving a file across filesystems
460 was attempted, so try copy and delete instead */
465 /* err, now we can't delete the source file so return FALSE */
480 gint rename_file(const gchar *s, const gchar *t)
485 if (!s || !t) return FALSE;
487 sl = path_from_utf8(s);
488 tl = path_from_utf8(t);
489 ret = (rename(sl, tl) == 0);
496 gchar *get_current_dir(void)
501 pathl = g_get_current_dir();
502 path8 = path_to_utf8(pathl);
508 static gint path_list_real(const gchar *path, GList **files, GList **dirs,
513 GList *f_list = NULL;
514 GList *d_list = NULL;
517 if (!path) return FALSE;
519 pathl = path_from_utf8(path);
529 if (pathl[0] == '/' && pathl[1] == '\0')
532 pathl = g_strdup("");
535 while ((dir = readdir(dp)) != NULL)
543 filepath = g_strconcat(pathl, "/", name, NULL);
547 result = stat(filepath, &st_buf);
551 result = lstat(filepath, &st_buf);
559 name8 = path_to_utf8(name);
560 path8 = g_strconcat(path, "/", name8, NULL);
563 if (dirs && S_ISDIR(st_buf.st_mode) &&
564 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) )
566 d_list = g_list_prepend(d_list, path8);
570 (S_ISREG(st_buf.st_mode) || (!follow_links && S_ISLNK(st_buf.st_mode))) )
572 f_list = g_list_prepend(f_list, path8);
585 if (dirs) *dirs = g_list_reverse(d_list);
586 if (files) *files = g_list_reverse(f_list);
591 gint path_list(const gchar *path, GList **files, GList **dirs)
593 return path_list_real(path, files, dirs, TRUE);
596 gint path_list_lstat(const gchar *path, GList **files, GList **dirs)
598 return path_list_real(path, files, dirs, FALSE);
601 void path_list_free(GList *list)
603 g_list_foreach(list, (GFunc)g_free, NULL);
607 GList *path_list_copy(GList *list)
609 GList *new_list = NULL;
620 new_list = g_list_prepend(new_list, g_strdup(path));
623 return g_list_reverse(new_list);
626 long checksum_simple(const gchar *path)
633 path8 = path_from_utf8(path);
634 f = fopen(path8, "r");
638 while((c = fgetc(f)) != EOF)
648 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
654 if (!divider) divider = "";
656 unique = g_strconcat(path, ext, NULL);
657 while (isname(unique))
662 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
666 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
680 gchar *unique_filename_simple(const gchar *path)
686 if (!path) return NULL;
688 name = filename_from_path(path);
689 if (!name) return NULL;
691 ext = extension_from_path(name);
695 unique = unique_filename(path, NULL, "_", TRUE);
701 base = remove_extension_from_path(path);
702 unique = unique_filename(base, ext, "_", TRUE);
709 const gchar *filename_from_path(const gchar *path)
713 if (!path) return NULL;
715 base = strrchr(path, '/');
716 if (base) return base + 1;
721 gchar *remove_level_from_path(const gchar *path)
724 const gchar *ptr = path;
727 if (!path) return NULL;
729 p = strlen(path) - 1;
730 if (p < 0) return NULL;
731 while(ptr[p] != '/' && p > 0) p--;
732 if (p == 0 && ptr[p] == '/') p++;
733 new_path = g_strndup(path, (guint)p);
737 gchar *concat_dir_and_file(const gchar *base, const gchar *name)
739 if (!base || !name) return NULL;
741 if (strcmp(base, "/") == 0) return g_strconcat(base, name, NULL);
743 return g_strconcat(base, "/", name, NULL);
746 const gchar *extension_from_path(const gchar *path)
748 if (!path) return NULL;
749 return strrchr(path, '.');
752 gint file_extension_match(const gchar *path, const gchar *ext)
757 if (!path) return FALSE;
758 if (!ext) return TRUE;
763 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
766 gchar *remove_extension_from_path(const gchar *path)
769 const gchar *ptr = path;
772 if (!path) return NULL;
773 if (strlen(path) < 2) return g_strdup(path);
775 p = strlen(path) - 1;
776 while(ptr[p] != '.' && p > 0) p--;
777 if (p == 0) p = strlen(path) - 1;
778 new_path = g_strndup(path, (guint)p);
782 void parse_out_relatives(gchar *path)
790 while (path[s] != '\0')
792 if (path[s] == '/' && path[s+1] == '.' && (path[s+2] == '/' || path[s+2] == '\0') )
796 else if (path[s] == '/' && path[s+1] == '.' && path[s+2] == '.' && (path[s+3] == '/' || path[s+3] == '\0') )
800 while (path[t] != '/' && t > 0) t--;
804 if (s != t) path[t] = path[s];
809 if (t == 0 && path[t] == '/') t++;
810 if (t > 1 && path[t-1] == '/') t--;
814 gint file_in_path(const gchar *name)
821 if (!name) return FALSE;
822 path = g_strdup(getenv("PATH"));
823 if (!path) return FALSE;
824 namel = path_from_utf8(name);
828 while (p < l && !ret)
832 while (path[e] != ':' && path[e] != '\0') e++;
835 f = g_strconcat(path + p, "/", namel, NULL);
836 if (isfile(f)) ret = TRUE;