dos.library: Possibly uninitialized 'res', and divide by zero if blocklen = 0
[AROS.git] / tools / toollib / stdiocb.c
blob01cf1562b135dbdbc57362da631a121d0956c633
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <string.h>
7 #include <errno.h>
8 #include <toollib/error.h>
9 #include <toollib/stdiocb.h>
11 static int
12 StdioGetCB (StdioStream * ss, int dummy, CBD data)
14 if (ss->in)
16 int c = getc (ss->in);
18 if (c == '\n')
19 Str_NextLine (ss);
21 return c;
24 errno = EINVAL;
25 return -1;
28 static int
29 StdioUngetCB (StdioStream * ss, int c, CBD data)
31 if (ss->in)
32 return ungetc (c, ss->in);
34 errno = EINVAL;
35 return -1;
38 static int
39 StdioPutCB (StdioStream * ss, int c, CBD data)
41 if (ss->out)
42 return putc (c, ss->out);
44 errno = EINVAL;
45 return -1;
48 static int
49 StdioPutsCB (StdioStream * ss, const char * str, CBD data)
51 if (ss->out)
52 return fputs (str, ss->out);
54 errno = EINVAL;
55 return -1;
58 StdioStream *
59 StdStr_New (const char * path, const char * mode)
61 StdioStream * ss = new (StdioStream);
62 FILE * fh = NULL;
64 if (strcmp (path, "-"))
66 fh = fopen (path, mode);
68 if (!fh)
70 PushStdError ("Can't open \"%s\" with mode \"%s\"\n", path, mode);
71 return NULL;
75 Str_Init (&ss->stream, path);
77 ss->stream.get = (CB) StdioGetCB;
78 ss->stream.unget = (CB) StdioUngetCB;
79 ss->stream.put = (CB) StdioPutCB;
80 ss->stream.puts = (CB) StdioPutsCB;
82 if (strchr (mode, 'r'))
84 if (strcmp (path, "-"))
86 ss->in = fh;
87 ss->closein = 1;
89 else
91 ss->in = stdin;
92 ss->closein = 0;
95 else
97 ss->in = NULL;
98 ss->closein = 0;
101 if (strchr (mode, 'w') || strchr (mode, 'a'))
103 if (strcmp (path, "-"))
105 ss->out = fh;
106 ss->closeout = 1;
108 else
110 ss->out = stdout;
111 ss->closeout = 0;
114 else
116 ss->out = NULL;
117 ss->closeout = 0;
120 if (ss->in && ss->out)
122 ss->closeout = 0;
125 return ss;
128 void
129 StdStr_Delete (StdioStream * ss)
131 if (ss->closein)
132 fclose (ss->in);
134 if (ss->closeout)
135 fclose (ss->out);
137 Str_Delete (&ss->stream);
139 xfree (ss);