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 *-----------------------------------------------------------------------------
41 void print_term(const gchar *text_utf8)
45 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
46 printf((text_l) ? text_l : text_utf8);
50 static void encoding_dialog(const gchar *path);
52 static gint encoding_dialog_idle(gpointer data)
56 encoding_dialog(path);
62 static gint encoding_dialog_delay(gpointer data)
64 g_idle_add(encoding_dialog_idle, data);
69 static void encoding_dialog(const gchar *path)
71 static gint warned_user = FALSE;
77 /* check that gtk is initialized (loop is level > 0) */
78 if (gtk_main_level() == 0)
80 /* gtk not initialized */
81 gtk_init_add(encoding_dialog_delay, g_strdup(path));
85 if (warned_user) return;
89 bf = getenv("G_BROKEN_FILENAMES");
92 string = g_string_new("");
93 g_string_append(string, "One or more filenames are not encoded with the preferred locale character set.\n");
94 g_string_append_printf(string, "Operations on, and display of these files with %s may not succeed.\n\n", PACKAGE);
95 g_string_append(string, "If your filenames are not encoded in utf-8, try setting\n");
96 g_string_append(string, "the environment variable G_BROKEN_FILENAMES=1\n");
97 g_string_append_printf(string, "It appears G_BROKEN_FILENAMES is %s%s\n\n",
98 (bf) ? "set to " : "not set.", (bf) ? bf : "");
99 g_string_append_printf(string, "The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n", (lc) ? lc : "undefined");
100 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
103 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
104 string = g_string_append(string, "\nPreferred encoding appears to be UTF-8, however the file:\n");
105 g_string_append_printf(string, "\"%s\"\n%s encoded in valid UTF-8.\n",
106 (name) ? name : "[name not displayable]",
107 (g_utf8_validate(path, -1, NULL)) ? "is": "is NOT");
111 gd = generic_dialog_new("Filename encoding locale mismatch",
112 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL);
113 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
115 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
116 "Filename encoding locale mismatch", string->str);
118 gtk_widget_show(gd->dialog);
120 g_string_free(string, TRUE);
123 gchar *path_to_utf8(const gchar *path)
126 GError *error = NULL;
128 if (!path) return NULL;
130 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
133 printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
135 encoding_dialog(path);
139 /* just let it through, but bad things may happen */
140 utf8 = g_strdup(path);
146 gchar *path_from_utf8(const gchar *utf8)
149 GError *error = NULL;
151 if (!utf8) return NULL;
153 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
156 printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
161 /* if invalid UTF-8, text probaby still in original form, so just copy it */
162 path = g_strdup(utf8);
168 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
169 const gchar *homedir(void)
171 static gchar *home = NULL;
175 home = path_to_utf8(getenv("HOME"));
179 struct passwd *pw = getpwuid(getuid());
180 if (pw) home = path_to_utf8(pw->pw_dir);
186 gint stat_utf8(const gchar *s, struct stat *st)
191 if (!s) return FALSE;
192 sl = path_from_utf8(s);
193 ret = (stat(sl, st) == 0);
199 gint lstat_utf8(const gchar *s, struct stat *st)
204 if (!s) return FALSE;
205 sl = path_from_utf8(s);
206 ret = (lstat(sl, st) == 0);
212 gint isname(const gchar *s)
216 return stat_utf8(s, &st);
219 gint isfile(const gchar *s)
223 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
226 gint isdir(const gchar *s)
230 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
233 gint islink(const gchar *s)
237 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode));
240 gint64 filesize(const gchar *s)
244 if (!stat_utf8(s, &st)) return 0;
245 return (gint)st.st_size;
248 time_t filetime(const gchar *s)
252 if (!stat_utf8(s, &st)) return 0;
256 gint filetime_set(const gchar *s, time_t tval)
265 ut.actime = ut.modtime = tval;
267 sl = path_from_utf8(s);
268 ret = (utime(sl, &ut) == 0);
275 gint access_file(const gchar *s, int mode)
280 if (!s) return FALSE;
282 sl = path_from_utf8(s);
283 ret = (access(sl, mode) == 0);
289 gint unlink_file(const gchar *s)
294 if (!s) return FALSE;
296 sl = path_from_utf8(s);
297 ret = (unlink(sl) == 0);
303 gint symlink_utf8(const gchar *source, const gchar *target)
309 if (!source || !target) return FALSE;
311 sl = path_from_utf8(source);
312 tl = path_from_utf8(target);
314 ret = (symlink(sl, tl) == 0);
322 gint mkdir_utf8(const gchar *s, int mode)
327 if (!s) return FALSE;
329 sl = path_from_utf8(s);
330 ret = (mkdir(sl, mode) == 0);
335 gint rmdir_utf8(const gchar *s)
340 if (!s) return FALSE;
342 sl = path_from_utf8(s);
343 ret = (rmdir(sl) == 0);
349 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
355 if (!s || !t) return FALSE;
357 sl = path_from_utf8(s);
358 tl = path_from_utf8(t);
360 if (stat(sl, &st) == 0)
366 /* set the dest file attributes to that of source (ignoring errors) */
368 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
369 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
371 tb.actime = st.st_atime;
372 tb.modtime = st.st_mtime;
373 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
382 /* paths are in filesystem encoding */
383 static gint hard_linked(const gchar *a, const gchar *b)
388 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
390 return (sta.st_dev == stb.st_dev &&
391 sta.st_ino == stb.st_ino);
394 gint copy_file(const gchar *s, const gchar *t)
402 sl = path_from_utf8(s);
403 tl = path_from_utf8(t);
405 if (hard_linked(sl, tl))
412 fi = fopen(sl, "rb");
415 fo = fopen(tl, "wb");
426 if (!fi || !fo) return FALSE;
428 while((b = fread(buf, sizeof(char), sizeof(buf), fi)) && b != 0)
430 if (fwrite(buf, sizeof(char), b, fo) != b)
441 copy_file_attributes(s, t, TRUE, TRUE);
446 gint move_file(const gchar *s, const gchar *t)
451 if (!s || !t) return FALSE;
453 sl = path_from_utf8(s);
454 tl = path_from_utf8(t);
455 if (rename(sl, tl) < 0)
457 /* this may have failed because moving a file across filesystems
458 was attempted, so try copy and delete instead */
463 /* err, now we can't delete the source file so return FALSE */
478 gint rename_file(const gchar *s, const gchar *t)
483 if (!s || !t) return FALSE;
485 sl = path_from_utf8(s);
486 tl = path_from_utf8(t);
487 ret = (rename(sl, tl) == 0);
494 gchar *get_current_dir(void)
499 pathl = g_get_current_dir();
500 path8 = path_to_utf8(pathl);
506 static gint path_list_real(const gchar *path, GList **files, GList **dirs,
511 GList *f_list = NULL;
512 GList *d_list = NULL;
515 if (!path) return FALSE;
517 pathl = path_from_utf8(path);
527 if (pathl[0] == '/' && pathl[1] == '\0')
530 pathl = g_strdup("");
533 while ((dir = readdir(dp)) != NULL)
541 filepath = g_strconcat(pathl, "/", name, NULL);
545 result = stat(filepath, &st_buf);
549 result = lstat(filepath, &st_buf);
557 name8 = path_to_utf8(name);
558 path8 = g_strconcat(path, "/", name8, NULL);
561 if (dirs && S_ISDIR(st_buf.st_mode) &&
562 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) )
564 d_list = g_list_prepend(d_list, path8);
568 (S_ISREG(st_buf.st_mode) || (!follow_links && S_ISLNK(st_buf.st_mode))) )
570 f_list = g_list_prepend(f_list, path8);
583 if (dirs) *dirs = g_list_reverse(d_list);
584 if (files) *files = g_list_reverse(f_list);
589 gint path_list(const gchar *path, GList **files, GList **dirs)
591 return path_list_real(path, files, dirs, TRUE);
594 gint path_list_lstat(const gchar *path, GList **files, GList **dirs)
596 return path_list_real(path, files, dirs, FALSE);
599 void path_list_free(GList *list)
601 g_list_foreach(list, (GFunc)g_free, NULL);
605 GList *path_list_copy(GList *list)
607 GList *new_list = NULL;
618 new_list = g_list_prepend(new_list, g_strdup(path));
621 return g_list_reverse(new_list);
624 long checksum_simple(const gchar *path)
631 path8 = path_from_utf8(path);
632 f = fopen(path8, "r");
636 while((c = fgetc(f)) != EOF)
646 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
652 if (!divider) divider = "";
654 unique = g_strconcat(path, ext, NULL);
655 while (isname(unique))
660 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
664 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
678 gchar *unique_filename_simple(const gchar *path)
684 if (!path) return NULL;
686 name = filename_from_path(path);
687 if (!name) return NULL;
689 ext = extension_from_path(name);
693 unique = unique_filename(path, NULL, "_", TRUE);
699 base = remove_extension_from_path(path);
700 unique = unique_filename(base, ext, "_", TRUE);
707 const gchar *filename_from_path(const gchar *path)
711 if (!path) return NULL;
713 base = strrchr(path, '/');
714 if (base) return base + 1;
719 gchar *remove_level_from_path(const gchar *path)
722 const gchar *ptr = path;
725 if (!path) return NULL;
727 p = strlen(path) - 1;
728 if (p < 0) return NULL;
729 while(ptr[p] != '/' && p > 0) p--;
730 if (p == 0 && ptr[p] == '/') p++;
731 new_path = g_strndup(path, (guint)p);
735 gchar *concat_dir_and_file(const gchar *base, const gchar *name)
737 if (!base || !name) return NULL;
739 if (strcmp(base, "/") == 0) return g_strconcat(base, name, NULL);
741 return g_strconcat(base, "/", name, NULL);
744 const gchar *extension_from_path(const gchar *path)
746 if (!path) return NULL;
747 return strrchr(path, '.');
750 gint file_extension_match(const gchar *path, const gchar *ext)
755 if (!path) return FALSE;
756 if (!ext) return TRUE;
761 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
764 gchar *remove_extension_from_path(const gchar *path)
767 const gchar *ptr = path;
770 if (!path) return NULL;
771 if (strlen(path) < 2) return g_strdup(path);
773 p = strlen(path) - 1;
774 while(ptr[p] != '.' && p > 0) p--;
775 if (p == 0) p = strlen(path) - 1;
776 new_path = g_strndup(path, (guint)p);
780 void parse_out_relatives(gchar *path)
788 while (path[s] != '\0')
790 if (path[s] == '/' && path[s+1] == '.' && (path[s+2] == '/' || path[s+2] == '\0') )
794 else if (path[s] == '/' && path[s+1] == '.' && path[s+2] == '.' && (path[s+3] == '/' || path[s+3] == '\0') )
798 while (path[t] != '/' && t > 0) t--;
802 if (s != t) path[t] = path[s];
807 if (t == 0 && path[t] == '/') t++;
808 if (t > 1 && path[t-1] == '/') t--;
812 gint file_in_path(const gchar *name)
819 if (!name) return FALSE;
820 path = g_strdup(getenv("PATH"));
821 if (!path) return FALSE;
822 namel = path_from_utf8(name);
826 while (p < l && !ret)
830 while (path[e] != ':' && path[e] != '\0') e++;
833 f = g_strconcat(path + p, "/", namel, NULL);
834 if (isfile(f)) ret = TRUE;