1 /* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 * Copyright (C) 1997-2000 NEC Research Institute.
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
9 static inline void *calloc_safe (size_t count
, size_t size
) {
12 res
= calloc (count
, size
);
14 die ("calloc (%"PRIuMAX
", %"PRIuMAX
") failed.\n",
15 (uintmax_t)count
, (uintmax_t)size
);
19 static inline void fclose_safe (FILE* f
) {
24 diee ("fclose (_) failed.\n");
28 static inline FILE *fdopen_safe (int fd
, const char *mode
) {
31 res
= fdopen (fd
, mode
);
33 diee ("fopen (%d, %s) failed.\n", fd
, mode
);
37 static inline FILE *fopen_safe (const char *fileName
, const char *mode
) {
40 res
= fopen (fileName
, mode
);
42 diee ("fopen (%s, %s) failed.\n", fileName
, mode
);
46 static inline void fread_safe (void *buf
, size_t size
, size_t count
, FILE *f
) {
49 res
= fread (buf
, size
, count
, f
);
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" : "");
58 static inline int fseek_safe (FILE *f
, long offset
, int whence
) {
61 res
= fseek (f
, offset
, whence
);
63 diee ("fseek (_, %"PRIuMAX
", %"PRIuMAX
") failed.\n",
64 (uintmax_t)offset
, (uintmax_t)whence
);
68 static inline void fwrite_safe (const void *buf
, size_t size
, size_t count
,
72 res
= fwrite (buf
, size
, count
, f
);
74 diee ("fwrite (_, %"PRIuMAX
", %"PRIuMAX
", _) failed "
75 "(only wrote %"PRIuMAX
").\n",
76 (uintmax_t)size
, (uintmax_t)count
, (uintmax_t)res
);
79 static inline void *malloc_safe (size_t size
) {
84 die ("malloc (%"PRIuMAX
") failed.\n", (uintmax_t)size
);
88 static inline int mkstemp_safe (char *template) {
91 fd
= mkstemp (template);
93 diee ("mkstemp (%s) failed.\n", template);
97 static inline void unlink_safe (const char *pathname
) {
100 res
= unlink (pathname
);
102 diee ("unlink (%s) failed.\n", pathname
);