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>
49 #define S_ISREG(mode) ((mode)&_S_IFREG)
53 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
56 #endif /* G_OS_WIN32 */
71 * @filename: a filename to test
72 * @test: bitfield of #GFileTest flags
74 * Returns %TRUE if any of the tests in the bitfield @test are
75 * %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
76 * G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
77 * the check whether it's a directory doesn't matter since the existence
78 * test is %TRUE. With the current set of available tests, there's no point
79 * passing in more than one test at a time.
81 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
82 * so for a symbolic link to a regular file g_file_test() will return
83 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
85 * Note, that for a dangling symbolic link g_file_test() will return
86 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
88 * You should never use g_file_test() to test whether it is safe
89 * to perform an operaton, because there is always the possibility
90 * of the condition changing before you actually perform the operation.
91 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
92 * to know whether it is is safe to write to a file without being
93 * tricked into writing into a different location. It doesn't work!
95 * <informalexample><programlisting>
96 * /* DON'T DO THIS */
97 * if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) {
98 * fd = g_open (filename, O_WRONLY);
99 * /* write to fd */
101 * </programlisting></informalexample>
103 * Another thing to note is that %G_FILE_TEST_EXISTS and
104 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
105 * system call. This usually doesn't matter, but if your program
106 * is setuid or setgid it means that these tests will give you
107 * the answer for the real user ID and group ID , rather than the
108 * effective user ID and group ID.
110 * Return value: whether a test was %TRUE
113 g_file_test (const gchar *filename,
117 if (G_WIN32_HAVE_WIDECHAR_API ())
119 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
121 if (wfilename == NULL)
124 if ((test & G_FILE_TEST_EXISTS) && (_waccess (wfilename, F_OK) == 0))
130 if (test & (G_FILE_TEST_IS_REGULAR |
132 G_FILE_TEST_IS_EXECUTABLE))
136 if (_wstat (wfilename, &s) == 0)
138 if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
144 if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
150 if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
151 (s.st_mode & _S_IEXEC))
165 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
167 if (cp_filename == NULL)
170 if ((test & G_FILE_TEST_EXISTS) && (access (cp_filename, F_OK) == 0))
172 g_free (cp_filename);
176 if (test & (G_FILE_TEST_IS_REGULAR |
178 G_FILE_TEST_IS_EXECUTABLE))
182 if (stat (cp_filename, &s) == 0)
184 if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
186 g_free (cp_filename);
190 if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
192 g_free (cp_filename);
196 if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
197 (s.st_mode & _S_IEXEC))
199 g_free (cp_filename);
205 g_free (cp_filename);
210 if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
213 if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
218 /* For root, on some POSIX systems, access (filename, X_OK)
219 * will succeed even if no executable bits are set on the
220 * file. We fall through to a stat test to avoid that.
224 test &= ~G_FILE_TEST_IS_EXECUTABLE;
226 if (test & G_FILE_TEST_IS_SYMLINK)
230 if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
234 if (test & (G_FILE_TEST_IS_REGULAR |
236 G_FILE_TEST_IS_EXECUTABLE))
240 if (stat (filename, &s) == 0)
242 if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
245 if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
248 /* The extra test for root when access (file, X_OK) succeeds.
250 if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
251 ((s.st_mode & S_IXOTH) ||
252 (s.st_mode & S_IXUSR) ||
253 (s.st_mode & S_IXGRP)))
266 /* Binary compatibility version. Not for newly compiled code. */
269 g_file_test (const gchar *filename,
272 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
275 if (utf8_filename == NULL)
278 retval = g_file_test_utf8 (utf8_filename, test);
280 g_free (utf8_filename);
288 g_file_error_quark (void)
292 q = g_quark_from_static_string ("g-file-error-quark");
298 * g_file_error_from_errno:
299 * @err_no: an "errno" value
301 * Gets a #GFileError constant based on the passed-in @errno.
302 * For example, if you pass in %EEXIST this function returns
303 * #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
304 * assume that all #GFileError values will exist.
306 * Normally a #GFileError value goes into a #GError returned
307 * from a function that manipulates files. So you would use
308 * g_file_error_from_errno() when constructing a #GError.
310 * Return value: #GFileError corresponding to the given @errno
313 g_file_error_from_errno (gint err_no)
319 return G_FILE_ERROR_EXIST;
325 return G_FILE_ERROR_ISDIR;
331 return G_FILE_ERROR_ACCES;
337 return G_FILE_ERROR_NAMETOOLONG;
343 return G_FILE_ERROR_NOENT;
349 return G_FILE_ERROR_NOTDIR;
355 return G_FILE_ERROR_NXIO;
361 return G_FILE_ERROR_NODEV;
367 return G_FILE_ERROR_ROFS;
373 return G_FILE_ERROR_TXTBSY;
379 return G_FILE_ERROR_FAULT;
385 return G_FILE_ERROR_LOOP;
391 return G_FILE_ERROR_NOSPC;
397 return G_FILE_ERROR_NOMEM;
403 return G_FILE_ERROR_MFILE;
409 return G_FILE_ERROR_NFILE;
415 return G_FILE_ERROR_BADF;
421 return G_FILE_ERROR_INVAL;
427 return G_FILE_ERROR_PIPE;
433 return G_FILE_ERROR_AGAIN;
439 return G_FILE_ERROR_INTR;
445 return G_FILE_ERROR_IO;
451 return G_FILE_ERROR_PERM;
457 return G_FILE_ERROR_NOSYS;
462 return G_FILE_ERROR_FAILED;
468 get_contents_stdio (const gchar *utf8_filename,
478 size_t total_allocated;
480 g_assert (f != NULL);
482 #define STARTING_ALLOC 64
485 total_allocated = STARTING_ALLOC;
486 str = g_malloc (STARTING_ALLOC);
490 bytes = fread (buf, 1, 2048, f);
492 while ((total_bytes + bytes + 1) > total_allocated)
494 total_allocated *= 2;
495 str = g_try_realloc (str, total_allocated);
502 _("Could not allocate %lu bytes to read file \"%s\""),
503 (gulong) total_allocated,
504 utf8_filename ? utf8_filename : "???");
514 g_file_error_from_errno (errno),
515 _("Error reading file '%s': %s"),
516 utf8_filename ? utf8_filename : "???",
522 memcpy (str + total_bytes, buf, bytes);
523 total_bytes += bytes;
528 str[total_bytes] = '\0';
531 *length = total_bytes;
548 get_contents_regfile (const gchar *utf8_filename,
549 struct stat *stat_buf,
560 size = stat_buf->st_size;
562 alloc_size = size + 1;
563 buf = g_try_malloc (alloc_size);
570 _("Could not allocate %lu bytes to read file \"%s\""),
572 utf8_filename ? utf8_filename : "???");
578 while (bytes_read < size)
582 rc = read (fd, buf + bytes_read, size - bytes_read);
591 g_file_error_from_errno (errno),
592 _("Failed to read from file '%s': %s"),
593 utf8_filename ? utf8_filename : "???",
605 buf[bytes_read] = '\0';
608 *length = bytes_read;
624 get_contents_posix (const gchar *filename,
629 struct stat stat_buf;
631 gchar *utf8_filename = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
633 /* O_BINARY useful on Cygwin */
634 fd = open (filename, O_RDONLY|O_BINARY);
640 g_file_error_from_errno (errno),
641 _("Failed to open file '%s': %s"),
642 utf8_filename ? utf8_filename : "???",
644 g_free (utf8_filename);
649 /* I don't think this will ever fail, aside from ENOMEM, but. */
650 if (fstat (fd, &stat_buf) < 0)
655 g_file_error_from_errno (errno),
656 _("Failed to get attributes of file '%s': fstat() failed: %s"),
657 utf8_filename ? utf8_filename : "???",
659 g_free (utf8_filename);
664 if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
666 gboolean retval = get_contents_regfile (utf8_filename,
672 g_free (utf8_filename);
681 f = fdopen (fd, "r");
687 g_file_error_from_errno (errno),
688 _("Failed to open file '%s': fdopen() failed: %s"),
689 utf8_filename ? utf8_filename : "???",
691 g_free (utf8_filename);
696 retval = get_contents_stdio (utf8_filename, f, contents, length, error);
697 g_free (utf8_filename);
703 #else /* G_OS_WIN32 */
706 get_contents_win32 (const gchar *filename,
713 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
715 f = _wfopen (wfilename, L"rb");
720 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
725 g_file_error_from_errno (errno),
726 _("Failed to open file '%s': %s"),
727 utf8_filename ? utf8_filename : "???",
729 g_free (utf8_filename);
734 retval = get_contents_stdio (filename, f, contents, length, error);
742 * g_file_get_contents:
743 * @filename: name of a file to read contents from, in the encoding used for filenames
744 * @contents: location to store an allocated string
745 * @length: location to store length in bytes of the contents
746 * @error: return location for a #GError
748 * Reads an entire file into allocated memory, with good error
749 * checking. If @error is set, %FALSE is returned, and @contents is set
750 * to %NULL. If %TRUE is returned, @error will not be set, and @contents
751 * will be set to the file contents. The string stored in @contents
752 * will be nul-terminated, so for text files you can pass %NULL for the
753 * @length argument. The error domain is #G_FILE_ERROR. Possible
754 * error codes are those in the #GFileError enumeration.
756 * Return value: %TRUE on success, %FALSE if error is set
759 g_file_get_contents (const gchar *filename,
764 g_return_val_if_fail (filename != NULL, FALSE);
765 g_return_val_if_fail (contents != NULL, FALSE);
772 return get_contents_win32 (filename, contents, length, error);
774 return get_contents_posix (filename, contents, length, error);
780 #undef g_file_get_contents
782 /* Binary compatibility version. Not for newly compiled code. */
785 g_file_get_contents (const gchar *filename,
790 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
793 if (utf8_filename == NULL)
796 retval = g_file_get_contents (utf8_filename, contents, length, error);
798 g_free (utf8_filename);
806 * mkstemp() implementation is from the GNU C library.
807 * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
811 * @tmpl: template filename
813 * Opens a temporary file. See the mkstemp() documentation
814 * on most UNIX-like systems. This is a portability wrapper, which simply calls
815 * mkstemp() on systems that have it, and implements
816 * it in GLib otherwise.
818 * The parameter is a string that should match the rules for
819 * mkstemp(), i.e. end in "XXXXXX". The X string will
820 * be modified to form the name of a file that didn't exist.
822 * Return value: A file handle (as from open()) to the file
823 * opened for reading and writing. The file is opened in binary mode
824 * on platforms where there is a difference. The file handle should be
825 * closed with close(). In case of errors, -1 is returned.
828 g_mkstemp (gchar *tmpl)
831 return mkstemp (tmpl);
836 static const char letters[] =
837 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
838 static const int NLETTERS = sizeof (letters) - 1;
841 static int counter = 0;
844 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
847 /* This is where the Xs start. */
848 XXXXXX = &tmpl[len - 6];
850 /* Get some more or less random data. */
851 g_get_current_time (&tv);
852 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
854 for (count = 0; count < 100; value += 7777, ++count)
858 /* Fill in the random bits. */
859 XXXXXX[0] = letters[v % NLETTERS];
861 XXXXXX[1] = letters[v % NLETTERS];
863 XXXXXX[2] = letters[v % NLETTERS];
865 XXXXXX[3] = letters[v % NLETTERS];
867 XXXXXX[4] = letters[v % NLETTERS];
869 XXXXXX[5] = letters[v % NLETTERS];
871 /* tmpl is in UTF-8 on Windows, thus use g_open() */
872 fd = g_open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
876 else if (errno != EEXIST)
877 /* Any other error will apply also to other names we might
878 * try, and there are 2^32 or so of them, so give up now.
883 /* We got out of the loop because we ran out of combinations to try. */
892 /* Binary compatibility version. Not for newly compiled code. */
895 g_mkstemp (gchar *tmpl)
900 static const char letters[] =
901 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
902 static const int NLETTERS = sizeof (letters) - 1;
905 static int counter = 0;
908 if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
911 /* This is where the Xs start. */
912 XXXXXX = &tmpl[len - 6];
914 /* Get some more or less random data. */
915 g_get_current_time (&tv);
916 value = (tv.tv_usec ^ tv.tv_sec) + counter++;
918 for (count = 0; count < 100; value += 7777, ++count)
922 /* Fill in the random bits. */
923 XXXXXX[0] = letters[v % NLETTERS];
925 XXXXXX[1] = letters[v % NLETTERS];
927 XXXXXX[2] = letters[v % NLETTERS];
929 XXXXXX[3] = letters[v % NLETTERS];
931 XXXXXX[4] = letters[v % NLETTERS];
933 XXXXXX[5] = letters[v % NLETTERS];
935 /* This is the backward compatibility system codepage version,
936 * thus use normal open().
938 fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
942 else if (errno != EEXIST)
943 /* Any other error will apply also to other names we might
944 * try, and there are 2^32 or so of them, so give up now.
949 /* We got out of the loop because we ran out of combinations to try. */
957 * @tmpl: Template for file name, as in g_mkstemp(), basename only
958 * @name_used: location to store actual name used
959 * @error: return location for a #GError
961 * Opens a file for writing in the preferred directory for temporary
962 * files (as returned by g_get_tmp_dir()).
964 * @tmpl should be a string ending with six 'X' characters, as the
965 * parameter to g_mkstemp() (or mkstemp()).
966 * However, unlike these functions, the template should only be a
967 * basename, no directory components are allowed. If template is %NULL,
968 * a default template is used.
970 * Note that in contrast to g_mkstemp() (and mkstemp())
971 * @tmpl is not modified, and might thus be a read-only literal string.
973 * The actual name used is returned in @name_used if non-%NULL. This
974 * string should be freed with g_free() when not needed any longer.
976 * Return value: A file handle (as from open()) to
977 * the file opened for reading and writing. The file is opened in binary
978 * mode on platforms where there is a difference. The file handle should be
979 * closed with close(). In case of errors, -1 is returned
980 * and @error will be set.
983 g_file_open_tmp (const gchar *tmpl,
996 if ((slash = strchr (tmpl, G_DIR_SEPARATOR)) != NULL
998 || (strchr (tmpl, '/') != NULL && (slash = "/"))
1008 G_FILE_ERROR_FAILED,
1009 _("Template '%s' invalid, should not contain a '%s'"),
1015 if (strlen (tmpl) < 6 ||
1016 strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
1020 G_FILE_ERROR_FAILED,
1021 _("Template '%s' doesn't end with XXXXXX"),
1026 tmpdir = g_get_tmp_dir ();
1028 if (G_IS_DIR_SEPARATOR (tmpdir [strlen (tmpdir) - 1]))
1031 sep = G_DIR_SEPARATOR_S;
1033 fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
1035 retval = g_mkstemp (fulltemplate);
1041 g_file_error_from_errno (errno),
1042 _("Failed to create file '%s': %s"),
1043 fulltemplate, g_strerror (errno));
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 readlink() function.
1388 * The returned string is in the encoding used for filenames. Use g_filename_to_utf8() to
1389 * 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 *utf8_filename = g_filename_to_utf8 (filename, -1,
1417 g_file_error_from_errno (errno),
1418 _("Failed to read the symbolic link '%s': %s"),
1419 utf8_filename ? utf8_filename : "???",
1420 g_strerror (errno));
1421 g_free (utf8_filename);
1426 if (read_size < size)
1428 buffer[read_size] = 0;
1433 buffer = g_realloc (buffer, size);
1439 _("Symbolic links not supported"));