1 /* gfileutils.c - File utility functions
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
35 #include <sys/types.h>
43 #endif /* G_OS_WIN32 */
58 * @filename: a filename to test in the GLib file name encoding
59 * @test: bitfield of #GFileTest flags
61 * Returns %TRUE if any of the tests in the bitfield @test are
62 * %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
63 * G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
64 * the check whether it's a directory doesn't matter since the existence
65 * test is %TRUE. With the current set of available tests, there's no point
66 * passing in more than one test at a time.
68 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
69 * so for a symbolic link to a regular file g_file_test() will return
70 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
72 * Note, that for a dangling symbolic link g_file_test() will return
73 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
75 * You should never use g_file_test() to test whether it is safe
76 * to perform an operation, because there is always the possibility
77 * of the condition changing before you actually perform the operation.
78 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
79 * to know whether it is is safe to write to a file without being
80 * tricked into writing into a different location. It doesn't work!
82 * <informalexample><programlisting>
83 * /* DON'T DO THIS */
84 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) {
85 * fd = g_open (filename, O_WRONLY);
86 * /* write to fd */
88 * </programlisting></informalexample>
90 * Another thing to note is that %G_FILE_TEST_EXISTS and
91 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
92 * system call. This usually doesn't matter, but if your program
93 * is setuid or setgid it means that these tests will give you
94 * the answer for the real user ID and group ID, rather than the
95 * effective user ID and group ID.
97 * On Windows, there are no symlinks, so testing for
98 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
99 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
100 * its name indicates that it is executable, checking for well-known
101 * extensions and those listed in the %PATHEXT environment variable.
103 * Return value: whether a test was %TRUE
106 g_file_test (const gchar *filename,
112 if (G_WIN32_HAVE_WIDECHAR_API ())
114 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
116 if (wfilename == NULL)
119 attributes = GetFileAttributesW (wfilename);
125 gchar *cpfilename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
127 if (cpfilename == NULL)
130 attributes = GetFileAttributesA (cpfilename);
135 if (attributes == INVALID_FILE_ATTRIBUTES)
138 if (test & G_FILE_TEST_EXISTS)
141 if (test & G_FILE_TEST_IS_REGULAR)
142 return (attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0;
144 if (test & G_FILE_TEST_IS_DIR)
145 return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
147 if (test & G_FILE_TEST_IS_EXECUTABLE)
149 const gchar *lastdot = strrchr (filename, '.');
150 const gchar *pathext = NULL, *p;
156 if (stricmp (lastdot, ".exe") == 0 ||
157 stricmp (lastdot, ".cmd") == 0 ||
158 stricmp (lastdot, ".bat") == 0 ||
159 stricmp (lastdot, ".com") == 0)
162 /* Check if it is one of the types listed in %PATHEXT% */
164 pathext = g_getenv ("PATHEXT");
168 pathext = g_utf8_casefold (pathext, -1);
170 lastdot = g_utf8_casefold (lastdot, -1);
171 extlen = strlen (lastdot);
176 const gchar *q = strchr (p, ';');
179 if (extlen == q - p &&
180 memcmp (lastdot, p, extlen) == 0)
182 g_free ((gchar *) pathext);
183 g_free ((gchar *) lastdot);
192 g_free ((gchar *) pathext);
193 g_free ((gchar *) lastdot);
199 if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
202 if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
207 /* For root, on some POSIX systems, access (filename, X_OK)
208 * will succeed even if no executable bits are set on the
209 * file. We fall through to a stat test to avoid that.
213 test &= ~G_FILE_TEST_IS_EXECUTABLE;
215 if (test & G_FILE_TEST_IS_SYMLINK)
219 if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
223 if (test & (G_FILE_TEST_IS_REGULAR |
225 G_FILE_TEST_IS_EXECUTABLE))
229 if (stat (filename, &s) == 0)
231 if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
234 if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
237 /* The extra test for root when access (file, X_OK) succeeds.
239 if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
240 ((s.st_mode & S_IXOTH) ||
241 (s.st_mode & S_IXUSR) ||
242 (s.st_mode & S_IXGRP)))
255 /* Binary compatibility version. Not for newly compiled code. */
258 g_file_test (const gchar *filename,
261 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
264 if (utf8_filename == NULL)
267 retval = g_file_test_utf8 (utf8_filename, test);
269 g_free (utf8_filename);
277 g_file_error_quark (void)
281 q = g_quark_from_static_string ("g-file-error-quark");
287 * g_file_error_from_errno:
288 * @err_no: an "errno" value
290 * Gets a #GFileError constant based on the passed-in @errno.
291 * For example, if you pass in %EEXIST this function returns
292 * #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
293 * assume that all #GFileError values will exist.
295 * Normally a #GFileError value goes into a #GError returned
296 * from a function that manipulates files. So you would use
297 * g_file_error_from_errno() when constructing a #GError.
299 * Return value: #GFileError corresponding to the given @errno
302 g_file_error_from_errno (gint err_no)
308 return G_FILE_ERROR_EXIST;
314 return G_FILE_ERROR_ISDIR;
320 return G_FILE_ERROR_ACCES;
326 return G_FILE_ERROR_NAMETOOLONG;
332 return G_FILE_ERROR_NOENT;
338 return G_FILE_ERROR_NOTDIR;
344 return G_FILE_ERROR_NXIO;
350 return G_FILE_ERROR_NODEV;
356 return G_FILE_ERROR_ROFS;
362 return G_FILE_ERROR_TXTBSY;
368 return G_FILE_ERROR_FAULT;
374 return G_FILE_ERROR_LOOP;
380 return G_FILE_ERROR_NOSPC;
386 return G_FILE_ERROR_NOMEM;
392 return G_FILE_ERROR_MFILE;
398 return G_FILE_ERROR_NFILE;
404 return G_FILE_ERROR_BADF;
410 return G_FILE_ERROR_INVAL;
416 return G_FILE_ERROR_PIPE;
422 return G_FILE_ERROR_AGAIN;
428 return G_FILE_ERROR_INTR;
434 return G_FILE_ERROR_IO;
440 return G_FILE_ERROR_PERM;
446 return G_FILE_ERROR_NOSYS;
451 return G_FILE_ERROR_FAILED;
457 get_contents_stdio (const gchar *display_filename,
467 size_t total_allocated;
469 g_assert (f != NULL);
471 #define STARTING_ALLOC 64
474 total_allocated = STARTING_ALLOC;
475 str = g_malloc (STARTING_ALLOC);
479 bytes = fread (buf, 1, 2048, f);
481 while ((total_bytes + bytes + 1) > total_allocated)
483 total_allocated *= 2;
484 str = g_try_realloc (str, total_allocated);
491 _("Could not allocate %lu bytes to read file \"%s\""),
492 (gulong) total_allocated,
503 g_file_error_from_errno (errno),
504 _("Error reading file '%s': %s"),
511 memcpy (str + total_bytes, buf, bytes);
512 total_bytes += bytes;
517 str[total_bytes] = '\0';
520 *length = total_bytes;
537 get_contents_regfile (const gchar *display_filename,
538 struct stat *stat_buf,
549 size = stat_buf->st_size;
551 alloc_size = size + 1;
552 buf = g_try_malloc (alloc_size);
559 _("Could not allocate %lu bytes to read file \"%s\""),
567 while (bytes_read < size)
571 rc = read (fd, buf + bytes_read, size - bytes_read);
580 g_file_error_from_errno (errno),
581 _("Failed to read from file '%s': %s"),
594 buf[bytes_read] = '\0';
597 *length = bytes_read;
613 get_contents_posix (const gchar *filename,
618 struct stat stat_buf;
620 gchar *display_filename = g_filename_display_name (filename);
622 /* O_BINARY useful on Cygwin */
623 fd = open (filename, O_RDONLY|O_BINARY);
629 g_file_error_from_errno (errno),
630 _("Failed to open file '%s': %s"),
633 g_free (display_filename);
638 /* I don't think this will ever fail, aside from ENOMEM, but. */
639 if (fstat (fd, &stat_buf) < 0)
644 g_file_error_from_errno (errno),
645 _("Failed to get attributes of file '%s': fstat() failed: %s"),
648 g_free (display_filename);
653 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
655 gboolean retval = get_contents_regfile (display_filename,
661 g_free (display_filename);
670 f = fdopen (fd, "r");
676 g_file_error_from_errno (errno),
677 _("Failed to open file '%s': fdopen() failed: %s"),
680 g_free (display_filename);
685 retval = get_contents_stdio (display_filename, f, contents, length, error);
686 g_free (display_filename);
692 #else /* G_OS_WIN32 */
695 get_contents_win32 (const gchar *filename,
702 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
703 gchar *display_filename = g_filename_display_name (filename);
705 f = _wfopen (wfilename, L"rb");
712 g_file_error_from_errno (errno),
713 _("Failed to open file '%s': %s"),
716 g_free (display_filename);
721 retval = get_contents_stdio (display_filename, f, contents, length, error);
722 g_free (display_filename);
730 * g_file_get_contents:
731 * @filename: name of a file to read contents from, in the GLib file name encoding
732 * @contents: location to store an allocated string
733 * @length: location to store length in bytes of the contents, or %NULL
734 * @error: return location for a #GError, or %NULL
736 * Reads an entire file into allocated memory, with good error
739 * If the call was successful, it returns %TRUE and sets @contents to the file
740 * contents and @length to the length of the file contents in bytes. The string
741 * stored in @contents will be nul-terminated, so for text files you can pass
742 * %NULL for the @length argument. If the call was not successful, it returns
743 * %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
744 * codes are those in the #GFileError enumeration. In the error case,
745 * @contents is set to %NULL and @length is set to zero.
747 * Return value: %TRUE on success, %FALSE if an error occurred
750 g_file_get_contents (const gchar *filename,
755 g_return_val_if_fail (filename != NULL, FALSE);
756 g_return_val_if_fail (contents != NULL, FALSE);
763 return get_contents_win32 (filename, contents, length, error);
765 return get_contents_posix (filename, contents, length, error);
771 #undef g_file_get_contents
773 /* Binary compatibility version. Not for newly compiled code. */
776 g_file_get_contents (const gchar *filename,
781 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
784 if (utf8_filename == NULL)
787 retval = g_file_get_contents (utf8_filename, contents, length, error);
789 g_free (utf8_filename);
797 * mkstemp() implementation is from the GNU C library.
798 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
802 * @tmpl: template filename
804 * Opens a temporary file. See the mkstemp() documentation
805 * on most UNIX-like systems. This is a portability wrapper, which simply calls
806 * mkstemp() on systems that have it, and implements
807 * it in GLib otherwise.
809 * The parameter is a string that should match the rules for
810 * mkstemp(), i.e. end in "XXXXXX". The X string will
811 * be modified to form the name of a file that didn't exist.
812 * The string should be in the GLib file name encoding. Most importantly,
813 * on Windows it should be in UTF-8.
815 * Return value: A file handle (as from open()) to the file
816 * opened for reading and writing. The file is opened in binary mode
817 * on platforms where there is a difference. The file handle should be
818 * closed with close(). In case of errors, -1 is returned.
821 g_mkstemp (gchar *tmpl)
824 return mkstemp (tmpl);
829 static const char letters[] =
830 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
831 static const int NLETTERS = sizeof (letters) - 1;
834 static int counter = 0;
837 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
840 /* This is where the Xs start. */
841 XXXXXX = &tmpl[len - 6];
843 /* Get some more or less random data. */
844 g_get_current_time (&tv);
845 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
847 for (count = 0; count < 100; value += 7777, ++count)
851 /* Fill in the random bits. */
852 XXXXXX[0] = letters[v % NLETTERS];
854 XXXXXX[1] = letters[v % NLETTERS];
856 XXXXXX[2] = letters[v % NLETTERS];
858 XXXXXX[3] = letters[v % NLETTERS];
860 XXXXXX[4] = letters[v % NLETTERS];
862 XXXXXX[5] = letters[v % NLETTERS];
864 /* tmpl is in UTF-8 on Windows, thus use g_open() */
865 fd = g_open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
869 else if (errno != EEXIST)
870 /* Any other error will apply also to other names we might
871 * try, and there are 2^32 or so of them, so give up now.
876 /* We got out of the loop because we ran out of combinations to try. */
885 /* Binary compatibility version. Not for newly compiled code. */
888 g_mkstemp (gchar *tmpl)
893 static const char letters[] =
894 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
895 static const int NLETTERS = sizeof (letters) - 1;
898 static int counter = 0;
901 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
904 /* This is where the Xs start. */
905 XXXXXX = &tmpl[len - 6];
907 /* Get some more or less random data. */
908 g_get_current_time (&tv);
909 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
911 for (count = 0; count < 100; value += 7777, ++count)
915 /* Fill in the random bits. */
916 XXXXXX[0] = letters[v % NLETTERS];
918 XXXXXX[1] = letters[v % NLETTERS];
920 XXXXXX[2] = letters[v % NLETTERS];
922 XXXXXX[3] = letters[v % NLETTERS];
924 XXXXXX[4] = letters[v % NLETTERS];
926 XXXXXX[5] = letters[v % NLETTERS];
928 /* This is the backward compatibility system codepage version,
929 * thus use normal open().
931 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
935 else if (errno != EEXIST)
936 /* Any other error will apply also to other names we might
937 * try, and there are 2^32 or so of them, so give up now.
942 /* We got out of the loop because we ran out of combinations to try. */
950 * @tmpl: Template for file name, as in g_mkstemp(), basename only
951 * @name_used: location to store actual name used
952 * @error: return location for a #GError
954 * Opens a file for writing in the preferred directory for temporary
955 * files (as returned by g_get_tmp_dir()).
957 * @tmpl should be a string in the GLib file name encoding ending with
958 * six 'X' characters, as the parameter to g_mkstemp() (or mkstemp()).
959 * However, unlike these functions, the template should only be a
960 * basename, no directory components are allowed. If template is
961 * %NULL, a default template is used.
963 * Note that in contrast to g_mkstemp() (and mkstemp())
964 * @tmpl is not modified, and might thus be a read-only literal string.
966 * The actual name used is returned in @name_used if non-%NULL. This
967 * string should be freed with g_free() when not needed any longer.
968 * The returned name is in the GLib file name encoding.
970 * Return value: A file handle (as from open()) to
971 * the file opened for reading and writing. The file is opened in binary
972 * mode on platforms where there is a difference. The file handle should be
973 * closed with close(). In case of errors, -1 is returned
974 * and @error will be set.
977 g_file_open_tmp (const gchar *tmpl,
990 if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
992 || (strchr (tmpl, '/') != NULL && (slash = "/"))
996 gchar *display_tmpl = g_filename_display_name (tmpl);
1003 G_FILE_ERROR_FAILED,
1004 _("Template '%s' invalid, should not contain a '%s'"),
1006 g_free (display_tmpl);
1011 if (strlen (tmpl) < 6 ||
1012 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
1014 gchar *display_tmpl = g_filename_display_name (tmpl);
1017 G_FILE_ERROR_FAILED,
1018 _("Template '%s' doesn't end with XXXXXX"),
1020 g_free (display_tmpl);
1024 tmpdir = g_get_tmp_dir ();
1026 if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1029 sep = G_DIR_SEPARATOR_S;
1031 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1033 retval = g_mkstemp (fulltemplate);
1037 gchar *display_fulltemplate = g_filename_display_name (fulltemplate);
1040 g_file_error_from_errno (errno),
1041 _("Failed to create file '%s': %s"),
1042 display_fulltemplate, g_strerror (errno));
1043 g_free (display_fulltemplate);
1044 g_free (fulltemplate);
1049 *name_used = fulltemplate;
1051 g_free (fulltemplate);
1058 #undef g_file_open_tmp
1060 /* Binary compatibility version. Not for newly compiled code. */
1063 g_file_open_tmp (const gchar *tmpl,
1067 gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error);
1068 gchar *utf8_name_used;
1071 if (utf8_tmpl == NULL)
1074 retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error);
1080 *name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL);
1082 g_free (utf8_name_used);
1090 g_build_pathv (const gchar *separator,
1091 const gchar *first_element,
1095 gint separator_len = strlen (separator);
1096 gboolean is_first = TRUE;
1097 gboolean have_leading = FALSE;
1098 const gchar *single_element = NULL;
1099 const gchar *next_element;
1100 const gchar *last_trailing = NULL;
1102 result = g_string_new (NULL);
1104 next_element = first_element;
1108 const gchar *element;
1114 element = next_element;
1115 next_element = va_arg (args, gchar *);
1120 /* Ignore empty elements */
1129 strncmp (start, separator, separator_len) == 0)
1130 start += separator_len;
1133 end = start + strlen (start);
1137 while (end >= start + separator_len &&
1138 strncmp (end - separator_len, separator, separator_len) == 0)
1139 end -= separator_len;
1141 last_trailing = end;
1142 while (last_trailing >= element + separator_len &&
1143 strncmp (last_trailing - separator_len, separator, separator_len) == 0)
1144 last_trailing -= separator_len;
1148 /* If the leading and trailing separator strings are in the
1149 * same element and overlap, the result is exactly that element
1151 if (last_trailing <= start)
1152 single_element = element;
1154 g_string_append_len (result, element, start - element);
1155 have_leading = TRUE;
1158 single_element = NULL;
1165 g_string_append (result, separator);
1167 g_string_append_len (result, start, end - start);
1173 g_string_free (result, TRUE);
1174 return g_strdup (single_element);
1179 g_string_append (result, last_trailing);
1181 return g_string_free (result, FALSE);
1187 * @separator: a string used to separator the elements of the path.
1188 * @first_element: the first element in the path
1189 * @Varargs: remaining elements in path, terminated by %NULL
1191 * Creates a path from a series of elements using @separator as the
1192 * separator between elements. At the boundary between two elements,
1193 * any trailing occurrences of separator in the first element, or
1194 * leading occurrences of separator in the second element are removed
1195 * and exactly one copy of the separator is inserted.
1197 * Empty elements are ignored.
1199 * The number of leading copies of the separator on the result is
1200 * the same as the number of leading copies of the separator on
1201 * the first non-empty element.
1203 * The number of trailing copies of the separator on the result is
1204 * the same as the number of trailing copies of the separator on
1205 * the last non-empty element. (Determination of the number of
1206 * trailing copies is done without stripping leading copies, so
1207 * if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
1208 * has 1 trailing copy.)
1210 * However, if there is only a single non-empty element, and there
1211 * are no characters in that element not part of the leading or
1212 * trailing separators, then the result is exactly the original value
1215 * Other than for determination of the number of leading and trailing
1216 * copies of the separator, elements consisting only of copies
1217 * of the separator are ignored.
1219 * Return value: a newly-allocated string that must be freed with g_free().
1222 g_build_path (const gchar *separator,
1223 const gchar *first_element,
1229 g_return_val_if_fail (separator != NULL, NULL);
1231 va_start (args, first_element);
1232 str = g_build_pathv (separator, first_element, args);
1240 * @first_element: the first element in the path
1241 * @Varargs: remaining elements in path, terminated by %NULL
1243 * Creates a filename from a series of elements using the correct
1244 * separator for filenames.
1246 * On Unix, this function behaves identically to <literal>g_build_path
1247 * (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
1249 * On Windows, it takes into account that either the backslash
1250 * (<literal>\</literal> or slash (<literal>/</literal>) can be used
1251 * as separator in filenames, but otherwise behaves as on Unix. When
1252 * file pathname separators need to be inserted, the one that last
1253 * previously occurred in the parameters (reading from left to right)
1256 * No attempt is made to force the resulting filename to be an absolute
1257 * path. If the first element is a relative path, the result will
1258 * be a relative path.
1260 * Return value: a newly-allocated string that must be freed with g_free().
1263 g_build_filename (const gchar *first_element,
1270 va_start (args, first_element);
1271 str = g_build_pathv (G_DIR_SEPARATOR_S, first_element, args);
1276 /* Code copied from g_build_pathv(), and modifed to use two
1277 * alternative single-character separators.
1281 gboolean is_first = TRUE;
1282 gboolean have_leading = FALSE;
1283 const gchar *single_element = NULL;
1284 const gchar *next_element;
1285 const gchar *last_trailing = NULL;
1286 gchar current_separator = '\\';
1288 va_start (args, first_element);
1290 result = g_string_new (NULL);
1292 next_element = first_element;
1296 const gchar *element;
1302 element = next_element;
1303 next_element = va_arg (args, gchar *);
1308 /* Ignore empty elements */
1317 (*start == '\\' || *start == '/'))
1319 current_separator = *start;
1324 end = start + strlen (start);
1328 while (end >= start + 1 &&
1329 (end[-1] == '\\' || end[-1] == '/'))
1331 current_separator = end[-1];
1335 last_trailing = end;
1336 while (last_trailing >= element + 1 &&
1337 (last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
1342 /* If the leading and trailing separator strings are in the
1343 * same element and overlap, the result is exactly that element
1345 if (last_trailing <= start)
1346 single_element = element;
1348 g_string_append_len (result, element, start - element);
1349 have_leading = TRUE;
1352 single_element = NULL;
1359 g_string_append_len (result, ¤t_separator, 1);
1361 g_string_append_len (result, start, end - start);
1369 g_string_free (result, TRUE);
1370 return g_strdup (single_element);
1375 g_string_append (result, last_trailing);
1377 return g_string_free (result, FALSE);
1384 * @filename: the symbolic link
1385 * @error: return location for a #GError
1387 * Reads the contents of the symbolic link @filename like the POSIX
1388 * readlink() function. The returned string is in the encoding used
1389 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
1391 * Returns: A newly allocated string with the contents of the symbolic link,
1392 * or %NULL if an error occurred.
1397 g_file_read_link (const gchar *filename,
1400 #ifdef HAVE_READLINK
1406 buffer = g_malloc (size);
1410 read_size = readlink (filename, buffer, size);
1411 if (read_size < 0) {
1412 gchar *display_filename = g_filename_display_name (filename);
1416 g_file_error_from_errno (errno),
1417 _("Failed to read the symbolic link '%s': %s"),
1419 g_strerror (errno));
1420 g_free (display_filename);
1425 if (read_size < size)
1427 buffer[read_size] = 0;
1432 buffer = g_realloc (buffer, size);
1438 _("Symbolic links not supported"));