annotate src/ui_fileops.c @ 704:839525c4e85f

Use size_t instead of gint, it silents a signed vs unsigned warning.
author zas_
date Tue, 20 May 2008 22:13:48 +0000
parents 911e3769b60c
children 362c9a769bb7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
1 /*
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
2 * (SLIK) SimpLIstic sKin functions
69
31759d770628 Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents: 40
diff changeset
3 * (C) 2006 John Ellis
475
48c8e49b571c updated copyright in source files
nadvornik
parents: 446
diff changeset
4 * Copyright (C) 2008 The Geeqie Team
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
5 *
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
6 * Author: John Ellis
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
7 *
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
8 * This software is released under the GNU General Public License (GNU GPL).
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
9 * Please read the included file COPYING for more information.
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
10 * This software comes with no warranty of any kind, use at your own risk!
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
11 */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
12
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
13 #ifdef HAVE_CONFIG_H
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
14 # include "config.h"
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
15 #endif
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
16
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
17 #include <pwd.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
18 #include <stdio.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
19 #include <stdlib.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
20 #include <string.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
21 #include <unistd.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
22 #include <sys/param.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
23 #include <dirent.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
24 #include <utime.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
25
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
26 #include <glib.h>
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
27 #include <gtk/gtk.h> /* for locale warning dialog */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
28
281
9995c5fb202a gqview.h -> main.h
zas_
parents: 254
diff changeset
29 #include "main.h"
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
30 #include "ui_fileops.h"
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
31
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
32 #include "ui_utildlg.h" /* for locale warning dialog */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
33
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
34 /*
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
35 *-----------------------------------------------------------------------------
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
36 * generic file information and manipulation routines (public)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
37 *-----------------------------------------------------------------------------
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
38 */
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
39
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
40
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
41
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
42 void print_term(const gchar *text_utf8)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
43 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
44 gchar *text_l;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
45
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
46 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
47 printf((text_l) ? text_l : text_utf8);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
48 g_free(text_l);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
49 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
50
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
51 static void encoding_dialog(const gchar *path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
52
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
53 static gint encoding_dialog_idle(gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
54 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
55 gchar *path = data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
56
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
57 encoding_dialog(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
58 g_free(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
59
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
60 return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
61 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
62
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
63 static gint encoding_dialog_delay(gpointer data)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
64 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
65 g_idle_add(encoding_dialog_idle, data);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
66
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
67 return 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
68 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
69
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
70 static void encoding_dialog(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
71 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
72 static gint warned_user = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
73 GenericDialog *gd;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
74 GString *string;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
75 const gchar *lc;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
76 const gchar *bf;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
77
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
78 /* check that gtk is initialized (loop is level > 0) */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
79 if (gtk_main_level() == 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
80 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
81 /* gtk not initialized */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
82 gtk_init_add(encoding_dialog_delay, g_strdup(path));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
83 return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
84 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
85
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
86 if (warned_user) return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
87 warned_user = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
88
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
89 lc = getenv("LANG");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
90 bf = getenv("G_BROKEN_FILENAMES");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
91 warned_user = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
92
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
93 string = g_string_new("");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
94 g_string_append(string, "One or more filenames are not encoded with the preferred locale character set.\n");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
95 g_string_append_printf(string, "Operations on, and display of these files with %s may not succeed.\n\n", PACKAGE);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
96 g_string_append(string, "If your filenames are not encoded in utf-8, try setting\n");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
97 g_string_append(string, "the environment variable G_BROKEN_FILENAMES=1\n");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
98 g_string_append_printf(string, "It appears G_BROKEN_FILENAMES is %s%s\n\n",
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
99 (bf) ? "set to " : "not set.", (bf) ? bf : "");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
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");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
101 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
102 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
103 gchar *name;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
104 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
105 string = g_string_append(string, "\nPreferred encoding appears to be UTF-8, however the file:\n");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
106 g_string_append_printf(string, "\"%s\"\n%s encoded in valid UTF-8.\n",
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
107 (name) ? name : "[name not displayable]",
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
108 (g_utf8_validate(path, -1, NULL)) ? "is": "is NOT");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
109 g_free(name);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
110 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
111
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
112 gd = generic_dialog_new("Filename encoding locale mismatch",
254
9faf34f047b1 Make the wmclass value unique among the code by defining
zas_
parents: 138
diff changeset
113 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
114 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
115
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
116 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
117 "Filename encoding locale mismatch", string->str);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
118
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
119 gtk_widget_show(gd->dialog);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
120
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
121 g_string_free(string, TRUE);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
122 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
123
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
124 gchar *path_to_utf8(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
125 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
126 gchar *utf8;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
127 GError *error = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
128
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
129 if (!path) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
130
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
131 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
132 if (error)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
133 {
673
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
134 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
135 g_error_free(error);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
136 encoding_dialog(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
137 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
138 if (!utf8)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
139 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
140 /* just let it through, but bad things may happen */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
141 utf8 = g_strdup(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
142 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
143
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
144 return utf8;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
145 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
146
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
147 gchar *path_from_utf8(const gchar *utf8)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
148 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
149 gchar *path;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
150 GError *error = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
151
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
152 if (!utf8) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
153
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
154 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
155 if (error)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
156 {
673
fbebf5cf4a55 Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents: 671
diff changeset
157 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
158 g_error_free(error);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
159 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
160 if (!path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
161 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
162 /* if invalid UTF-8, text probaby still in original form, so just copy it */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
163 path = g_strdup(utf8);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
164 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
165
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
166 return path;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
167 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
168
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
169 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
170 const gchar *homedir(void)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
171 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
172 static gchar *home = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
173
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
174 if (!home)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
175 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
176 home = path_to_utf8(getenv("HOME"));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
177 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
178 if (!home)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
179 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
180 struct passwd *pw = getpwuid(getuid());
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
181 if (pw) home = path_to_utf8(pw->pw_dir);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
182 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
183
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
184 return home;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
185 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
186
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
187 gint stat_utf8(const gchar *s, struct stat *st)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
188 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
189 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
190 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
191
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
192 if (!s) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
193 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
194 ret = (stat(sl, st) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
195 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
196
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
197 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
198 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
199
40
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
200 gint lstat_utf8(const gchar *s, struct stat *st)
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
201 {
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
202 gchar *sl;
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
203 gint ret;
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
204
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
205 if (!s) return FALSE;
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
206 sl = path_from_utf8(s);
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
207 ret = (lstat(sl, st) == 0);
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
208 g_free(sl);
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
209
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
210 return ret;
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
211 }
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
212
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
213 gint isname(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
214 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
215 struct stat st;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
216
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
217 return stat_utf8(s, &st);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
218 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
219
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
220 gint isfile(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
221 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
222 struct stat st;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
223
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
224 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
225 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
226
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
227 gint isdir(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
228 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
229 struct stat st;
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
230
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
231 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
232 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
233
40
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
234 gint islink(const gchar *s)
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
235 {
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
236 struct stat st;
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
237
40
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
238 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode));
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
239 }
dcc04a6a58bf Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents: 9
diff changeset
240
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
241 gint64 filesize(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
242 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
243 struct stat st;
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
244
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
245 if (!stat_utf8(s, &st)) return 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
246 return (gint)st.st_size;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
247 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
248
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
249 time_t filetime(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
250 {
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
251 struct stat st;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
252
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
253 if (!stat_utf8(s, &st)) return 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
254 return st.st_mtime;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
255 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
256
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
257 gint filetime_set(const gchar *s, time_t tval)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
258 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
259 gint ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
260
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
261 if (tval > 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
262 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
263 struct utimbuf ut;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
264 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
265
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
266 ut.actime = ut.modtime = tval;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
267
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
268 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
269 ret = (utime(sl, &ut) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
270 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
271 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
272
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
273 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
274 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
275
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
276 gint access_file(const gchar *s, int mode)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
277 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
278 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
279 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
280
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
281 if (!s) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
282
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
283 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
284 ret = (access(sl, mode) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
285 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
286
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
287 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
288 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
289
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
290 gint unlink_file(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
291 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
292 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
293 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
294
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
295 if (!s) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
296
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
297 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
298 ret = (unlink(sl) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
299 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
300
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
301 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
302 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
303
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
304 gint symlink_utf8(const gchar *source, const gchar *target)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
305 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
306 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
307 gchar *tl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
308 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
309
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
310 if (!source || !target) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
311
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
312 sl = path_from_utf8(source);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
313 tl = path_from_utf8(target);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
314
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
315 ret = (symlink(sl, tl) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
316
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
317 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
318 g_free(tl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
319
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
320 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
321 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
322
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
323 gint mkdir_utf8(const gchar *s, int mode)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
324 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
325 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
326 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
327
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
328 if (!s) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
329
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
330 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
331 ret = (mkdir(sl, mode) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
332 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
333 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
334 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
335
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
336 gint rmdir_utf8(const gchar *s)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
337 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
338 gchar *sl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
339 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
340
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
341 if (!s) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
342
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
343 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
344 ret = (rmdir(sl) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
345 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
346
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
347 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
348 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
349
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
350 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
351 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
352 struct stat st;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
353 gchar *sl, *tl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
354 gint ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
355
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
356 if (!s || !t) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
357
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
358 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
359 tl = path_from_utf8(t);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
360
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
361 if (stat(sl, &st) == 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
362 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
363 struct utimbuf tb;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
364
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
365 ret = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
366
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
367 /* set the dest file attributes to that of source (ignoring errors) */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
368
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
369 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
370 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
371
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
372 tb.actime = st.st_atime;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
373 tb.modtime = st.st_mtime;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
374 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
375 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
376
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
377 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
378 g_free(tl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
379
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
380 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
381 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
382
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
383 /* paths are in filesystem encoding */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
384 static gint hard_linked(const gchar *a, const gchar *b)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
385 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
386 struct stat sta;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
387 struct stat stb;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
388
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
389 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
390
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
391 return (sta.st_dev == stb.st_dev &&
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
392 sta.st_ino == stb.st_ino);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
393 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
394
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
395 gint copy_file(const gchar *s, const gchar *t)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
396 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
397 FILE *fi = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
398 FILE *fo = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
399 gchar *sl, *tl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
400 gchar buf[4096];
704
839525c4e85f Use size_t instead of gint, it silents a signed vs unsigned warning.
zas_
parents: 703
diff changeset
401 size_t b;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
402
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
403 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
404 tl = path_from_utf8(t);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
405
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
406 if (hard_linked(sl, tl))
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
407 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
408 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
409 g_free(tl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
410 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
411 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
412
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
413 fi = fopen(sl, "rb");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
414 if (fi)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
415 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
416 fo = fopen(tl, "wb");
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
417 if (!fo)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
418 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
419 fclose(fi);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
420 fi = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
421 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
422 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
423
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
424 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
425 g_free(tl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
426
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
427 if (!fi || !fo) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
428
516
b7e99bfeadc9 Convert the minority of while() to while ().
zas_
parents: 475
diff changeset
429 while ((b = fread(buf, sizeof(char), sizeof(buf), fi)) && b != 0)
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
430 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
431 if (fwrite(buf, sizeof(char), b, fo) != b)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
432 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
433 fclose(fi);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
434 fclose(fo);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
435 return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
436 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
437 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
438
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
439 fclose(fi);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
440 fclose(fo);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
441
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
442 copy_file_attributes(s, t, TRUE, TRUE);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
443
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
444 return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
445 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
446
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
447 gint move_file(const gchar *s, const gchar *t)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
448 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
449 gchar *sl, *tl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
450 gint ret = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
451
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
452 if (!s || !t) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
453
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
454 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
455 tl = path_from_utf8(t);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
456 if (rename(sl, tl) < 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
457 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
458 /* this may have failed because moving a file across filesystems
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
459 was attempted, so try copy and delete instead */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
460 if (copy_file(s, t))
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
461 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
462 if (unlink(sl) < 0)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
463 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
464 /* err, now we can't delete the source file so return FALSE */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
465 ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
466 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
467 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
468 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
469 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
470 ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
471 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
472 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
473 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
474 g_free(tl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
475
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
476 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
477 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
478
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
479 gint rename_file(const gchar *s, const gchar *t)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
480 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
481 gchar *sl, *tl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
482 gint ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
483
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
484 if (!s || !t) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
485
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
486 sl = path_from_utf8(s);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
487 tl = path_from_utf8(t);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
488 ret = (rename(sl, tl) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
489 g_free(sl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
490 g_free(tl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
491
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
492 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
493 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
494
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
495 gchar *get_current_dir(void)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
496 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
497 gchar *pathl;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
498 gchar *path8;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
499
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
500 pathl = g_get_current_dir();
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
501 path8 = path_to_utf8(pathl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
502 g_free(pathl);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
503
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
504 return path8;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
505 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
506
576
9dc0513837b5 dropped path_list functions, use filelist functions everywhere
nadvornik
parents: 545
diff changeset
507 void string_list_free(GList *list)
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
508 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
509 g_list_foreach(list, (GFunc)g_free, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
510 g_list_free(list);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
511 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
512
576
9dc0513837b5 dropped path_list functions, use filelist functions everywhere
nadvornik
parents: 545
diff changeset
513 GList *string_list_copy(GList *list)
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
514 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
515 GList *new_list = NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
516 GList *work;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
517
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
518 work = list;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
519 while (work)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
520 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
521 gchar *path;
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
522
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
523 path = work->data;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
524 work = work->next;
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
525
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
526 new_list = g_list_prepend(new_list, g_strdup(path));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
527 }
442
4b2d7f9af171 Big whitespaces cleanup:
zas_
parents: 435
diff changeset
528
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
529 return g_list_reverse(new_list);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
530 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
531
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
532 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
533 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
534 gchar *unique;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
535 gint n = 1;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
536
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
537 if (!ext) ext = "";
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
538 if (!divider) divider = "";
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
539
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
540 unique = g_strconcat(path, ext, NULL);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
541 while (isname(unique))
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
542 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
543 g_free(unique);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
544 if (pad)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
545 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
546 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
547 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
548 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
549 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
550 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
551 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
552 n++;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
553 if (n > 999)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
554 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
555 /* well, we tried */
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
556 g_free(unique);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
557 return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
558 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
559 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
560
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
561 return unique;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
562 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
563
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
564 gchar *unique_filename_simple(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
565 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
566 gchar *unique;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
567 const gchar *name;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
568 const gchar *ext;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
569
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
570 if (!path) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
571
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
572 name = filename_from_path(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
573 if (!name) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
574
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
575 ext = extension_from_path(name);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
576
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
577 if (!ext)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
578 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
579 unique = unique_filename(path, NULL, "_", TRUE);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
580 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
581 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
582 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
583 gchar *base;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
584
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
585 base = remove_extension_from_path(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
586 unique = unique_filename(base, ext, "_", TRUE);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
587 g_free(base);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
588 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
589
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
590 return unique;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
591 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
592
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
593 const gchar *filename_from_path(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
594 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
595 const gchar *base;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
596
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
597 if (!path) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
598
701
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
599 base = strrchr(path, G_DIR_SEPARATOR);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
600 if (base) return base + 1;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
601
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
602 return path;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
603 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
604
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
605 gchar *remove_level_from_path(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
606 {
543
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
607 gint p = 0, n = -1;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
608
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
609 if (!path) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
610
543
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
611 while (path[p])
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
612 {
701
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
613 if (path[p] == G_DIR_SEPARATOR) n = p;
543
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
614 p++;
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
615 }
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
616 if (n <= 0) n++;
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
617
b68fd1eb3243 remove_level_from_path() was simplified.
zas_
parents: 516
diff changeset
618 return g_strndup(path, (gsize) n);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
619 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
620
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
621 const gchar *extension_from_path(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
622 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
623 if (!path) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
624 return strrchr(path, '.');
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
625 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
626
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
627 gint file_extension_match(const gchar *path, const gchar *ext)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
628 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
629 gint p;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
630 gint e;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
631
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
632 if (!path) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
633 if (!ext) return TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
634
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
635 p = strlen(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
636 e = strlen(ext);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
637
605
651ae2be1031 Use g_ascii_strncasecmp() instead of strncasecmp() where applicable.
zas_
parents: 576
diff changeset
638 /* FIXME: utf8 */
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
639 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
640 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
641
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
642 gchar *remove_extension_from_path(const gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
643 {
544
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
644 gint p = 0, n = -1;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
645
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
646 if (!path) return NULL;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
647
544
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
648 while (path[p])
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
649 {
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
650 if (path[p] == '.') n = p;
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
651 p++;
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
652 }
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
653 if (n < 0) n = p;
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
654
8de7cdde59dd Simplify remove_extension_from_path().
zas_
parents: 543
diff changeset
655 return g_strndup(path, (gsize) n);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
656 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
657
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
658 void parse_out_relatives(gchar *path)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
659 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
660 gint s, t;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
661
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
662 if (!path) return;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
663
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
664 s = t = 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
665
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
666 while (path[s] != '\0')
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
667 {
701
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
668 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.' && (path[s+2] == G_DIR_SEPARATOR || path[s+2] == '\0') )
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
669 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
670 s += 2;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
671 }
701
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
672 else if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.' && path[s+2] == '.' && (path[s+3] == G_DIR_SEPARATOR || path[s+3] == '\0') )
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
673 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
674 s += 3;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
675 if (t > 0) t--;
701
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
676 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
677 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
678 else
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
679 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
680 if (s != t) path[t] = path[s];
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
681 t++;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
682 s++;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
683 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
684 }
701
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
685 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
28af4b6dd9ef Use G_DIR_SEPARATOR instead of '/' where applicable.
zas_
parents: 673
diff changeset
686 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
687 path[t] = '\0';
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
688 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
689
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
690 gint file_in_path(const gchar *name)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
691 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
692 gchar *path;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
693 gchar *namel;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
694 gint p, l;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
695 gint ret = FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
696
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
697 if (!name) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
698 path = g_strdup(getenv("PATH"));
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
699 if (!path) return FALSE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
700 namel = path_from_utf8(name);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
701
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
702 p = 0;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
703 l = strlen(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
704 while (p < l && !ret)
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
705 {
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
706 gchar *f;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
707 gint e = p;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
708 while (path[e] != ':' && path[e] != '\0') e++;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
709 path[e] = '\0';
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
710 e++;
703
911e3769b60c Use g_build_filename() instead of g_strconcat().
zas_
parents: 702
diff changeset
711 f = g_build_filename(path + p, namel, NULL);
9
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
712 if (isfile(f)) ret = TRUE;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
713 g_free(f);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
714 p = e;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
715 }
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
716 g_free(namel);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
717 g_free(path);
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
718
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
719 return ret;
d907d608745f Sync to GQview 1.5.9 release.
gqview
parents:
diff changeset
720 }