iraf-community / iraf

IRAF - Image Reduction and Analysis Facility
https://iraf-community.github.io
Other
151 stars 51 forks source link

Use fpurge/__fpurge to cancel buffered output #366

Closed olebole closed 8 months ago

olebole commented 8 months ago

This is an alternative to NOIRLAB IRAFs 48b2f614fbaf28219bcc35e291b440fcc11be03f, which defines fcancel() as setvbuf() on macOS: https://github.com/iraf-community/iraf/blob/3761a03d6bb3d91e5da0a9562e778654f59ab2a3/unix/os/zxwhen.c#L90-L96

setvbuf() is not what fcancel() is supposed to do, namely to cancel any buffered output. Instead, it just makes the output unbuffered. i.e. when applying this patch, stdout will become unbuffered for the rest of the session after ^C. Also, setvbuf() is not macOS specific but part of the standard C library (and therefore also available for Linux). And, hard-coding _IONBF instead of using the proper include file (stdio.h) is bad style.

The better solution (although non-portable) is to use fpurge() on macOS (and BSD variants), and __fpurge() on Linux (and glibc variants, like HURD). This is done in this PR. While not really portable, these are the libc functions for this task. Linux is covered with the __GLIBC__ macro here, because it is not a Linux but a glibc feature. HURD is handled like Linux.

@mjfitzpatrick, FYI.