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>
42 #endif /* G_OS_WIN32 */
57 * @filename: a filename to test in the GLib file name encoding
58 * @test: bitfield of #GFileTest flags
60 * Returns %TRUE if any of the tests in the bitfield @test are
61 * %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
62 * G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
63 * the check whether it's a directory doesn't matter since the existence
64 * test is %TRUE. With the current set of available tests, there's no point
65 * passing in more than one test at a time.
67 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
68 * so for a symbolic link to a regular file g_file_test() will return
69 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
71 * Note, that for a dangling symbolic link g_file_test() will return
72 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
74 * You should never use g_file_test() to test whether it is safe
75 * to perform an operation, because there is always the possibility
76 * of the condition changing before you actually perform the operation.
77 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
78 * to know whether it is is safe to write to a file without being
79 * tricked into writing into a different location. It doesn't work!
81 * <informalexample><programlisting>
82 * /* DON'T DO THIS */
83 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) {
84 * fd = g_open (filename, O_WRONLY);
85 * /* write to fd */
87 * </programlisting></informalexample>
89 * Another thing to note is that %G_FILE_TEST_EXISTS and
90 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
91 * system call. This usually doesn't matter, but if your program
92 * is setuid or setgid it means that these tests will give you
93 * the answer for the real user ID and group ID, rather than the
94 * effective user ID and group ID.
96 * On Windows, there are no symlinks, so testing for
97 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
98 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
99 * its name indicates that it is executable, checking for well-known
100 * extensions and those listed in the %PATHEXT environment variable.
102 * Return value: whether a test was %TRUE
105 g_file_test (const gchar *filename,
111 if (G_WIN32_HAVE_WIDECHAR_API ())
113 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
115 if (wfilename == NULL)
118 attributes = GetFileAttributesW (wfilename);
124 gchar *cpfilename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
126 if (cpfilename == NULL)
129 attributes = GetFileAttributesA (cpfilename);
134 if (attributes == INVALID_FILE_ATTRIBUTES)
137 if (test & G_FILE_TEST_EXISTS)
140 if (test & G_FILE_TEST_IS_REGULAR)
141 return (attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0;
143 if (test & G_FILE_TEST_IS_DIR)
144 return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
146 if (test & G_FILE_TEST_IS_EXECUTABLE)
148 const gchar *lastdot = strrchr (filename, '.');
149 const gchar *pathext = NULL, *p;
155 if (stricmp (lastdot, ".exe") == 0 ||
156 stricmp (lastdot, ".cmd") == 0 ||
157 stricmp (lastdot, ".bat") == 0 ||
158 stricmp (lastdot, ".com") == 0)
161 /* Check if it is one of the types listed in %PATHEXT% */
163 pathext = g_getenv ("PATHEXT");
167 pathext = g_utf8_casefold (pathext, -1);
169 lastdot = g_utf8_casefold (lastdot, -1);
170 extlen = strlen (lastdot);
175 const gchar *q = strchr (p, ';');
178 if (extlen == q - p &&
179 memcmp (lastdot, p, extlen) == 0)
181 g_free ((gchar *) pathext);
182 g_free ((gchar *) lastdot);
191 g_free ((gchar *) pathext);
192 g_free ((gchar *) lastdot);
198 if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
201 if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
206 /* For root, on some POSIX systems, access (filename, X_OK)
207 * will succeed even if no executable bits are set on the
208 * file. We fall through to a stat test to avoid that.
212 test &= ~G_FILE_TEST_IS_EXECUTABLE;
214 if (test & G_FILE_TEST_IS_SYMLINK)
218 if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
222 if (test & (G_FILE_TEST_IS_REGULAR |
224 G_FILE_TEST_IS_EXECUTABLE))
228 if (stat (filename, &s) == 0)
230 if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
233 if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
236 /* The extra test for root when access (file, X_OK) succeeds.
238 if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
239 ((s.st_mode & S_IXOTH) ||
240 (s.st_mode & S_IXUSR) ||
241 (s.st_mode & S_IXGRP)))
254 /* Binary compatibility version. Not for newly compiled code. */
257 g_file_test (const gchar *filename,
260 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
263 if (utf8_filename == NULL)
266 retval = g_file_test_utf8 (utf8_filename, test);
268 g_free (utf8_filename);
276 g_file_error_quark (void)
280 q = g_quark_from_static_string ("g-file-error-quark");
286 * g_file_error_from_errno:
287 * @err_no: an "errno" value
289 * Gets a #GFileError constant based on the passed-in @errno.
290 * For example, if you pass in %EEXIST this function returns
291 * #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
292 * assume that all #GFileError values will exist.
294 * Normally a #GFileError value goes into a #GError returned
295 * from a function that manipulates files. So you would use
296 * g_file_error_from_errno() when constructing a #GError.
298 * Return value: #GFileError corresponding to the given @errno
301 g_file_error_from_errno (gint err_no)
307 return G_FILE_ERROR_EXIST;
313 return G_FILE_ERROR_ISDIR;
319 return G_FILE_ERROR_ACCES;
325 return G_FILE_ERROR_NAMETOOLONG;
331 return G_FILE_ERROR_NOENT;
337 return G_FILE_ERROR_NOTDIR;
343 return G_FILE_ERROR_NXIO;
349 return G_FILE_ERROR_NODEV;
355 return G_FILE_ERROR_ROFS;
361 return G_FILE_ERROR_TXTBSY;
367 return G_FILE_ERROR_FAULT;
373 return G_FILE_ERROR_LOOP;
379 return G_FILE_ERROR_NOSPC;
385 return G_FILE_ERROR_NOMEM;
391 return G_FILE_ERROR_MFILE;
397 return G_FILE_ERROR_NFILE;
403 return G_FILE_ERROR_BADF;
409 return G_FILE_ERROR_INVAL;
415 return G_FILE_ERROR_PIPE;
421 return G_FILE_ERROR_AGAIN;
427 return G_FILE_ERROR_INTR;
433 return G_FILE_ERROR_IO;
439 return G_FILE_ERROR_PERM;
445 return G_FILE_ERROR_NOSYS;
450 return G_FILE_ERROR_FAILED;
456 get_contents_stdio (const gchar *display_filename,
466 size_t total_allocated;
468 g_assert (f != NULL);
470 #define STARTING_ALLOC 64
473 total_allocated = STARTING_ALLOC;
474 str = g_malloc (STARTING_ALLOC);
478 bytes = fread (buf, 1, 2048, f);
480 while ((total_bytes + bytes + 1) > total_allocated)
482 total_allocated *= 2;
483 str = g_try_realloc (str, total_allocated);
490 _("Could not allocate %lu bytes to read file \"%s\""),
491 (gulong) total_allocated,
502 g_file_error_from_errno (errno),
503 _("Error reading file '%s': %s"),
510 memcpy (str + total_bytes, buf, bytes);
511 total_bytes += bytes;
516 str[total_bytes] = '\0';
519 *length = total_bytes;
536 get_contents_regfile (const gchar *display_filename,
537 struct stat *stat_buf,
548 size = stat_buf->st_size;
550 alloc_size = size + 1;
551 buf = g_try_malloc (alloc_size);
558 _("Could not allocate %lu bytes to read file \"%s\""),
566 while (bytes_read < size)
570 rc = read (fd, buf + bytes_read, size - bytes_read);
579 g_file_error_from_errno (errno),
580 _("Failed to read from file '%s': %s"),
593 buf[bytes_read] = '\0';
596 *length = bytes_read;
612 get_contents_posix (const gchar *filename,
617 struct stat stat_buf;
619 gchar *display_filename = g_filename_display_name (filename);
621 /* O_BINARY useful on Cygwin */
622 fd = open (filename, O_RDONLY|O_BINARY);
628 g_file_error_from_errno (errno),
629 _("Failed to open file '%s': %s"),
632 g_free (display_filename);
637 /* I don't think this will ever fail, aside from ENOMEM, but. */
638 if (fstat (fd, &stat_buf) < 0)
643 g_file_error_from_errno (errno),
644 _("Failed to get attributes of file '%s': fstat() failed: %s"),
647 g_free (display_filename);
652 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
654 gboolean retval = get_contents_regfile (display_filename,
660 g_free (display_filename);
669 f = fdopen (fd, "r");
675 g_file_error_from_errno (errno),
676 _("Failed to open file '%s': fdopen() failed: %s"),
679 g_free (display_filename);
684 retval = get_contents_stdio (display_filename, f, contents, length, error);
685 g_free (display_filename);
691 #else /* G_OS_WIN32 */
694 get_contents_win32 (const gchar *filename,
701 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
702 gchar *display_filename = g_filename_display_name (filename);
704 f = _wfopen (wfilename, L"rb");
711 g_file_error_from_errno (errno),
712 _("Failed to open file '%s': %s"),
715 g_free (display_filename);
720 retval = get_contents_stdio (display_filename, f, contents, length, error);
721 g_free (display_filename);
729 * g_file_get_contents:
730 * @filename: name of a file to read contents from, in the GLib file name encoding
731 * @contents: location to store an allocated string
732 * @length: location to store length in bytes of the contents
733 * @error: return location for a #GError
735 * Reads an entire file into allocated memory, with good error
736 * checking. If @error is set, %FALSE is returned, and @contents is set
737 * to %NULL. If %TRUE is returned, @error will not be set, and @contents
738 * will be set to the file contents. The string stored in @contents
739 * will be nul-terminated, so for text files you can pass %NULL for the
740 * @length argument. The error domain is #G_FILE_ERROR. Possible
741 * error codes are those in the #GFileError enumeration.
743 * Return value: %TRUE on success, %FALSE if error is set
746 g_file_get_contents (const gchar *filename,
751 g_return_val_if_fail (filename != NULL, FALSE);
752 g_return_val_if_fail (contents != NULL, FALSE);
759 return get_contents_win32 (filename, contents, length, error);
761 return get_contents_posix (filename, contents, length, error);
767 #undef g_file_get_contents
769 /* Binary compatibility version. Not for newly compiled code. */
772 g_file_get_contents (const gchar *filename,
777 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
780 if (utf8_filename == NULL)
783 retval = g_file_get_contents (utf8_filename, contents, length, error);
785 g_free (utf8_filename);
793 * mkstemp() implementation is from the GNU C library.
794 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
798 * @tmpl: template filename
800 * Opens a temporary file. See the mkstemp() documentation
801 * on most UNIX-like systems. This is a portability wrapper, which simply calls
802 * mkstemp() on systems that have it, and implements
803 * it in GLib otherwise.
805 * The parameter is a string that should match the rules for
806 * mkstemp(), i.e. end in "XXXXXX". The X string will
807 * be modified to form the name of a file that didn't exist.
808 * The string should be in the GLib file name encoding. Most importantly,
809 * on Windows it should be in UTF-8.
811 * Return value: A file handle (as from open()) to the file
812 * opened for reading and writing. The file is opened in binary mode
813 * on platforms where there is a difference. The file handle should be
814 * closed with close(). In case of errors, -1 is returned.
817 g_mkstemp (gchar *tmpl)
820 return mkstemp (tmpl);
825 static const char letters[] =
826 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
827 static const int NLETTERS = sizeof (letters) - 1;
830 static int counter = 0;
833 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
836 /* This is where the Xs start. */
837 XXXXXX = &tmpl[len - 6];
839 /* Get some more or less random data. */
840 g_get_current_time (&tv);
841 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
843 for (count = 0; count < 100; value += 7777, ++count)
847 /* Fill in the random bits. */
848 XXXXXX[0] = letters[v % NLETTERS];
850 XXXXXX[1] = letters[v % NLETTERS];
852 XXXXXX[2] = letters[v % NLETTERS];
854 XXXXXX[3] = letters[v % NLETTERS];
856 XXXXXX[4] = letters[v % NLETTERS];
858 XXXXXX[5] = letters[v % NLETTERS];
860 /* tmpl is in UTF-8 on Windows, thus use g_open() */
861 fd = g_open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
865 else if (errno != EEXIST)
866 /* Any other error will apply also to other names we might
867 * try, and there are 2^32 or so of them, so give up now.
872 /* We got out of the loop because we ran out of combinations to try. */
881 /* Binary compatibility version. Not for newly compiled code. */
884 g_mkstemp (gchar *tmpl)
889 static const char letters[] =
890 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
891 static const int NLETTERS = sizeof (letters) - 1;
894 static int counter = 0;
897 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
900 /* This is where the Xs start. */
901 XXXXXX = &tmpl[len - 6];
903 /* Get some more or less random data. */
904 g_get_current_time (&tv);
905 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
907 for (count = 0; count < 100; value += 7777, ++count)
911 /* Fill in the random bits. */
912 XXXXXX[0] = letters[v % NLETTERS];
914 XXXXXX[1] = letters[v % NLETTERS];
916 XXXXXX[2] = letters[v % NLETTERS];
918 XXXXXX[3] = letters[v % NLETTERS];
920 XXXXXX[4] = letters[v % NLETTERS];
922 XXXXXX[5] = letters[v % NLETTERS];
924 /* This is the backward compatibility system codepage version,
925 * thus use normal open().
927 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
931 else if (errno != EEXIST)
932 /* Any other error will apply also to other names we might
933 * try, and there are 2^32 or so of them, so give up now.
938 /* We got out of the loop because we ran out of combinations to try. */
946 * @tmpl: Template for file name, as in g_mkstemp(), basename only
947 * @name_used: location to store actual name used
948 * @error: return location for a #GError
950 * Opens a file for writing in the preferred directory for temporary
951 * files (as returned by g_get_tmp_dir()).
953 * @tmpl should be a string in the GLib file name encoding ending with
954 * six 'X' characters, as the parameter to g_mkstemp() (or mkstemp()).
955 * However, unlike these functions, the template should only be a
956 * basename, no directory components are allowed. If template is
957 * %NULL, a default template is used.
959 * Note that in contrast to g_mkstemp() (and mkstemp())
960 * @tmpl is not modified, and might thus be a read-only literal string.
962 * The actual name used is returned in @name_used if non-%NULL. This
963 * string should be freed with g_free() when not needed any longer.
964 * The returned name is in the GLib file name encoding.
966 * Return value: A file handle (as from open()) to
967 * the file opened for reading and writing. The file is opened in binary
968 * mode on platforms where there is a difference. The file handle should be
969 * closed with close(). In case of errors, -1 is returned
970 * and @error will be set.
973 g_file_open_tmp (const gchar *tmpl,
986 if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
988 || (strchr (tmpl, '/') != NULL && (slash = "/"))
992 gchar *display_tmpl = g_filename_display_name (tmpl);
1000 _("Template '%s' invalid, should not contain a '%s'"),
1002 g_free (display_tmpl);
1007 if (strlen (tmpl) < 6 ||
1008 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
1010 gchar *display_tmpl = g_filename_display_name (tmpl);
1013 G_FILE_ERROR_FAILED,
1014 _("Template '%s' doesn't end with XXXXXX"),
1016 g_free (display_tmpl);
1020 tmpdir = g_get_tmp_dir ();
1022 if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1025 sep = G_DIR_SEPARATOR_S;
1027 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1029 retval = g_mkstemp (fulltemplate);
1033 gchar *display_fulltemplate = g_filename_display_name (fulltemplate);
1036 g_file_error_from_errno (errno),
1037 _("Failed to create file '%s': %s"),
1038 display_fulltemplate, g_strerror (errno));
1039 g_free (display_fulltemplate);
1040 g_free (fulltemplate);
1045 *name_used = fulltemplate;
1047 g_free (fulltemplate);
1054 #undef g_file_open_tmp
1056 /* Binary compatibility version. Not for newly compiled code. */
1059 g_file_open_tmp (const gchar *tmpl,
1063 gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error);
1064 gchar *utf8_name_used;
1067 if (utf8_tmpl == NULL)
1070 retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error);
1076 *name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL);
1078 g_free (utf8_name_used);
1086 g_build_pathv (const gchar *separator,
1087 const gchar *first_element,
1091 gint separator_len = strlen (separator);
1092 gboolean is_first = TRUE;
1093 gboolean have_leading = FALSE;
1094 const gchar *single_element = NULL;
1095 const gchar *next_element;
1096 const gchar *last_trailing = NULL;
1098 result = g_string_new (NULL);
1100 next_element = first_element;
1104 const gchar *element;
1110 element = next_element;
1111 next_element = va_arg (args, gchar *);
1116 /* Ignore empty elements */
1125 strncmp (start, separator, separator_len) == 0)
1126 start += separator_len;
1129 end = start + strlen (start);
1133 while (end >= start + separator_len &&
1134 strncmp (end - separator_len, separator, separator_len) == 0)
1135 end -= separator_len;
1137 last_trailing = end;
1138 while (last_trailing >= element + separator_len &&
1139 strncmp (last_trailing - separator_len, separator, separator_len) == 0)
1140 last_trailing -= separator_len;
1144 /* If the leading and trailing separator strings are in the
1145 * same element and overlap, the result is exactly that element
1147 if (last_trailing <= start)
1148 single_element = element;
1150 g_string_append_len (result, element, start - element);
1151 have_leading = TRUE;
1154 single_element = NULL;
1161 g_string_append (result, separator);
1163 g_string_append_len (result, start, end - start);
1169 g_string_free (result, TRUE);
1170 return g_strdup (single_element);
1175 g_string_append (result, last_trailing);
1177 return g_string_free (result, FALSE);
1183 * @separator: a string used to separator the elements of the path.
1184 * @first_element: the first element in the path
1185 * @Varargs: remaining elements in path, terminated by %NULL
1187 * Creates a path from a series of elements using @separator as the
1188 * separator between elements. At the boundary between two elements,
1189 * any trailing occurrences of separator in the first element, or
1190 * leading occurrences of separator in the second element are removed
1191 * and exactly one copy of the separator is inserted.
1193 * Empty elements are ignored.
1195 * The number of leading copies of the separator on the result is
1196 * the same as the number of leading copies of the separator on
1197 * the first non-empty element.
1199 * The number of trailing copies of the separator on the result is
1200 * the same as the number of trailing copies of the separator on
1201 * the last non-empty element. (Determination of the number of
1202 * trailing copies is done without stripping leading copies, so
1203 * if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
1204 * has 1 trailing copy.)
1206 * However, if there is only a single non-empty element, and there
1207 * are no characters in that element not part of the leading or
1208 * trailing separators, then the result is exactly the original value
1211 * Other than for determination of the number of leading and trailing
1212 * copies of the separator, elements consisting only of copies
1213 * of the separator are ignored.
1215 * Return value: a newly-allocated string that must be freed with g_free().
1218 g_build_path (const gchar *separator,
1219 const gchar *first_element,
1225 g_return_val_if_fail (separator != NULL, NULL);
1227 va_start (args, first_element);
1228 str = g_build_pathv (separator, first_element, args);
1236 * @first_element: the first element in the path
1237 * @Varargs: remaining elements in path, terminated by %NULL
1239 * Creates a filename from a series of elements using the correct
1240 * separator for filenames.
1242 * On Unix, this function behaves identically to <literal>g_build_path
1243 * (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
1245 * On Windows, it takes into account that either the backslash
1246 * (<literal>\</literal> or slash (<literal>/</literal>) can be used
1247 * as separator in filenames, but otherwise behaves as on Unix. When
1248 * file pathname separators need to be inserted, the one that last
1249 * previously occurred in the parameters (reading from left to right)
1252 * No attempt is made to force the resulting filename to be an absolute
1253 * path. If the first element is a relative path, the result will
1254 * be a relative path.
1256 * Return value: a newly-allocated string that must be freed with g_free().
1259 g_build_filename (const gchar *first_element,
1266 va_start (args, first_element);
1267 str = g_build_pathv (G_DIR_SEPARATOR_S, first_element, args);
1272 /* Code copied from g_build_pathv(), and modifed to use two
1273 * alternative single-character separators.
1277 gboolean is_first = TRUE;
1278 gboolean have_leading = FALSE;
1279 const gchar *single_element = NULL;
1280 const gchar *next_element;
1281 const gchar *last_trailing = NULL;
1282 gchar current_separator = '\\';
1284 va_start (args, first_element);
1286 result = g_string_new (NULL);
1288 next_element = first_element;
1292 const gchar *element;
1298 element = next_element;
1299 next_element = va_arg (args, gchar *);
1304 /* Ignore empty elements */
1313 (*start == '\\' || *start == '/'))
1315 current_separator = *start;
1320 end = start + strlen (start);
1324 while (end >= start + 1 &&
1325 (end[-1] == '\\' || end[-1] == '/'))
1327 current_separator = end[-1];
1331 last_trailing = end;
1332 while (last_trailing >= element + 1 &&
1333 (last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
1338 /* If the leading and trailing separator strings are in the
1339 * same element and overlap, the result is exactly that element
1341 if (last_trailing <= start)
1342 single_element = element;
1344 g_string_append_len (result, element, start - element);
1345 have_leading = TRUE;
1348 single_element = NULL;
1355 g_string_append_len (result, ¤t_separator, 1);
1357 g_string_append_len (result, start, end - start);
1365 g_string_free (result, TRUE);
1366 return g_strdup (single_element);
1371 g_string_append (result, last_trailing);
1373 return g_string_free (result, FALSE);
1380 * @filename: the symbolic link
1381 * @error: return location for a #GError
1383 * Reads the contents of the symbolic link @filename like the POSIX
1384 * readlink() function. The returned string is in the encoding used
1385 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
1387 * Returns: A newly allocated string with the contents of the symbolic link,
1388 * or %NULL if an error occurred.
1393 g_file_read_link (const gchar *filename,
1396 #ifdef HAVE_READLINK
1402 buffer = g_malloc (size);
1406 read_size = readlink (filename, buffer, size);
1407 if (read_size < 0) {
1408 gchar *display_filename = g_filename_display_name (filename);
1412 g_file_error_from_errno (errno),
1413 _("Failed to read the symbolic link '%s': %s"),
1415 g_strerror (errno));
1416 g_free (display_filename);
1421 if (read_size < size)
1423 buffer[read_size] = 0;
1428 buffer = g_realloc (buffer, size);
1434 _("Symbolic links not supported"));