mirror of
https://github.com/drasko/codezero.git
synced 2026-03-15 00:31:50 +01:00
Created libc under containers/posix which now all tasks use to build.
There is a problem in the new libc that test0 now misbehaves. Going to be fixed.
This commit is contained in:
31
containers/posix/libc/SConscript
Normal file
31
containers/posix/libc/SConscript
Normal file
@@ -0,0 +1,31 @@
|
||||
# -*- mode: python; coding: utf-8; -*-
|
||||
|
||||
# Codezero -- a microkernel for embedded systems.
|
||||
#
|
||||
# Copyright © 2009 B Labs Ltd
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU
|
||||
# General Public License as published by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
# License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with this program. If not, see
|
||||
# <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# Author: Russel Winder
|
||||
|
||||
Import('environment', 'posixServicesDirectory')
|
||||
|
||||
e = environment.Clone()
|
||||
e.Append(CPPPATH = ['include', 'include/arch'])
|
||||
|
||||
e['CCFLAGS'] = ['-g', '-nostdlib', '-Wall', '-ffreestanding', '-std=gnu99']
|
||||
|
||||
objects = e.StaticObject(Glob('src/*.c') + Glob('src/arch-' + environment['ARCH'] + '/*.[cS]'))
|
||||
Depends(objects, e['configFiles'])
|
||||
library = e.StaticLibrary('c', objects)
|
||||
|
||||
Return('library')
|
||||
1
containers/posix/libc/include/arch
Symbolic link
1
containers/posix/libc/include/arch
Symbolic link
@@ -0,0 +1 @@
|
||||
arch-arm
|
||||
407
containers/posix/libc/include/arch-arm/pl011_uart.h
Normal file
407
containers/posix/libc/include/arch-arm/pl011_uart.h
Normal file
@@ -0,0 +1,407 @@
|
||||
|
||||
#ifndef __PL011__UART__H__
|
||||
#define __PL011__UART__H__
|
||||
|
||||
/*
|
||||
* PL011 UART Generic driver implementation.
|
||||
* Copyright Bahadir Balban (C) 2006
|
||||
*
|
||||
* The particular intention of this code is that it has been carefully
|
||||
* written as decoupled from os-specific code and in a verbose way such
|
||||
* that it clearly demonstrates how the device operates, reducing the
|
||||
* amount of time to be spent for understanding the operational model
|
||||
* and implementing a driver from scratch. This is the very first to be
|
||||
* such a driver so far, hopefully it will turn out to be useful.
|
||||
*/
|
||||
|
||||
/* Default base address for this chip */
|
||||
#define PL011_USR_BASE 0x500000
|
||||
#define PL011_BASE PL011_USR_BASE
|
||||
|
||||
/* Architecture specific memory access macros */
|
||||
#define read(val, address) val = *((volatile unsigned int *) address)
|
||||
#define write(val, address) *((volatile unsigned int *) address) = val
|
||||
|
||||
/* Register offsets */
|
||||
#define PL011_UARTDR (PL011_BASE + 0x00)
|
||||
#define PL011_UARTRSR (PL011_BASE + 0x04)
|
||||
#define PL011_UARTECR (PL011_BASE + 0x04)
|
||||
#define PL011_UARTFR (PL011_BASE + 0x18)
|
||||
#define PL011_UARTILPR (PL011_BASE + 0x20)
|
||||
#define PL011_UARTIBRD (PL011_BASE + 0x24)
|
||||
#define PL011_UARTFBRD (PL011_BASE + 0x28)
|
||||
#define PL011_UARTLCR_H (PL011_BASE + 0x2C)
|
||||
#define PL011_UARTCR (PL011_BASE + 0x30)
|
||||
#define PL011_UARTIFLS (PL011_BASE + 0x34)
|
||||
#define PL011_UARTIMSC (PL011_BASE + 0x38)
|
||||
#define PL011_UARTRIS (PL011_BASE + 0x3C)
|
||||
#define PL011_UARTMIS (PL011_BASE + 0x40)
|
||||
#define PL011_UARTICR (PL011_BASE + 0x44)
|
||||
#define PL011_UARTDMACR (PL011_BASE + 0x48)
|
||||
|
||||
/* IRQ bits for each uart irq event */
|
||||
#define PL011_RXIRQ (1 << 4)
|
||||
#define PL011_TXIRQ (1 << 5)
|
||||
#define PL011_RXTIMEOUTIRQ (1 << 6)
|
||||
#define PL011_FEIRQ (1 << 7)
|
||||
#define PL011_PEIRQ (1 << 8)
|
||||
#define PL011_BEIRQ (1 << 9)
|
||||
#define PL011_OEIRQ (1 << 10)
|
||||
|
||||
struct pl011_uart;
|
||||
|
||||
int pl011_initialise(struct pl011_uart *);
|
||||
int pl011_tx_char(char);
|
||||
int pl011_rx_char(char *);
|
||||
|
||||
void pl011_set_baudrate(unsigned int, unsigned int);
|
||||
void pl011_set_irq_mask(unsigned int);
|
||||
void pl011_clr_irq_mask(unsigned int);
|
||||
|
||||
void pl011_irq_handler(struct pl011_uart *);
|
||||
void pl011_tx_irq_handler(struct pl011_uart *, unsigned int);
|
||||
void pl011_rx_irq_handler(struct pl011_uart *, unsigned int);
|
||||
void pl011_error_irq_handler(struct pl011_uart *, unsigned int);
|
||||
|
||||
static inline void pl011_uart_enable(void);
|
||||
static inline void pl011_uart_disable(void);
|
||||
static inline void pl011_tx_enable(void);
|
||||
static inline void pl011_tx_disable(void);
|
||||
static inline void pl011_rx_enable(void);
|
||||
static inline void pl011_rx_disable(void);
|
||||
static inline void pl011_irq_clear(unsigned int flags);
|
||||
static inline unsigned int pl011_read_irqstat(void);
|
||||
static inline unsigned int pl011_read_irqmask(void);
|
||||
static inline void pl011_rx_dma_disable(void);
|
||||
static inline void pl011_rx_dma_enable(void);
|
||||
static inline void pl011_tx_dma_enable(void);
|
||||
static inline void pl011_tx_dma_disable(void);
|
||||
static inline void pl011_set_irq_fifolevel(unsigned int xfer,
|
||||
unsigned int level);
|
||||
static inline void pl011_set_word_width(int size);
|
||||
static inline void pl011_disable_fifos(void);
|
||||
static inline void pl011_set_parity_even(void);
|
||||
static inline void pl011_parity_enable(void);
|
||||
static inline void pl011_set_stopbits(int stopbits);
|
||||
|
||||
static inline void pl011_set_parity_odd(void);
|
||||
static inline void pl011_enable_fifos(void);
|
||||
static inline void pl011_parity_disable(void);
|
||||
|
||||
struct pl011_uart_ops {
|
||||
int (*initialise)(struct pl011_uart *);
|
||||
|
||||
int (*tx_char)(char);
|
||||
int (*rx_char)(char *);
|
||||
|
||||
void (*set_baudrate)(unsigned int, unsigned int);
|
||||
void (*set_irq_mask)(unsigned int);
|
||||
void (*clr_irq_mask)(unsigned int);
|
||||
|
||||
void (*irq_handler)(struct pl011_uart *);
|
||||
void (*tx_irq_handler)(struct pl011_uart *, unsigned int);
|
||||
void (*rx_irq_handler)(struct pl011_uart *, unsigned int);
|
||||
void (*error_irq_handler)(struct pl011_uart *, unsigned int);
|
||||
};
|
||||
|
||||
struct pl011_uart {
|
||||
unsigned int base;
|
||||
struct pl011_uart_ops ops;
|
||||
unsigned int frame_errors;
|
||||
unsigned int parity_errors;
|
||||
unsigned int break_errors;
|
||||
unsigned int overrun_errors;
|
||||
unsigned int rx_timeout_errors;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define PL011_UARTEN (1 << 0)
|
||||
static inline void pl011_uart_enable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTCR);
|
||||
val |= PL011_UARTEN;
|
||||
write(val, PL011_UARTCR);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_uart_disable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTCR);
|
||||
val &= ~PL011_UARTEN;
|
||||
write(val, PL011_UARTCR);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#define PL011_TXE (1 << 8)
|
||||
static inline void pl011_tx_enable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTCR);
|
||||
val |= PL011_TXE;
|
||||
write(val, PL011_UARTCR);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_tx_disable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTCR);
|
||||
val &= ~PL011_TXE;
|
||||
write(val, PL011_UARTCR);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#define PL011_RXE (1 << 9)
|
||||
static inline void pl011_rx_enable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTCR);
|
||||
val |= PL011_RXE;
|
||||
write(val, PL011_UARTCR);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_rx_disable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTCR);
|
||||
val &= ~PL011_RXE;
|
||||
write(val, PL011_UARTCR);
|
||||
return;
|
||||
}
|
||||
|
||||
#define PL011_TWO_STOPBITS_SELECT (1 << 3)
|
||||
static inline void pl011_set_stopbits(int stopbits)
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
|
||||
if(stopbits == 2) { /* Set to two bits */
|
||||
val |= PL011_TWO_STOPBITS_SELECT;
|
||||
} else { /* Default is 1 */
|
||||
val &= ~PL011_TWO_STOPBITS_SELECT;
|
||||
}
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
#define PL011_PARITY_ENABLE (1 << 1)
|
||||
static inline void pl011_parity_enable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val |= PL011_PARITY_ENABLE;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_parity_disable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val &= ~PL011_PARITY_ENABLE;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
#define PL011_PARITY_EVEN (1 << 2)
|
||||
static inline void pl011_set_parity_even()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val |= PL011_PARITY_EVEN;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_set_parity_odd()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val &= ~PL011_PARITY_EVEN;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
#define PL011_ENABLE_FIFOS (1 << 4)
|
||||
static inline void pl011_enable_fifos()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val |= PL011_ENABLE_FIFOS;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_disable_fifos()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val &= ~PL011_ENABLE_FIFOS;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#define PL011_WORD_WIDTH_SHIFT (5)
|
||||
/* Sets the transfer word width for the data register. */
|
||||
static inline void pl011_set_word_width(int size)
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
if(size < 5 || size > 8) /* Default is 8 */
|
||||
size = 8;
|
||||
|
||||
/* Clear size field */
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val &= ~(0x3 << PL011_WORD_WIDTH_SHIFT);
|
||||
write(val, PL011_UARTLCR_H);
|
||||
|
||||
/* The formula is to write 5 less of size given:
|
||||
* 11 = 8 bits
|
||||
* 10 = 7 bits
|
||||
* 01 = 6 bits
|
||||
* 00 = 5 bits
|
||||
*/
|
||||
read(val, PL011_UARTLCR_H);
|
||||
val |= (size - 5) << PL011_WORD_WIDTH_SHIFT;
|
||||
write(val, PL011_UARTLCR_H);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Defines at which level of fifo fullness an irq will be generated.
|
||||
* @xfer: tx fifo = 0, rx fifo = 1
|
||||
* @level: Generate irq if:
|
||||
* 0 rxfifo >= 1/8 full txfifo <= 1/8 full
|
||||
* 1 rxfifo >= 1/4 full txfifo <= 1/4 full
|
||||
* 2 rxfifo >= 1/2 full txfifo <= 1/2 full
|
||||
* 3 rxfifo >= 3/4 full txfifo <= 3/4 full
|
||||
* 4 rxfifo >= 7/8 full txfifo <= 7/8 full
|
||||
* 5-7 reserved reserved
|
||||
*/
|
||||
static inline void pl011_set_irq_fifolevel(unsigned int xfer, unsigned int level)
|
||||
{
|
||||
if(xfer != 1 && xfer != 0) /* Invalid fifo */
|
||||
return;
|
||||
|
||||
if(level > 4) /* Invalid level */
|
||||
return;
|
||||
|
||||
write(level << (xfer * 3), PL011_UARTIFLS);
|
||||
return;
|
||||
}
|
||||
|
||||
/* returns which irqs are masked */
|
||||
static inline unsigned int pl011_read_irqmask(void)
|
||||
{
|
||||
unsigned int flags;
|
||||
read(flags, PL011_UARTIMSC);
|
||||
return flags;
|
||||
}
|
||||
|
||||
/* returns masked irq status */
|
||||
static inline unsigned int pl011_read_irqstat(void)
|
||||
{
|
||||
unsigned int irqstatus;
|
||||
read(irqstatus, PL011_UARTMIS);
|
||||
return irqstatus;
|
||||
}
|
||||
/* Clears the given asserted irqs */
|
||||
static inline void pl011_irq_clear(unsigned int flags)
|
||||
{
|
||||
if(flags > 0x3FF) { /* Invalid irq clearing bitvector */
|
||||
return;
|
||||
}
|
||||
/* Simply write the flags since it's a write-only register */
|
||||
write(flags, PL011_UARTICR);
|
||||
return;
|
||||
}
|
||||
|
||||
#define PL011_TXDMAEN (1 << 1)
|
||||
#define PL011_RXDMAEN (1 << 0)
|
||||
|
||||
/* Enables dma transfers for uart. The dma controller
|
||||
* must be initialised, set-up and enabled separately.
|
||||
*/
|
||||
static inline void pl011_tx_dma_enable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTDMACR);
|
||||
val |= PL011_TXDMAEN;
|
||||
write(val, PL011_UARTDMACR);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Disables dma transfers for uart */
|
||||
static inline void pl011_tx_dma_disable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTDMACR);
|
||||
val &= ~PL011_TXDMAEN;
|
||||
write(val, PL011_UARTDMACR);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_rx_dma_enable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTDMACR);
|
||||
val |= PL011_RXDMAEN;
|
||||
write(val, PL011_UARTDMACR);
|
||||
return;
|
||||
}
|
||||
|
||||
static inline void pl011_rx_dma_disable()
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTDMACR);
|
||||
val &= ~PL011_RXDMAEN;
|
||||
write(val, PL011_UARTDMACR);
|
||||
return;
|
||||
}
|
||||
|
||||
#endif /* __PL011__UART__ */
|
||||
|
||||
92
containers/posix/libc/include/arch-arm/stdint.h
Normal file
92
containers/posix/libc/include/arch-arm/stdint.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 National ICT Australia
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
|
||||
* National ICT Australia
|
||||
* http://www.ertos.nicta.com.au
|
||||
*
|
||||
* Permission is granted by National ICT Australia, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of National ICT Australia, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on National ICT
|
||||
* Australia or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of National ICT Australia or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
/*
|
||||
Author: Ben Leslie
|
||||
*/
|
||||
typedef signed char int8_t;
|
||||
typedef short int16_t;
|
||||
typedef int int32_t;
|
||||
typedef long long int64_t;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
|
||||
#define __PTR_SIZE 32
|
||||
7
containers/posix/libc/include/arch-arm/uart.h
Normal file
7
containers/posix/libc/include/arch-arm/uart.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef __LIBC_UART_H__
|
||||
#define __LIBC_UART_H__
|
||||
|
||||
void uart_init(void);
|
||||
void uart_putc(char c);
|
||||
|
||||
#endif /* __LIBC_UART_H__ */
|
||||
9
containers/posix/libc/include/printf.h
Normal file
9
containers/posix/libc/include/printf.h
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
#ifndef __PRINTF_H__
|
||||
#define __PRINTF_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
int printf(char *format, ...) __attribute__((format (printf, 1, 2)));
|
||||
|
||||
#endif /* __PRINTF_H__ */
|
||||
98
containers/posix/libc/include/stdbool.h
Normal file
98
containers/posix/libc/include/stdbool.h
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 National ICT Australia
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
|
||||
* National ICT Australia
|
||||
* http://www.ertos.nicta.com.au
|
||||
*
|
||||
* Permission is granted by National ICT Australia, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of National ICT Australia, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on National ICT
|
||||
* Australia or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of National ICT Australia or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
|
||||
/*
|
||||
Author: Ben Leslie
|
||||
Description:
|
||||
Implementation based on C99 Section 7.16 Boolean type and values
|
||||
*/
|
||||
|
||||
#ifndef __STDBOOL_H__
|
||||
#define __STDBOOL_H__
|
||||
|
||||
#ifndef __bool_true_false_are_defined
|
||||
|
||||
#define __bool_true_false_are_defined 1
|
||||
#define bool _Bool
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
#endif /* __bool_true_false_are_defined */
|
||||
|
||||
#endif /* __STDBOOL_H__ */
|
||||
276
containers/posix/libc/include/stdint.h
Normal file
276
containers/posix/libc/include/stdint.h
Normal file
@@ -0,0 +1,276 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 National ICT Australia
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
|
||||
* National ICT Australia
|
||||
* http://www.ertos.nicta.com.au
|
||||
*
|
||||
* Permission is granted by National ICT Australia, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of National ICT Australia, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on National ICT
|
||||
* Australia or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of National ICT Australia or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
#ifndef _STDINT_H_
|
||||
#define _STDINT_H_
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
/*
|
||||
* 7.18.1.1 Exact-width integers
|
||||
*/
|
||||
#include <arch/stdint.h>
|
||||
|
||||
/*
|
||||
* 7.18.2.1 Limits of exact-wdith integer types
|
||||
*/
|
||||
#define INT8_MIN SCHAR_MIN
|
||||
#define INT16_MIN SHRT_MIN
|
||||
#define INT32_MIN INT_MIN
|
||||
#define INT64_MIN LLONG_MIN
|
||||
|
||||
#define INT8_MAX SCHAR_MAX
|
||||
#define INT16_MAX SHRT_MAX
|
||||
#define INT32_MAX INT_MAX
|
||||
#define INT64_MAX LLONG_MAX
|
||||
|
||||
#define UINT8_MAX UCHAR_MAX
|
||||
#define UINT16_MAX USHRT_MAX
|
||||
#define UINT32_MAX UINT_MAX
|
||||
#define UINT64_MAX ULLONG_MAX
|
||||
|
||||
|
||||
#ifndef __ARCH_HAS_LEAST
|
||||
/*
|
||||
* 7.18.1.2 Minimum-width integers
|
||||
*/
|
||||
typedef int8_t int_least8_t;
|
||||
typedef int16_t int_least16_t;
|
||||
typedef int32_t int_least32_t;
|
||||
typedef int64_t int_least64_t;
|
||||
|
||||
typedef uint8_t uint_least8_t;
|
||||
typedef uint16_t uint_least16_t;
|
||||
typedef uint32_t uint_least32_t;
|
||||
typedef uint64_t uint_least64_t;
|
||||
|
||||
/*
|
||||
* 7.18.2.2 Limits of minimum-width integers
|
||||
*/
|
||||
#define INT_LEAST8_MIN INT8_MIN
|
||||
#define INT_LEAST16_MIN INT16_MIN
|
||||
#define INT_LEAST32_MIN INT32_MIN
|
||||
#define INT_LEAST64_MIN INT64_MIN
|
||||
|
||||
#define INT_LEAST8_MAX INT8_MAX
|
||||
#define INT_LEAST16_MAX INT16_MAX
|
||||
#define INT_LEAST32_MAX INT32_MAX
|
||||
#define INT_LEAST64_MAX INT64_MAX
|
||||
|
||||
#define UINT_LEAST8_MAX UINT8_MAX
|
||||
#define UINT_LEAST16_MAX UINT16_MAX
|
||||
#define UINT_LEAST32_MAX UINT32_MAX
|
||||
#define UINT_LEAST64_MAX UINT64_MAX
|
||||
#else
|
||||
#undef __ARCH_HAS_LEAST
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef __ARCH_HAS_FAST
|
||||
/*
|
||||
* 7.8.1.3 Fastest minimum-width integer types
|
||||
* Note -- We fulfil the spec, however we don't really know
|
||||
* which are fastest here. I assume `int' is probably fastest
|
||||
* more most, and should be used for [u]int_fast[8,16,32]_t.
|
||||
*/
|
||||
|
||||
typedef int8_t int_fast8_t;
|
||||
typedef int16_t int_fast16_t;
|
||||
typedef int32_t int_fast32_t;
|
||||
typedef int64_t int_fast64_t;
|
||||
|
||||
typedef uint8_t uint_fast8_t;
|
||||
typedef uint16_t uint_fast16_t;
|
||||
typedef uint32_t uint_fast32_t;
|
||||
typedef uint64_t uint_fast64_t;
|
||||
|
||||
/*
|
||||
* 7.18.2.2 Limits of fastest minimum-width integers
|
||||
*/
|
||||
#define INT_FAST8_MIN INT8_MIN
|
||||
#define INT_FAST16_MIN INT16_MIN
|
||||
#define INT_FAST32_MIN INT32_MIN
|
||||
#define INT_FAST64_MIN INT64_MIN
|
||||
|
||||
#define INT_FAST8_MAX INT8_MAX
|
||||
#define INT_FAST16_MAX INT16_MAX
|
||||
#define INT_FAST32_MAX INT32_MAX
|
||||
#define INT_FAST64_MAX INT64_MAX
|
||||
|
||||
#define UINT_FAST8_MAX UINT8_MAX
|
||||
#define UINT_FAST16_MAX UINT16_MAX
|
||||
#define UINT_FAST32_MAX UINT32_MAX
|
||||
#define UINT_FAST64_MAX UINT64_MAX
|
||||
#else
|
||||
#undef __ARCH_HAS_FAST
|
||||
#endif
|
||||
|
||||
/*
|
||||
* 7.18.1.4 Integer types capable of holding object pointers
|
||||
* We should fix this to be 32/64 clean.
|
||||
*/
|
||||
#if __PTR_SIZE==32
|
||||
typedef int32_t intptr_t;
|
||||
typedef uint32_t uintptr_t;
|
||||
|
||||
#define INTPTR_MIN INT32_MIN
|
||||
#define INTPTR_MAX INT32_MAX
|
||||
#define UINTPTR_MAX UINT32_MAX
|
||||
|
||||
#elif __PTR_SIZE==64
|
||||
typedef int64_t intptr_t;
|
||||
typedef uint64_t uintptr_t;
|
||||
|
||||
#define INTPTR_MIN INT64_MIN
|
||||
#define INTPTR_MAX INT64_MAX
|
||||
#define UINTPTR_MAX UINT64_MAX
|
||||
#else
|
||||
#error Unknown pointer size
|
||||
#endif
|
||||
|
||||
#undef __PTR_SIZE
|
||||
|
||||
/*
|
||||
* 7.18.1.5 Greatest-wdith integer types
|
||||
*/
|
||||
typedef long long int intmax_t;
|
||||
typedef unsigned long long int uintmax_t;
|
||||
|
||||
/*
|
||||
* 7.18.2.5 Limits of greateast-width integer types
|
||||
*/
|
||||
#define INTMAX_MIN LLONG_MIN
|
||||
#define INTMAX_MAX LLONG_MAX
|
||||
#define UINTMAX_MAX ULLONG_MAX
|
||||
|
||||
/*
|
||||
* 7.18.3 Limits of other integer types
|
||||
*/
|
||||
/* FIXME: Check these limits are correct */
|
||||
#define PTRDIFF_MIN INTPTR_MIN
|
||||
#define PTRDIFF_MAX INTPTR_MAX
|
||||
|
||||
#define SIG_ATOMIC_MIN INT_MIN
|
||||
#define SIG_ATOMIC_MAX INT_MAX
|
||||
|
||||
#define SIZE_MAX UINTPTR_MAX
|
||||
|
||||
#define WCHAR_MIN 0
|
||||
#define WCHAR_MAX UINT16_MAX
|
||||
|
||||
#define WINT_MIN 0
|
||||
#define WINT_MAX UINT16_MAX
|
||||
|
||||
/*
|
||||
* 7.18.4 Macros for integer constants
|
||||
*/
|
||||
|
||||
#define INT8_C(x) (int8_t)(x)
|
||||
#define INT16_C(x) (int16_t)(x)
|
||||
#define INT32_C(x) (int32_t)(x)
|
||||
#define INT64_C(x) (int64_t)(x)
|
||||
#define UINT8_C(x) (uint8_t)(x)
|
||||
#define UINT16_C(x) (uint16_t)(x)
|
||||
#define UINT32_C(x) (uint32_t)(x)
|
||||
#define UINT64_C(x) (uint64_t)(x)
|
||||
|
||||
#define INT_FAST8_C(x) (int_fast8_t)(x)
|
||||
#define INT_FAST16_C(x) (int_fast16_t)(x)
|
||||
#define INT_FAST32_C(x) (int_fast32_t)(x)
|
||||
#define INT_FAST64_C(x) (int_fast64_t)(x)
|
||||
#define UINT_FAST8_C(x) (uint_fast8_t)(x)
|
||||
#define UINT_FAST16_C(x) (uint_fast16_t)(x)
|
||||
#define UINT_FAST32_C(x) (uint_fast32_t)(x)
|
||||
#define UINT_FAST64_C(x) (uint_fast64_t)(x)
|
||||
|
||||
#define INT_LEAST8_C(x) (int_least8_t)(x)
|
||||
#define INT_LEAST16_C(x) (int_least16_t)(x)
|
||||
#define INT_LEAST32_C(x) (int_least32_t)(x)
|
||||
#define INT_LEAST64_C(x) (int_least64_t)(x)
|
||||
#define UINT_LEAST8_C(x) (uint_least8_t)(x)
|
||||
#define UINT_LEAST16_C(x) (uint_least16_t)(x)
|
||||
#define UINT_LEAST32_C(x) (uint_least32_t)(x)
|
||||
#define UINT_LEAST64_C(x) (uint_least64_t)(x)
|
||||
|
||||
#define INTPTR_C(x) (intptr_t)(x)
|
||||
#define UINTPTR_C(x) (uintptr_t)(x)
|
||||
|
||||
#define INTMAX_C(x) (intmax_t)(x)
|
||||
#define UINTMAX_C(x) (uintmax_t)(x)
|
||||
|
||||
#endif /* _STDINT_H_ */
|
||||
215
containers/posix/libc/include/stdio.h
Normal file
215
containers/posix/libc/include/stdio.h
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 National ICT Australia
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
|
||||
* National ICT Australia
|
||||
* http://www.ertos.nicta.com.au
|
||||
*
|
||||
* Permission is granted by National ICT Australia, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of National ICT Australia, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on National ICT
|
||||
* Australia or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of National ICT Australia or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
|
||||
/*
|
||||
Author: Ben Leslie
|
||||
*/
|
||||
|
||||
#ifndef _STDIO_H_
|
||||
#define _STDIO_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef THREAD_SAFE
|
||||
#include <mutex/mutex.h>
|
||||
#define lock_stream(s) mutex_count_lock(&(s)->mutex)
|
||||
#define unlock_stream(s) mutex_count_unlock(&(s)->mutex)
|
||||
#else
|
||||
#define lock_stream(s)
|
||||
#define unlock_stream(s)
|
||||
#endif
|
||||
|
||||
#define __UNGET_SIZE 10
|
||||
|
||||
struct __file {
|
||||
void *handle;
|
||||
|
||||
size_t (*read_fn)(void *, long int, size_t, void *);
|
||||
size_t (*write_fn)(void *, long int, size_t, void *);
|
||||
int (*close_fn)(void *);
|
||||
long int (*eof_fn)(void *);
|
||||
|
||||
unsigned char buffering_mode;
|
||||
char *buffer;
|
||||
|
||||
unsigned char unget_pos;
|
||||
long int current_pos;
|
||||
|
||||
#ifdef THREAD_SAFE
|
||||
struct mutex mutex;
|
||||
#endif
|
||||
|
||||
int eof;
|
||||
int error;
|
||||
|
||||
char unget_stack[__UNGET_SIZE];
|
||||
};
|
||||
|
||||
typedef struct __file FILE; /* This needs to be done correctly */
|
||||
typedef long fpos_t; /* same */
|
||||
|
||||
#define _IOFBF 0
|
||||
#define _IOLBF 1
|
||||
#define _IONBF 2
|
||||
|
||||
#define BUFSIZ 37
|
||||
#define EOF (-1)
|
||||
|
||||
#define FOPEN_MAX 37
|
||||
#define FILENAME_MAX 37
|
||||
#define L_tmpnam 37
|
||||
|
||||
//#define SEEK_CUR 0
|
||||
//#define SEEK_END 1
|
||||
//#define SEEK_SET 2
|
||||
|
||||
#define TMP_MAX 37
|
||||
|
||||
extern FILE *stderr;
|
||||
extern FILE *stdin;
|
||||
extern FILE *stdout;
|
||||
|
||||
/* 7.19.4 Operations on files */
|
||||
int remove(const char *);
|
||||
int rename(const char *, const char *);
|
||||
FILE *tmpfile(void);
|
||||
char *tmpnam(char *);
|
||||
int fclose(FILE *);
|
||||
int fflush(FILE *);
|
||||
FILE *fopen(const char *, const char *);
|
||||
FILE *freopen(const char *, const char *, FILE *);
|
||||
void setbuf(FILE *, char *);
|
||||
int setvbuf(FILE *, char *, int, size_t);
|
||||
|
||||
/* 7.19.6 Format i/o functions */
|
||||
int fprintf(FILE *, const char *, ...);
|
||||
int fscanf(FILE *, const char *, ...);
|
||||
//int printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
|
||||
int scanf(const char *, ...);
|
||||
int snprintf(char *, size_t , const char *, ...);
|
||||
int sprintf(char *, const char *, ...);
|
||||
int sscanf(const char *, const char *, ...);
|
||||
int vfprintf(FILE *, const char *, va_list);
|
||||
int vfscanf(FILE *, const char *, va_list);
|
||||
int vprintf(const char *, va_list);
|
||||
int vscanf(const char *, va_list);
|
||||
int vsnprintf(char *, size_t, const char *, va_list );
|
||||
int vsprintf(char *, const char *format, va_list arg);
|
||||
int vsscanf(const char *s, const char *format, va_list arg);
|
||||
|
||||
/* 7.19.7 Character i/o functions */
|
||||
int fgetc(FILE *);
|
||||
char *fgets(char *, int, FILE *);
|
||||
//int fputc(int, FILE *);
|
||||
int fputs(const char *, FILE *);
|
||||
|
||||
/* getc is specified to be the same as fgetc, so it makes
|
||||
the most sense to implement as a macro here */
|
||||
#define getc fgetc /*int getc(FILE *); */
|
||||
|
||||
int getchar(void);
|
||||
char *gets(char *);
|
||||
|
||||
/* putc is specified to be the same as fputc, so it makes
|
||||
the most sense to implement as a macro here */
|
||||
//#define putc fputc /*int fetc(int, FILE *); */
|
||||
|
||||
int putchar(int);
|
||||
int puts(const char *);
|
||||
int ungetc(int, FILE *);
|
||||
|
||||
/* 7.19.8 Direct I/O functions */
|
||||
size_t fread(void *, size_t, size_t, FILE *);
|
||||
size_t fwrite(const void *, size_t, size_t, FILE *);
|
||||
|
||||
/* 7.19.9 File positioning functions */
|
||||
int fgetpos(FILE *, fpos_t *);
|
||||
int fseek(FILE *, long int, int);
|
||||
int fsetpos(FILE *, const fpos_t *);
|
||||
long int ftell(FILE *);
|
||||
void rewind(FILE *);
|
||||
|
||||
/* 7.19.10 Error-handling functions */
|
||||
void clearerr(FILE *);
|
||||
int feof(FILE *);
|
||||
int ferror(FILE *);
|
||||
void perror(const char *);
|
||||
|
||||
#endif /* _STDIO_H_ */
|
||||
11
containers/posix/libc/include/string.h
Normal file
11
containers/posix/libc/include/string.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef __LIB_STRING_H__
|
||||
#define __LIB_STRING_H__
|
||||
|
||||
int strlen(const char *s);
|
||||
char *strcpy(char *to, const char *from);
|
||||
char *strncpy(char *dest, const char *src, int count);
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
void *memset(void *p, int c, int size);
|
||||
void *memcpy(void *d, void *s, int size);
|
||||
|
||||
#endif /* __LIB_STRING_H__ */
|
||||
1
containers/posix/libc/src/arch
Symbolic link
1
containers/posix/libc/src/arch
Symbolic link
@@ -0,0 +1 @@
|
||||
arch-arm
|
||||
12
containers/posix/libc/src/arch-arm/eabi.c
Normal file
12
containers/posix/libc/src/arch-arm/eabi.c
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
/* Dummies to keep Codesourcery 4.1.1 libgcc division exceptions silent. */
|
||||
void raise(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void __aeabi_unwind_cpp_pr0(void)
|
||||
{
|
||||
}
|
||||
|
||||
191
containers/posix/libc/src/arch-arm/uart.c
Normal file
191
containers/posix/libc/src/arch-arm/uart.c
Normal file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Ties up platform's uart driver functions with generic API
|
||||
*
|
||||
* Copyright (C) 2007 Bahadir Balban
|
||||
*/
|
||||
|
||||
#include <arch/uart.h>
|
||||
#include <arch/pl011_uart.h>
|
||||
|
||||
/* UART-specific internal error codes */
|
||||
#define PL011_ERROR 1
|
||||
#define PL011_EAGAIN 2
|
||||
|
||||
/* Error status bits in receive status register */
|
||||
#define PL011_FE (1 << 0)
|
||||
#define PL011_PE (1 << 1)
|
||||
#define PL011_BE (1 << 2)
|
||||
#define PL011_OE (1 << 3)
|
||||
|
||||
/* Status bits in flag register */
|
||||
#define PL011_TXFE (1 << 7)
|
||||
#define PL011_RXFF (1 << 6)
|
||||
#define PL011_TXFF (1 << 5)
|
||||
#define PL011_RXFE (1 << 4)
|
||||
#define PL011_BUSY (1 << 3)
|
||||
#define PL011_DCD (1 << 2)
|
||||
#define PL011_DSR (1 << 1)
|
||||
#define PL011_CTS (1 << 0)
|
||||
|
||||
struct pl011_uart uart;
|
||||
|
||||
int pl011_tx_char(char c)
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTFR);
|
||||
if(val & PL011_TXFF) { /* TX FIFO Full */
|
||||
return -PL011_EAGAIN;
|
||||
}
|
||||
write(c, PL011_UARTDR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pl011_rx_char(char * c)
|
||||
{
|
||||
unsigned int data;
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
read(val, PL011_UARTFR);
|
||||
if(val & PL011_RXFE) { /* RX FIFO Empty */
|
||||
return -PL011_EAGAIN;
|
||||
}
|
||||
|
||||
read(data, PL011_UARTDR);
|
||||
*c = (char) data;
|
||||
|
||||
if((data >> 8) & 0xF) { /* There were errors */
|
||||
return -1; /* Signal error in xfer */
|
||||
}
|
||||
return 0; /* No error return */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Sets the baud rate in kbps. It is recommended to use
|
||||
* standard rates such as: 1200, 2400, 3600, 4800, 7200,
|
||||
* 9600, 14400, 19200, 28800, 38400, 57600 76800, 115200.
|
||||
*/
|
||||
void pl011_set_baudrate(unsigned int baud, unsigned int clkrate)
|
||||
{
|
||||
const unsigned int uartclk = 24000000; /* 24Mhz clock fixed on pb926 */
|
||||
unsigned int val;
|
||||
unsigned int ipart, fpart;
|
||||
unsigned int remainder;
|
||||
|
||||
remainder = 0;
|
||||
ipart = 0;
|
||||
fpart = 0;
|
||||
val = 0;
|
||||
|
||||
/* Use default pb926 rate if no rate is supplied */
|
||||
if(clkrate == 0) {
|
||||
clkrate = uartclk;
|
||||
}
|
||||
if(baud > 115200 || baud < 1200) {
|
||||
baud = 38400; /* Default rate. */
|
||||
}
|
||||
/* 24000000 / (38400 * 16) */
|
||||
ipart = 39;
|
||||
|
||||
write(ipart, PL011_UARTIBRD);
|
||||
write(fpart, PL011_UARTFBRD);
|
||||
|
||||
/* For the IBAUD and FBAUD to update, we need to
|
||||
* write to UARTLCR_H because the 3 registers are
|
||||
* actually part of a single register in hardware
|
||||
* which only updates by a write to UARTLCR_H */
|
||||
read(val, PL011_UARTLCR_H);
|
||||
write(val, PL011_UARTLCR_H);
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* Masks the irqs given in the flags bitvector. */
|
||||
void pl011_set_irq_mask(unsigned int flags)
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
if(flags > 0x3FF) { /* Invalid irqmask bitvector */
|
||||
return;
|
||||
}
|
||||
|
||||
read(val, PL011_UARTIMSC);
|
||||
val |= flags;
|
||||
write(val, PL011_UARTIMSC);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* Clears the irqs given in flags from masking */
|
||||
void pl011_clr_irq_mask(unsigned int flags)
|
||||
{
|
||||
unsigned int val;
|
||||
val = 0;
|
||||
|
||||
if(flags > 0x3FF) { /* Invalid irqmask bitvector */
|
||||
return;
|
||||
}
|
||||
|
||||
read(val, PL011_UARTIMSC);
|
||||
val &= ~flags;
|
||||
write(val, PL011_UARTIMSC);
|
||||
return;
|
||||
}
|
||||
|
||||
int pl011_initialise(struct pl011_uart * uart)
|
||||
{
|
||||
|
||||
uart->frame_errors = 0;
|
||||
uart->parity_errors = 0;
|
||||
uart->break_errors = 0;
|
||||
uart->overrun_errors = 0;
|
||||
|
||||
/* Initialise data register for 8 bit data read/writes */
|
||||
pl011_set_word_width(8);
|
||||
|
||||
/* Fifos are disabled because by default it is assumed the port
|
||||
* will be used as a user terminal, and in that case the typed
|
||||
* characters will only show up when fifos are flushed, rather than
|
||||
* when each character is typed. We avoid this by not using fifos.
|
||||
*/
|
||||
pl011_disable_fifos();
|
||||
|
||||
/* Set default baud rate of 38400 */
|
||||
pl011_set_baudrate(38400, 24000000);
|
||||
|
||||
/* Set default settings of 1 stop bit, no parity, no hw flow ctrl */
|
||||
pl011_set_stopbits(1);
|
||||
pl011_parity_disable();
|
||||
|
||||
/* Disable all irqs */
|
||||
pl011_set_irq_mask(0x3FF);
|
||||
|
||||
/* Enable rx, tx, and uart chip */
|
||||
pl011_tx_enable();
|
||||
pl011_rx_enable();
|
||||
pl011_uart_enable();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void uart_init()
|
||||
{
|
||||
uart.base = PL011_BASE;
|
||||
pl011_initialise(&uart);
|
||||
}
|
||||
|
||||
/* Generic uart function that lib/putchar.c expects to see implemented */
|
||||
void uart_putc(char c)
|
||||
{
|
||||
int res;
|
||||
/* Platform specific uart implementation */
|
||||
do {
|
||||
res = pl011_tx_char(c);
|
||||
} while (res < 0);
|
||||
}
|
||||
|
||||
543
containers/posix/libc/src/format.c
Normal file
543
containers/posix/libc/src/format.c
Normal file
@@ -0,0 +1,543 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 University of New South Wales
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Operating Systems and Distributed Systems Group (DiSy)
|
||||
* University of New South Wales
|
||||
* http://www.disy.cse.unsw.edu.au
|
||||
*
|
||||
* Permission is granted by University of New South Wales, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of University of New South Wales, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on University of New South
|
||||
* Wales or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of University of New South Wales or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
/*
|
||||
Authors: Cristan Szmadja, Ben Leslie
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include "stdio.h"
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "format.h"
|
||||
#define fputc putc
|
||||
/*
|
||||
* lookup tables for umaxtostr
|
||||
*/
|
||||
static const char xdigits[16] = "0123456789abcdef";
|
||||
static const char Xdigits[16] = "0123456789ABCDEF";
|
||||
|
||||
/*
|
||||
* Convert an unsigned integer to a string of digits in the specified base.
|
||||
* Buf should point to the END of the buffer: 22 characters is probably big
|
||||
* enough. NO '\0' is appended to buf.
|
||||
*
|
||||
* If u == 0, NO digits are generated. The '0' is supplied by vfprintf using
|
||||
* its default zero padding, except in certain rare situations (e.g., "%.0d").
|
||||
*/
|
||||
static inline char *
|
||||
umaxtostr(char *buf, uintmax_t u, int base, const char *digits)
|
||||
{
|
||||
unsigned long u2;
|
||||
|
||||
/*
|
||||
* generate the digits in reverse order
|
||||
*/
|
||||
#if UINTMAX_MAX > ULONG_MAX
|
||||
/*
|
||||
* Uintmax_t arithmetic may be very slow. Use it only until the
|
||||
* residual fits in an unsigned long.
|
||||
*/
|
||||
while (u > ULONG_MAX) {
|
||||
*--buf = digits[u % base];
|
||||
u /= base;
|
||||
}
|
||||
#endif
|
||||
for (u2 = u; u2 != 0UL;) {
|
||||
*--buf = digits[u2 % base];
|
||||
u2 /= base;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This macro is *really* nasty.
|
||||
|
||||
It isn't an inline function because it relies on variables declared in the
|
||||
surrounding scope. Specifically:
|
||||
stream_or_memory -> Indicates if we are going to a file, or memory
|
||||
r -> The output counter
|
||||
n -> max size
|
||||
output -> output buffer (if going to memory)
|
||||
stream -> output stream (if going to file)
|
||||
*/
|
||||
|
||||
#define WRITE_CHAR(x) { \
|
||||
if (n != -1 && r == n) { \
|
||||
*output++ = '\0'; \
|
||||
overflowed = 1; \
|
||||
} \
|
||||
if (stream_or_memory) { \
|
||||
fputc(x, stream); \
|
||||
} else if (! overflowed) { \
|
||||
*output++ = x; \
|
||||
} \
|
||||
r++; \
|
||||
} \
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Print one formatted field. The length of s is len; any '\0's in s are
|
||||
* IGNORED. The field may have an optional prefix ('+', ' ', '-', '0x', or
|
||||
* '0X', packed into an unsigned int), and is padded appropriately to the
|
||||
* specified width. If width < 0, the field is left-justified.
|
||||
*/
|
||||
static inline int
|
||||
fprintf1(char *output, FILE *stream, bool stream_or_memory, size_t r, size_t n,
|
||||
const char *s, int len, unsigned int prefix,
|
||||
int prefixlen, int width, int prec, bool *over)
|
||||
{
|
||||
size_t i;
|
||||
size_t y = r; /* Keep a copy the starting value */
|
||||
bool overflowed = *over; /* Current start of overflow flag */
|
||||
|
||||
if (stream != NULL)
|
||||
lock_stream(stream);
|
||||
if (width - prec - prefixlen > 0) {
|
||||
for (i = 0; i < width - prec - prefixlen; i++) {
|
||||
WRITE_CHAR(' '); /* left-padding (if any) */
|
||||
}
|
||||
}
|
||||
|
||||
for (; prefix != 0; prefix >>= 8) {
|
||||
WRITE_CHAR(prefix & 0377); /* prefix string */
|
||||
}
|
||||
|
||||
for (i = 0; i < prec - len; i++) {
|
||||
WRITE_CHAR('0'); /* zero-padding (if any) */
|
||||
}
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
WRITE_CHAR(s[i]); /* actual string */
|
||||
}
|
||||
|
||||
if (width < 0) {
|
||||
while(y < -width) {
|
||||
WRITE_CHAR(' '); /* right-padding (if any) */
|
||||
}
|
||||
}
|
||||
|
||||
*over = overflowed; /* Set overflow flag in the caller */
|
||||
|
||||
if (stream != NULL)
|
||||
unlock_stream(stream);
|
||||
return r - y; /* We return the number of chars added */
|
||||
}
|
||||
|
||||
#include <assert.h>
|
||||
/*
|
||||
* parse printf format string
|
||||
* if stream_or_memory == 1 -> use fputc, otherwise write to memory
|
||||
* if n == -1, then don't check overflow
|
||||
*/
|
||||
int
|
||||
format_string(char *output, FILE *stream, bool stream_or_memory, size_t n,
|
||||
const char *fmt, va_list ap)
|
||||
{
|
||||
bool alt, ljust, point, zeropad, overflowed = 0;
|
||||
int lflags; /* 'h', 'j', 'l', 't', 'z' */
|
||||
unsigned int prefix; /* a very small string */
|
||||
int width, prec, base = 0, prefixlen;
|
||||
size_t r, len;
|
||||
const char *p, *s, *digits;
|
||||
char buf[24], *const buf_end = buf + sizeof buf;
|
||||
intmax_t d;
|
||||
uintmax_t u = 0;
|
||||
|
||||
r = 0;
|
||||
if (stream != NULL)
|
||||
lock_stream(stream);
|
||||
for (p = fmt; *p != '\0'; p++) {
|
||||
if (*p != '%') {
|
||||
putc:
|
||||
WRITE_CHAR(*p);
|
||||
continue;
|
||||
}
|
||||
alt = false;
|
||||
ljust = false;
|
||||
point = false;
|
||||
zeropad = false;
|
||||
lflags = 0;
|
||||
prefix = '\0';
|
||||
prefixlen = 0;
|
||||
width = 0;
|
||||
prec = 1; /* make sure 0 prints as "0" */
|
||||
digits = xdigits;
|
||||
for (p++;; p++) {
|
||||
again:
|
||||
switch (*p) {
|
||||
case '%':
|
||||
goto putc;
|
||||
case '#':
|
||||
alt = true;
|
||||
continue;
|
||||
case '-': /* takes precedence over '0' */
|
||||
ljust = true;
|
||||
continue;
|
||||
case '0':
|
||||
zeropad = true;
|
||||
continue;
|
||||
case '+': /* XXX should take precedence over
|
||||
* ' ' */
|
||||
case ' ':
|
||||
prefix = *p;
|
||||
prefixlen = 1;
|
||||
continue;
|
||||
case '*':
|
||||
width = va_arg(ap, int);
|
||||
if (ljust)
|
||||
width = -width;
|
||||
continue;
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
/*
|
||||
* width = strtol(p, &p, 10), sort of
|
||||
*/
|
||||
width = *p - '0';
|
||||
for (p++; (unsigned int) (*p - '0') < 10;
|
||||
p++)
|
||||
width = width * 10 + (*p - '0');
|
||||
if (ljust)
|
||||
width = -width;
|
||||
goto again; /* don't increment p */
|
||||
case '.':
|
||||
point = true;
|
||||
if (*++p == '*') {
|
||||
prec = va_arg(ap, int);
|
||||
continue;
|
||||
} else {
|
||||
/*
|
||||
* prec = strtol(p, &p, 10), sort
|
||||
* of
|
||||
*/
|
||||
for (prec = 0;
|
||||
(unsigned int) (*p - '0') <
|
||||
10; p++)
|
||||
prec =
|
||||
prec * 10 + (*p - '0');
|
||||
goto again; /* don't increment
|
||||
* p */
|
||||
}
|
||||
case 'h':
|
||||
lflags--;
|
||||
continue;
|
||||
case 'L':
|
||||
case 'l':
|
||||
lflags++;
|
||||
continue;
|
||||
case 't':
|
||||
case 'z':
|
||||
lflags = 1; /* assume ptrdiff_t and
|
||||
* size_t are long */
|
||||
continue;
|
||||
case 'j':
|
||||
lflags = 2; /* assume intmax_t is long
|
||||
* long */
|
||||
continue;
|
||||
#ifndef NO_FLOAT
|
||||
case 'a':
|
||||
case 'A':
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'f':
|
||||
case 'g':
|
||||
case 'G':
|
||||
/*
|
||||
* NOT IMPLEMENTED
|
||||
*/
|
||||
switch (lflags) {
|
||||
case 0:
|
||||
va_arg(ap, double);
|
||||
break;
|
||||
case 1:
|
||||
va_arg(ap, long double);
|
||||
break;
|
||||
default:
|
||||
goto default_case;
|
||||
}
|
||||
break;
|
||||
#endif /* !NO_FLOAT */
|
||||
case 'c':
|
||||
#ifndef NO_WCHAR
|
||||
/*
|
||||
* NOT IMPLEMENTED
|
||||
*/
|
||||
if (lflags > 0)
|
||||
va_arg(ap, wchar_t);
|
||||
else
|
||||
#endif
|
||||
*(buf_end - 1) = va_arg(ap, int);
|
||||
s = buf_end - 1;
|
||||
len = 1;
|
||||
goto common3;
|
||||
case 'd':
|
||||
case 'i':
|
||||
switch (lflags) {
|
||||
case -2:
|
||||
// d = va_arg(ap, signed char);
|
||||
d = va_arg(ap, int);
|
||||
break;
|
||||
case -1:
|
||||
// d = va_arg(ap, short);
|
||||
d = va_arg(ap, int);
|
||||
break;
|
||||
case 0:
|
||||
d = va_arg(ap, int);
|
||||
break;
|
||||
case 1:
|
||||
d = va_arg(ap, long);
|
||||
break;
|
||||
#ifndef NO_LONG_LONG
|
||||
case 2:
|
||||
d = va_arg(ap, long long);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
goto default_case;
|
||||
}
|
||||
if (d < 0LL) {
|
||||
/*
|
||||
* safely negate d, even
|
||||
* INTMAX_MIN
|
||||
*/
|
||||
u = -(uintmax_t) d;
|
||||
prefix = '-'; /* override ' ' or
|
||||
* '+' */
|
||||
prefixlen = 1;
|
||||
} else {
|
||||
u = d;
|
||||
}
|
||||
base = 10;
|
||||
goto common2;
|
||||
case 'n':
|
||||
switch (lflags) {
|
||||
case -2:
|
||||
*va_arg(ap, signed char *) = r;
|
||||
break;
|
||||
case -1:
|
||||
*va_arg(ap, short *) = r;
|
||||
break;
|
||||
case 0:
|
||||
*va_arg(ap, int *) = r;
|
||||
break;
|
||||
case 1:
|
||||
*va_arg(ap, long *) = r;
|
||||
break;
|
||||
case 2:
|
||||
*va_arg(ap, long long *) = r;
|
||||
break;
|
||||
default:
|
||||
goto default_case;
|
||||
}
|
||||
break;
|
||||
case 'o':
|
||||
base = 8;
|
||||
goto common1;
|
||||
case 'p':
|
||||
u = (uintptr_t) va_arg(ap, const void *);
|
||||
if (u != (uintptr_t) NULL) {
|
||||
base = 16;
|
||||
prec = 2 * sizeof(const void *);
|
||||
prefix = '0' | 'x' << 8;
|
||||
prefixlen = 2;
|
||||
goto common2;
|
||||
} else {
|
||||
s = "(nil)";
|
||||
len = 5;
|
||||
goto common3;
|
||||
}
|
||||
case 's':
|
||||
s = va_arg(ap, const char *);
|
||||
/*
|
||||
* XXX left-justified strings are scanned
|
||||
* twice
|
||||
*/
|
||||
if (point) {
|
||||
/*
|
||||
* len = min(prec, strlen(s))
|
||||
*/
|
||||
for (len = 0; len < prec; len++)
|
||||
if (s[len] == '\0')
|
||||
break;
|
||||
} else {
|
||||
len = strlen(s);
|
||||
}
|
||||
goto common3;
|
||||
case 'u':
|
||||
base = 10;
|
||||
goto common1;
|
||||
case 'X':
|
||||
digits = Xdigits;
|
||||
/*
|
||||
* FALLTHROUGH
|
||||
*/
|
||||
case 'x':
|
||||
base = 16;
|
||||
if (alt) {
|
||||
prefix = '0' | *p << 8;
|
||||
prefixlen = 2;
|
||||
}
|
||||
/*
|
||||
* FALLTHROUGH
|
||||
*/
|
||||
common1:
|
||||
/*
|
||||
* common code for %o, %u, %X, and %x
|
||||
*/
|
||||
switch (lflags) {
|
||||
case -2:
|
||||
// u = va_arg(ap, unsigned char);
|
||||
u = va_arg(ap, int);
|
||||
break;
|
||||
case -1:
|
||||
// u = va_arg(ap, unsigned short);
|
||||
u = va_arg(ap, int);
|
||||
break;
|
||||
case 0:
|
||||
u = va_arg(ap, unsigned int);
|
||||
break;
|
||||
case 1:
|
||||
u = va_arg(ap, unsigned long);
|
||||
break;
|
||||
#ifndef NO_LONG_LONG
|
||||
case 2:
|
||||
u = va_arg(ap, unsigned long long);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
goto default_case;
|
||||
}
|
||||
/*
|
||||
* FALLTHROUGH
|
||||
*/
|
||||
common2:
|
||||
s = umaxtostr(buf_end, u, base, digits);
|
||||
len = buf_end - s;
|
||||
/*
|
||||
* the field may overflow prec
|
||||
*/
|
||||
if (prec < len)
|
||||
/*
|
||||
* FALLTHOUGH
|
||||
*/
|
||||
common3:
|
||||
prec = len;
|
||||
if (zeropad && prec < width - prefixlen)
|
||||
prec = width - prefixlen;
|
||||
else if (alt && base == 8 && u != 0LL)
|
||||
prec++;
|
||||
|
||||
{
|
||||
int tmp = fprintf1(output, stream, stream_or_memory, r, n,
|
||||
s, len, prefix,
|
||||
prefixlen, width, prec, &overflowed);
|
||||
r += tmp;
|
||||
output += tmp;
|
||||
}
|
||||
|
||||
break;
|
||||
default: /* unrecognized conversion
|
||||
* specifier */
|
||||
default_case:
|
||||
/*
|
||||
* print uninterpreted
|
||||
*/
|
||||
for (s = p - 1; *s != '%'; s--);
|
||||
for (; s <= p; s++) {
|
||||
WRITE_CHAR(*p);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break; /* finished the conversion specifier */
|
||||
}
|
||||
}
|
||||
if (! stream_or_memory && ! overflowed)
|
||||
*output++ = '\0';
|
||||
if (stream != NULL)
|
||||
unlock_stream(stream);
|
||||
return r;
|
||||
}
|
||||
86
containers/posix/libc/src/format.h
Normal file
86
containers/posix/libc/src/format.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 National ICT Australia
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
|
||||
* National ICT Australia
|
||||
* http://www.ertos.nicta.com.au
|
||||
*
|
||||
* Permission is granted by National ICT Australia, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of National ICT Australia, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on National ICT
|
||||
* Australia or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of National ICT Australia or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
/*
|
||||
Author: Ben Leslie
|
||||
*/
|
||||
#ifndef _FORMAT_H_
|
||||
#define _FORMAT_H_
|
||||
#include <stdbool.h>
|
||||
int format_string(char *output, FILE *stream, bool stream_or_memory, size_t n, const char *fmt, va_list ap);
|
||||
#endif
|
||||
446
containers/posix/libc/src/printf.c
Normal file
446
containers/posix/libc/src/printf.c
Normal file
@@ -0,0 +1,446 @@
|
||||
/*********************************************************************
|
||||
*
|
||||
* Copyright (C) 2002-2004 Karlsruhe University
|
||||
*
|
||||
* File path: generic/printk.cc
|
||||
* Description: Implementation of printf
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
********************************************************************/
|
||||
#include <stdarg.h> /* for va_list, ... comes with gcc */
|
||||
#include <stdio.h>
|
||||
|
||||
/* FIXME: LICENSE LICENCE */
|
||||
typedef unsigned int word_t;
|
||||
|
||||
extern void putc(const char c);
|
||||
extern int print_tid (word_t val, word_t width, word_t precision, int adjleft);
|
||||
|
||||
|
||||
/* convert nibble to lowercase hex char */
|
||||
#define hexchars(x) (((x) < 10) ? ('0' + (x)) : ('a' + ((x) - 10)))
|
||||
|
||||
/**
|
||||
* Print hexadecimal value
|
||||
*
|
||||
* @param val value to print
|
||||
* @param width width in caracters
|
||||
* @param precision minimum number of digits to apprear
|
||||
* @param adjleft left adjust the value
|
||||
* @param nullpad pad with leading zeros (when right padding)
|
||||
*
|
||||
* Prints a hexadecimal value with leading zeroes of given width
|
||||
* using putc(), or if adjleft argument is given, print
|
||||
* hexadecimal value with space padding to the right.
|
||||
*
|
||||
* @returns the number of charaters printed (should be same as width).
|
||||
*/
|
||||
int print_hex64(unsigned long long val, int width, int precision, int adjleft, int nullpad)
|
||||
{
|
||||
int i, n = 0;
|
||||
int nwidth = 0;
|
||||
unsigned int high, low;
|
||||
|
||||
high = val >> 32;
|
||||
low = (unsigned int)val;
|
||||
|
||||
// Find width of hexnumber
|
||||
if (high) {
|
||||
while ((high >> (4 * nwidth)) && ((unsigned) nwidth < 2 * sizeof (unsigned int)))
|
||||
nwidth++;
|
||||
nwidth += 32;
|
||||
} else {
|
||||
while ((low >> (4 * nwidth)) && ((unsigned) nwidth < 2 * sizeof (unsigned int)))
|
||||
nwidth++;
|
||||
}
|
||||
|
||||
if (nwidth == 0)
|
||||
nwidth = 1;
|
||||
|
||||
// May need to increase number of printed digits
|
||||
if (precision > nwidth)
|
||||
nwidth = precision;
|
||||
|
||||
// May need to increase number of printed characters
|
||||
if (width == 0 && width < nwidth)
|
||||
width = nwidth;
|
||||
|
||||
// Print number with padding
|
||||
if (high)
|
||||
{
|
||||
if (!adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (nullpad ? '0' : ' ');
|
||||
for (i = 4 * (nwidth - 33); i >= 0; i -= 4, n++)
|
||||
putc (hexchars ((high >> i) & 0xF));
|
||||
if (adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (' ');
|
||||
width -= 32;
|
||||
nwidth -= 32;
|
||||
nullpad = 1;
|
||||
}
|
||||
if (! adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (nullpad ? '0' : ' ');
|
||||
for (i = 4 * (nwidth - 1); i >= 0; i -= 4, n++)
|
||||
putc (hexchars ((low >> i) & 0xF));
|
||||
if (adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (' ');
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int print_hex_3arg(const word_t val, int width, int precision)
|
||||
{
|
||||
long i, n = 0;
|
||||
long nwidth = 0;
|
||||
int adjleft = 0;
|
||||
int nullpad = 0;
|
||||
|
||||
// Find width of hexnumber
|
||||
while ((val >> (4 * nwidth)) && (word_t) nwidth < 2 * sizeof (word_t))
|
||||
nwidth++;
|
||||
|
||||
if (nwidth == 0)
|
||||
nwidth = 1;
|
||||
|
||||
// May need to increase number of printed digits
|
||||
if (precision > nwidth)
|
||||
nwidth = precision;
|
||||
|
||||
// May need to increase number of printed characters
|
||||
if (width == 0 && width < nwidth)
|
||||
width = nwidth;
|
||||
|
||||
// Print number with padding
|
||||
if (! adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (nullpad ? '0' : ' ');
|
||||
for (i = 4 * (nwidth - 1); i >= 0; i -= 4, n++)
|
||||
putc (hexchars ((val >> i) & 0xF));
|
||||
if (adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (' ');
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int print_hex_5arg(const word_t val, int width,
|
||||
int precision, int adjleft, int nullpad)
|
||||
{
|
||||
long i, n = 0;
|
||||
long nwidth = 0;
|
||||
|
||||
// Find width of hexnumber
|
||||
while ((val >> (4 * nwidth)) && (word_t) nwidth < 2 * sizeof (word_t))
|
||||
nwidth++;
|
||||
|
||||
if (nwidth == 0)
|
||||
nwidth = 1;
|
||||
|
||||
// May need to increase number of printed digits
|
||||
if (precision > nwidth)
|
||||
nwidth = precision;
|
||||
|
||||
// May need to increase number of printed characters
|
||||
if (width == 0 && width < nwidth)
|
||||
width = nwidth;
|
||||
|
||||
// Print number with padding
|
||||
if (! adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (nullpad ? '0' : ' ');
|
||||
for (i = 4 * (nwidth - 1); i >= 0; i -= 4, n++)
|
||||
putc (hexchars ((val >> i) & 0xF));
|
||||
if (adjleft)
|
||||
for (i = width - nwidth; i > 0; i--, n++)
|
||||
putc (' ');
|
||||
|
||||
return n;
|
||||
}
|
||||
/**
|
||||
* Print a string
|
||||
*
|
||||
* @param s zero-terminated string to print
|
||||
* @param width minimum width of printed string
|
||||
*
|
||||
* Prints the zero-terminated string using putc(). The printed
|
||||
* string will be right padded with space to so that it will be
|
||||
* at least WIDTH characters wide.
|
||||
*
|
||||
* @returns the number of charaters printed.
|
||||
*/
|
||||
int print_string_3arg(const char * s, const int width, const int precision)
|
||||
{
|
||||
int n = 0;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (*s == 0)
|
||||
break;
|
||||
|
||||
putc(*s++);
|
||||
n++;
|
||||
if (precision && n >= precision)
|
||||
break;
|
||||
}
|
||||
|
||||
while (n < width) { putc(' '); n++; }
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
int print_string_1arg(const char * s)
|
||||
{
|
||||
int n = 0;
|
||||
int width = 0;
|
||||
int precision = 0;
|
||||
|
||||
for (;;) {
|
||||
if (*s == 0)
|
||||
break;
|
||||
|
||||
putc(*s++);
|
||||
n++;
|
||||
if (precision && n >= precision)
|
||||
break;
|
||||
}
|
||||
|
||||
while (n < width) {
|
||||
putc(' ');
|
||||
n++;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Print hexadecimal value with a separator
|
||||
*
|
||||
* @param val value to print
|
||||
* @param bits number of lower-most bits before which to
|
||||
* place the separator
|
||||
* @param sep the separator to print
|
||||
*
|
||||
* @returns the number of charaters printed.
|
||||
*/
|
||||
int print_hex_sep(const word_t val, const int bits, const char *sep)
|
||||
{
|
||||
int n = 0;
|
||||
|
||||
n = print_hex_3arg(val >> bits, 0, 0);
|
||||
n += print_string_1arg(sep);
|
||||
n += print_hex_3arg(val & ((1 << bits) - 1), 0, 0);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Print decimal value
|
||||
*
|
||||
* @param val value to print
|
||||
* @param width width of field
|
||||
* @param pad character used for padding value up to width
|
||||
*
|
||||
* Prints a value as a decimal in the given WIDTH with leading
|
||||
* whitespaces.
|
||||
*
|
||||
* @returns the number of characters printed (may be more than WIDTH)
|
||||
*/
|
||||
int print_dec(const word_t val, int width)
|
||||
{
|
||||
word_t divisor;
|
||||
int digits;
|
||||
/* estimate number of spaces and digits */
|
||||
for (divisor = 1, digits = 1; val/divisor >= 10; divisor *= 10, digits++);
|
||||
|
||||
/* print spaces */
|
||||
for ( ; digits < width; digits++ )
|
||||
putc(' ');
|
||||
|
||||
/* print digits */
|
||||
do {
|
||||
putc(((val/divisor) % 10) + '0');
|
||||
} while (divisor /= 10);
|
||||
|
||||
/* report number of digits printed */
|
||||
return digits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the real printk work
|
||||
*
|
||||
* @param format_p pointer to format string
|
||||
* @param args list of arguments, variable length
|
||||
*
|
||||
* Prints the given arguments as specified by the format string.
|
||||
* Implements a subset of the well-known printf plus some L4-specifics.
|
||||
*
|
||||
* @returns the number of characters printed
|
||||
*/
|
||||
int do_printk(char* format_p, va_list args)
|
||||
{
|
||||
const char* format = format_p;
|
||||
int n = 0;
|
||||
int i = 0;
|
||||
int width = 8;
|
||||
int precision = 0;
|
||||
int adjleft = 0, nullpad = 0;
|
||||
|
||||
#define arg(x) va_arg(args, x)
|
||||
|
||||
/* sanity check */
|
||||
if (format == '\0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (*format)
|
||||
{
|
||||
switch (*(format))
|
||||
{
|
||||
case '%':
|
||||
width = precision = 0;
|
||||
adjleft = nullpad = 0;
|
||||
reentry:
|
||||
switch (*(++format))
|
||||
{
|
||||
/* modifiers */
|
||||
case '.':
|
||||
for (format++; *format >= '0' && *format <= '9'; format++)
|
||||
precision = precision * 10 + (*format) - '0';
|
||||
if (*format == 'w')
|
||||
{
|
||||
// Set precision to printsize of a hex word
|
||||
precision = sizeof (word_t) * 2;
|
||||
format++;
|
||||
}
|
||||
format--;
|
||||
goto reentry;
|
||||
case '0':
|
||||
nullpad = (width == 0);
|
||||
case '1'...'9':
|
||||
width = width*10 + (*format)-'0';
|
||||
goto reentry;
|
||||
case 'w':
|
||||
// Set width to printsize of a hex word
|
||||
width = sizeof (word_t) * 2;
|
||||
goto reentry;
|
||||
case '-':
|
||||
adjleft = 0;
|
||||
goto reentry;
|
||||
case 'l':
|
||||
goto reentry;
|
||||
break;
|
||||
case 'c':
|
||||
putc(arg(int));
|
||||
n++;
|
||||
break;
|
||||
case 'm': /* microseconds */
|
||||
{
|
||||
n += print_hex64(arg(unsigned long long), width, precision,
|
||||
adjleft, nullpad);
|
||||
break;
|
||||
}
|
||||
case 'd':
|
||||
{
|
||||
long val = arg(long);
|
||||
if (val < 0)
|
||||
{
|
||||
putc('-');
|
||||
val = -val;
|
||||
}
|
||||
n += print_dec(val, width);
|
||||
break;
|
||||
}
|
||||
case 'u':
|
||||
n += print_dec(arg(long), width);
|
||||
break;
|
||||
case 'p':
|
||||
precision = sizeof (word_t) * 2;
|
||||
case 'x':
|
||||
n += print_hex_5arg(arg(long), width, precision, adjleft, nullpad);
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
char* s = arg(char*);
|
||||
if (s)
|
||||
n += print_string_3arg(s, width, precision);
|
||||
else
|
||||
n += print_string_3arg("(null)", width, precision);
|
||||
}
|
||||
break;
|
||||
|
||||
case 't':
|
||||
case 'T':
|
||||
// Do nothing for now.
|
||||
//n += print_tid (arg (word_t), width, precision, adjleft);
|
||||
break;
|
||||
|
||||
case '%':
|
||||
putc('%');
|
||||
n++;
|
||||
format++;
|
||||
continue;
|
||||
default:
|
||||
n += print_string_1arg("?");
|
||||
break;
|
||||
};
|
||||
i++;
|
||||
break;
|
||||
default:
|
||||
putc(*format);
|
||||
n++;
|
||||
break;
|
||||
}
|
||||
format++;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flexible print function
|
||||
*
|
||||
* @param format string containing formatting and parameter type
|
||||
* information
|
||||
* @param ... variable list of parameters
|
||||
*
|
||||
* @returns the number of characters printed
|
||||
*/
|
||||
int printf(char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
int i;
|
||||
|
||||
va_start(args, format);
|
||||
i = do_printk(format, args);
|
||||
va_end(args);
|
||||
return i;
|
||||
};
|
||||
|
||||
|
||||
8
containers/posix/libc/src/putc.c
Normal file
8
containers/posix/libc/src/putc.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <arch/uart.h>
|
||||
|
||||
void putc(char c)
|
||||
{
|
||||
if (c == '\n')
|
||||
uart_putc('\r');
|
||||
uart_putc(c);
|
||||
}
|
||||
107
containers/posix/libc/src/sprintf.c
Normal file
107
containers/posix/libc/src/sprintf.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Australian Public Licence B (OZPLB)
|
||||
*
|
||||
* Version 1-0
|
||||
*
|
||||
* Copyright (c) 2004 National ICT Australia
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Developed by: Embedded, Real-time and Operating Systems Program (ERTOS)
|
||||
* National ICT Australia
|
||||
* http://www.ertos.nicta.com.au
|
||||
*
|
||||
* Permission is granted by National ICT Australia, free of charge, to
|
||||
* any person obtaining a copy of this software and any associated
|
||||
* documentation files (the "Software") to deal with the Software without
|
||||
* restriction, including (without limitation) the rights to use, copy,
|
||||
* modify, adapt, merge, publish, distribute, communicate to the public,
|
||||
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
||||
* to permit persons to whom the Software is furnished to do so, subject
|
||||
* to the following conditions:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimers.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following
|
||||
* disclaimers in the documentation and/or other materials provided
|
||||
* with the distribution.
|
||||
*
|
||||
* * Neither the name of National ICT Australia, nor the names of its
|
||||
* contributors, may be used to endorse or promote products derived
|
||||
* from this Software without specific prior written permission.
|
||||
*
|
||||
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
||||
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
||||
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
||||
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
||||
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
||||
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
||||
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
||||
*
|
||||
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
||||
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
||||
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
||||
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
||||
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
||||
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
||||
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
||||
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
||||
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
||||
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
||||
* DAMAGES OR OTHER LIABILITY.
|
||||
*
|
||||
* If applicable legislation implies representations, warranties, or
|
||||
* conditions, or imposes obligations or liability on National ICT
|
||||
* Australia or one of its contributors in respect of the Software that
|
||||
* cannot be wholly or partly excluded, restricted or modified, the
|
||||
* liability of National ICT Australia or the contributor is limited, to
|
||||
* the full extent permitted by the applicable legislation, at its
|
||||
* option, to:
|
||||
* a. in the case of goods, any one or more of the following:
|
||||
* i. the replacement of the goods or the supply of equivalent goods;
|
||||
* ii. the repair of the goods;
|
||||
* iii. the payment of the cost of replacing the goods or of acquiring
|
||||
* equivalent goods;
|
||||
* iv. the payment of the cost of having the goods repaired; or
|
||||
* b. in the case of services:
|
||||
* i. the supplying of the services again; or
|
||||
* ii. the payment of the cost of having the services supplied again.
|
||||
*
|
||||
* The construction, validity and performance of this licence is governed
|
||||
* by the laws in force in New South Wales, Australia.
|
||||
*/
|
||||
/*
|
||||
Author: Ben Leslie <benjl@cse.unsw.edu.au>
|
||||
*/
|
||||
#ifndef NULL
|
||||
#define NULL 0
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include "format.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
int
|
||||
vsprintf(char *s, const char *format, va_list arg)
|
||||
{
|
||||
return format_string(s, NULL, 0, -1, format, arg);
|
||||
}
|
||||
int
|
||||
sprintf(char *s, const char *format, ...)
|
||||
{
|
||||
int ret;
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
ret = vsprintf(s, format, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
84
containers/posix/libc/src/string.c
Normal file
84
containers/posix/libc/src/string.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <string.h>
|
||||
|
||||
int strlen(const char *s)
|
||||
{
|
||||
const char *p;
|
||||
|
||||
for (p = s; *p != '\0'; p++);
|
||||
return p - s;
|
||||
}
|
||||
|
||||
char *strcpy(char *to, const char *from)
|
||||
{
|
||||
char *t = to;
|
||||
|
||||
while ((*to++ = *from++) != '\0')
|
||||
;
|
||||
return t;
|
||||
}
|
||||
|
||||
void *memset(void *p, int c, int size)
|
||||
{
|
||||
char ch;
|
||||
char *pp;
|
||||
|
||||
pp = (char *)p;
|
||||
ch = (char)c;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
*pp++ = ch;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void *memcpy(void *d, void *s, int size)
|
||||
{
|
||||
char *dst = (char *)d;
|
||||
char *src = (char *)s;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
*dst = *src;
|
||||
dst++;
|
||||
src++;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
int strcmp(const char *s1, const char *s2)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
int d;
|
||||
|
||||
while(1) {
|
||||
d = (unsigned char)s1[i] - (unsigned char)s2[i];
|
||||
if (d != 0 || s1[i] == '\0')
|
||||
return d;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Copies string pointed by @from to string pointed by @to.
|
||||
*
|
||||
* If count is greater than the length of string in @from,
|
||||
* pads rest of the locations with null.
|
||||
*/
|
||||
char *strncpy(char *to, const char *from, int count)
|
||||
{
|
||||
char *temp = to;
|
||||
|
||||
while (count) {
|
||||
*temp = *from;
|
||||
|
||||
/*
|
||||
* Stop updating from if null
|
||||
* terminator is reached.
|
||||
*/
|
||||
if (*from)
|
||||
from++;
|
||||
temp++;
|
||||
count--;
|
||||
}
|
||||
return to;
|
||||
}
|
||||
Reference in New Issue
Block a user