2 Copyright (C) 2000 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library 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 The GNU C Library 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; 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. */
28 static const char testdata[] = "This is a test";
29 static const char testdata2[] = "And here we go again";
35 const char *tmpdir = getenv ("TMPDIR");
44 if (tmpdir == NULL || *tmpdir == '\0')
46 tmpdirlen = strlen (tmpdir);
48 name = (char *) malloc (tmpdirlen + strlen ("/forkXXXXXX") + 1);
50 error (EXIT_FAILURE, errno, "cannot allocate file name");
52 mempcpy (mempcpy (name, tmpdir, tmpdirlen),
53 "/forkXXXXXX", sizeof ("/forkXXXXXX"));
55 /* Open our test file. */
58 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
60 /* Make sure it gets removed. */
63 /* Write some data. */
64 if (write (fd, testdata, strlen (testdata)) != strlen (testdata))
65 error (EXIT_FAILURE, errno, "cannot write test data");
67 /* Get the position in the stream. */
68 off = lseek (fd, 0, SEEK_CUR);
69 if (off == (off_t) -1 || off != strlen (testdata))
70 error (EXIT_FAILURE, errno, "wrong file position");
72 /* Now fork of the process. */
76 /* This is the child. First get the position of the descriptor. */
77 off = lseek (fd, 0, SEEK_CUR);
78 if (off == (off_t) -1 || off != strlen (testdata))
79 error (EXIT_FAILURE, errno, "wrong file position in child");
81 /* Reset the position. */
82 if (lseek (fd, 0, SEEK_SET) != 0)
83 error (EXIT_FAILURE, errno, "cannot reset position in child");
86 if (read (fd, buf, sizeof buf) != strlen (testdata))
87 error (EXIT_FAILURE, errno, "cannot read data in child");
89 /* Compare the data. */
90 if (memcmp (buf, testdata, strlen (testdata)) != 0)
91 error (EXIT_FAILURE, 0, "data comparison failed in child");
93 /* Reset position again. */
94 if (lseek (fd, 0, SEEK_SET) != 0)
95 error (EXIT_FAILURE, errno, "cannot reset position again in child");
98 if (write (fd, testdata2, strlen (testdata2)) != strlen (testdata2))
99 error (EXIT_FAILURE, errno, "cannot write new data in child");
101 /* Close the file. This must not remove it. */
107 /* Something went wrong. */
108 error (EXIT_FAILURE, errno, "cannot fork");
110 /* Wait for the child. */
111 if (waitpid (pid, &status, 0) != pid)
112 error (EXIT_FAILURE, 0, "Oops, wrong test program terminated");
114 if (WTERMSIG (status) != 0)
115 error (EXIT_FAILURE, 0, "Child terminated incorrectly");
116 status = WEXITSTATUS (status);
120 /* Test whether the child wrote the right data. First test the
121 position. It must be the same as in the child. */
122 if (lseek (fd, 0, SEEK_CUR) != strlen (testdata2))
123 error (EXIT_FAILURE, 0, "file position not changed");
125 if (lseek (fd, 0, SEEK_SET) != 0)
126 error (EXIT_FAILURE, errno, "cannot reset file position");
128 if (read (fd, buf, sizeof buf) != strlen (testdata2))
129 error (EXIT_FAILURE, errno, "cannot read new data");
131 if (memcmp (buf, testdata2, strlen (testdata2)) != 0)
132 error (EXIT_FAILURE, 0, "new data not read correctly");