File: safe.h

package info (click to toggle)
mlton 20100608-5.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,628 kB
  • ctags: 70,047
  • sloc: ansic: 18,441; lisp: 2,879; makefile: 1,572; sh: 1,326; pascal: 256; asm: 97
file content (104 lines) | stat: -rw-r--r-- 2,529 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
 *    Jagannathan, and Stephen Weeks.
 * Copyright (C) 1997-2000 NEC Research Institute.
 *
 * MLton is released under a BSD-style license.
 * See the file MLton-LICENSE for details.
 */

static inline void *calloc_safe (size_t count, size_t size) {
  void *res;

  res = calloc (count, size);
  if (NULL == res)
    die ("calloc (%"PRIuMAX", %"PRIuMAX") failed.\n",
         (uintmax_t)count, (uintmax_t)size);
  return res;
}

static inline void fclose_safe (FILE* f) {
  int res;

  res = fclose (f);
  if (-1 == res)
    diee ("fclose (_) failed.\n");
  return;
}

static inline FILE *fdopen_safe (int fd, const char *mode) {
  FILE *res;

  res = fdopen (fd, mode);
  if (0 == res)
    diee ("fopen (%d, %s) failed.\n", fd, mode);
  return res;
}

static inline FILE *fopen_safe (const char *fileName, const char *mode) {
  FILE *res;

  res = fopen (fileName, mode);
  if (0 == res)
    diee ("fopen (%s, %s) failed.\n", fileName, mode);
  return res;
}

static inline void fread_safe (void *buf, size_t size, size_t count, FILE *f) {
  size_t res;

  res = fread (buf, size, count, f);
  if (res != count) {
    diee ("fread ("FMTPTR", %"PRIuMAX", %"PRIuMAX", _) failed "
          "(only read %"PRIuMAX"%s).\n",
          (uintptr_t)buf, (uintmax_t)size, (uintmax_t)count, (uintmax_t)res,
          feof (f) ? "; eof" : "");
  }
}

static inline int fseek_safe (FILE *f, long offset, int whence) {
  int res;

  res = fseek (f, offset, whence);
  if (-1 == res)
    diee ("fseek (_, %"PRIuMAX", %"PRIuMAX") failed.\n",
          (uintmax_t)offset, (uintmax_t)whence);
  return res;
}

static inline void fwrite_safe (const void *buf, size_t size, size_t count,
                                FILE *f) {
  size_t res;

  res = fwrite (buf, size, count, f);
  if (res != count)
    diee ("fwrite (_, %"PRIuMAX", %"PRIuMAX", _) failed "
          "(only wrote %"PRIuMAX").\n",
          (uintmax_t)size, (uintmax_t)count, (uintmax_t)res);
}

static inline void *malloc_safe (size_t size) {
  void *res;

  res = malloc (size);
  if (NULL == res)
    die ("malloc (%"PRIuMAX") failed.\n", (uintmax_t)size);
  return res;
}

static inline int mkstemp_safe (char *template) {
  int fd;

  fd = mkstemp (template);
  if (-1 == fd)
    diee ("mkstemp (%s) failed.\n", template);
  return fd;
}

static inline void unlink_safe (const char *pathname) {
  int res;

  res = unlink (pathname);
  if (-1 == res)
    diee ("unlink (%s) failed.\n", pathname);
  return;
}