Skip to content

Commit c020741

Browse files
Support dprintf on systems where it isn't natively available
This fixes issue #35 by implementing dprintf using sprintf and write. #35 This is based on work by greengoblinbear in pull request #36: #36
1 parent f9e5ffd commit c020741

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FRONTEND_DEBUG = $(FRONTEND)-debug
33

44
BASE = src/
55

6-
SRC_DIR = . prep parse sema global parse/stmt sema/stmt sema/pass
6+
SRC_DIR = . util prep parse sema global parse/stmt sema/stmt sema/pass
77
SRC_DIR_BASE = $(addprefix $(BASE),$(SRC_DIR))
88

99
GCC_VER_MAJ = $(shell $(CC) -dumpversion | cut -f 1 -d '.')

include/ofc/util/dprintf.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* Copyright 2017 Codethink Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#ifndef __ofc_dprintf_h__
17+
#define __ofc_dprintf_h__
18+
19+
#include <stdio.h>
20+
21+
#if defined(_GNU_SOURCE) || (_XOPEN_SOURCE >= 700) || (_POSIX_C_SOURCE >= 200809L)
22+
#define NATIVE_DPRINTF
23+
#endif
24+
25+
#ifndef NATIVE_DPRINTF
26+
int dprintf(int fd, const char *format, ...)
27+
__attribute__ ((format (printf, 2, 3)));
28+
#endif
29+
30+
#endif

src/colstr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <ctype.h>
2020

2121
#include "ofc/colstr.h"
22+
#include "ofc/util/dprintf.h"
2223

2324

2425
struct ofc_colstr_s

src/util/dprintf.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* Copyright 2017 Codethink Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "ofc/util/dprintf.h"
17+
#include <stdarg.h>
18+
#include <unistd.h>
19+
20+
#ifndef NATIVE_DPRINTF
21+
int dprintf(int fd, const char *format, ...)
22+
{
23+
va_list args;
24+
va_start(args, format);
25+
26+
va_list largs;
27+
va_copy(largs, args);
28+
int len = vsnprintf(NULL, 0, format, largs);
29+
va_end(largs);
30+
31+
if (len <= 0)
32+
{
33+
va_end(args);
34+
return len;
35+
}
36+
37+
char buff[len + 1];
38+
int plen = vsnprintf(buff, (len + 1), format, args);
39+
va_end(args);
40+
41+
if (len != plen)
42+
return -1;
43+
44+
return (int)write(fd, buff, len);
45+
}
46+
#endif

0 commit comments

Comments
 (0)