1 /* Check that the syscalls implementing fdopen work trivially. */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 void
perr(const char * s)10 perr (const char *s)
11 {
12 perror (s);
13 exit (1);
14 }
15
16 int
main(void)17 main (void)
18 {
19 FILE *f;
20 int fd;
21 const char fname[] = "sk1test.dat";
22 const char tsttxt1[]
23 = "This is the first and only line of this file.\n";
24 char buf[sizeof (tsttxt1)] = "";
25
26 fd = open (fname, O_WRONLY|O_TRUNC|O_CREAT, S_IRWXU);
27 if (fd <= 0)
28 perr ("open-w");
29
30 f = fdopen (fd, "w");
31 if (f == NULL
32 || fwrite (tsttxt1, 1, strlen (tsttxt1), f) != strlen (tsttxt1))
33 perr ("fdopen or fwrite");
34
35 if (fclose (f) != 0)
36 perr ("fclose");
37
38 fd = open (fname, O_RDONLY);
39 if (fd <= 0)
40 perr ("open-r");
41
42 f = fdopen (fd, "r");
43 if (f == NULL
44 || fread (buf, 1, sizeof (buf), f) != strlen (tsttxt1)
45 || strcmp (buf, tsttxt1) != 0
46 || fclose (f) != 0)
47 {
48 printf ("fail\n");
49 exit (1);
50 }
51
52 printf ("pass\n");
53 exit (0);
54 }
55