| 1 |
/* { dg-additional-options "-D_MODE_T_DECLARED=1" { target newlib } } */ |
| 2 |
/* { dg-additional-options "-D_DEFINED_mode_t" { target *-*-vxworks* } } */ |
| 3 |
#if defined(_AIX) || defined(__hpux) |
| 4 |
#define _MODE_T |
| 5 |
#endif |
| 6 |
#include <stdio.h> |
| 7 |
|
| 8 |
int open(const char *, int mode); |
| 9 |
void close(int fd); |
| 10 |
int write (int fd, void *buf, int nbytes); |
| 11 |
int read (int fd, void *buf, int nbytes); |
| 12 |
|
| 13 |
#define O_ACCMODE 0xf |
| 14 |
#define O_RDONLY 0 |
| 15 |
#define O_WRONLY 1 |
| 16 |
#define O_RDWR 2 |
| 17 |
|
| 18 |
typedef enum { |
| 19 |
S_IRWXU |
| 20 |
// etc |
| 21 |
} mode_t; |
| 22 |
|
| 23 |
int creat (const char *, mode_t mode); |
| 24 |
|
| 25 |
void |
| 26 |
test_1 (const char *path, void *buf) |
| 27 |
{ |
| 28 |
int fd = open (path, O_RDONLY); /* { dg-message "opened here as read-only" } */ |
| 29 |
if (fd >= 0) /* { dg-message "assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */ |
| 30 |
/* { dg-message "following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */ |
| 31 |
{ |
| 32 |
write (fd, buf, 1); /* { dg-warning "'write' on read-only file descriptor 'fd'" "warning" } */ |
| 33 |
/* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */ |
| 34 |
/* { dg-message "\\(5\\) 'write' on read-only file descriptor 'fd'" "event2" { target *-*-* } .-2 } */ |
| 35 |
close (fd); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
void |
| 40 |
test_2 (const char *path, void *buf) |
| 41 |
{ |
| 42 |
int fd = open (path, O_WRONLY); /* { dg-message "opened here as write-only" } */ |
| 43 |
if (fd >= 0) /* { dg-message "assuming 'fd' is a valid file descriptor \\(>= 0\\)" "event1" } */ |
| 44 |
/* { dg-message "following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */ |
| 45 |
{ |
| 46 |
read (fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd'" "warning" } */ |
| 47 |
/* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */ |
| 48 |
/* { dg-message "\\(5\\) 'read' on write-only file descriptor 'fd'" "event2" { target *-*-* } .-2 } */ |
| 49 |
close (fd); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
void |
| 55 |
test_3 (const char *path, void *buf) |
| 56 |
{ |
| 57 |
int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */ |
| 58 |
if (fd >= 0) |
| 59 |
{ |
| 60 |
close(fd); /* {dg-message "\\(2\\) closed here"} */ |
| 61 |
read(fd, buf, 1); /* { dg-warning "'read' on closed file descriptor 'fd'" } */ |
| 62 |
/* {dg-message "\\(3\\) 'read' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" {target *-*-*} .-1 } */ |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
test_4 (const char *path, void *buf) |
| 68 |
{ |
| 69 |
int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */ |
| 70 |
if (fd >= 0) |
| 71 |
{ |
| 72 |
close(fd); /* {dg-message "\\(2\\) closed here"} */ |
| 73 |
write(fd, buf, 1); /* { dg-warning "'write' on closed file descriptor 'fd'" } */ |
| 74 |
/* {dg-message "\\(3\\) 'write' on closed file descriptor 'fd'; 'close' was at \\(2\\)" "" {target *-*-*} .-1 } */ |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
test_5 (const char *path) |
| 80 |
{ |
| 81 |
int fd = open (path, O_RDWR); |
| 82 |
close(fd); |
| 83 |
printf("%d", fd); /* { dg-bogus "'printf' on a closed file descriptor 'fd'" } */ |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
void |
| 88 |
test_6 (const char *path, mode_t mode, void *buf) |
| 89 |
{ |
| 90 |
int fd = creat (path, mode); |
| 91 |
if (fd != -1) |
| 92 |
{ |
| 93 |
read (fd, buf, 1); /* { dg-warning "'read' on write-only file descriptor 'fd'" } */ |
| 94 |
close(fd); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
test_7 (const char *path, mode_t mode, void *buf) |
| 100 |
{ |
| 101 |
int fd = creat (path, mode); |
| 102 |
if (fd != -1) |
| 103 |
{ |
| 104 |
write (fd, buf, 1); |
| 105 |
close(fd); |
| 106 |
} |
| 107 |
} |