- if (S_ISCHR(sb.st_mode))
- io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
- else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
- io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
+ if (S_ISREG(sb.st_mode))
+ io->flags |= ISTRUNC;
+ if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
+ if (ioctl(io->fd, FIODTYPE, &type) == -1) {
+ err(1, "%s", io->name);
+ } else {
+#ifdef __APPLE__ /* MacOSX uses enumeration for type not a bitmask */
+ if (type == D_TAPE)
+ io->flags |= ISTAPE;
+ else if (type == D_DISK || type == D_TTY) {
+#else /* !__APPLE__ */
+ if (type & D_TAPE)
+ io->flags |= ISTAPE;
+ else if (type & (D_DISK | D_MEM)) {
+ if (type & D_DISK) {
+ const int one = 1;
+
+ (void)ioctl(io->fd, DIOCWLABEL, &one);
+ }
+#endif /* __APPLE__ */
+ io->flags |= ISSEEK;
+ }
+#ifdef __APPLE__
+ if (S_ISCHR(sb.st_mode) && (type != D_TAPE))
+#else /* !__APPLE__ */
+ if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
+#endif /* __APPLE__ */
+ io->flags |= ISCHR;
+ }
+ return;
+ }
+ errno = 0;
+ if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
+ io->flags |= ISPIPE;
+ else
+ io->flags |= ISSEEK;