diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util-common.c | 25 | ||||
-rw-r--r-- | src/common/util-common.h | 3 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/common/util-common.c b/src/common/util-common.c index 3316eaec..ab74e7c6 100644 --- a/src/common/util-common.c +++ b/src/common/util-common.c @@ -17,6 +17,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <errno.h> #include <stdlib.h> #include <string.h> @@ -102,6 +103,30 @@ int llstat(char *path, struct stat *buf) return ret; } +/** Wrapper around fgets() which properly handles EINTR + * @param s string to read into + * @param size maximum length to read + * @param stream stream to read from + * @return value returned by fgets() + */ +char *safe_fgets(char *s, int size, FILE *stream) +{ + char *ret; + int errno_save = errno, ferror_save = ferror(stream); + while((ret = fgets(s, size, stream)) == NULL && !feof(stream)) { + if(errno == EINTR) { + /* clear any errors we set and try again */ + errno = errno_save; + if(!ferror_save) { + clearerr(stream); + } + } else { + break; + } + } + return ret; +} + #ifndef HAVE_STRNDUP /* A quick and dirty implementation derived from glibc */ /** Determines the length of a fixed-size string. diff --git a/src/common/util-common.h b/src/common/util-common.h index 576702fa..ca8db5ab 100644 --- a/src/common/util-common.h +++ b/src/common/util-common.h @@ -20,6 +20,7 @@ #ifndef _PM_UTIL_COMMON_H #define _PM_UTIL_COMMON_H +#include <stdio.h> #include <sys/stat.h> /* struct stat */ const char *mbasename(const char *path); @@ -27,6 +28,8 @@ char *mdirname(const char *path); int llstat(char *path, struct stat *buf); +char *safe_fgets(char *s, int size, FILE *stream); + #ifndef HAVE_STRNDUP char *strndup(const char *s, size_t n); #endif |