#undef GLIB_COMPILATION
#endif
-#include <stdio.h>
#include <string.h>
#include <glib.h>
+#include <gstdio.h>
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
+#include <fcntl.h> /* For open() */
+
#ifdef G_OS_WIN32
#include <io.h> /* For read(), write() etc */
#endif
strcpy (template, "foobar");
fd = g_mkstemp (template);
if (fd != -1)
- g_warning ("g_mkstemp works even if template doesn't end in XXXXXX");
+ g_warning ("g_mkstemp works even if template doesn't contain XXXXXX");
+ close (fd);
+
+ strcpy (template, "foobarXXX");
+ fd = g_mkstemp (template);
+ if (fd != -1)
+ g_warning ("g_mkstemp works even if template contains less than six X");
close (fd);
strcpy (template, "fooXXXXXX");
close (fd);
remove (template);
+
+ 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);
+}
+
+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);
}
static void
error = NULL;
data = g_file_read_link (link3, &error);
g_assert (data == NULL && "could read link3");
- g_assert (error != NULL && "error not set");
+ g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
error = NULL;
data = g_file_read_link (filename, &error);
g_assert (data == NULL && "could read regular file as link");
- g_assert (error != NULL && "error not set");
+ g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
remove (filename);
remove (link1);
#endif
}
+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);
+}
+
int
main (int argc, char *argv[])
{
test_mkstemp ();
+ test_mkdtemp ();
test_readlink ();
+ test_get_contents ();
return 0;
}