summaryrefslogtreecommitdiffstats
path: root/cpukit/libcsupport/src/printerfprintfputc.c
blob: 8b502fa489f27c67f81379e0c3bfdf0e4dd6a154 (plain) (blame)
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
/*
 * Copyright (c) 2017 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/printer.h>
#include <rtems/bspIo.h>

#include <pthread.h>

static pthread_once_t fprintf_putc_once = PTHREAD_ONCE_INIT;

static FILE fprintf_putc_file;

static _READ_WRITE_RETURN_TYPE fprintf_putc_write(
  struct _reent            *ptr,
  void                     *cookie,
  char const               *buf,
  _READ_WRITE_BUFSIZE_TYPE  n
)
{
  _READ_WRITE_RETURN_TYPE i;
  _READ_WRITE_RETURN_TYPE m;

  m = (_READ_WRITE_RETURN_TYPE) n;
  for (i = 0; i < m; ++i) {
    rtems_putc(buf[i]);
  }

  return m;
}

static void fprintf_putc_init(void)
{
  FILE *f;

  f = &fprintf_putc_file;
  f->_flags = __SWR | __SNBF;
  f->_cookie = f;
  f->_write = fprintf_putc_write;
  __lock_init_recursive(f->_lock);
}

static int fprintf_putc_printer(void *context, const char *fmt, va_list ap)
{
  return vfprintf(context, fmt, ap);
}

void rtems_print_printer_fprintf_putc(rtems_printer *printer)
{
  pthread_once(&fprintf_putc_once, fprintf_putc_init);
  printer->context = &fprintf_putc_file;
  printer->printer = fprintf_putc_printer;
}