COIN-OR::LEMON - Graph Library

source: lemon-project-template-glpk/deps/glpk/src/zlib/zio.c @ 9:33de93886c88

subpack-glpk
Last change on this file since 9:33de93886c88 was 9:33de93886c88, checked in by Alpar Juttner <alpar@…>, 12 years ago

Import GLPK 4.47

File size: 2.3 KB
Line 
1/* zio.c (simulation of non-standard low-level i/o functions) */
2
3/* Written by Andrew Makhorin <mao@gnu.org>, April 2011
4 * For conditions of distribution and use, see copyright notice in
5 * zlib.h */
6
7/* (reserved for copyright notice) */
8
9#include <assert.h>
10#include <stdio.h>
11#include "zio.h"
12
13static FILE *file[FOPEN_MAX];
14static int initialized = 0;
15
16static void initialize(void)
17{     int fd;
18      assert(!initialized);
19      file[0] = stdin;
20      file[1] = stdout;
21      file[2] = stderr;
22      for (fd = 3; fd < FOPEN_MAX; fd++)
23         file[fd] = NULL;
24      initialized = 1;
25      return;
26}
27
28int open(const char *path, int oflag, ...)
29{     FILE *fp;
30      int fd;
31      if (!initialized) initialize();
32      /* see file gzlib.c, function gz_open */
33      if (oflag == O_RDONLY)
34         fp = fopen(path, "rb");
35      else if (oflag == (O_WRONLY | O_CREAT | O_TRUNC))
36         fp = fopen(path, "wb");
37      else if (oflag == (O_WRONLY | O_CREAT | O_APPEND))
38         fp = fopen(path, "ab");
39      else
40         assert(oflag != oflag);
41      if (fp == NULL)
42         return -1;
43      for (fd = 0; fd < FOPEN_MAX; fd++)
44         if (file[fd] == NULL) break;
45      assert(fd < FOPEN_MAX);
46      file[fd] = fp;
47      return fd;
48}
49
50long read(int fd, void *buf, unsigned long nbyte)
51{     unsigned long count;
52      if (!initialized) initialize();
53      assert(0 <= fd && fd < FOPEN_MAX);
54      assert(file[fd] != NULL);
55      count = fread(buf, 1, nbyte, file[fd]);
56      if (ferror(file[fd]))
57         return -1;
58      return count;
59}
60
61long write(int fd, const void *buf, unsigned long nbyte)
62{     unsigned long count;
63      if (!initialized) initialize();
64      assert(0 <= fd && fd < FOPEN_MAX);
65      assert(file[fd] != NULL);
66      count = fwrite(buf, 1, nbyte, file[fd]);
67      if (count != nbyte)
68         return -1;
69      if (fflush(file[fd]) != 0)
70         return -1;
71      return count;
72}
73
74long lseek(int fd, long offset, int whence)
75{     if (!initialized) initialize();
76      assert(0 <= fd && fd < FOPEN_MAX);
77      assert(file[fd] != NULL);
78      if (fseek(file[fd], offset, whence) != 0)
79         return -1;
80      return ftell(file[fd]);
81}
82
83int close(int fd)
84{     if (!initialized) initialize();
85      assert(0 <= fd && fd < FOPEN_MAX);
86      assert(file[fd] != NULL);
87      fclose(file[fd]);
88      file[fd] = NULL;
89      return 0;
90}
91
92/* eof */
Note: See TracBrowser for help on using the repository browser.