| 1 | /* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh |
| 2 | * Jagannathan, and Stephen Weeks. |
| 3 | * Copyright (C) 1997-2000 NEC Research Institute. |
| 4 | * |
| 5 | * MLton is released under a BSD-style license. |
| 6 | * See the file MLton-LICENSE for details. |
| 7 | */ |
| 8 | |
| 9 | static inline void *calloc_safe (size_t count, size_t size) { |
| 10 | void *res; |
| 11 | |
| 12 | res = calloc (count, size); |
| 13 | if (NULL == res) |
| 14 | die ("calloc (%"PRIuMAX", %"PRIuMAX") failed.\n", |
| 15 | (uintmax_t)count, (uintmax_t)size); |
| 16 | return res; |
| 17 | } |
| 18 | |
| 19 | static inline void fclose_safe (FILE* f) { |
| 20 | int res; |
| 21 | |
| 22 | res = fclose (f); |
| 23 | if (-1 == res) |
| 24 | diee ("fclose (_) failed.\n"); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | static inline FILE *fdopen_safe (int fd, const char *mode) { |
| 29 | FILE *res; |
| 30 | |
| 31 | res = fdopen (fd, mode); |
| 32 | if (0 == res) |
| 33 | diee ("fopen (%d, %s) failed.\n", fd, mode); |
| 34 | return res; |
| 35 | } |
| 36 | |
| 37 | static inline FILE *fopen_safe (const char *fileName, const char *mode) { |
| 38 | FILE *res; |
| 39 | |
| 40 | res = fopen (fileName, mode); |
| 41 | if (0 == res) |
| 42 | diee ("fopen (%s, %s) failed.\n", fileName, mode); |
| 43 | return res; |
| 44 | } |
| 45 | |
| 46 | static inline void fread_safe (void *buf, size_t size, size_t count, FILE *f) { |
| 47 | size_t res; |
| 48 | |
| 49 | res = fread (buf, size, count, f); |
| 50 | if (res != count) { |
| 51 | diee ("fread ("FMTPTR", %"PRIuMAX", %"PRIuMAX", _) failed " |
| 52 | "(only read %"PRIuMAX"%s).\n", |
| 53 | (uintptr_t)buf, (uintmax_t)size, (uintmax_t)count, (uintmax_t)res, |
| 54 | feof (f) ? "; eof" : ""); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static inline int fseek_safe (FILE *f, long offset, int whence) { |
| 59 | int res; |
| 60 | |
| 61 | res = fseek (f, offset, whence); |
| 62 | if (-1 == res) |
| 63 | diee ("fseek (_, %"PRIuMAX", %"PRIuMAX") failed.\n", |
| 64 | (uintmax_t)offset, (uintmax_t)whence); |
| 65 | return res; |
| 66 | } |
| 67 | |
| 68 | static inline void fwrite_safe (const void *buf, size_t size, size_t count, |
| 69 | FILE *f) { |
| 70 | size_t res; |
| 71 | |
| 72 | res = fwrite (buf, size, count, f); |
| 73 | if (res != count) |
| 74 | diee ("fwrite (_, %"PRIuMAX", %"PRIuMAX", _) failed " |
| 75 | "(only wrote %"PRIuMAX").\n", |
| 76 | (uintmax_t)size, (uintmax_t)count, (uintmax_t)res); |
| 77 | } |
| 78 | |
| 79 | static inline void *malloc_safe (size_t size) { |
| 80 | void *res; |
| 81 | |
| 82 | res = malloc (size); |
| 83 | if (NULL == res) |
| 84 | die ("malloc (%"PRIuMAX") failed.\n", (uintmax_t)size); |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | static inline int mkstemp_safe (char *template) { |
| 89 | int fd; |
| 90 | |
| 91 | fd = mkstemp (template); |
| 92 | if (-1 == fd) |
| 93 | diee ("mkstemp (%s) failed.\n", template); |
| 94 | return fd; |
| 95 | } |
| 96 | |
| 97 | static inline void unlink_safe (const char *pathname) { |
| 98 | int res; |
| 99 | |
| 100 | res = unlink (pathname); |
| 101 | if (-1 == res) |
| 102 | diee ("unlink (%s) failed.\n", pathname); |
| 103 | return; |
| 104 | } |