| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | /* GLIB - Library of useful routines for C programming
 | 
					
						
							|  |  |  |  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU Lesser General Public | 
					
						
							|  |  |  |  * License as published by the Free Software Foundation; either | 
					
						
							|  |  |  |  * version 2 of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
					
						
							|  |  |  |  * Lesser General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Lesser General Public | 
					
						
							| 
									
										
										
										
											2014-01-23 12:58:29 +01:00
										 |  |  |  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS | 
					
						
							|  |  |  |  * file for a list of people on the GLib Team.  See the ChangeLog | 
					
						
							|  |  |  |  * files for a list of changes.  These files are distributed with | 
					
						
							|  |  |  |  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef G_DISABLE_ASSERT
 | 
					
						
							|  |  |  | #undef G_LOG_DOMAIN
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef GLIB_COMPILATION
 | 
					
						
							|  |  |  | #undef GLIB_COMPILATION
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <glib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-10 17:12:35 +00:00
										 |  |  | #include <gstdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Replace #ifdef HAVE_UNISTD_H checks with #ifdef G_OS_UNIX
In Windows development environments that have it, <unistd.h> is mostly
just a wrapper around several other native headers (in particular,
<io.h>, which contains read(), close(), etc, and <process.h>, which
contains getpid()). But given that some Windows dev environments don't
have <unistd.h>, everything that uses those functions on Windows
already needed to include the correct Windows header as well, and so
there is never any point to including <unistd.h> on Windows.
Also, remove some <unistd.h> includes (and a few others) that were
unnecessary even on unix.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
											
										 
											2013-10-19 13:04:00 -04:00
										 |  |  | #include <fcntl.h>		/* For open() */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-05 12:58:40 +08:00
										 |  |  | #ifdef G_OS_UNIX
 | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-08-05 22:21:19 +00:00
										 |  |  | #ifdef G_OS_WIN32
 | 
					
						
							|  |  |  | #include <io.h>			/* For read(), write() etc */
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | test_mkstemp (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char template[32]; | 
					
						
							|  |  |  |   int fd; | 
					
						
							|  |  |  |   int i; | 
					
						
							|  |  |  |   const char hello[] = "Hello, World"; | 
					
						
							|  |  |  |   const int hellolen = sizeof (hello) - 1; | 
					
						
							|  |  |  |   char chars[62]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "foobar"); | 
					
						
							|  |  |  |   fd = g_mkstemp (template); | 
					
						
							| 
									
										
										
										
											2004-02-05 00:17:28 +00:00
										 |  |  |   if (fd != -1) | 
					
						
							| 
									
										
										
										
											2013-11-05 12:58:40 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |       g_warning ("g_mkstemp works even if template doesn't contain XXXXXX"); | 
					
						
							|  |  |  |       close (fd); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-06-16 15:12:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "foobarXXX"); | 
					
						
							|  |  |  |   fd = g_mkstemp (template); | 
					
						
							|  |  |  |   if (fd != -1) | 
					
						
							| 
									
										
										
										
											2013-11-05 12:58:40 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |       g_warning ("g_mkstemp works even if template contains less than six X"); | 
					
						
							|  |  |  |       close (fd); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "fooXXXXXX"); | 
					
						
							|  |  |  |   fd = g_mkstemp (template); | 
					
						
							|  |  |  |   g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX"); | 
					
						
							|  |  |  |   i = write (fd, hello, hellolen); | 
					
						
							|  |  |  |   g_assert (i != -1 && "write() failed"); | 
					
						
							|  |  |  |   g_assert (i == hellolen && "write() has written too few bytes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   lseek (fd, 0, 0); | 
					
						
							|  |  |  |   i = read (fd, chars, sizeof (chars)); | 
					
						
							|  |  |  |   g_assert (i != -1 && "read() failed: %s"); | 
					
						
							|  |  |  |   g_assert (i == hellolen && "read() has got wrong number of bytes"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   chars[i] = 0; | 
					
						
							|  |  |  |   g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   close (fd); | 
					
						
							|  |  |  |   remove (template); | 
					
						
							| 
									
										
										
										
											2006-06-16 15:12:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "fooXXXXXX.pdf"); | 
					
						
							|  |  |  |   fd = g_mkstemp (template); | 
					
						
							|  |  |  |   g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX.pdf"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   close (fd); | 
					
						
							|  |  |  |   remove (template); | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-14 14:09:58 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | test_mkdtemp (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char template[32], *retval; | 
					
						
							|  |  |  |   int fd; | 
					
						
							|  |  |  |   int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "foodir"); | 
					
						
							|  |  |  |   retval = g_mkdtemp (template); | 
					
						
							|  |  |  |   if (retval != NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_warning ("g_mkdtemp works even if template doesn't contain XXXXXX"); | 
					
						
							|  |  |  |       g_rmdir (retval); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "foodir"); | 
					
						
							|  |  |  |   retval = g_mkdtemp (template); | 
					
						
							|  |  |  |   if (retval != NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_warning ("g_mkdtemp works even if template contains less than six X"); | 
					
						
							|  |  |  |       g_rmdir (retval); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "fooXXXXXX"); | 
					
						
							|  |  |  |   retval = g_mkdtemp (template); | 
					
						
							|  |  |  |   g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX"); | 
					
						
							|  |  |  |   g_assert (retval == template && "g_mkdtemp allocated the resulting string?"); | 
					
						
							|  |  |  |   g_assert (!g_file_test (template, G_FILE_TEST_IS_REGULAR)); | 
					
						
							|  |  |  |   g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   strcat (template, "/abc"); | 
					
						
							|  |  |  |   fd = g_open (template, O_WRONLY | O_CREAT, 0600); | 
					
						
							|  |  |  |   g_assert (fd != -1 && "couldn't open file in temporary directory"); | 
					
						
							|  |  |  |   close (fd); | 
					
						
							|  |  |  |   g_assert (g_file_test (template, G_FILE_TEST_IS_REGULAR)); | 
					
						
							|  |  |  |   i = g_unlink (template); | 
					
						
							|  |  |  |   g_assert (i != -1 && "couldn't unlink file in temporary directory"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   template[9] = '\0'; | 
					
						
							|  |  |  |   i = g_rmdir (template); | 
					
						
							|  |  |  |   g_assert (i != -1 && "couldn't remove temporary directory"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   strcpy (template, "fooXXXXXX.dir"); | 
					
						
							|  |  |  |   retval = g_mkdtemp (template); | 
					
						
							|  |  |  |   g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX.dir"); | 
					
						
							|  |  |  |   g_assert (g_file_test (template, G_FILE_TEST_IS_DIR)); | 
					
						
							|  |  |  |   g_rmdir (template); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | test_readlink (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef HAVE_SYMLINK
 | 
					
						
							|  |  |  |   FILE *file; | 
					
						
							|  |  |  |   int result; | 
					
						
							|  |  |  |   char *filename = "file-test-data"; | 
					
						
							|  |  |  |   char *link1 = "file-test-link1"; | 
					
						
							|  |  |  |   char *link2 = "file-test-link2"; | 
					
						
							|  |  |  |   char *link3 = "file-test-link3"; | 
					
						
							|  |  |  |   char *data; | 
					
						
							|  |  |  |   GError *error; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   file = fopen (filename, "w"); | 
					
						
							|  |  |  |   g_assert (file != NULL && "fopen() failed"); | 
					
						
							|  |  |  |   fclose (file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   result = symlink (filename, link1); | 
					
						
							|  |  |  |   g_assert (result == 0 && "symlink() failed"); | 
					
						
							|  |  |  |   result = symlink (link1, link2); | 
					
						
							|  |  |  |   g_assert (result == 0 && "symlink() failed"); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   error = NULL; | 
					
						
							| 
									
										
										
										
											2003-07-31 16:56:27 +00:00
										 |  |  |   data = g_file_read_link (link1, &error); | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  |   g_assert (data != NULL && "couldn't read link1"); | 
					
						
							|  |  |  |   g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data"); | 
					
						
							|  |  |  |   g_free (data); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   error = NULL; | 
					
						
							| 
									
										
										
										
											2003-07-31 16:56:27 +00:00
										 |  |  |   data = g_file_read_link (link2, &error); | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  |   g_assert (data != NULL && "couldn't read link2"); | 
					
						
							|  |  |  |   g_assert (strcmp (data, link1) == 0 && "link2 contains wrong data"); | 
					
						
							|  |  |  |   g_free (data); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   error = NULL; | 
					
						
							| 
									
										
										
										
											2003-07-31 16:56:27 +00:00
										 |  |  |   data = g_file_read_link (link3, &error); | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  |   g_assert (data == NULL && "could read link3"); | 
					
						
							| 
									
										
										
										
											2008-09-27 01:43:29 +00:00
										 |  |  |   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); | 
					
						
							| 
									
										
										
										
											2013-11-09 20:29:56 +01:00
										 |  |  |   g_error_free (error); | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   error = NULL; | 
					
						
							| 
									
										
										
										
											2003-07-31 16:56:27 +00:00
										 |  |  |   data = g_file_read_link (filename, &error); | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  |   g_assert (data == NULL && "could read regular file as link"); | 
					
						
							| 
									
										
										
										
											2008-09-27 01:43:29 +00:00
										 |  |  |   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL); | 
					
						
							| 
									
										
										
										
											2013-11-09 20:29:56 +01:00
										 |  |  |   g_error_free (error); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  |   remove (filename); | 
					
						
							|  |  |  |   remove (link1); | 
					
						
							|  |  |  |   remove (link2); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-10 12:56:01 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | test_get_contents (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const gchar *text = "abcdefghijklmnopqrstuvwxyz"; | 
					
						
							|  |  |  |   const gchar *filename = "file-test-get-contents"; | 
					
						
							|  |  |  |   gchar *contents; | 
					
						
							|  |  |  |   gsize len; | 
					
						
							|  |  |  |   FILE *f; | 
					
						
							|  |  |  |   GError *error = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   f = g_fopen (filename, "w"); | 
					
						
							|  |  |  |   fwrite (text, 1, strlen (text), f); | 
					
						
							|  |  |  |   fclose (f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (! g_file_get_contents (filename, &contents, &len, &error)) | 
					
						
							|  |  |  |     g_error ("g_file_get_contents() failed: %s", error->message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_assert (strcmp (text, contents) == 0 && "content mismatch"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_free (contents); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | int  | 
					
						
							|  |  |  | main (int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   test_mkstemp (); | 
					
						
							| 
									
										
										
										
											2011-08-14 14:09:58 -04:00
										 |  |  |   test_mkdtemp (); | 
					
						
							| 
									
										
										
										
											2003-07-28 19:49:47 +00:00
										 |  |  |   test_readlink (); | 
					
						
							| 
									
										
										
										
											2005-03-10 12:56:01 +00:00
										 |  |  |   test_get_contents (); | 
					
						
							| 
									
										
										
										
											2003-07-26 09:24:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } |