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, or %NULL
733 * @error: return location for a #GError, or %NULL
735 * Reads an entire file into allocated memory, with good error
738 * If the call was successful, it returns %TRUE and sets @contents to the file
739 * contents and @length to the length of the file contents in bytes. The string
740 * stored in @contents will be nul-terminated, so for text files you can pass
741 * %NULL for the @length argument. If the call was not successful, it returns
742 * %FALSE and sets @error. The error domain is #G_FILE_ERROR. Possible error
743 * codes are those in the #GFileError enumeration. In the error case,
744 * @contents is set to %NULL and @length is set to zero.
746 * Return value: %TRUE on success, %FALSE if an error occurred
749 g_file_get_contents (const gchar *filename,
754 g_return_val_if_fail (filename != NULL, FALSE);
755 g_return_val_if_fail (contents != NULL, FALSE);
762 return get_contents_win32 (filename, contents, length, error);
764 return get_contents_posix (filename, contents, length, error);
770 #undef g_file_get_contents
772 /* Binary compatibility version. Not for newly compiled code. */
775 g_file_get_contents (const gchar *filename,
780 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
783 if (utf8_filename == NULL)
786 retval = g_file_get_contents (utf8_filename, contents, length, error);
788 g_free (utf8_filename);
796 * mkstemp() implementation is from the GNU C library.
797 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
801 * @tmpl: template filename
803 * Opens a temporary file. See the mkstemp() documentation
804 * on most UNIX-like systems. This is a portability wrapper, which simply calls
805 * mkstemp() on systems that have it, and implements
806 * it in GLib otherwise.
808 * The parameter is a string that should match the rules for
809 * mkstemp(), i.e. end in "XXXXXX". The X string will
810 * be modified to form the name of a file that didn't exist.
811 * The string should be in the GLib file name encoding. Most importantly,
812 * on Windows it should be in UTF-8.
814 * Return value: A file handle (as from open()) to the file
815 * opened for reading and writing. The file is opened in binary mode
816 * on platforms where there is a difference. The file handle should be
817 * closed with close(). In case of errors, -1 is returned.
820 g_mkstemp (gchar *tmpl)
823 return mkstemp (tmpl);
828 static const char letters[] =
829 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
830 static const int NLETTERS = sizeof (letters) - 1;
833 static int counter = 0;
836 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
839 /* This is where the Xs start. */
840 XXXXXX = &tmpl[len - 6];
842 /* Get some more or less random data. */
843 g_get_current_time (&tv);
844 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
846 for (count = 0; count < 100; value += 7777, ++count)
850 /* Fill in the random bits. */
851 XXXXXX[0] = letters[v % NLETTERS];
853 XXXXXX[1] = letters[v % NLETTERS];
855 XXXXXX[2] = letters[v % NLETTERS];
857 XXXXXX[3] = letters[v % NLETTERS];
859 XXXXXX[4] = letters[v % NLETTERS];
861 XXXXXX[5] = letters[v % NLETTERS];
863 /* tmpl is in UTF-8 on Windows, thus use g_open() */
864 fd = g_open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
868 else if (errno != EEXIST)
869 /* Any other error will apply also to other names we might
870 * try, and there are 2^32 or so of them, so give up now.
875 /* We got out of the loop because we ran out of combinations to try. */
884 /* Binary compatibility version. Not for newly compiled code. */
887 g_mkstemp (gchar *tmpl)
892 static const char letters[] =
893 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
894 static const int NLETTERS = sizeof (letters) - 1;
897 static int counter = 0;
900 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
903 /* This is where the Xs start. */
904 XXXXXX = &tmpl[len - 6];
906 /* Get some more or less random data. */
907 g_get_current_time (&tv);
908 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
910 for (count = 0; count < 100; value += 7777, ++count)
914 /* Fill in the random bits. */
915 XXXXXX[0] = letters[v % NLETTERS];
917 XXXXXX[1] = letters[v % NLETTERS];
919 XXXXXX[2] = letters[v % NLETTERS];
921 XXXXXX[3] = letters[v % NLETTERS];
923 XXXXXX[4] = letters[v % NLETTERS];
925 XXXXXX[5] = letters[v % NLETTERS];
927 /* This is the backward compatibility system codepage version,
928 * thus use normal open().
930 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
934 else if (errno != EEXIST)
935 /* Any other error will apply also to other names we might
936 * try, and there are 2^32 or so of them, so give up now.
941 /* We got out of the loop because we ran out of combinations to try. */
949 * @tmpl: Template for file name, as in g_mkstemp(), basename only
950 * @name_used: location to store actual name used
951 * @error: return location for a #GError
953 * Opens a file for writing in the preferred directory for temporary
954 * files (as returned by g_get_tmp_dir()).
956 * @tmpl should be a string in the GLib file name encoding ending with
957 * six 'X' characters, as the parameter to g_mkstemp() (or mkstemp()).
958 * However, unlike these functions, the template should only be a
959 * basename, no directory components are allowed. If template is
960 * %NULL, a default template is used.
962 * Note that in contrast to g_mkstemp() (and mkstemp())
963 * @tmpl is not modified, and might thus be a read-only literal string.
965 * The actual name used is returned in @name_used if non-%NULL. This
966 * string should be freed with g_free() when not needed any longer.
967 * The returned name is in the GLib file name encoding.
969 * Return value: A file handle (as from open()) to
970 * the file opened for reading and writing. The file is opened in binary
971 * mode on platforms where there is a difference. The file handle should be
972 * closed with close(). In case of errors, -1 is returned
973 * and @error will be set.
976 g_file_open_tmp (const gchar *tmpl,
989 if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
991 || (strchr (tmpl, '/') != NULL && (slash = "/"))
995 gchar *display_tmpl = g_filename_display_name (tmpl);
1002 G_FILE_ERROR_FAILED,
1003 _("Template '%s' invalid, should not contain a '%s'"),
1005 g_free (display_tmpl);
1010 if (strlen (tmpl) < 6 ||
1011 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
1013 gchar *display_tmpl = g_filename_display_name (tmpl);
1016 G_FILE_ERROR_FAILED,
1017 _("Template '%s' doesn't end with XXXXXX"),
1019 g_free (display_tmpl);
1023 tmpdir = g_get_tmp_dir ();
1025 if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1028 sep = G_DIR_SEPARATOR_S;
1030 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1032 retval = g_mkstemp (fulltemplate);
1036 gchar *display_fulltemplate = g_filename_display_name (fulltemplate);
1039 g_file_error_from_errno (errno),
1040 _("Failed to create file '%s': %s"),
1041 display_fulltemplate, g_strerror (errno));
1042 g_free (display_fulltemplate);
1043 g_free (fulltemplate);
1048 *name_used = fulltemplate;
1050 g_free (fulltemplate);
1057 #undef g_file_open_tmp
1059 /* Binary compatibility version. Not for newly compiled code. */
1062 g_file_open_tmp (const gchar *tmpl,
1066 gchar *utf8_tmpl = g_locale_to_utf8 (tmpl, -1, NULL, NULL, error);
1067 gchar *utf8_name_used;
1070 if (utf8_tmpl == NULL)
1073 retval = g_file_open_tmp_utf8 (utf8_tmpl, &utf8_name_used, error);
1079 *name_used = g_locale_from_utf8 (utf8_name_used, -1, NULL, NULL, NULL);
1081 g_free (utf8_name_used);
1089 g_build_pathv (const gchar *separator,
1090 const gchar *first_element,
1094 gint separator_len = strlen (separator);
1095 gboolean is_first = TRUE;
1096 gboolean have_leading = FALSE;
1097 const gchar *single_element = NULL;
1098 const gchar *next_element;
1099 const gchar *last_trailing = NULL;
1101 result = g_string_new (NULL);
1103 next_element = first_element;
1107 const gchar *element;
1113 element = next_element;
1114 next_element = va_arg (args, gchar *);
1119 /* Ignore empty elements */
1128 strncmp (start, separator, separator_len) == 0)
1129 start += separator_len;
1132 end = start + strlen (start);
1136 while (end >= start + separator_len &&
1137 strncmp (end - separator_len, separator, separator_len) == 0)
1138 end -= separator_len;
1140 last_trailing = end;
1141 while (last_trailing >= element + separator_len &&
1142 strncmp (last_trailing - separator_len, separator, separator_len) == 0)
1143 last_trailing -= separator_len;
1147 /* If the leading and trailing separator strings are in the
1148 * same element and overlap, the result is exactly that element
1150 if (last_trailing <= start)
1151 single_element = element;
1153 g_string_append_len (result, element, start - element);
1154 have_leading = TRUE;
1157 single_element = NULL;
1164 g_string_append (result, separator);
1166 g_string_append_len (result, start, end - start);
1172 g_string_free (result, TRUE);
1173 return g_strdup (single_element);
1178 g_string_append (result, last_trailing);
1180 return g_string_free (result, FALSE);
1186 * @separator: a string used to separator the elements of the path.
1187 * @first_element: the first element in the path
1188 * @Varargs: remaining elements in path, terminated by %NULL
1190 * Creates a path from a series of elements using @separator as the
1191 * separator between elements. At the boundary between two elements,
1192 * any trailing occurrences of separator in the first element, or
1193 * leading occurrences of separator in the second element are removed
1194 * and exactly one copy of the separator is inserted.
1196 * Empty elements are ignored.
1198 * The number of leading copies of the separator on the result is
1199 * the same as the number of leading copies of the separator on
1200 * the first non-empty element.
1202 * The number of trailing copies of the separator on the result is
1203 * the same as the number of trailing copies of the separator on
1204 * the last non-empty element. (Determination of the number of
1205 * trailing copies is done without stripping leading copies, so
1206 * if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
1207 * has 1 trailing copy.)
1209 * However, if there is only a single non-empty element, and there
1210 * are no characters in that element not part of the leading or
1211 * trailing separators, then the result is exactly the original value
1214 * Other than for determination of the number of leading and trailing
1215 * copies of the separator, elements consisting only of copies
1216 * of the separator are ignored.
1218 * Return value: a newly-allocated string that must be freed with g_free().
1221 g_build_path (const gchar *separator,
1222 const gchar *first_element,
1228 g_return_val_if_fail (separator != NULL, NULL);
1230 va_start (args, first_element);
1231 str = g_build_pathv (separator, first_element, args);
1239 * @first_element: the first element in the path
1240 * @Varargs: remaining elements in path, terminated by %NULL
1242 * Creates a filename from a series of elements using the correct
1243 * separator for filenames.
1245 * On Unix, this function behaves identically to <literal>g_build_path
1246 * (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
1248 * On Windows, it takes into account that either the backslash
1249 * (<literal>\</literal> or slash (<literal>/</literal>) can be used
1250 * as separator in filenames, but otherwise behaves as on Unix. When
1251 * file pathname separators need to be inserted, the one that last
1252 * previously occurred in the parameters (reading from left to right)
1255 * No attempt is made to force the resulting filename to be an absolute
1256 * path. If the first element is a relative path, the result will
1257 * be a relative path.
1259 * Return value: a newly-allocated string that must be freed with g_free().
1262 g_build_filename (const gchar *first_element,
1269 va_start (args, first_element);
1270 str = g_build_pathv (G_DIR_SEPARATOR_S, first_element, args);
1275 /* Code copied from g_build_pathv(), and modifed to use two
1276 * alternative single-character separators.
1280 gboolean is_first = TRUE;
1281 gboolean have_leading = FALSE;
1282 const gchar *single_element = NULL;
1283 const gchar *next_element;
1284 const gchar *last_trailing = NULL;
1285 gchar current_separator = '\\';
1287 va_start (args, first_element);
1289 result = g_string_new (NULL);
1291 next_element = first_element;
1295 const gchar *element;
1301 element = next_element;
1302 next_element = va_arg (args, gchar *);
1307 /* Ignore empty elements */
1316 (*start == '\\' || *start == '/'))
1318 current_separator = *start;
1323 end = start + strlen (start);
1327 while (end >= start + 1 &&
1328 (end[-1] == '\\' || end[-1] == '/'))
1330 current_separator = end[-1];
1334 last_trailing = end;
1335 while (last_trailing >= element + 1 &&
1336 (last_trailing[-1] == '\\' || last_trailing[-1] == '/'))
1341 /* If the leading and trailing separator strings are in the
1342 * same element and overlap, the result is exactly that element
1344 if (last_trailing <= start)
1345 single_element = element;
1347 g_string_append_len (result, element, start - element);
1348 have_leading = TRUE;
1351 single_element = NULL;
1358 g_string_append_len (result, ¤t_separator, 1);
1360 g_string_append_len (result, start, end - start);
1368 g_string_free (result, TRUE);
1369 return g_strdup (single_element);
1374 g_string_append (result, last_trailing);
1376 return g_string_free (result, FALSE);
1383 * @filename: the symbolic link
1384 * @error: return location for a #GError
1386 * Reads the contents of the symbolic link @filename like the POSIX
1387 * readlink() function. The returned string is in the encoding used
1388 * for filenames. Use g_filename_to_utf8() to convert it to UTF-8.
1390 * Returns: A newly allocated string with the contents of the symbolic link,
1391 * or %NULL if an error occurred.
1396 g_file_read_link (const gchar *filename,
1399 #ifdef HAVE_READLINK
1405 buffer = g_malloc (size);
1409 read_size = readlink (filename, buffer, size);
1410 if (read_size < 0) {
1411 gchar *display_filename = g_filename_display_name (filename);
1415 g_file_error_from_errno (errno),
1416 _("Failed to read the symbolic link '%s': %s"),
1418 g_strerror (errno));
1419 g_free (display_filename);
1424 if (read_size < size)
1426 buffer[read_size] = 0;
1431 buffer = g_realloc (buffer, size);
1437 _("Symbolic links not supported"));