1 /*
2 Copyright (C) 2016-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4svbuf.ctr
12 */
13 
14 /**	@file dk4svbuf.c The dk4svbuf module.
15 */
16 
17 
18 
19 #include "dk4conf.h"
20 #include <libdk4c/dk4svbuf.h>
21 
22 #if	DK4_HAVE_ERRNO_H
23 #ifndef	ERRNO_H_INCLUDED
24 #include <errno.h>
25 #define	ERRNO_H_INCLUDED	1
26 #endif
27 #endif
28 
29 #if DK4_HAVE_ASSERT_H
30 #ifndef	ASSERT_H_INCLUDED
31 #include <assert.h>
32 #define	ASSERT_H_INCLUDED 1
33 #endif
34 #endif
35 
36 
37 
38 
39 
40 /**	Buffering type constants.
41 */
42 static int buffering_types[] = {
43 	_IONBF, _IOLBF, _IOFBF
44 };
45 
46 
47 int
dk4setvbuf(FILE * fipo,dk4_file_buffering_t tp,char * pbuf,size_t szbuf,dk4_er_t * erp)48 dk4setvbuf(
49 	FILE					*fipo,
50 	dk4_file_buffering_t	 tp,
51 	char					*pbuf,
52 	size_t					 szbuf,
53 	dk4_er_t				*erp
54 )
55 {
56 	int		 back	= 0;
57 #if	DK4_HAVE_SETVBUF
58 	int		 svbres	= 0;
59 #endif
60 	int		 btp;
61 
62 #if	DK4_USE_ASSERT
63   assert(NULL != fipo);
64 #endif
65 	if (NULL != fipo) {
66 		btp = (
67 			((0 <= (int)tp) && (3 > (int)tp))
68 			? (buffering_types[(int)tp])
69 			: (buffering_types[2])
70 		);
71 #if DK4_HAVE_SETVBUF
72 		errno = 0;
73 		svbres = setvbuf(
74 			fipo,
75 			((0 < szbuf) ? (pbuf) : (NULL)),
76 			(((NULL != pbuf) && (0 < szbuf)) ? (btp) : (buffering_types[0])),
77 			szbuf
78 		);
79 		if (0 == svbres) {
80 			back = 1;
81 		}
82 		else {
83 			dk4error_set_idetails(erp, DK4_E_SYSTEM, errno);
84 		}
85 #else
86 #if	DK4_HAVE_SETBUFFER
87 		setbuffer(fipo, pbuf, szbuf);
88 		back = 1;
89 #else
90 		back = 1;
91 		dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
92 #endif
93 #endif
94 	}
95 	else {
96 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
97 	}
98 	return back;
99 }
100 
101 /* vim: set ai sw=4 ts=4 : */
102 
103