glib/libglib/src/gstdio.c
branchRCL_3
changeset 57 2efc27d87e1c
parent 0 e4d67989cc36
equal deleted inserted replaced
56:acd3cd4aaceb 57:2efc27d87e1c
       
     1 /* gstdio.c - wrappers for C library functions
       
     2  *
       
     3  * Copyright 2004 Tor Lillqvist
       
     4  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     5  *
       
     6  * GLib is free software; you can redistribute it and/or modify it
       
     7  * under the terms of the GNU Lesser General Public License as
       
     8  * published by the Free Software Foundation; either version 2 of the
       
     9  * License, or (at your option) any later version.
       
    10  *
       
    11  * GLib is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with GLib; see the file COPYING.LIB.  If not,
       
    18  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    19  * Boston, MA 02111-1307, USA.
       
    20  */
       
    21 
       
    22 #include "config.h"
       
    23 
       
    24 #define G_STDIO_NO_WRAP_ON_UNIX
       
    25 
       
    26 #include "glib.h"
       
    27 
       
    28 #include <sys/types.h>
       
    29 #include <sys/stat.h>
       
    30 #include <fcntl.h>
       
    31 
       
    32 #ifdef HAVE_UNISTD_H
       
    33 #include <unistd.h>
       
    34 #endif
       
    35 
       
    36 #ifdef G_OS_WIN32
       
    37 #include <windows.h>
       
    38 #include <errno.h>
       
    39 #include <wchar.h>
       
    40 #include <direct.h>
       
    41 #include <io.h>
       
    42 #endif
       
    43 
       
    44 #include "gstdio.h"
       
    45 
       
    46 #include "galias.h"
       
    47 
       
    48 #if !defined (G_OS_UNIX) && !defined (G_OS_WIN32) && !defined (G_OS_BEOS)
       
    49 #error Please port this to your operating system
       
    50 #endif
       
    51 
       
    52 
       
    53 /**
       
    54  * g_access:
       
    55  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
    56  * @mode: as in access()
       
    57  *
       
    58  * A wrapper for the POSIX access() function. This function is used to
       
    59  * test a pathname for one or several of read, write or execute
       
    60  * permissions, or just existence. On Windows, the underlying access()
       
    61  * function in the C library only checks the READONLY attribute, and
       
    62  * does not look at the ACL at all. Software that needs to handle file
       
    63  * permissions on Windows more exactly should use the Win32 API.
       
    64  *
       
    65  * See the C library manual for more details about access().
       
    66  *
       
    67  * Returns: zero if the pathname refers to an existing file system
       
    68  * object that has all the tested permissions, or -1 otherwise or on
       
    69  * error.
       
    70  * 
       
    71  * Since: 2.8
       
    72  */
       
    73 EXPORT_C int
       
    74 g_access (const gchar *filename,
       
    75 	  int          mode)
       
    76 {
       
    77 #ifdef G_OS_WIN32
       
    78   if (G_WIN32_HAVE_WIDECHAR_API ())
       
    79     {
       
    80       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
    81       int retval;
       
    82       int save_errno;
       
    83       
       
    84       if (wfilename == NULL)
       
    85 	{
       
    86 	  errno = EINVAL;
       
    87 	  return -1;
       
    88 	}
       
    89 
       
    90       retval = _waccess (wfilename, mode);
       
    91       save_errno = errno;
       
    92 
       
    93       g_free (wfilename);
       
    94 
       
    95       errno = save_errno;
       
    96       return retval;
       
    97     }
       
    98   else
       
    99     {    
       
   100       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   101       int retval;
       
   102       int save_errno;
       
   103 
       
   104       if (cp_filename == NULL)
       
   105 	{
       
   106 	  errno = EINVAL;
       
   107 	  return -1;
       
   108 	}
       
   109 
       
   110       retval = access (cp_filename, mode);
       
   111       save_errno = errno;
       
   112 
       
   113       g_free (cp_filename);
       
   114 
       
   115       errno = save_errno;
       
   116       return retval;
       
   117     }
       
   118 #else
       
   119   return access (filename, mode);
       
   120 #endif
       
   121 }
       
   122 
       
   123 /**
       
   124  * g_chmod:
       
   125  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   126  * @mode: as in chmod()
       
   127  *
       
   128  * A wrapper for the POSIX chmod() function. The chmod() function is
       
   129  * used to set the permissions of a file system object. Note that on
       
   130  * Windows the file protection mechanism is not at all POSIX-like, and
       
   131  * the underlying chmod() function in the C library just sets or
       
   132  * clears the READONLY attribute. It does not touch any ACL. Software
       
   133  * that needs to manage file permissions on Windows exactly should
       
   134  * use the Win32 API.
       
   135  *
       
   136  * See the C library manual for more details about chmod().
       
   137  *
       
   138  * Returns: zero if the operation succeeded, -1 on error.
       
   139  * 
       
   140  * Since: 2.8
       
   141  */
       
   142 EXPORT_C int
       
   143 g_chmod (const gchar *filename,
       
   144 	 int          mode)
       
   145 {
       
   146 #ifdef G_OS_WIN32
       
   147   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   148     {
       
   149       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   150       int retval;
       
   151       int save_errno;
       
   152       
       
   153       if (wfilename == NULL)
       
   154 	{
       
   155 	  errno = EINVAL;
       
   156 	  return -1;
       
   157 	}
       
   158 
       
   159       retval = _wchmod (wfilename, mode);
       
   160       save_errno = errno;
       
   161 
       
   162       g_free (wfilename);
       
   163 
       
   164       errno = save_errno;
       
   165       return retval;
       
   166     }
       
   167   else
       
   168     {    
       
   169       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   170       int retval;
       
   171       int save_errno;
       
   172 
       
   173       if (cp_filename == NULL)
       
   174 	{
       
   175 	  errno = EINVAL;
       
   176 	  return -1;
       
   177 	}
       
   178 
       
   179       retval = chmod (cp_filename, mode);
       
   180       save_errno = errno;
       
   181 
       
   182       g_free (cp_filename);
       
   183 
       
   184       errno = save_errno;
       
   185       return retval;
       
   186     }
       
   187 #else
       
   188   return chmod (filename, mode);
       
   189 #endif
       
   190 }
       
   191 
       
   192 /**
       
   193  * g_open:
       
   194  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   195  * @flags: as in open()
       
   196  * @mode: as in open()
       
   197  *
       
   198  * A wrapper for the POSIX open() function. The open() function is
       
   199  * used to convert a pathname into a file descriptor. Note that on
       
   200  * POSIX systems file descriptors are implemented by the operating
       
   201  * system. On Windows, it's the C library that implements open() and
       
   202  * file descriptors. The actual Windows API for opening files is
       
   203  * something different.
       
   204  *
       
   205  * See the C library manual for more details about open().
       
   206  *
       
   207  * Returns: a new file descriptor, or -1 if an error occurred. The
       
   208  * return value can be used exactly like the return value from open().
       
   209  * 
       
   210  * Since: 2.6
       
   211  */
       
   212 EXPORT_C int
       
   213 g_open (const gchar *filename,
       
   214 	int          flags,
       
   215 	int          mode)
       
   216 {
       
   217 #ifdef G_OS_WIN32
       
   218   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   219     {
       
   220       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   221       int retval;
       
   222       int save_errno;
       
   223       
       
   224       if (wfilename == NULL)
       
   225 	{
       
   226 	  errno = EINVAL;
       
   227 	  return -1;
       
   228 	}
       
   229 
       
   230       retval = _wopen (wfilename, flags, mode);
       
   231       save_errno = errno;
       
   232 
       
   233       g_free (wfilename);
       
   234 
       
   235       errno = save_errno;
       
   236       return retval;
       
   237     }
       
   238   else
       
   239     {    
       
   240       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   241       int retval;
       
   242       int save_errno;
       
   243 
       
   244       if (cp_filename == NULL)
       
   245 	{
       
   246 	  errno = EINVAL;
       
   247 	  return -1;
       
   248 	}
       
   249 
       
   250       retval = open (cp_filename, flags, mode);
       
   251       save_errno = errno;
       
   252 
       
   253       g_free (cp_filename);
       
   254 
       
   255       errno = save_errno;
       
   256       return retval;
       
   257     }
       
   258 #else
       
   259   return open (filename, flags, mode);
       
   260 #endif
       
   261 }
       
   262 
       
   263 /**
       
   264  * g_creat:
       
   265  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   266  * @mode: as in creat()
       
   267  *
       
   268  * A wrapper for the POSIX creat() function. The creat() function is
       
   269  * used to convert a pathname into a file descriptor, creating a file
       
   270  * if necessar. Note that on POSIX systems file descriptors are
       
   271  * implemented by the operating system. On Windows, it's the C library
       
   272  * that implements creat() and file descriptors. The actual Windows
       
   273  * API for opening files is something different.
       
   274  *
       
   275  * See the C library manual for more details about creat().
       
   276  *
       
   277  * Returns: a new file descriptor, or -1 if an error occurred. The
       
   278  * return value can be used exactly like the return value from creat().
       
   279  * 
       
   280  * Since: 2.8
       
   281  */
       
   282 EXPORT_C int
       
   283 g_creat (const gchar *filename,
       
   284 	 int          mode)
       
   285 {
       
   286 #ifdef G_OS_WIN32
       
   287   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   288     {
       
   289       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   290       int retval;
       
   291       int save_errno;
       
   292       
       
   293       if (wfilename == NULL)
       
   294 	{
       
   295 	  errno = EINVAL;
       
   296 	  return -1;
       
   297 	}
       
   298 
       
   299       retval = _wcreat (wfilename, mode);
       
   300       save_errno = errno;
       
   301 
       
   302       g_free (wfilename);
       
   303 
       
   304       errno = save_errno;
       
   305       return retval;
       
   306     }
       
   307   else
       
   308     {    
       
   309       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   310       int retval;
       
   311       int save_errno;
       
   312 
       
   313       if (cp_filename == NULL)
       
   314 	{
       
   315 	  errno = EINVAL;
       
   316 	  return -1;
       
   317 	}
       
   318 
       
   319       retval = creat (cp_filename, mode);
       
   320       save_errno = errno;
       
   321 
       
   322       g_free (cp_filename);
       
   323 
       
   324       errno = save_errno;
       
   325       return retval;
       
   326     }
       
   327 #else
       
   328   return creat (filename, mode);
       
   329 #endif
       
   330 }
       
   331 
       
   332 /**
       
   333  * g_rename:
       
   334  * @oldfilename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   335  * @newfilename: a pathname in the GLib file name encoding
       
   336  *
       
   337  * A wrapper for the POSIX rename() function. The rename() function 
       
   338  * renames a file, moving it between directories if required.
       
   339  * 
       
   340  * See your C library manual for more details about how rename() works
       
   341  * on your system. Note in particular that on Win9x it is not possible
       
   342  * to rename a file if a file with the new name already exists. Also
       
   343  * it is not possible in general on Windows to rename an open file.
       
   344  *
       
   345  * Returns: 0 if the renaming succeeded, -1 if an error occurred
       
   346  * 
       
   347  * Since: 2.6
       
   348  */
       
   349 EXPORT_C int
       
   350 g_rename (const gchar *oldfilename,
       
   351 	  const gchar *newfilename)
       
   352 {
       
   353 #ifdef G_OS_WIN32
       
   354   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   355     {
       
   356       wchar_t *woldfilename = g_utf8_to_utf16 (oldfilename, -1, NULL, NULL, NULL);
       
   357       wchar_t *wnewfilename;
       
   358       int retval;
       
   359       int save_errno;
       
   360 
       
   361       if (woldfilename == NULL)
       
   362 	{
       
   363 	  errno = EINVAL;
       
   364 	  return -1;
       
   365 	}
       
   366 
       
   367       wnewfilename = g_utf8_to_utf16 (newfilename, -1, NULL, NULL, NULL);
       
   368 
       
   369       if (wnewfilename == NULL)
       
   370 	{
       
   371 	  g_free (woldfilename);
       
   372 	  errno = EINVAL;
       
   373 	  return -1;
       
   374 	}
       
   375 
       
   376       if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING))
       
   377 	retval = 0;
       
   378       else
       
   379 	{
       
   380 	  retval = -1;
       
   381 	  switch (GetLastError ())
       
   382 	    {
       
   383 #define CASE(a,b) case ERROR_##a: save_errno = b; break
       
   384 	    CASE (FILE_NOT_FOUND, ENOENT);
       
   385 	    CASE (PATH_NOT_FOUND, ENOENT);
       
   386 	    CASE (ACCESS_DENIED, EACCES);
       
   387 	    CASE (NOT_SAME_DEVICE, EXDEV);
       
   388 	    CASE (LOCK_VIOLATION, EACCES);
       
   389 	    CASE (SHARING_VIOLATION, EACCES);
       
   390 	    CASE (FILE_EXISTS, EEXIST);
       
   391 	    CASE (ALREADY_EXISTS, EEXIST);
       
   392 #undef CASE
       
   393 	    default: save_errno = EIO;
       
   394 	    }
       
   395 	}
       
   396 
       
   397       g_free (woldfilename);
       
   398       g_free (wnewfilename);
       
   399       
       
   400       errno = save_errno;
       
   401       return retval;
       
   402     }
       
   403   else
       
   404     {
       
   405       gchar *cp_oldfilename = g_locale_from_utf8 (oldfilename, -1, NULL, NULL, NULL);
       
   406       gchar *cp_newfilename;
       
   407       int retval;
       
   408       int save_errno;
       
   409 
       
   410       if (cp_oldfilename == NULL)
       
   411 	{
       
   412 	  errno = EINVAL;
       
   413 	  return -1;
       
   414 	}
       
   415 
       
   416       cp_newfilename = g_locale_from_utf8 (newfilename, -1, NULL, NULL, NULL);
       
   417 
       
   418       if (cp_newfilename == NULL)
       
   419 	{
       
   420 	  g_free (cp_oldfilename);
       
   421 	  errno = EINVAL;
       
   422 	  return -1;
       
   423 	}
       
   424 	
       
   425       retval = rename (cp_oldfilename, cp_newfilename);
       
   426       save_errno = errno;
       
   427 
       
   428       g_free (cp_oldfilename);
       
   429       g_free (cp_newfilename);
       
   430 
       
   431       errno = save_errno;
       
   432       return retval;
       
   433     }
       
   434 #else
       
   435   return rename (oldfilename, newfilename);
       
   436 #endif
       
   437 }
       
   438 
       
   439 /**
       
   440  * g_mkdir: 
       
   441  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   442  * @mode: permissions to use for the newly created directory
       
   443  *
       
   444  * A wrapper for the POSIX mkdir() function. The mkdir() function 
       
   445  * attempts to create a directory with the given name and permissions.
       
   446  * 
       
   447  * See the C library manual for more details about mkdir().
       
   448  *
       
   449  * Returns: 0 if the directory was successfully created, -1 if an error 
       
   450  *    occurred
       
   451  * 
       
   452  * Since: 2.6
       
   453  */
       
   454 EXPORT_C int
       
   455 g_mkdir (const gchar *filename,
       
   456 	 int          mode)
       
   457 {
       
   458 #ifdef G_OS_WIN32
       
   459   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   460     {
       
   461       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   462       int retval;
       
   463       int save_errno;
       
   464 
       
   465       if (wfilename == NULL)
       
   466 	{
       
   467 	  errno = EINVAL;
       
   468 	  return -1;
       
   469 	}
       
   470 
       
   471       retval = _wmkdir (wfilename);
       
   472       save_errno = errno;
       
   473 
       
   474       g_free (wfilename);
       
   475       
       
   476       errno = save_errno;
       
   477       return retval;
       
   478     }
       
   479   else
       
   480     {
       
   481       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   482       int retval;
       
   483       int save_errno;
       
   484 
       
   485       if (cp_filename == NULL)
       
   486 	{
       
   487 	  errno = EINVAL;
       
   488 	  return -1;
       
   489 	}
       
   490 
       
   491       retval = mkdir (cp_filename);
       
   492       save_errno = errno;
       
   493 
       
   494       g_free (cp_filename);
       
   495 
       
   496       errno = save_errno;
       
   497       return retval;
       
   498     }
       
   499 #else
       
   500   return mkdir (filename, mode);
       
   501 #endif
       
   502 }
       
   503 
       
   504 /**
       
   505  * g_chdir: 
       
   506  * @path: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   507  *
       
   508  * A wrapper for the POSIX chdir() function. The function changes the
       
   509  * current directory of the process to @path.
       
   510  * 
       
   511  * See your C library manual for more details about chdir().
       
   512  *
       
   513  * Returns: 0 on success, -1 if an error occurred.
       
   514  * 
       
   515  * Since: 2.8
       
   516  */
       
   517 EXPORT_C int
       
   518 g_chdir (const gchar *path)
       
   519 {
       
   520 #ifdef G_OS_WIN32
       
   521   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   522     {
       
   523       wchar_t *wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
       
   524       int retval;
       
   525       int save_errno;
       
   526 
       
   527       if (wpath == NULL)
       
   528 	{
       
   529 	  errno = EINVAL;
       
   530 	  return -1;
       
   531 	}
       
   532 
       
   533       retval = _wchdir (wpath);
       
   534       save_errno = errno;
       
   535 
       
   536       g_free (wpath);
       
   537       
       
   538       errno = save_errno;
       
   539       return retval;
       
   540     }
       
   541   else
       
   542     {
       
   543       gchar *cp_path = g_locale_from_utf8 (path, -1, NULL, NULL, NULL);
       
   544       int retval;
       
   545       int save_errno;
       
   546 
       
   547       if (cp_path == NULL)
       
   548 	{
       
   549 	  errno = EINVAL;
       
   550 	  return -1;
       
   551 	}
       
   552 
       
   553       retval = chdir (cp_path);
       
   554       save_errno = errno;
       
   555 
       
   556       g_free (cp_path);
       
   557 
       
   558       errno = save_errno;
       
   559       return retval;
       
   560     }
       
   561 #else
       
   562   return chdir (path);
       
   563 #endif
       
   564 }
       
   565 
       
   566 /**
       
   567  * g_stat: 
       
   568  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   569  * @buf: a pointer to a <structname>stat</structname> struct, which
       
   570  *    will be filled with the file information
       
   571  *
       
   572  * A wrapper for the POSIX stat() function. The stat() function 
       
   573  * returns information about a file.
       
   574  * 
       
   575  * See the C library manual for more details about stat().
       
   576  *
       
   577  * Returns: 0 if the information was successfully retrieved, -1 if an error 
       
   578  *    occurred
       
   579  * 
       
   580  * Since: 2.6
       
   581  */
       
   582 EXPORT_C int
       
   583 g_stat (const gchar *filename,
       
   584 	struct stat *buf)
       
   585 {
       
   586 #ifdef G_OS_WIN32
       
   587   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   588     {
       
   589       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   590       int retval;
       
   591       int save_errno;
       
   592       int len;
       
   593 
       
   594       if (wfilename == NULL)
       
   595 	{
       
   596 	  errno = EINVAL;
       
   597 	  return -1;
       
   598 	}
       
   599 
       
   600       len = wcslen (wfilename);
       
   601       while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
       
   602 	len--;
       
   603       if (len > 0 &&
       
   604 	  (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
       
   605 	wfilename[len] = '\0';
       
   606 
       
   607       retval = _wstat (wfilename, (struct _stat *) buf);
       
   608       save_errno = errno;
       
   609 
       
   610       g_free (wfilename);
       
   611 
       
   612       errno = save_errno;
       
   613       return retval;
       
   614     }
       
   615   else
       
   616     {
       
   617       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   618       int retval;
       
   619       int save_errno;
       
   620       int len;
       
   621 
       
   622       if (cp_filename == NULL)
       
   623 	{
       
   624 	  errno = EINVAL;
       
   625 	  return -1;
       
   626 	}
       
   627 
       
   628       len = strlen (cp_filename);
       
   629       while (len > 0 && G_IS_DIR_SEPARATOR (cp_filename[len-1]))
       
   630 	len--;
       
   631       if (len > 0 &&
       
   632 	  (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
       
   633 	cp_filename[len] = '\0';
       
   634       
       
   635       retval = stat (cp_filename, buf);
       
   636       save_errno = errno;
       
   637 
       
   638       g_free (cp_filename);
       
   639 
       
   640       errno = save_errno;
       
   641       return retval;
       
   642     }
       
   643 #else
       
   644   return stat (filename, buf);
       
   645 #endif
       
   646 }
       
   647 
       
   648 /**
       
   649  * g_lstat: 
       
   650  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   651  * @buf: a pointer to a <structname>stat</structname> struct, which
       
   652  *    will be filled with the file information
       
   653  *
       
   654  * A wrapper for the POSIX lstat() function. The lstat() function is
       
   655  * like stat() except that in the case of symbolic links, it returns
       
   656  * information about the symbolic link itself and not the file that it
       
   657  * refers to. If the system does not support symbolic links g_lstat()
       
   658  * is identical to g_stat().
       
   659  * 
       
   660  * See the C library manual for more details about lstat().
       
   661  *
       
   662  * Returns: 0 if the information was successfully retrieved, -1 if an error 
       
   663  *    occurred
       
   664  * 
       
   665  * Since: 2.6
       
   666  */
       
   667 EXPORT_C int
       
   668 g_lstat (const gchar *filename,
       
   669 	 struct stat *buf)
       
   670 {
       
   671 #ifdef HAVE_LSTAT
       
   672   /* This can't be Win32, so don't do the widechar dance. */
       
   673   return lstat (filename, buf);
       
   674 #else
       
   675   return g_stat (filename, buf);
       
   676 #endif
       
   677 }
       
   678 
       
   679 /**
       
   680  * g_unlink:
       
   681  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   682  *
       
   683  * A wrapper for the POSIX unlink() function. The unlink() function 
       
   684  * deletes a name from the filesystem. If this was the last link to the 
       
   685  * file and no processes have it opened, the diskspace occupied by the
       
   686  * file is freed.
       
   687  * 
       
   688  * See your C library manual for more details about unlink(). Note
       
   689  * that on Windows, it is in general not possible to delete files that
       
   690  * are open to some process, or mapped into memory.
       
   691  *
       
   692  * Returns: 0 if the name was successfully deleted, -1 if an error 
       
   693  *    occurred
       
   694  * 
       
   695  * Since: 2.6
       
   696  */
       
   697 EXPORT_C int
       
   698 g_unlink (const gchar *filename)
       
   699 {
       
   700 #ifdef G_OS_WIN32
       
   701   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   702     {
       
   703       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   704       int retval;
       
   705       int save_errno;
       
   706 
       
   707       if (wfilename == NULL)
       
   708 	{
       
   709 	  errno = EINVAL;
       
   710 	  return -1;
       
   711 	}
       
   712 
       
   713       retval = _wunlink (wfilename);
       
   714       save_errno = errno;
       
   715 
       
   716       g_free (wfilename);
       
   717 
       
   718       errno = save_errno;
       
   719       return retval;
       
   720     }
       
   721   else
       
   722     {
       
   723       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   724       int retval;
       
   725       int save_errno;
       
   726 
       
   727       if (cp_filename == NULL)
       
   728 	{
       
   729 	  errno = EINVAL;
       
   730 	  return -1;
       
   731 	}
       
   732 
       
   733       retval = unlink (cp_filename);
       
   734       save_errno = errno;
       
   735 
       
   736       g_free (cp_filename);
       
   737 
       
   738       errno = save_errno;
       
   739       return retval;
       
   740     }
       
   741 #else
       
   742   return unlink (filename);
       
   743 #endif
       
   744 }
       
   745 
       
   746 /**
       
   747  * g_remove:
       
   748  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   749  *
       
   750  * A wrapper for the POSIX remove() function. The remove() function
       
   751  * deletes a name from the filesystem.
       
   752  * 
       
   753  * See your C library manual for more details about how remove() works
       
   754  * on your system. On Unix, remove() removes also directories, as it
       
   755  * calls unlink() for files and rmdir() for directories. On Windows,
       
   756  * although remove() in the C library only works for files, this
       
   757  * function tries first remove() and then if that fails rmdir(), and
       
   758  * thus works for both files and directories. Note however, that on
       
   759  * Windows, it is in general not possible to remove a file that is
       
   760  * open to some process, or mapped into memory.
       
   761  *
       
   762  * If this function fails on Windows you can't infer too much from the
       
   763  * errno value. rmdir() is tried regardless of what caused remove() to
       
   764  * fail. Any errno value set by remove() will be overwritten by that
       
   765  * set by rmdir().
       
   766  *
       
   767  * Returns: 0 if the file was successfully removed, -1 if an error 
       
   768  *    occurred
       
   769  * 
       
   770  * Since: 2.6
       
   771  */
       
   772 EXPORT_C int
       
   773 g_remove (const gchar *filename)
       
   774 {
       
   775 #ifdef G_OS_WIN32
       
   776   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   777     {
       
   778       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   779       int retval;
       
   780       int save_errno;
       
   781 
       
   782       if (wfilename == NULL)
       
   783 	{
       
   784 	  errno = EINVAL;
       
   785 	  return -1;
       
   786 	}
       
   787 
       
   788       retval = _wremove (wfilename);
       
   789       if (retval == -1)
       
   790 	retval = _wrmdir (wfilename);
       
   791       save_errno = errno;
       
   792 
       
   793       g_free (wfilename);
       
   794 
       
   795       errno = save_errno;
       
   796       return retval;
       
   797     }
       
   798   else
       
   799     {
       
   800       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   801       int retval;
       
   802       int save_errno;
       
   803       
       
   804       if (cp_filename == NULL)
       
   805 	{
       
   806 	  errno = EINVAL;
       
   807 	  return -1;
       
   808 	}
       
   809 
       
   810       retval = remove (cp_filename);
       
   811       if (retval == -1)
       
   812 	retval = rmdir (cp_filename);
       
   813       save_errno = errno;
       
   814 
       
   815       g_free (cp_filename);
       
   816 
       
   817       errno = save_errno;
       
   818       return retval;
       
   819     }
       
   820 #else
       
   821   return remove (filename);
       
   822 #endif
       
   823 }
       
   824 
       
   825 /**
       
   826  * g_rmdir:
       
   827  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   828  *
       
   829  * A wrapper for the POSIX rmdir() function. The rmdir() function
       
   830  * deletes a directory from the filesystem.
       
   831  * 
       
   832  * See your C library manual for more details about how rmdir() works
       
   833  * on your system.
       
   834  *
       
   835  * Returns: 0 if the directory was successfully removed, -1 if an error 
       
   836  *    occurred
       
   837  * 
       
   838  * Since: 2.6
       
   839  */
       
   840 EXPORT_C int
       
   841 g_rmdir (const gchar *filename)
       
   842 {
       
   843 #ifdef G_OS_WIN32
       
   844   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   845     {
       
   846       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   847       int retval;
       
   848       int save_errno;
       
   849 
       
   850       if (wfilename == NULL)
       
   851 	{
       
   852 	  errno = EINVAL;
       
   853 	  return -1;
       
   854 	}
       
   855       
       
   856       retval = _wrmdir (wfilename);
       
   857       save_errno = errno;
       
   858 
       
   859       g_free (wfilename);
       
   860 
       
   861       errno = save_errno;
       
   862       return retval;
       
   863     }
       
   864   else
       
   865     {
       
   866       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   867       int retval;
       
   868       int save_errno;
       
   869 
       
   870       if (cp_filename == NULL)
       
   871 	{
       
   872 	  errno = EINVAL;
       
   873 	  return -1;
       
   874 	}
       
   875 
       
   876       retval = rmdir (cp_filename);
       
   877       save_errno = errno;
       
   878 
       
   879       g_free (cp_filename);
       
   880 
       
   881       errno = save_errno;
       
   882       return retval;
       
   883     }
       
   884 #else
       
   885   return rmdir (filename);
       
   886 #endif
       
   887 }
       
   888 
       
   889 /**
       
   890  * g_fopen:
       
   891  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   892  * @mode: a string describing the mode in which the file should be 
       
   893  *   opened
       
   894  *
       
   895  * A wrapper for the POSIX fopen() function. The fopen() function opens
       
   896  * a file and associates a new stream with it. 
       
   897  * 
       
   898  * See the C library manual for more details about fopen().
       
   899  *
       
   900  * Returns: A <type>FILE</type> pointer if the file was successfully
       
   901  *    opened, or %NULL if an error occurred
       
   902  * 
       
   903  * Since: 2.6
       
   904  */
       
   905 EXPORT_C FILE *
       
   906 g_fopen (const gchar *filename,
       
   907 	 const gchar *mode)
       
   908 {
       
   909 #ifdef G_OS_WIN32
       
   910   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   911     {
       
   912       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   913       wchar_t *wmode;
       
   914       FILE *retval;
       
   915       int save_errno;
       
   916 
       
   917       if (wfilename == NULL)
       
   918 	{
       
   919 	  errno = EINVAL;
       
   920 	  return NULL;
       
   921 	}
       
   922 
       
   923       wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
       
   924 
       
   925       if (wmode == NULL)
       
   926 	{
       
   927 	  g_free (wfilename);
       
   928 	  errno = EINVAL;
       
   929 	  return NULL;
       
   930 	}
       
   931 	
       
   932       retval = _wfopen (wfilename, wmode);
       
   933       save_errno = errno;
       
   934 
       
   935       g_free (wfilename);
       
   936       g_free (wmode);
       
   937 
       
   938       errno = save_errno;
       
   939       return retval;
       
   940     }
       
   941   else
       
   942     {
       
   943       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
   944       FILE *retval;
       
   945       int save_errno;
       
   946 
       
   947       if (cp_filename == NULL)
       
   948 	{
       
   949 	  errno = EINVAL;
       
   950 	  return NULL;
       
   951 	}
       
   952 
       
   953       retval = fopen (cp_filename, mode);
       
   954       save_errno = errno;
       
   955 
       
   956       g_free (cp_filename);
       
   957 
       
   958       errno = save_errno;
       
   959       return retval;
       
   960     }
       
   961 #else
       
   962   return fopen (filename, mode);
       
   963 #endif
       
   964 }
       
   965 
       
   966 /**
       
   967  * g_freopen:
       
   968  * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
       
   969  * @mode: a string describing the mode in which the file should be 
       
   970  *   opened
       
   971  * @stream: an existing stream which will be reused, or %NULL
       
   972  *
       
   973  * A wrapper for the POSIX freopen() function. The freopen() function
       
   974  * opens a file and associates it with an existing stream.
       
   975  * 
       
   976  * See the C library manual for more details about freopen().
       
   977  *
       
   978  * Returns: A <type>FILE</type> pointer if the file was successfully
       
   979  *    opened, or %NULL if an error occurred.
       
   980  * 
       
   981  * Since: 2.6
       
   982  */
       
   983 EXPORT_C FILE *
       
   984 g_freopen (const gchar *filename,
       
   985 	   const gchar *mode,
       
   986 	   FILE        *stream)
       
   987 {
       
   988 #ifdef G_OS_WIN32
       
   989   if (G_WIN32_HAVE_WIDECHAR_API ())
       
   990     {
       
   991       wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
       
   992       wchar_t *wmode;
       
   993       FILE *retval;
       
   994       int save_errno;
       
   995 
       
   996       if (wfilename == NULL)
       
   997 	{
       
   998 	  errno = EINVAL;
       
   999 	  return NULL;
       
  1000 	}
       
  1001       
       
  1002       wmode = g_utf8_to_utf16 (mode, -1, NULL, NULL, NULL);
       
  1003 
       
  1004       if (wmode == NULL)
       
  1005 	{
       
  1006 	  g_free (wfilename);
       
  1007 	  errno = EINVAL;
       
  1008 	  return NULL;
       
  1009 	}
       
  1010       
       
  1011       retval = _wfreopen (wfilename, wmode, stream);
       
  1012       save_errno = errno;
       
  1013 
       
  1014       g_free (wfilename);
       
  1015       g_free (wmode);
       
  1016 
       
  1017       errno = save_errno;
       
  1018       return retval;
       
  1019     }
       
  1020   else
       
  1021     {
       
  1022       gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
       
  1023       FILE *retval;
       
  1024       int save_errno;
       
  1025 
       
  1026       if (cp_filename == NULL)
       
  1027 	{
       
  1028 	  errno = EINVAL;
       
  1029 	  return NULL;
       
  1030 	}
       
  1031 
       
  1032       retval = freopen (cp_filename, mode, stream);
       
  1033       save_errno = errno;
       
  1034 
       
  1035       g_free (cp_filename);
       
  1036 
       
  1037       errno = save_errno;
       
  1038       return retval;
       
  1039     }
       
  1040 #else
       
  1041   return freopen (filename, mode, stream);
       
  1042 #endif
       
  1043 }
       
  1044 
       
  1045 #define __G_STDIO_C__
       
  1046 #include "galiasdef.c"