├── .gitignore ├── .tfignore ├── README.md ├── Test Drivers ├── ArmTest │ ├── ArmTest.c │ ├── ArmTest.componentinfo.xml │ ├── ArmTest.cppproj │ ├── Device_Startup │ │ ├── samc21g17a_flash.ld │ │ └── startup_samc21.c │ └── sam_spec.h └── WinTest │ ├── Driver.c │ ├── Print.c │ ├── WinTest.c │ ├── WinTest.sln │ ├── WinTest.vcxproj │ ├── WinTest.vcxproj.filters │ └── math.h ├── arm-lib-out ├── libstdio-l-flt.a ├── libstdio-l.a ├── libstdio-ll-dbl.a ├── libstdio-ll-flt-dbl.a ├── libstdio-ll-flt.a └── libstdio-ll.a ├── doc ├── dox.css ├── dox_footer.html ├── dox_header.html └── doxygen.config.in ├── libc ├── include │ └── stdio.h └── stdio │ ├── atof.c │ ├── clearerr.c │ ├── convtoa.h │ ├── dtoa.c │ ├── feof.c │ ├── ferror.c │ ├── fgetc.c │ ├── fgets.c │ ├── fprintf.c │ ├── fputc.c │ ├── fputs.c │ ├── fread.c │ ├── fscanf.c │ ├── ftoa.c │ ├── fwrite.c │ ├── getchar.c │ ├── gets.c │ ├── mulpower100d.c │ ├── mulpower100f.c │ ├── printf.c │ ├── putchar.c │ ├── puts.c │ ├── scanf.c │ ├── snprintf.c │ ├── sprintf.c │ ├── sscanf.c │ ├── stdio_private.h │ ├── strconv.h │ ├── strconvd.c │ ├── strconvf.c │ ├── strconvhelp.c │ ├── strtod.c │ ├── strtof.c │ ├── ungetc.c │ ├── vfprintf.c │ ├── vfscanf.c │ ├── vprintf.c │ ├── vscanf.c │ ├── vsnprintf.c │ ├── vsprintf.c │ └── vsscanf.c ├── stdio-mini.atsln ├── stdio-mini.componentinfo.xml └── stdio-mini.cproj /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | DebugUpdate/ 25 | ReleaseUpdate/ 26 | stdio-*/ 27 | 28 | # Visual Studio 2015 cache/options directory 29 | .vs/ 30 | # Uncomment if you have tasks that create the project's static files in wwwroot 31 | #wwwroot/ 32 | 33 | # MSTest test Results 34 | [Tt]est[Rr]esult*/ 35 | [Bb]uild[Ll]og.* 36 | 37 | # NUNIT 38 | *.VisualState.xml 39 | TestResult.xml 40 | 41 | # Build Results of an ATL Project 42 | [Dd]ebugPS/ 43 | [Rr]eleasePS/ 44 | dlldata.c 45 | 46 | # DNX 47 | project.lock.json 48 | project.fragment.lock.json 49 | artifacts/ 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # NCrunch 117 | _NCrunch_* 118 | .*crunch*.local.xml 119 | nCrunchTemp_* 120 | 121 | # MightyMoose 122 | *.mm.* 123 | AutoTest.Net/ 124 | 125 | # Web workbench (sass) 126 | .sass-cache/ 127 | 128 | # Installshield output folder 129 | [Ee]xpress/ 130 | 131 | # DocProject is a documentation generator add-in 132 | DocProject/buildhelp/ 133 | DocProject/Help/*.HxT 134 | DocProject/Help/*.HxC 135 | DocProject/Help/*.hhc 136 | DocProject/Help/*.hhk 137 | DocProject/Help/*.hhp 138 | DocProject/Help/Html2 139 | DocProject/Help/html 140 | 141 | # Click-Once directory 142 | publish/ 143 | 144 | # Publish Web Output 145 | *.[Pp]ublish.xml 146 | *.azurePubxml 147 | # TODO: Comment the next line if you want to checkin your web deploy settings 148 | # but database connection strings (with potential passwords) will be unencrypted 149 | #*.pubxml 150 | *.publishproj 151 | 152 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 153 | # checkin your Azure Web App publish settings, but sensitive information contained 154 | # in these scripts will be unencrypted 155 | PublishScripts/ 156 | 157 | # NuGet Packages 158 | *.nupkg 159 | # The packages folder can be ignored because of Package Restore 160 | **/packages/* 161 | # except build/, which is used as an MSBuild target. 162 | !**/packages/build/ 163 | # Uncomment if necessary however generally it will be regenerated when needed 164 | #!**/packages/repositories.config 165 | # NuGet v3's project.json files produces more ignoreable files 166 | *.nuget.props 167 | *.nuget.targets 168 | 169 | # Microsoft Azure Build Output 170 | csx/ 171 | *.build.csdef 172 | 173 | # Microsoft Azure Emulator 174 | ecf/ 175 | rcf/ 176 | 177 | # Windows Store app package directories and files 178 | AppPackages/ 179 | BundleArtifacts/ 180 | Package.StoreAssociation.xml 181 | _pkginfo.txt 182 | 183 | # Visual Studio cache files 184 | # files ending in .cache can be ignored 185 | *.[Cc]ache 186 | # but keep track of directories ending in .cache 187 | !*.[Cc]ache/ 188 | 189 | # Others 190 | ClientBin/ 191 | ~$* 192 | *~ 193 | *.dbmdl 194 | *.dbproj.schemaview 195 | *.jfm 196 | *.pfx 197 | *.publishsettings 198 | node_modules/ 199 | orleans.codegen.cs 200 | 201 | # Since there are multiple workflows, uncomment next line to ignore bower_components 202 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 203 | #bower_components/ 204 | 205 | # RIA/Silverlight projects 206 | Generated_Code/ 207 | 208 | # Backup & report files from converting an old project file 209 | # to a newer Visual Studio version. Backup files are not needed, 210 | # because we have git ;-) 211 | _UpgradeReport_Files/ 212 | Backup*/ 213 | UpgradeLog*.XML 214 | UpgradeLog*.htm 215 | 216 | # SQL Server files 217 | *.mdf 218 | *.ldf 219 | 220 | # Business Intelligence projects 221 | *.rdl.data 222 | *.bim.layout 223 | *.bim_*.settings 224 | 225 | # Microsoft Fakes 226 | FakesAssemblies/ 227 | 228 | # GhostDoc plugin setting file 229 | *.GhostDoc.xml 230 | 231 | # Node.js Tools for Visual Studio 232 | .ntvs_analysis.dat 233 | 234 | # Visual Studio 6 build log 235 | *.plg 236 | 237 | # Visual Studio 6 workspace options file 238 | *.opt 239 | 240 | # Visual Studio LightSwitch build output 241 | **/*.HTMLClient/GeneratedArtifacts 242 | **/*.DesktopClient/GeneratedArtifacts 243 | **/*.DesktopClient/ModelManifest.xml 244 | **/*.Server/GeneratedArtifacts 245 | **/*.Server/ModelManifest.xml 246 | _Pvt_Extensions 247 | 248 | # Paket dependency manager 249 | .paket/paket.exe 250 | paket-files/ 251 | 252 | # FAKE - F# Make 253 | .fake/ 254 | 255 | # JetBrains Rider 256 | .idea/ 257 | *.sln.iml 258 | 259 | # CodeRush 260 | .cr/ 261 | 262 | # Python Tools for Visual Studio (PTVS) 263 | __pycache__/ 264 | *.pyc -------------------------------------------------------------------------------- /.tfignore: -------------------------------------------------------------------------------- 1 | \.git -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Portable, Device-Oriented `stdio` C Runtime Library 2 | The purpose of this library is to allow a device (such as a serial 3 | port) to work with `stdio` functions like `printf` and `scanf`. 4 | It defines the C `FILE` structure to have references to user-written 5 | functions to read and write a byte at a time. This has been a 6 | feature of AVR-LIBC, the standard `stdio` C runtime 7 | library for the 8-bit AVR microcontroller family. The library 8 | presented here started from AVR-LIBC soure code, cloned from 9 | https://github.com/stevenj/avr-libc3 in April 2020. 10 | 11 | ### Functions Provided 12 | The following standard C runtime functions are included. This is 13 | basically the normal set except for functions to open, 14 | close, or position a file. 15 | 16 | | formatted out | formatted in | simple out | simple in | 17 | |-----------|---------|---------|---------| 18 | | fprintf | fscanf | fputc | fgetc | 19 | | printf | scanf | fputs | fgets | 20 | | snprintf | sscanf | fwrite | fread | 21 | | sprintf | vfscanf | putc | getc | 22 | | vfprintf | vscanf | putchar | getchar | 23 | | vprintf | vsscanf | puts | gets | 24 | | vsnprintf | | | ungetc | 25 | | vsprintf | | | | 26 | 27 | In addition, there are some functions and macros to create and 28 | initialize the C `FILE` structure. The user must provide 29 | read and/or write functions, which have the following signature: 30 | 31 | void _fdev_put(void *, char); // write a char 32 | int _fdev_get(void *); // read a char or EOF 33 | 34 | The `void *` is for user data, which can be anything but is the 35 | same for both functions in a given FILE. This signature is compatible 36 | with C++ method calls where the first argument is the `this` pointer. 37 | 38 | To leverage the conversions from string to floating-point required by 39 | the `scanf` family, implementations of `strtod`, `strtof`, and `atof` 40 | have also been included. 41 | 42 | ### Math Requirements 43 | This library can be compiled into several different flavors depending 44 | on the level of math support needed. At the low end is support for integers 45 | up to size `long`, and no floating-point. At the other end is support 46 | for `long long` integers and both `float` and `double` math. 47 | 48 | Support is set separately for integer and floating-point math. The 49 | options are defined in `stdio_private.h`: 50 | 51 | // Values for INT_MATH_LEVEL 52 | #define INT_MATH_MIN 0 53 | #define INT_MATH_LONG_LONG 1 54 | 55 | // Values for FP_MATH_LEVEL 56 | #define FP_MATH_NONE 0 57 | #define FP_MATH_FLT 1 58 | #define FP_MATH_DBL 2 59 | #define FP_MATH_FLT_DBL 3 60 | 61 | It is expected that the values for `INT_MATH_LEVEL` and `FP_MATH_LEVEL` 62 | would be set on the compiler command line, but they can also be set 63 | by editting the file. 64 | 65 | Note that `FP_MATH_FLT_DBL` only applies to the `scanf` family. `scanf` 66 | can potentially be asked to read in a `float` and/or a `double`, so the 67 | math for whichever will be used must be present. 68 | 69 | The `printf` family all use variadic arguments, which means that `float` 70 | is promoted to `double`. This library supports a non-stardard hack 71 | in which only `float` support is present. In order to pass a `float` 72 | without promoting it to `double`, the bits are passed in a 32-bit 73 | integer. This can be done using a union of `float` and `int32_t`. 74 | The compiler may produce a warning that the type passed 75 | does not match the type specified by the format string. 76 | 77 | ### Platforms 78 | The code is written entirely in C (with an optimization for ARM of 79 | a single line of inline assmbly language). It has been shown to work 80 | on 32-bit and 64-bit Windows as well as ARM Cortex-M0+. It is 81 | intended that it can be compiled for any platform with a standard 82 | C compiler. Optimizations originally present for the 8-bit AVR have 83 | generally been removed. 84 | 85 | This project was developed in Microchip Studio targeting ARM Cortex-M0+ 86 | microcontrollers. Fully compiled and ready-to-link libraries in 87 | every combination of math support are included for this target. 88 | The Microchip Studio project files are also included. 89 | -------------------------------------------------------------------------------- /Test Drivers/ArmTest/ArmTest.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ArmTest.c 3 | * 4 | * Created: 4/29/2020 4:56:44 PM 5 | * Author : Tim 6 | */ 7 | 8 | 9 | #include "sam.h" 10 | #include "sam_spec.h" 11 | #include "..\..\libc\include\stdio.h" 12 | #include 13 | 14 | 15 | #define VERSION 1 16 | #define F_CPU 48000000 17 | #define BAUD_RATE 500000 18 | 19 | //********************************************************************* 20 | // Initialization Helpers 21 | //********************************************************************* 22 | 23 | enum RxPad 24 | { 25 | RXPAD_Pad0, 26 | RXPAD_Pad1, 27 | RXPAD_Pad2, 28 | RXPAD_Pad3 29 | }; 30 | 31 | enum TxPad 32 | { 33 | TXPAD_Pad0, 34 | TXPAD_Pad2, 35 | TXPAD_Pad0_RTS_Pad2_CTS_Pad3, 36 | TXPAD_Pad0_TE_Pad2 37 | }; 38 | 39 | //********************************************************************* 40 | 41 | void StartClock() 42 | { 43 | // Two wait states needed for 48MHz operation 44 | NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_RWS(2) | NVMCTRL_CTRLB_MANW; 45 | 46 | // Initialize 48MHz clock 47 | OSCCTRL->CAL48M.reg = NVM_SOFTWARE_CAL->CAL48M_3V3; 48 | OSCCTRL->OSC48MDIV.reg = 0; // Bump it to 48 MHz 49 | } 50 | 51 | //********************************************************************* 52 | 53 | uint16_t CalcBaudRate(uint32_t rate, uint32_t clock) 54 | { 55 | uint32_t quo; 56 | uint32_t quoBit; 57 | 58 | rate *= 16; // actual clock frequency 59 | // Need 17-bit result of rate / clock 60 | for (quo = 0, quoBit = 1 << 16; quoBit != 0; quoBit >>= 1) 61 | { 62 | if (rate >= clock) 63 | { 64 | rate -= clock; 65 | quo |= quoBit; 66 | } 67 | rate <<= 1; 68 | } 69 | // Round 70 | if (rate >= clock) 71 | quo++; 72 | return (uint16_t)-quo; 73 | } 74 | 75 | //********************************************************************* 76 | 77 | void Init() 78 | { 79 | SERCOM_USART_CTRLA_Type serCtrlA; 80 | 81 | // Enable clock 82 | MCLK->APBCMASK.reg |= MCLK_APBCMASK_SERCOM3; 83 | 84 | // Clock it with GCLK0 85 | GCLK->PCHCTRL[SERCOM3_GCLK_ID_CORE].reg = GCLK_PCHCTRL_GEN_GCLK0 | 86 | GCLK_PCHCTRL_CHEN; 87 | 88 | PORT->Group[0].WRCONFIG.reg = 89 | PORT_WRCONFIG_WRPMUX | 90 | PORT_WRCONFIG_PMUX(MUX_PA24C_SERCOM3_PAD2) | 91 | PORT_WRCONFIG_INEN | 92 | PORT_WRCONFIG_PMUXEN | 93 | PORT_WRCONFIG_WRPINCFG | 94 | PORT_WRCONFIG_HWSEL | 95 | PORT_WRCONFIG_PINMASK((PORT_PA24 | PORT_PA25) >> 16); 96 | 97 | SERCOM3->USART.BAUD.reg = CalcBaudRate(BAUD_RATE, F_CPU); 98 | 99 | // standard 8,N,1 parameters 100 | serCtrlA.reg = 0; 101 | serCtrlA.bit.DORD = 1; // LSB first 102 | serCtrlA.bit.MODE = 1; // internal clock 103 | serCtrlA.bit.RXPO = RXPAD_Pad3; 104 | serCtrlA.bit.TXPO = TXPAD_Pad2; 105 | serCtrlA.bit.ENABLE = 1; 106 | SERCOM3->USART.CTRLA.reg = serCtrlA.reg; 107 | SERCOM3->USART.CTRLB.reg = SERCOM_USART_CTRLB_TXEN | SERCOM_USART_CTRLB_RXEN; 108 | } 109 | 110 | //********************************************************************* 111 | // File I/O 112 | //********************************************************************* 113 | 114 | void WriteByte(void *pv, char c) 115 | { 116 | while (!SERCOM3->USART.INTFLAG.bit.DRE); 117 | SERCOM3->USART.DATA.reg = c; 118 | } 119 | 120 | int ReadByte(void *pv) 121 | { 122 | while (!SERCOM3->USART.INTFLAG.bit.RXC); 123 | return SERCOM3->USART.DATA.reg; 124 | } 125 | 126 | FILE SercomIo = FDEV_SETUP_STREAM(WriteByte, ReadByte, _FDEV_SETUP_RW | _FDEV_SETUP_CRLF); 127 | 128 | FDEV_STANDARD_STREAMS(&SercomIo, &SercomIo); // stdout, stdin 129 | 130 | //********************************************************************* 131 | // Main program 132 | //********************************************************************* 133 | 134 | int main(void) 135 | { 136 | float flt; 137 | double dbl; 138 | uint64_t ull; 139 | 140 | StartClock(); 141 | Init(); 142 | 143 | // Test runtime initialization too 144 | fdev_setup_stream(&SercomIo, WriteByte, ReadByte, _FDEV_SETUP_RW | _FDEV_SETUP_CRLF); 145 | 146 | printf("\nStarting version %i\n", VERSION); 147 | strtod("12.34E-1", NULL); 148 | strtof("12.34E-1", NULL); 149 | printf("%g\n", atof("-123456789E-52")); 150 | 151 | while (1) 152 | { 153 | printf("Enter 64-bit hex value: "); 154 | scanf("%llx", &ull); 155 | printf("\n%llx %llu\n", ull, ull); 156 | 157 | printf("Enter float value: "); 158 | scanf("%f", &flt); 159 | //printf("\n%g\n", PASS_FLOAT(flt)); 160 | printf("%g\n", flt); 161 | 162 | printf("Enter double value: "); 163 | scanf("%lf", &dbl); 164 | printf("\n%g\n", dbl); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /Test Drivers/ArmTest/ArmTest.componentinfo.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | CMSIS 8 | CORE 9 | 10 | 11 | ARM 12 | 5.1.2 13 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs 14 | 15 | 16 | 17 | 18 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\arm\CMSIS\5.4.0\CMSIS\Documentation\Core\html\index.html 19 | 20 | doc 21 | 22 | 23 | 24 | CMSIS/Documentation/Core/html/index.html 25 | 26 | 27 | 28 | 29 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\arm\CMSIS\5.4.0\CMSIS\Core\Include\ 30 | 31 | include 32 | 33 | 34 | 35 | CMSIS/Core/Include/ 36 | 37 | 38 | 39 | 40 | CMSIS 41 | C:/Program Files (x86)/Atmel/Studio/7.0/Packs/arm/CMSIS/5.4.0/ARM.CMSIS.pdsc 42 | 5.4.0 43 | true 44 | ARMv6_7_8-M Device 45 | 46 | 47 | 48 | Resolved 49 | Fixed 50 | true 51 | 52 | 53 | 54 | 55 | Device 56 | Startup 57 | 58 | 59 | Atmel 60 | 1.2.0 61 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs 62 | 63 | 64 | 65 | 66 | 67 | 68 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\include 69 | 70 | include 71 | C 72 | 73 | 74 | samc21/include 75 | 76 | 77 | 78 | 79 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\include\sam.h 80 | 81 | header 82 | C 83 | 2Rk75rgdSdFbgPkrL7+Wow== 84 | 85 | samc21/include/sam.h 86 | 87 | 88 | 89 | 90 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\templates\main.c 91 | template 92 | source 93 | C Exe 94 | o0ncidL5gR0Z0YwWJ1YroQ== 95 | 96 | samc21/templates/main.c 97 | Main file (.c) 98 | 99 | 100 | 101 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\templates\main.cpp 102 | template 103 | source 104 | C Exe 105 | nU+WlKcYaWh0AWBBS+WVpA== 106 | 107 | samc21/templates/main.cpp 108 | Main file (.cpp) 109 | 110 | 111 | 112 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\gcc\system_samc21.c 113 | config 114 | source 115 | GCC Exe 116 | iZApSPlKT5sFShfs2MMw3A== 117 | 118 | samc21/gcc/system_samc21.c 119 | 120 | 121 | 122 | 123 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\gcc\gcc\startup_samc21.c 124 | config 125 | source 126 | GCC Exe 127 | bpsG10hjzf37y3bP6vwBHA== 128 | 129 | samc21/gcc/gcc/startup_samc21.c 130 | 131 | 132 | 133 | 134 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\gcc\gcc\samc21g17a_flash.ld 135 | config 136 | linkerScript 137 | GCC Exe 138 | WiHfOU6baRjGUUBGgLlc/g== 139 | 140 | samc21/gcc/gcc/samc21g17a_flash.ld 141 | 142 | 143 | 144 | 145 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\gcc\gcc\samc21g17a_sram.ld 146 | config 147 | other 148 | GCC Exe 149 | F0FsbY4kYDaKHxTuui42iQ== 150 | 151 | samc21/gcc/gcc/samc21g17a_sram.ld 152 | 153 | 154 | 155 | 156 | SAMC21_DFP 157 | C:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/SAMC21_DFP/1.2.176/Atmel.SAMC21_DFP.pdsc 158 | 1.2.176 159 | true 160 | ATSAMC21G17A 161 | 162 | 163 | 164 | Resolved 165 | Fixed 166 | true 167 | 168 | 169 | -------------------------------------------------------------------------------- /Test Drivers/ArmTest/Device_Startup/samc21g17a_flash.ld: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief Linker script for running in internal FLASH on the SAMC21G17A 5 | * 6 | * Copyright (c) 2018 Microchip Technology Inc. 7 | * 8 | * \asf_license_start 9 | * 10 | * \page License 11 | * 12 | * SPDX-License-Identifier: Apache-2.0 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 15 | * not use this file except in compliance with the License. 16 | * You may obtain a copy of the Licence at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | * \asf_license_stop 27 | * 28 | */ 29 | 30 | 31 | OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") 32 | OUTPUT_ARCH(arm) 33 | SEARCH_DIR(.) 34 | 35 | /* Memory Spaces Definitions */ 36 | MEMORY 37 | { 38 | rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00020000 39 | ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00004000 40 | } 41 | 42 | /* The stack size used by the application. NOTE: you need to adjust according to your application. */ 43 | STACK_SIZE = DEFINED(STACK_SIZE) ? STACK_SIZE : DEFINED(__stack_size__) ? __stack_size__ : 0x1000; 44 | 45 | /* Section Definitions */ 46 | SECTIONS 47 | { 48 | .text : 49 | { 50 | . = ALIGN(4); 51 | _sfixed = .; 52 | KEEP(*(.vectors .vectors.*)) 53 | *(.text .text.* .gnu.linkonce.t.*) 54 | *(.glue_7t) *(.glue_7) 55 | *(.rodata .rodata* .gnu.linkonce.r.*) 56 | *(.ARM.extab* .gnu.linkonce.armextab.*) 57 | 58 | /* Support C constructors, and C destructors in both user code 59 | and the C library. This also provides support for C++ code. */ 60 | . = ALIGN(4); 61 | KEEP(*(.init)) 62 | . = ALIGN(4); 63 | __preinit_array_start = .; 64 | KEEP (*(.preinit_array)) 65 | __preinit_array_end = .; 66 | 67 | . = ALIGN(4); 68 | __init_array_start = .; 69 | KEEP (*(SORT(.init_array.*))) 70 | KEEP (*(.init_array)) 71 | __init_array_end = .; 72 | 73 | . = ALIGN(4); 74 | KEEP (*crtbegin.o(.ctors)) 75 | KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) 76 | KEEP (*(SORT(.ctors.*))) 77 | KEEP (*crtend.o(.ctors)) 78 | 79 | . = ALIGN(4); 80 | KEEP(*(.fini)) 81 | 82 | . = ALIGN(4); 83 | __fini_array_start = .; 84 | KEEP (*(.fini_array)) 85 | KEEP (*(SORT(.fini_array.*))) 86 | __fini_array_end = .; 87 | 88 | KEEP (*crtbegin.o(.dtors)) 89 | KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) 90 | KEEP (*(SORT(.dtors.*))) 91 | KEEP (*crtend.o(.dtors)) 92 | 93 | . = ALIGN(4); 94 | _efixed = .; /* End of text section */ 95 | } > rom 96 | 97 | /* .ARM.exidx is sorted, so has to go in its own output section. */ 98 | PROVIDE_HIDDEN (__exidx_start = .); 99 | .ARM.exidx : 100 | { 101 | *(.ARM.exidx* .gnu.linkonce.armexidx.*) 102 | } > rom 103 | PROVIDE_HIDDEN (__exidx_end = .); 104 | 105 | . = ALIGN(4); 106 | _etext = .; 107 | 108 | .relocate : AT (_etext) 109 | { 110 | . = ALIGN(4); 111 | _srelocate = .; 112 | *(.ramfunc .ramfunc.*); 113 | *(.data .data.*); 114 | . = ALIGN(4); 115 | _erelocate = .; 116 | } > ram 117 | 118 | /* .bss section which is used for uninitialized data */ 119 | .bss (NOLOAD) : 120 | { 121 | . = ALIGN(4); 122 | _sbss = . ; 123 | _szero = .; 124 | *(.bss .bss.*) 125 | *(COMMON) 126 | . = ALIGN(4); 127 | _ebss = . ; 128 | _ezero = .; 129 | } > ram 130 | 131 | /* stack section */ 132 | .stack (NOLOAD): 133 | { 134 | . = ALIGN(8); 135 | _sstack = .; 136 | . = . + STACK_SIZE; 137 | . = ALIGN(8); 138 | _estack = .; 139 | } > ram 140 | 141 | . = ALIGN(4); 142 | _end = . ; 143 | } 144 | -------------------------------------------------------------------------------- /Test Drivers/ArmTest/Device_Startup/startup_samc21.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief gcc starttup file for SAMC21 5 | * 6 | * Copyright (c) 2018 Microchip Technology Inc. 7 | * 8 | * \asf_license_start 9 | * 10 | * \page License 11 | * 12 | * SPDX-License-Identifier: Apache-2.0 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 15 | * not use this file except in compliance with the License. 16 | * You may obtain a copy of the Licence at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | * 26 | * \asf_license_stop 27 | * 28 | */ 29 | 30 | #include "samc21.h" 31 | 32 | /* Initialize segments */ 33 | extern uint32_t _sfixed; 34 | extern uint32_t _efixed; 35 | extern uint32_t _etext; 36 | extern uint32_t _srelocate; 37 | extern uint32_t _erelocate; 38 | extern uint32_t _szero; 39 | extern uint32_t _ezero; 40 | extern uint32_t _sstack; 41 | extern uint32_t _estack; 42 | 43 | /** \cond DOXYGEN_SHOULD_SKIP_THIS */ 44 | int main(void); 45 | /** \endcond */ 46 | 47 | void __libc_init_array(void); 48 | 49 | /* Default empty handler */ 50 | void Dummy_Handler(void); 51 | 52 | /* Cortex-M0+ core handlers */ 53 | void NonMaskableInt_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 54 | void HardFault_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 55 | void SVCall_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 56 | void PendSV_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 57 | void SysTick_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 58 | 59 | /* Peripherals handlers */ 60 | void SYSTEM_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); /* MCLK, OSC32KCTRL, OSCCTRL, PAC, PM, SUPC, TAL */ 61 | void WDT_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 62 | void RTC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 63 | void EIC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 64 | void FREQM_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 65 | #ifdef ID_TSENS 66 | void TSENS_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 67 | #endif 68 | void NVMCTRL_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 69 | void DMAC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 70 | void EVSYS_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 71 | void SERCOM0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 72 | void SERCOM1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 73 | void SERCOM2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 74 | void SERCOM3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 75 | #ifdef ID_SERCOM4 76 | void SERCOM4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 77 | #endif 78 | #ifdef ID_SERCOM5 79 | void SERCOM5_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 80 | #endif 81 | #ifdef ID_CAN0 82 | void CAN0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 83 | #endif 84 | #ifdef ID_CAN1 85 | void CAN1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 86 | #endif 87 | void TCC0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 88 | #ifdef ID_TCC1 89 | void TCC1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 90 | #endif 91 | #ifdef ID_TCC2 92 | void TCC2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 93 | #endif 94 | void TC0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 95 | void TC1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 96 | void TC2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 97 | void TC3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 98 | void TC4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 99 | #ifdef ID_ADC0 100 | void ADC0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 101 | #endif 102 | #ifdef ID_ADC1 103 | void ADC1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 104 | #endif 105 | #ifdef ID_AC 106 | void AC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 107 | #endif 108 | #ifdef ID_DAC 109 | void DAC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 110 | #endif 111 | #ifdef ID_SDADC 112 | void SDADC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 113 | #endif 114 | #ifdef ID_PTC 115 | void PTC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); 116 | #endif 117 | 118 | /* Exception Table */ 119 | __attribute__ ((section(".vectors"))) 120 | const DeviceVectors exception_table = { 121 | 122 | /* Configure Initial Stack Pointer, using linker-generated symbols */ 123 | .pvStack = (void*) (&_estack), 124 | 125 | .pfnReset_Handler = (void*) Reset_Handler, 126 | .pfnNonMaskableInt_Handler = (void*) NonMaskableInt_Handler, 127 | .pfnHardFault_Handler = (void*) HardFault_Handler, 128 | .pvReservedM12 = (void*) (0UL), /* Reserved */ 129 | .pvReservedM11 = (void*) (0UL), /* Reserved */ 130 | .pvReservedM10 = (void*) (0UL), /* Reserved */ 131 | .pvReservedM9 = (void*) (0UL), /* Reserved */ 132 | .pvReservedM8 = (void*) (0UL), /* Reserved */ 133 | .pvReservedM7 = (void*) (0UL), /* Reserved */ 134 | .pvReservedM6 = (void*) (0UL), /* Reserved */ 135 | .pfnSVCall_Handler = (void*) SVCall_Handler, 136 | .pvReservedM4 = (void*) (0UL), /* Reserved */ 137 | .pvReservedM3 = (void*) (0UL), /* Reserved */ 138 | .pfnPendSV_Handler = (void*) PendSV_Handler, 139 | .pfnSysTick_Handler = (void*) SysTick_Handler, 140 | 141 | /* Configurable interrupts */ 142 | .pfnSYSTEM_Handler = (void*) SYSTEM_Handler, /* 0 Main Clock, 32k Oscillators Control, Oscillators Control, Peripheral Access Controller, Power Manager, Supply Controller, Trigger Allocator */ 143 | .pfnWDT_Handler = (void*) WDT_Handler, /* 1 Watchdog Timer */ 144 | .pfnRTC_Handler = (void*) RTC_Handler, /* 2 Real-Time Counter */ 145 | .pfnEIC_Handler = (void*) EIC_Handler, /* 3 External Interrupt Controller */ 146 | .pfnFREQM_Handler = (void*) FREQM_Handler, /* 4 Frequency Meter */ 147 | #ifdef ID_TSENS 148 | .pfnTSENS_Handler = (void*) TSENS_Handler, /* 5 Temperature Sensor */ 149 | #else 150 | .pvReserved5 = (void*) (0UL), /* 5 Reserved */ 151 | #endif 152 | .pfnNVMCTRL_Handler = (void*) NVMCTRL_Handler, /* 6 Non-Volatile Memory Controller */ 153 | .pfnDMAC_Handler = (void*) DMAC_Handler, /* 7 Direct Memory Access Controller */ 154 | .pfnEVSYS_Handler = (void*) EVSYS_Handler, /* 8 Event System Interface */ 155 | .pfnSERCOM0_Handler = (void*) SERCOM0_Handler, /* 9 Serial Communication Interface 0 */ 156 | .pfnSERCOM1_Handler = (void*) SERCOM1_Handler, /* 10 Serial Communication Interface 1 */ 157 | .pfnSERCOM2_Handler = (void*) SERCOM2_Handler, /* 11 Serial Communication Interface 2 */ 158 | .pfnSERCOM3_Handler = (void*) SERCOM3_Handler, /* 12 Serial Communication Interface 3 */ 159 | #ifdef ID_SERCOM4 160 | .pfnSERCOM4_Handler = (void*) SERCOM4_Handler, /* 13 Serial Communication Interface 4 */ 161 | #else 162 | .pvReserved13 = (void*) (0UL), /* 13 Reserved */ 163 | #endif 164 | #ifdef ID_SERCOM5 165 | .pfnSERCOM5_Handler = (void*) SERCOM5_Handler, /* 14 Serial Communication Interface 5 */ 166 | #else 167 | .pvReserved14 = (void*) (0UL), /* 14 Reserved */ 168 | #endif 169 | #ifdef ID_CAN0 170 | .pfnCAN0_Handler = (void*) CAN0_Handler, /* 15 Control Area Network 0 */ 171 | #else 172 | .pvReserved15 = (void*) (0UL), /* 15 Reserved */ 173 | #endif 174 | #ifdef ID_CAN1 175 | .pfnCAN1_Handler = (void*) CAN1_Handler, /* 16 Control Area Network 1 */ 176 | #else 177 | .pvReserved16 = (void*) (0UL), /* 16 Reserved */ 178 | #endif 179 | .pfnTCC0_Handler = (void*) TCC0_Handler, /* 17 Timer Counter Control 0 */ 180 | #ifdef ID_TCC1 181 | .pfnTCC1_Handler = (void*) TCC1_Handler, /* 18 Timer Counter Control 1 */ 182 | #else 183 | .pvReserved18 = (void*) (0UL), /* 18 Reserved */ 184 | #endif 185 | #ifdef ID_TCC2 186 | .pfnTCC2_Handler = (void*) TCC2_Handler, /* 19 Timer Counter Control 2 */ 187 | #else 188 | .pvReserved19 = (void*) (0UL), /* 19 Reserved */ 189 | #endif 190 | .pfnTC0_Handler = (void*) TC0_Handler, /* 20 Basic Timer Counter 0 */ 191 | .pfnTC1_Handler = (void*) TC1_Handler, /* 21 Basic Timer Counter 1 */ 192 | .pfnTC2_Handler = (void*) TC2_Handler, /* 22 Basic Timer Counter 2 */ 193 | .pfnTC3_Handler = (void*) TC3_Handler, /* 23 Basic Timer Counter 3 */ 194 | .pfnTC4_Handler = (void*) TC4_Handler, /* 24 Basic Timer Counter 4 */ 195 | #ifdef ID_ADC0 196 | .pfnADC0_Handler = (void*) ADC0_Handler, /* 25 Analog Digital Converter 0 */ 197 | #else 198 | .pvReserved25 = (void*) (0UL), /* 25 Reserved */ 199 | #endif 200 | #ifdef ID_ADC1 201 | .pfnADC1_Handler = (void*) ADC1_Handler, /* 26 Analog Digital Converter 1 */ 202 | #else 203 | .pvReserved26 = (void*) (0UL), /* 26 Reserved */ 204 | #endif 205 | #ifdef ID_AC 206 | .pfnAC_Handler = (void*) AC_Handler, /* 27 Analog Comparators */ 207 | #else 208 | .pvReserved27 = (void*) (0UL), /* 27 Reserved */ 209 | #endif 210 | #ifdef ID_DAC 211 | .pfnDAC_Handler = (void*) DAC_Handler, /* 28 Digital Analog Converter */ 212 | #else 213 | .pvReserved28 = (void*) (0UL), /* 28 Reserved */ 214 | #endif 215 | #ifdef ID_SDADC 216 | .pfnSDADC_Handler = (void*) SDADC_Handler, /* 29 Sigma-Delta Analog Digital Converter */ 217 | #else 218 | .pvReserved29 = (void*) (0UL), /* 29 Reserved */ 219 | #endif 220 | #ifdef ID_PTC 221 | .pfnPTC_Handler = (void*) PTC_Handler /* 30 Peripheral Touch Controller */ 222 | #else 223 | .pvReserved30 = (void*) (0UL) /* 30 Reserved */ 224 | #endif 225 | }; 226 | 227 | /** 228 | * \brief This is the code that gets called on processor reset. 229 | * To initialize the device, and call the main() routine. 230 | */ 231 | void Reset_Handler(void) 232 | { 233 | uint32_t *pSrc, *pDest; 234 | 235 | /* Initialize the relocate segment */ 236 | pSrc = &_etext; 237 | pDest = &_srelocate; 238 | 239 | if (pSrc != pDest) { 240 | for (; pDest < &_erelocate;) { 241 | *pDest++ = *pSrc++; 242 | } 243 | } 244 | 245 | /* Clear the zero segment */ 246 | for (pDest = &_szero; pDest < &_ezero;) { 247 | *pDest++ = 0; 248 | } 249 | 250 | /* Set the vector table base address */ 251 | pSrc = (uint32_t *) & _sfixed; 252 | SCB->VTOR = ((uint32_t) pSrc & SCB_VTOR_TBLOFF_Msk); 253 | 254 | /* Initialize the C library */ 255 | __libc_init_array(); 256 | 257 | /* Branch to main function */ 258 | main(); 259 | 260 | /* Infinite loop */ 261 | while (1); 262 | } 263 | 264 | /** 265 | * \brief Default interrupt handler for unused IRQs. 266 | */ 267 | void Dummy_Handler(void) 268 | { 269 | while (1) { 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /Test Drivers/ArmTest/sam_spec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sam_spec.h 3 | * 4 | * Created: 8/18/2018 12:13:08 PM 5 | * Author: Tim 6 | */ 7 | 8 | #pragma once 9 | 10 | #if defined(_SYSTEM_SAMC20_H_INCLUDED_) 11 | 12 | #define __SAMC20__ 13 | 14 | #elif defined(_SYSTEM_SAMC21_H_INCLUDED_) 15 | 16 | #define __SAMC21__ 17 | 18 | #elif defined(_SYSTEM_SAMD10_H_INCLUDED_) 19 | 20 | #define __SAMD10__ 21 | 22 | #elif defined(_SYSTEM_SAMD11_H_INCLUDED_) 23 | 24 | #define __SAMD11__ 25 | 26 | #elif defined(_SYSTEM_SAMD20_H_INCLUDED_) 27 | 28 | #define __SAMD20__ 29 | 30 | #elif defined(_SYSTEM_SAMD21_H_INCLUDED_) 31 | 32 | #define __SAMD21__ 33 | 34 | #endif 35 | 36 | //********************************************************************* 37 | // Define calibration row 38 | 39 | #if defined(__SAMC20__) || defined(__SAMC21__) 40 | 41 | typedef struct 42 | { 43 | uint64_t ADC0_BIASREFBUF:3; 44 | uint64_t ADC0_BIASCOMP:3; 45 | uint64_t ADC1_LINEARITY:3; 46 | uint64_t ADC1_BIAS:3; 47 | uint64_t OSC32K:7; 48 | uint64_t CAL48M_5V:22; 49 | uint64_t CAL48M_3V3:22; 50 | } NvmSofwareCal_t; 51 | 52 | #define NVM_SOFTWARE_CAL ((NvmSofwareCal_t *)NVMCTRL_OTP5) 53 | 54 | #elif defined(__SAMD10__) || defined(__SAMD11__) || defined(__SAMD20__) || defined(__SAMD21__) 55 | 56 | struct NvmSofwareCal_t 57 | { 58 | uint64_t :27; 59 | uint64_t ADC_LINEARITY:8; 60 | uint64_t ADC_BIAS:3; 61 | uint64_t OSC32K:7; 62 | 63 | // USB fields apply only to D21 64 | uint64_t USB_TRANSN:5; 65 | uint64_t USB_TRANSP:5; 66 | uint64_t USB_TRIM:3; 67 | 68 | uint64_t DFLL48M_COARSE:6; // not set in Rev C 69 | uint64_t DFLL48M_FINE:10; // not set in Rev C 70 | uint64_t :54; 71 | }; 72 | 73 | #define NVM_SOFTWARE_CAL ((NvmSofwareCal_t *)NVMCTRL_OTP4) 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /Test Drivers/WinTest/Driver.c: -------------------------------------------------------------------------------- 1 | 2 | #include "..\..\libc\stdio\stdio_private.h" 3 | 4 | char Buf[100]; 5 | 6 | char* TestFloat(char* pszFormat, float fVal) 7 | { 8 | union {uint32_t l; float f;} u; 9 | u.f = fVal; 10 | snprintf(Buf, sizeof(Buf), pszFormat, u.l, 'x', 't'); 11 | return Buf; 12 | } 13 | 14 | char* Test(char* pszFormat, ...) 15 | { 16 | va_list ap; 17 | 18 | va_start(ap, pszFormat); 19 | vsnprintf(Buf, sizeof(Buf), pszFormat, ap); 20 | va_end(ap); 21 | return Buf; 22 | } 23 | -------------------------------------------------------------------------------- /Test Drivers/WinTest/Print.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | static HANDLE s_hConsole; 4 | 5 | void print(const char *str) 6 | { 7 | WriteConsoleA(s_hConsole, str, (unsigned long)strlen(str), NULL, NULL); 8 | WriteConsoleA(s_hConsole, "\n", 1, NULL, NULL); 9 | } 10 | 11 | void InitConsole() 12 | { 13 | s_hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 14 | } -------------------------------------------------------------------------------- /Test Drivers/WinTest/WinTest.c: -------------------------------------------------------------------------------- 1 | // WinTest.c : This file contains the 'main' function. Program execution begins and ends there. 2 | // 3 | 4 | #include "..\..\libc\stdio\stdio_private.h" 5 | 6 | 7 | // In Print.c 8 | void InitConsole(); 9 | void print(const char* str); 10 | 11 | // In Driver.c 12 | char* TestFloat(char* pszFormat, float fVal); 13 | char* Test(char* pszFormat, ...); 14 | 15 | FDEV_STANDARD_STREAMS(NULL, NULL); 16 | 17 | 18 | int main(int argc, char** argv) 19 | { 20 | InitConsole(); 21 | print("Starting Tests\n"); 22 | print(Test("INT_MATH_LEVEL = %i", INT_MATH_LEVEL)); 23 | print(Test("FP_MATH_LEVEL = %i", FP_MATH_LEVEL)); 24 | 25 | print(Test("%g", strtod(" -512.34E-1 ", NULL))); 26 | print(Test("%g", strtof(" -612.34E-1 ", NULL))); 27 | print(Test("%g", atof(" -712.34E-1 "))); 28 | 29 | // Integer tests 30 | print("\nIntegers"); 31 | long l; 32 | int i; 33 | short s; 34 | sscanf("1234567890", "%li", &l); 35 | print(Test("%li", l)); 36 | sscanf("12345678", "%i", &i); 37 | print(Test("%i", i)); 38 | sscanf("12345", "%hi", &s); 39 | print(Test("%hi", s)); 40 | 41 | print(Test("%4i", 0)); 42 | print(Test("%i", 1234567890)); 43 | 44 | #if FP_MATH_LEVEL == FP_MATH_FLT 45 | float flt; 46 | print("\nSingle floating point"); 47 | print(TestFloat("%.2f next:%c%c", 0.999f)); 48 | print(TestFloat("%.2f next:%c%c", -0.999f)); 49 | 50 | sscanf(" -123.456E-2", "%f", &flt); 51 | print(TestFloat("%g", flt)); 52 | 53 | print(TestFloat("%G next:%c%c", 1.23456789E0f)); 54 | print(TestFloat("%G next:%c%c", 1.23456789E1f)); 55 | print(TestFloat("%G next:%c%c", 1.23456789E2f)); 56 | print(TestFloat("%G next:%c%c", 1.23456789E3f)); 57 | print(TestFloat("%G next:%c%c", 1.23456789E4f)); 58 | print(TestFloat("%G next:%c%c", 1.23456789E5f)); 59 | print(TestFloat("%G next:%c%c", 1.23456789E6f)); 60 | print(TestFloat("%G next:%c%c", 1.23456789E7f)); 61 | print(TestFloat("%G next:%c%c", 1.23456789E8f)); 62 | print(TestFloat("%G next:%c%c", 1.23456789E9f)); 63 | print(TestFloat("%G next:%c%c", 1.23456789E10f)); 64 | print(TestFloat("%G next:%c%c", 1.23456789E11f)); 65 | print(TestFloat("%G next:%c%c", 1.23456789E12f)); 66 | print(TestFloat("%G next:%c%c", 1.23456789E13f)); 67 | print(TestFloat("%G next:%c%c", 1.23456789E14f)); 68 | print(TestFloat("%G next:%c%c", 1.23456789E15f)); 69 | print(TestFloat("%G next:%c%c", 1.23456789E16f)); 70 | print(TestFloat("%G next:%c%c", 1.23456789E17f)); 71 | print(TestFloat("%G next:%c%c", 1.23456789E18f)); 72 | print(TestFloat("%G next:%c%c", 1.23456789E19f)); 73 | print(TestFloat("%G next:%c%c", 1.23456789E20f)); 74 | print(TestFloat("%G next:%c%c", 1.23456789E21f)); 75 | print(TestFloat("%G next:%c%c", 1.23456789E22f)); 76 | print(TestFloat("%G next:%c%c", 1.23456789E23f)); 77 | print(TestFloat("%G next:%c%c", 1.23456789E24f)); 78 | print(TestFloat("%G next:%c%c", 1.23456789E25f)); 79 | print(TestFloat("%G next:%c%c", 1.23456789E26f)); 80 | print(TestFloat("%G next:%c%c", 1.23456789E27f)); 81 | print(TestFloat("%G next:%c%c", 1.23456789E28f)); 82 | print(TestFloat("%G next:%c%c", 1.23456789E29f)); 83 | print(TestFloat("%G next:%c%c", 1.23456789E30f)); 84 | print(TestFloat("%G next:%c%c", 1.23456789E31f)); 85 | print(TestFloat("%G next:%c%c", 1.23456789E32f)); 86 | print(TestFloat("%G next:%c%c", 1.23456789E33f)); 87 | print(TestFloat("%G next:%c%c", 1.23456789E34f)); 88 | print(TestFloat("%G next:%c%c", 1.23456789E35f)); 89 | print(TestFloat("%G next:%c%c", 1.23456789E36f)); 90 | print(TestFloat("%G next:%c%c", 1.23456789E37f)); 91 | print(TestFloat("%G next:%c%c", 1.23456789E38f)); 92 | print(TestFloat("%G next:%c%c", (float)1.23456789E39)); 93 | 94 | print(TestFloat("%G next:%c%c", 1.23456789E-0f)); 95 | print(TestFloat("%G next:%c%c", 1.23456789E-1f)); 96 | print(TestFloat("%G next:%c%c", 1.23456789E-2f)); 97 | print(TestFloat("%G next:%c%c", 1.23456789E-3f)); 98 | print(TestFloat("%G next:%c%c", 1.23456789E-4f)); 99 | print(TestFloat("%G next:%c%c", 1.23456789E-5f)); 100 | print(TestFloat("%G next:%c%c", 1.23456789E-6f)); 101 | print(TestFloat("%G next:%c%c", 1.23456789E-7f)); 102 | print(TestFloat("%G next:%c%c", 1.23456789E-8f)); 103 | print(TestFloat("%G next:%c%c", 1.23456789E-9f)); 104 | print(TestFloat("%G next:%c%c", 1.23456789E-10f)); 105 | print(TestFloat("%G next:%c%c", 1.23456789E-11f)); 106 | print(TestFloat("%G next:%c%c", 1.23456789E-12f)); 107 | print(TestFloat("%G next:%c%c", 1.23456789E-13f)); 108 | print(TestFloat("%G next:%c%c", 1.23456789E-14f)); 109 | print(TestFloat("%G next:%c%c", 1.23456789E-15f)); 110 | print(TestFloat("%G next:%c%c", 1.23456789E-16f)); 111 | print(TestFloat("%G next:%c%c", 1.23456789E-17f)); 112 | print(TestFloat("%G next:%c%c", 1.23456789E-18f)); 113 | print(TestFloat("%G next:%c%c", 1.23456789E-19f)); 114 | print(TestFloat("%G next:%c%c", 1.23456789E-20f)); 115 | print(TestFloat("%G next:%c%c", 1.23456789E-21f)); 116 | print(TestFloat("%G next:%c%c", 1.23456789E-22f)); 117 | print(TestFloat("%G next:%c%c", 1.23456789E-23f)); 118 | print(TestFloat("%G next:%c%c", 1.23456789E-24f)); 119 | print(TestFloat("%G next:%c%c", 1.23456789E-25f)); 120 | print(TestFloat("%G next:%c%c", 1.23456789E-26f)); 121 | print(TestFloat("%G next:%c%c", 1.23456789E-27f)); 122 | print(TestFloat("%G next:%c%c", 1.23456789E-28f)); 123 | print(TestFloat("%G next:%c%c", 1.23456789E-29f)); 124 | print(TestFloat("%G next:%c%c", 1.23456789E-30f)); 125 | print(TestFloat("%G next:%c%c", 1.23456789E-31f)); 126 | print(TestFloat("%G next:%c%c", 1.23456789E-32f)); 127 | print(TestFloat("%G next:%c%c", 1.23456789E-33f)); 128 | print(TestFloat("%G next:%c%c", 1.23456789E-34f)); 129 | print(TestFloat("%G next:%c%c", 1.23456789E-35f)); 130 | print(TestFloat("%G next:%c%c", 1.23456789E-36f)); 131 | print(TestFloat("%G next:%c%c", 1.23456789E-37f)); 132 | print(TestFloat("%G next:%c%c", 1.23456789E-38f)); 133 | print(TestFloat("%G next:%c%c", 1.23456789E-39f)); 134 | print(TestFloat("%G next:%c%c", 1.23456789E-40f)); 135 | print(TestFloat("%G next:%c%c", 1.23456789E-41f)); 136 | print(TestFloat("%G next:%c%c", 1.23456789E-42f)); 137 | print(TestFloat("%G next:%c%c", 1.23456789E-43f)); 138 | print(TestFloat("%G next:%c%c", 1.23456789E-44f)); 139 | print(TestFloat("%G next:%c%c", 1.23456789E-45f)); 140 | print(TestFloat("%G next:%c%c", 1.23456789E-46f)); 141 | #endif 142 | 143 | #if FP_MATH_LEVEL >= FP_MATH_DBL 144 | double dbl; 145 | print("\nDouble floating point"); 146 | 147 | sscanf(" -123.456E-2", "%lf", &dbl); 148 | print(Test("%g", dbl)); 149 | 150 | print(Test("%G next:%c%c", 1.23456789E0, 'x', 't')); 151 | print(Test("%G next:%c%c", 1.23456789E1, 'x', 't')); 152 | print(Test("%G next:%c%c", 1.23456789E2, 'x', 't')); 153 | print(Test("%G next:%c%c", 1.23456789E3, 'x', 't')); 154 | print(Test("%G next:%c%c", 1.23456789E4, 'x', 't')); 155 | print(Test("%G next:%c%c", 1.23456789E5, 'x', 't')); 156 | print(Test("%G next:%c%c", 1.23456789E6, 'x', 't')); 157 | print(Test("%G next:%c%c", 1.23456789E7, 'x', 't')); 158 | print(Test("%G next:%c%c", 1.23456789E8, 'x', 't')); 159 | print(Test("%G next:%c%c", 1.23456789E9, 'x', 't')); 160 | print(Test("%G next:%c%c", 1.23456789E10, 'x', 't')); 161 | print(Test("%G next:%c%c", 1.23456789E11, 'x', 't')); 162 | print(Test("%G next:%c%c", 1.23456789E12, 'x', 't')); 163 | print(Test("%G next:%c%c", 1.23456789E13, 'x', 't')); 164 | print(Test("%G next:%c%c", 1.23456789E14, 'x', 't')); 165 | print(Test("%G next:%c%c", 1.23456789E15, 'x', 't')); 166 | print(Test("%G next:%c%c", 1.23456789E16, 'x', 't')); 167 | print(Test("%G next:%c%c", 1.23456789E17, 'x', 't')); 168 | print(Test("%G next:%c%c", 1.23456789E18, 'x', 't')); 169 | print(Test("%G next:%c%c", 1.23456789E19, 'x', 't')); 170 | print(Test("%G next:%c%c", 1.23456789E20, 'x', 't')); 171 | print(Test("%G next:%c%c", 1.23456789E21, 'x', 't')); 172 | print(Test("%G next:%c%c", 1.23456789E22, 'x', 't')); 173 | print(Test("%G next:%c%c", 1.23456789E23, 'x', 't')); 174 | print(Test("%G next:%c%c", 1.23456789E24, 'x', 't')); 175 | print(Test("%G next:%c%c", 1.23456789E25, 'x', 't')); 176 | print(Test("%G next:%c%c", 1.23456789E26, 'x', 't')); 177 | print(Test("%G next:%c%c", 1.23456789E27, 'x', 't')); 178 | print(Test("%G next:%c%c", 1.23456789E28, 'x', 't')); 179 | print(Test("%G next:%c%c", 1.23456789E29, 'x', 't')); 180 | print(Test("%G next:%c%c", 1.23456789E30, 'x', 't')); 181 | print(Test("%G next:%c%c", 1.23456789E31, 'x', 't')); 182 | print(Test("%G next:%c%c", 1.23456789E32, 'x', 't')); 183 | print(Test("%G next:%c%c", 1.23456789E33, 'x', 't')); 184 | print(Test("%G next:%c%c", 1.23456789E34, 'x', 't')); 185 | print(Test("%G next:%c%c", 1.23456789E62, 'x', 't')); 186 | print(Test("%G next:%c%c", 1.23456789E63, 'x', 't')); 187 | print(Test("%G next:%c%c", 1.23456789E64, 'x', 't')); 188 | print(Test("%G next:%c%c", 1.23456789E65, 'x', 't')); 189 | print(Test("%G next:%c%c", 1.23456789E66, 'x', 't')); 190 | print(Test("%G next:%c%c", 1.23456789E94, 'x', 't')); 191 | print(Test("%G next:%c%c", 1.23456789E95, 'x', 't')); 192 | print(Test("%G next:%c%c", 1.23456789E96, 'x', 't')); 193 | print(Test("%G next:%c%c", 1.23456789E97, 'x', 't')); 194 | print(Test("%G next:%c%c", 1.23456789E98, 'x', 't')); 195 | print(Test("%G next:%c%c", 1.23456789E126, 'x', 't')); 196 | print(Test("%G next:%c%c", 1.23456789E127, 'x', 't')); 197 | print(Test("%G next:%c%c", 1.23456789E128, 'x', 't')); 198 | print(Test("%G next:%c%c", 1.23456789E129, 'x', 't')); 199 | print(Test("%G next:%c%c", 1.23456789E130, 'x', 't')); 200 | print(Test("%G next:%c%c", 1.23456789E158, 'x', 't')); 201 | print(Test("%G next:%c%c", 1.23456789E159, 'x', 't')); 202 | print(Test("%G next:%c%c", 1.23456789E160, 'x', 't')); 203 | print(Test("%G next:%c%c", 1.23456789E161, 'x', 't')); 204 | print(Test("%G next:%c%c", 1.23456789E162, 'x', 't')); 205 | print(Test("%G next:%c%c", 1.23456789E190, 'x', 't')); 206 | print(Test("%G next:%c%c", 1.23456789E191, 'x', 't')); 207 | print(Test("%G next:%c%c", 1.23456789E192, 'x', 't')); 208 | print(Test("%G next:%c%c", 1.23456789E193, 'x', 't')); 209 | print(Test("%G next:%c%c", 1.23456789E194, 'x', 't')); 210 | print(Test("%G next:%c%c", 1.23456789E222, 'x', 't')); 211 | print(Test("%G next:%c%c", 1.23456789E223, 'x', 't')); 212 | print(Test("%G next:%c%c", 1.23456789E224, 'x', 't')); 213 | print(Test("%G next:%c%c", 1.23456789E225, 'x', 't')); 214 | print(Test("%G next:%c%c", 1.23456789E226, 'x', 't')); 215 | print(Test("%G next:%c%c", 1.23456789E254, 'x', 't')); 216 | print(Test("%G next:%c%c", 1.23456789E255, 'x', 't')); 217 | print(Test("%G next:%c%c", 1.23456789E256, 'x', 't')); 218 | print(Test("%G next:%c%c", 1.23456789E257, 'x', 't')); 219 | print(Test("%G next:%c%c", 1.23456789E258, 'x', 't')); 220 | print(Test("%G next:%c%c", 1.23456789E286, 'x', 't')); 221 | print(Test("%G next:%c%c", 1.23456789E287, 'x', 't')); 222 | print(Test("%G next:%c%c", 1.23456789E288, 'x', 't')); 223 | print(Test("%G next:%c%c", 1.23456789E289, 'x', 't')); 224 | print(Test("%G next:%c%c", 1.23456789E290, 'x', 't')); 225 | print(Test("%G next:%c%c", 1.23456789E305, 'x', 't')); 226 | print(Test("%G next:%c%c", 1.23456789E306, 'x', 't')); 227 | print(Test("%G next:%c%c", 1.23456789E307, 'x', 't')); 228 | print(Test("%G next:%c%c", 1.23456789E308, 'x', 't')); 229 | print(Test("%G next:%c%c", 1.23456789E308 * 10, 'x', 't')); 230 | 231 | print(Test("%G next:%c%c", 1.23456789E-0, 'x', 't')); 232 | print(Test("%G next:%c%c", 1.23456789E-1, 'x', 't')); 233 | print(Test("%G next:%c%c", 1.23456789E-2, 'x', 't')); 234 | print(Test("%G next:%c%c", 1.23456789E-3, 'x', 't')); 235 | print(Test("%G next:%c%c", 1.23456789E-4, 'x', 't')); 236 | print(Test("%G next:%c%c", 1.23456789E-5, 'x', 't')); 237 | print(Test("%G next:%c%c", 1.23456789E-6, 'x', 't')); 238 | print(Test("%G next:%c%c", 1.23456789E-7, 'x', 't')); 239 | print(Test("%G next:%c%c", 1.23456789E-8, 'x', 't')); 240 | print(Test("%G next:%c%c", 1.23456789E-9, 'x', 't')); 241 | print(Test("%G next:%c%c", 1.23456789E-10, 'x', 't')); 242 | print(Test("%G next:%c%c", 1.23456789E-11, 'x', 't')); 243 | print(Test("%G next:%c%c", 1.23456789E-12, 'x', 't')); 244 | print(Test("%G next:%c%c", 1.23456789E-13, 'x', 't')); 245 | print(Test("%G next:%c%c", 1.23456789E-14, 'x', 't')); 246 | print(Test("%G next:%c%c", 1.23456789E-15, 'x', 't')); 247 | print(Test("%G next:%c%c", 1.23456789E-16, 'x', 't')); 248 | print(Test("%G next:%c%c", 1.23456789E-17, 'x', 't')); 249 | print(Test("%G next:%c%c", 1.23456789E-18, 'x', 't')); 250 | print(Test("%G next:%c%c", 1.23456789E-19, 'x', 't')); 251 | print(Test("%G next:%c%c", 1.23456789E-20, 'x', 't')); 252 | print(Test("%G next:%c%c", 1.23456789E-21, 'x', 't')); 253 | print(Test("%G next:%c%c", 1.23456789E-22, 'x', 't')); 254 | print(Test("%G next:%c%c", 1.23456789E-23, 'x', 't')); 255 | print(Test("%G next:%c%c", 1.23456789E-24, 'x', 't')); 256 | print(Test("%G next:%c%c", 1.23456789E-25, 'x', 't')); 257 | print(Test("%G next:%c%c", 1.23456789E-26, 'x', 't')); 258 | print(Test("%G next:%c%c", 1.23456789E-27, 'x', 't')); 259 | print(Test("%G next:%c%c", 1.23456789E-28, 'x', 't')); 260 | print(Test("%G next:%c%c", 1.23456789E-29, 'x', 't')); 261 | print(Test("%G next:%c%c", 1.23456789E-30, 'x', 't')); 262 | print(Test("%G next:%c%c", 1.23456789E-31, 'x', 't')); 263 | print(Test("%G next:%c%c", 1.23456789E-32, 'x', 't')); 264 | print(Test("%G next:%c%c", 1.23456789E-33, 'x', 't')); 265 | print(Test("%G next:%c%c", 1.23456789E-34, 'x', 't')); 266 | print(Test("%G next:%c%c", 1.23456789E-62, 'x', 't')); 267 | print(Test("%G next:%c%c", 1.23456789E-63, 'x', 't')); 268 | print(Test("%G next:%c%c", 1.23456789E-64, 'x', 't')); 269 | print(Test("%G next:%c%c", 1.23456789E-65, 'x', 't')); 270 | print(Test("%G next:%c%c", 1.23456789E-66, 'x', 't')); 271 | print(Test("%G next:%c%c", 1.23456789E-94, 'x', 't')); 272 | print(Test("%G next:%c%c", 1.23456789E-95, 'x', 't')); 273 | print(Test("%G next:%c%c", 1.23456789E-96, 'x', 't')); 274 | print(Test("%G next:%c%c", 1.23456789E-97, 'x', 't')); 275 | print(Test("%G next:%c%c", 1.23456789E-98, 'x', 't')); 276 | print(Test("%G next:%c%c", 1.23456789E-126, 'x', 't')); 277 | print(Test("%G next:%c%c", 1.23456789E-127, 'x', 't')); 278 | print(Test("%G next:%c%c", 1.23456789E-128, 'x', 't')); 279 | print(Test("%G next:%c%c", 1.23456789E-129, 'x', 't')); 280 | print(Test("%G next:%c%c", 1.23456789E-130, 'x', 't')); 281 | print(Test("%G next:%c%c", 1.23456789E-158, 'x', 't')); 282 | print(Test("%G next:%c%c", 1.23456789E-159, 'x', 't')); 283 | print(Test("%G next:%c%c", 1.23456789E-160, 'x', 't')); 284 | print(Test("%G next:%c%c", 1.23456789E-161, 'x', 't')); 285 | print(Test("%G next:%c%c", 1.23456789E-162, 'x', 't')); 286 | print(Test("%G next:%c%c", 1.23456789E-190, 'x', 't')); 287 | print(Test("%G next:%c%c", 1.23456789E-191, 'x', 't')); 288 | print(Test("%G next:%c%c", 1.23456789E-192, 'x', 't')); 289 | print(Test("%G next:%c%c", 1.23456789E-193, 'x', 't')); 290 | print(Test("%G next:%c%c", 1.23456789E-194, 'x', 't')); 291 | print(Test("%G next:%c%c", 1.23456789E-222, 'x', 't')); 292 | print(Test("%G next:%c%c", 1.23456789E-223, 'x', 't')); 293 | print(Test("%G next:%c%c", 1.23456789E-224, 'x', 't')); 294 | print(Test("%G next:%c%c", 1.23456789E-225, 'x', 't')); 295 | print(Test("%G next:%c%c", 1.23456789E-226, 'x', 't')); 296 | print(Test("%G next:%c%c", 1.23456789E-254, 'x', 't')); 297 | print(Test("%G next:%c%c", 1.23456789E-255, 'x', 't')); 298 | print(Test("%G next:%c%c", 1.23456789E-256, 'x', 't')); 299 | print(Test("%G next:%c%c", 1.23456789E-257, 'x', 't')); 300 | print(Test("%G next:%c%c", 1.23456789E-258, 'x', 't')); 301 | print(Test("%G next:%c%c", 1.23456789E-286, 'x', 't')); 302 | print(Test("%G next:%c%c", 1.23456789E-287, 'x', 't')); 303 | print(Test("%G next:%c%c", 1.23456789E-288, 'x', 't')); 304 | print(Test("%G next:%c%c", 1.23456789E-289, 'x', 't')); 305 | print(Test("%G next:%c%c", 1.23456789E-290, 'x', 't')); 306 | print(Test("%G next:%c%c", 1.23456789E-305, 'x', 't')); 307 | print(Test("%G next:%c%c", 1.23456789E-306, 'x', 't')); 308 | print(Test("%G next:%c%c", 1.23456789E-307, 'x', 't')); 309 | print(Test("%G next:%c%c", 1.23456789E-308, 'x', 't')); 310 | print(Test("%G next:%c%c", 1.23456789E-309, 'x', 't')); 311 | print(Test("%G next:%c%c", 1.23456789E-310, 'x', 't')); 312 | print(Test("%G next:%c%c", 1.23456789E-311, 'x', 't')); 313 | print(Test("%G next:%c%c", 1.23456789E-312, 'x', 't')); 314 | print(Test("%G next:%c%c", 1.23456789E-313, 'x', 't')); 315 | print(Test("%G next:%c%c", 1.23456789E-314, 'x', 't')); 316 | print(Test("%G next:%c%c", 1.23456789E-315, 'x', 't')); 317 | print(Test("%G next:%c%c", 1.23456789E-316, 'x', 't')); 318 | print(Test("%G next:%c%c", 1.23456789E-317, 'x', 't')); 319 | print(Test("%G next:%c%c", 1.23456789E-318, 'x', 't')); 320 | print(Test("%G next:%c%c", 1.23456789E-319, 'x', 't')); 321 | print(Test("%G next:%c%c", 1.23456789E-320, 'x', 't')); 322 | print(Test("%G next:%c%c", 1.23456789E-321, 'x', 't')); 323 | print(Test("%G next:%c%c", 1.23456789E-322, 'x', 't')); 324 | print(Test("%G next:%c%c", 1.23456789E-323, 'x', 't')); 325 | print(Test("%G next:%c%c", 1.23456789E-324, 'x', 't')); 326 | #endif 327 | 328 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 329 | print("\n64-bit integers"); 330 | 331 | long long ll; 332 | sscanf("1234567890123456789", "%lli", &ll); 333 | print(Test("%lli", ll)); 334 | 335 | print(Test("%28lli next:%c%c", 1234567890123456789, 'x', 't')); 336 | print(Test("%28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 337 | print(Test("%28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 338 | print(Test("%28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 339 | print(Test("%28lli next:%c%c", -1234567890123456789, 'x', 't')); 340 | print(Test("%28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 341 | print(Test("%-28lli next:%c%c", 1234567890123456789, 'x', 't')); 342 | print(Test("%-28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 343 | print(Test("%-28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 344 | print(Test("%-28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 345 | print(Test("%-28lli next:%c%c", -1234567890123456789, 'x', 't')); 346 | print(Test("%-28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 347 | print(Test("% 28lli next:%c%c", 1234567890123456789, 'x', 't')); 348 | print(Test("% 28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 349 | print(Test("% 28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 350 | print(Test("% 28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 351 | print(Test("% 28lli next:%c%c", -1234567890123456789, 'x', 't')); 352 | print(Test("% 28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 353 | print(Test("%- 28lli next:%c%c", 1234567890123456789, 'x', 't')); 354 | print(Test("%- 28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 355 | print(Test("%- 28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 356 | print(Test("%- 28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 357 | print(Test("%- 28lli next:%c%c", -1234567890123456789, 'x', 't')); 358 | print(Test("%- 28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 359 | print(Test("% +28lli next:%c%c", 1234567890123456789, 'x', 't')); 360 | print(Test("% +28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 361 | print(Test("% +28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 362 | print(Test("% +28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 363 | print(Test("% +28lli next:%c%c", -1234567890123456789, 'x', 't')); 364 | print(Test("% +28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 365 | print(Test("%- +28lli next:%c%c", 1234567890123456789, 'x', 't')); 366 | print(Test("%- +28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 367 | print(Test("%- +28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 368 | print(Test("%- +28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 369 | print(Test("%- +28lli next:%c%c", -1234567890123456789, 'x', 't')); 370 | print(Test("%- +28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 371 | print(Test("%0+28lli next:%c%c", 1234567890123456789, 'x', 't')); 372 | print(Test("%0+28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 373 | print(Test("%0+28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 374 | print(Test("%0+28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 375 | print(Test("%0+28lli next:%c%c", -1234567890123456789, 'x', 't')); 376 | print(Test("%0+28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 377 | print(Test("%-0+28lli next:%c%c", 1234567890123456789, 'x', 't')); 378 | print(Test("%-0+28lli next:%c%c", 0x7FFFFFFFFFFFFFFF, 'x', 't')); 379 | print(Test("%-0+28lli next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 380 | print(Test("%-0+28llu next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 381 | print(Test("%-0+28lli next:%c%c", -1234567890123456789, 'x', 't')); 382 | print(Test("%-0+28lli next:%c%c", -0x7FFFFFFFFFFFFFFF, 'x', 't')); 383 | 384 | // Hex/octal/pointer 385 | print(Test("%28llo next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 386 | print(Test("%28llx next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 387 | print(Test("%28llX next:%c%c", 0xFFFFFFFFFFFFFFFF, 'x', 't')); 388 | print(Test("%28llo next:%c%c", 0x123456789ABCDEF0, 'x', 't')); 389 | print(Test("%28llo next:%c%c", 0x123456789ABCDEF, 'x', 't')); 390 | print(Test("%28llx next:%c%c", 0x123456789ABCDEF0, 'x', 't')); 391 | print(Test("%28llX next:%c%c", 0x123456789ABCDEF0, 'x', 't')); 392 | print(Test("%28llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 393 | print(Test("%28llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 394 | print(Test("%28llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 395 | print(Test("%28llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 396 | print(Test("%28p next:%c%c", &print, 'x', 't')); 397 | print(Test("%28P next:%c%c", &print, 'x', 't')); 398 | print(Test("%#28llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 399 | print(Test("%#28llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 400 | print(Test("%#28llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 401 | print(Test("%#28llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 402 | print(Test("%#28p next:%c%c", &print, 'x', 't')); 403 | print(Test("%#28P next:%c%c", &print, 'x', 't')); 404 | print(Test("%0#28llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 405 | print(Test("%0#28llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 406 | print(Test("%0#28llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 407 | print(Test("%0#28llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 408 | print(Test("%0#28p next:%c%c", &print, 'x', 't')); 409 | print(Test("%0#28P next:%c%c", &print, 'x', 't')); 410 | print(Test("%028llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 411 | print(Test("%028llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 412 | print(Test("%028llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 413 | print(Test("%028llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 414 | print(Test("%028p next:%c%c", &print, 'x', 't')); 415 | print(Test("%028P next:%c%c", &print, 'x', 't')); 416 | print(Test("%28.24llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 417 | print(Test("%28.24llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 418 | print(Test("%28.24llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 419 | print(Test("%28.24llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 420 | print(Test("%28.24p next:%c%c", &print, 'x', 't')); 421 | print(Test("%28.24P next:%c%c", &print, 'x', 't')); 422 | print(Test("%#28.24llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 423 | print(Test("%#28.24llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 424 | print(Test("%#28.24llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 425 | print(Test("%#28.24llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 426 | print(Test("%#28.24p next:%c%c", &print, 'x', 't')); 427 | print(Test("%#28.24P next:%c%c", &print, 'x', 't')); 428 | print(Test("%028.24llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 429 | print(Test("%028.24llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 430 | print(Test("%028.24llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 431 | print(Test("%028.24llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 432 | print(Test("%028.24p next:%c%c", &print, 'x', 't')); 433 | print(Test("%028.24P next:%c%c", &print, 'x', 't')); 434 | print(Test("%.24llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 435 | print(Test("%.24llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 436 | print(Test("%.24llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 437 | print(Test("%.24llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 438 | print(Test("%.24p next:%c%c", &print, 'x', 't')); 439 | print(Test("%.24P next:%c%c", &print, 'x', 't')); 440 | print(Test("%#.24llo next:%c%c", 0xABCDEF0123456789, 'x', 't')); 441 | print(Test("%#.24llo next:%c%c", 0xABCDEF012345678, 'x', 't')); 442 | print(Test("%#.24llx next:%c%c", 0xABCDEF0123456789, 'x', 't')); 443 | print(Test("%#.24llX next:%c%c", 0xABCDEF0123456789, 'x', 't')); 444 | print(Test("%#.24p next:%c%c", &print, 'x', 't')); 445 | print(Test("%#.24P next:%c%c", &print, 'x', 't')); 446 | print(Test("%#24llo next:%c%c", 0LL, 'x', 't')); 447 | print(Test("%#24llo next:%c%c", 0LL, 'x', 't')); 448 | print(Test("%#24llx next:%c%c", 0LL, 'x', 't')); 449 | print(Test("%#24llX next:%c%c", 0LL, 'x', 't')); 450 | print(Test("%#24p next:%c%c", NULL, 'x', 't')); 451 | print(Test("%#24P next:%c%c", NULL, 'x', 't')); 452 | 453 | print(Test("%*.24llo next:%c%c", 28, 0xABCDEF0123456789, 'x', 't')); 454 | print(Test("%*.24llo next:%c%c", 28, 0xABCDEF012345678, 'x', 't')); 455 | print(Test("%*.24llx next:%c%c", 28, 0xABCDEF0123456789, 'x', 't')); 456 | print(Test("%*.24llX next:%c%c", 28, 0xABCDEF0123456789, 'x', 't')); 457 | print(Test("%*.24p next:%c%c", 28, &print, 'x', 't')); 458 | print(Test("%*.24P next:%c%c", 28, &print, 'x', 't')); 459 | print(Test("%28.*llo next:%c%c", 24, 0xABCDEF0123456789, 'x', 't')); 460 | print(Test("%28.*llo next:%c%c", 24, 0xABCDEF012345678, 'x', 't')); 461 | print(Test("%28.*llx next:%c%c", 24, 0xABCDEF0123456789, 'x', 't')); 462 | print(Test("%28.*llX next:%c%c", 24, 0xABCDEF0123456789, 'x', 't')); 463 | print(Test("%28.*p next:%c%c", 24, &print, 'x', 't')); 464 | print(Test("%28.*P next:%c%c", 24, &print, 'x', 't')); 465 | print(Test("%*.*llo next:%c%c", 28, 24, 0xABCDEF0123456789, 'x', 't')); 466 | print(Test("%*.*llo next:%c%c", 28, 24, 0xABCDEF012345678, 'x', 't')); 467 | print(Test("%*.*llx next:%c%c", 28, 24, 0xABCDEF0123456789, 'x', 't')); 468 | print(Test("%*.*llX next:%c%c", 28, 24, 0xABCDEF0123456789, 'x', 't')); 469 | print(Test("%*.*p next:%c%c", 28, 24, &print, 'x', 't')); 470 | print(Test("%*.*P next:%c%c", 28, 24, &print, 'x', 't')); 471 | #endif 472 | } 473 | -------------------------------------------------------------------------------- /Test Drivers/WinTest/WinTest.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29209.62 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinTest", "WinTest.vcxproj", "{913CCF73-1D08-4CC5-89DF-5F1C199C5E88}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | stdio-l|x64 = stdio-l|x64 11 | stdio-l|x86 = stdio-l|x86 12 | stdio-l-flt|x64 = stdio-l-flt|x64 13 | stdio-l-flt|x86 = stdio-l-flt|x86 14 | stdio-ll|x64 = stdio-ll|x64 15 | stdio-ll|x86 = stdio-ll|x86 16 | stdio-ll-dbl|x64 = stdio-ll-dbl|x64 17 | stdio-ll-dbl|x86 = stdio-ll-dbl|x86 18 | stdio-ll-flt|x64 = stdio-ll-flt|x64 19 | stdio-ll-flt|x86 = stdio-ll-flt|x86 20 | stdio-ll-flt-dbl|x64 = stdio-ll-flt-dbl|x64 21 | stdio-ll-flt-dbl|x86 = stdio-ll-flt-dbl|x86 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l|x64.ActiveCfg = stdio-l|x64 25 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l|x64.Build.0 = stdio-l|x64 26 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l|x86.ActiveCfg = stdio-l|Win32 27 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l|x86.Build.0 = stdio-l|Win32 28 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l-flt|x64.ActiveCfg = stdio-l-flt|x64 29 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l-flt|x64.Build.0 = stdio-l-flt|x64 30 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l-flt|x86.ActiveCfg = stdio-l-flt|Win32 31 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-l-flt|x86.Build.0 = stdio-l-flt|Win32 32 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll|x64.ActiveCfg = stdio-ll|x64 33 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll|x64.Build.0 = stdio-ll|x64 34 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll|x86.ActiveCfg = stdio-ll|Win32 35 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll|x86.Build.0 = stdio-ll|Win32 36 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-dbl|x64.ActiveCfg = stdio-ll-dbl|x64 37 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-dbl|x64.Build.0 = stdio-ll-dbl|x64 38 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-dbl|x86.ActiveCfg = stdio-ll-dbl|Win32 39 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-dbl|x86.Build.0 = stdio-ll-dbl|Win32 40 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt|x64.ActiveCfg = stdio-ll-flt|x64 41 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt|x64.Build.0 = stdio-ll-flt|x64 42 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt|x86.ActiveCfg = stdio-ll-flt|Win32 43 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt|x86.Build.0 = stdio-ll-flt|Win32 44 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt-dbl|x64.ActiveCfg = stdio-ll-flt-dbl|x64 45 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt-dbl|x64.Build.0 = stdio-ll-flt-dbl|x64 46 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt-dbl|x86.ActiveCfg = stdio-ll-flt-dbl|Win32 47 | {913CCF73-1D08-4CC5-89DF-5F1C199C5E88}.stdio-ll-flt-dbl|x86.Build.0 = stdio-ll-flt-dbl|Win32 48 | EndGlobalSection 49 | GlobalSection(SolutionProperties) = preSolution 50 | HideSolutionNode = FALSE 51 | EndGlobalSection 52 | GlobalSection(ExtensibilityGlobals) = postSolution 53 | SolutionGuid = {92BFC825-733C-4007-A55A-AC4ADD51486E} 54 | EndGlobalSection 55 | EndGlobal 56 | -------------------------------------------------------------------------------- /Test Drivers/WinTest/WinTest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | Source Files 53 | 54 | 55 | Source Files 56 | 57 | 58 | Source Files 59 | 60 | 61 | Source Files 62 | 63 | 64 | Source Files 65 | 66 | 67 | Source Files 68 | 69 | 70 | Source Files 71 | 72 | 73 | Source Files 74 | 75 | 76 | Source Files 77 | 78 | 79 | Source Files 80 | 81 | 82 | Source Files 83 | 84 | 85 | Source Files 86 | 87 | 88 | Source Files 89 | 90 | 91 | Source Files 92 | 93 | 94 | Source Files 95 | 96 | 97 | Source Files 98 | 99 | 100 | Source Files 101 | 102 | 103 | Source Files 104 | 105 | 106 | Source Files 107 | 108 | 109 | Source Files 110 | 111 | 112 | Source Files 113 | 114 | 115 | Source Files 116 | 117 | 118 | Source Files 119 | 120 | 121 | Source Files 122 | 123 | 124 | Source Files 125 | 126 | 127 | Source Files 128 | 129 | 130 | Source Files 131 | 132 | 133 | Source Files 134 | 135 | 136 | Source Files 137 | 138 | 139 | 140 | 141 | Source Files 142 | 143 | 144 | Header Files 145 | 146 | 147 | Header Files 148 | 149 | 150 | Header Files 151 | 152 | 153 | Header Files 154 | 155 | 156 | -------------------------------------------------------------------------------- /Test Drivers/WinTest/math.h: -------------------------------------------------------------------------------- 1 | #define atof atof_ 2 | #include "C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\math.h" 3 | #undef atof 4 | -------------------------------------------------------------------------------- /arm-lib-out/libstdio-l-flt.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/arm-lib-out/libstdio-l-flt.a -------------------------------------------------------------------------------- /arm-lib-out/libstdio-l.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/arm-lib-out/libstdio-l.a -------------------------------------------------------------------------------- /arm-lib-out/libstdio-ll-dbl.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/arm-lib-out/libstdio-ll-dbl.a -------------------------------------------------------------------------------- /arm-lib-out/libstdio-ll-flt-dbl.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/arm-lib-out/libstdio-ll-flt-dbl.a -------------------------------------------------------------------------------- /arm-lib-out/libstdio-ll-flt.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/arm-lib-out/libstdio-ll-flt.a -------------------------------------------------------------------------------- /arm-lib-out/libstdio-ll.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/arm-lib-out/libstdio-ll.a -------------------------------------------------------------------------------- /doc/dox_footer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | 14 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /doc/dox_header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | $projectname: $title 10 | $title 11 | 12 | 13 | 14 | $treeview 15 | $search 16 | $mathjax 17 | 18 | $extrastylesheet 19 | 20 | 21 |
22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 38 | 39 | 40 | 41 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 |
33 |
$projectname 34 |  $projectnumber 35 |
36 |
$projectbrief
37 |
42 |
$projectbrief
43 |
$searchbox
54 |
55 | 56 | 57 | -------------------------------------------------------------------------------- /libc/include/stdio.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TimPaterson/stdio-mini/63f4f44e45c471ce73f2b4f2fc97542a9a2f63a6/libc/include/stdio.h -------------------------------------------------------------------------------- /libc/stdio/atof.c: -------------------------------------------------------------------------------- 1 | /* 2 | * atof.c 3 | * 4 | * Created: 5/27/2020 3:46:45 PM 5 | * Author: Tim 6 | */ 7 | 8 | 9 | #include "stdio_private.h" 10 | 11 | 12 | double atof(const char *psz) 13 | { 14 | return strtod(psz, NULL); 15 | } 16 | -------------------------------------------------------------------------------- /libc/stdio/clearerr.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2005 Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | #undef clearerr 35 | 36 | void clearerr(FILE *stream) 37 | { 38 | stream->flags &= ~(__SERR | __SEOF); 39 | } 40 | -------------------------------------------------------------------------------- /libc/stdio/convtoa.h: -------------------------------------------------------------------------------- 1 | /* 2 | * convtoa.h 3 | * 4 | * Created: 4/25/2020 11:15:35 AM 5 | * Author: Tim 6 | */ 7 | 8 | 9 | #ifndef CONVTOA_H_ 10 | #define CONVTOA_H_ 11 | 12 | int __ftoa(float val, char *buf, int prec, int maxdgs); 13 | int __dtoa(double val, char *buf, int prec, int maxdgs); 14 | 15 | /* '__ftoa_engine' return next flags (in buf[0]): */ 16 | #define FTOA_MINUS 1 17 | #define FTOA_ZERO 2 18 | #define FTOA_INF 4 19 | #define FTOA_NAN 8 20 | #define FTOA_CARRY 16 /* Carry was to master position. */ 21 | 22 | 23 | #endif /* CONVTOA_H_ */ -------------------------------------------------------------------------------- /libc/stdio/dtoa.c: -------------------------------------------------------------------------------- 1 | /* 2 | * dtoa.c 3 | * 4 | * Created: 5/22/2020 11:00:39 AM 5 | * Author: Tim 6 | */ 7 | 8 | #include 9 | #include "stdio_private.h" 10 | #include "convtoa.h" 11 | #include "strconv.h" 12 | 13 | 14 | //************************************************************************* 15 | 16 | // IEEE double 17 | #define EXP_BITS 11 18 | #define MANTISSA_BITS 52 19 | #define EXP_BIAS 1023 20 | #define MAX_EXP ((1 << EXP_BITS) - 1) 21 | #define SIGN_BIT (1LL << 63) 22 | 23 | typedef union 24 | { 25 | double d; 26 | int64_t ll; 27 | struct 28 | { 29 | uint64_t mant:MANTISSA_BITS; 30 | uint64_t exp:EXP_BITS; 31 | uint64_t sign:1; 32 | } bits; 33 | } double_struct; 34 | 35 | #define Log10_2 0.30102999566398119521 36 | #define Log10_2_shift 18 37 | #define Log10_2_mask ((1 << (Log10_2_shift + 1)) - 1) 38 | #define Exp2toExp10 (lround(Log10_2 * (double)(1 << Log10_2_shift))) 39 | 40 | //************************************************************************* 41 | /* 42 | int __dtoa(double val, char *buf, int prec, int maxdgs) 43 | 44 | Input: 45 | val - value to convert 46 | buf - output buffer address 47 | prec - precision: number of decimal digits is 'prec + 1' 48 | maxdgs - (0 if unused) precision restriction for "%f" specification 49 | 50 | Output: 51 | return - decimal exponent of first digit 52 | buf[0] - flags (FTOA_***) 53 | buf[1],... - decimal digits 54 | Number of digits: 55 | maxdgs == 0 ? prec+1 : aver(1, maxdgs+exp, prec+1) 56 | 57 | Notes: 58 | * Output string is not 0-terminated. For possibility of user's buffer 59 | usage in any case. 60 | * If used, 'maxdgs' is a number of digits for value with zero exponent. 61 | */ 62 | 63 | int __dtoa(double val, char* buf, int prec, int maxdgs) 64 | { 65 | int exp; 66 | int exp10; 67 | char digit; 68 | char flags; 69 | uint64_t mant; 70 | double_struct dbl; 71 | 72 | // Multiply the input by a power of 10 so that 0.04 <= val < 8. 73 | // The ideal would be 0.05 <= val < 10, but we can't be that precise 74 | // using only the binary exponent. This means the true binary 75 | // exponent will be -5 <= exp <= 2. 76 | // 77 | // By multiplying by log10(2), we can directly calculate the power 78 | // of 10. Specifically, 10^-(ceil((exp - 2) * log10(2))). 79 | // 80 | // We only use even powers of 10 (10^2, 10^4...) to cut table space 81 | // in half. This can result in an additional multiply by 10 to get 82 | // the first digit. But by that time it's an integer multiply, so 83 | // it's fast and without loss of precision. 84 | 85 | dbl.d = val; 86 | if ((dbl.ll & ~SIGN_BIT) == 0) 87 | { 88 | *buf++ = FTOA_ZERO; 89 | for (; prec > -2; prec--) 90 | *buf++ = '0'; 91 | return 0; 92 | } 93 | 94 | exp = (int)dbl.bits.exp; 95 | flags = dbl.bits.sign ? FTOA_MINUS : 0; 96 | if (exp == MAX_EXP) 97 | { 98 | // Infinity or NAN 99 | flags |= dbl.bits.mant == 0 ? FTOA_INF : FTOA_NAN; 100 | *buf = flags; 101 | return 0; 102 | } 103 | *buf++ = flags; 104 | 105 | exp -= EXP_BIAS + 2; 106 | // Adding Log10_2_mask below is what performs the ceil() function. 107 | // Shift by Log10_2_shift + 1 because we only use even powers of 10. 108 | exp10 = (((long)exp * Exp2toExp10) + Log10_2_mask) >> (Log10_2_shift + 1); 109 | dbl.d = __mulpower100d(dbl.d, -exp10); 110 | exp10 *= 2; // back to actual power of 10 111 | mant = dbl.bits.mant | (1LL << MANTISSA_BITS); 112 | exp = (int)dbl.bits.exp - EXP_BIAS; 113 | // Shift mantissa for actual binary exponent. Target range was 114 | // -5 <= exp <= 2, but it could be less if original number was 115 | // denormal. We add 5 so in normal case we only shift one way. 116 | // If exp were zero, the shift would put mantissa MSB at 117 | // MANTISSA_BITS + 5, and the bit would represent the 118 | // value 1. So our decimal digit will be formed from that bit 119 | // and the bits above it. 120 | #define DIGIT_SHIFT (MANTISSA_BITS + 5) 121 | exp += 5; // exp <= 2, so max of 7 122 | if (exp < 0) 123 | { 124 | // Handle denormal case. 125 | uint64_t lsb, rnd; 126 | 127 | lsb = 1LL << -exp; 128 | rnd = lsb >> 1; 129 | // If result LSB is zero (even) and all bits below rounding bits 130 | // are zero, skip round up. 131 | if ((mant & (lsb | (rnd - 1))) != 0) 132 | mant += rnd; 133 | mant >>= -exp; 134 | } 135 | else 136 | mant <<= exp; 137 | 138 | // Scan off leading zeros 139 | for (;;) 140 | { 141 | digit = mant >> DIGIT_SHIFT; 142 | if (digit != 0) 143 | break; 144 | 145 | mant *= 10; 146 | exp10--; 147 | } 148 | 149 | // Calculate the number of digits we want 150 | prec++; 151 | if (maxdgs == 0) 152 | maxdgs = prec; 153 | else 154 | { 155 | maxdgs += exp10; 156 | if (maxdgs < 1) 157 | maxdgs = 1; 158 | else if (maxdgs > prec) 159 | maxdgs = prec; 160 | } 161 | 162 | // pump out the digits 163 | prec = maxdgs; 164 | for (;;) 165 | { 166 | // upper four bits has digit 167 | digit += '0'; 168 | *buf = digit; 169 | mant &= (1LL << DIGIT_SHIFT) - 1; 170 | if (--maxdgs == 0) 171 | break; 172 | mant *= 10; 173 | digit = mant >> DIGIT_SHIFT; 174 | buf++; 175 | } 176 | 177 | // Round up if mantissa is > 0.5 or == 0.5 and digit is odd 178 | #define ROUND_VALUE (1LL << (DIGIT_SHIFT - 1)) 179 | if (mant > ROUND_VALUE || (mant == ROUND_VALUE && (digit & 1))) 180 | { 181 | // End with a zero in case we round from 9.9..9 to 10.0..0 182 | buf[1] = '0'; 183 | 184 | for (;;) 185 | { 186 | digit++; 187 | if (digit > '9') 188 | digit = '0'; 189 | *buf = digit; 190 | if (--prec == 0) 191 | { 192 | if (digit == '0') 193 | { 194 | *buf = '1'; 195 | exp10++; 196 | } 197 | buf[-1] |= FTOA_CARRY; 198 | break; 199 | } 200 | if (digit != '0') 201 | break; 202 | digit = *--buf; 203 | } 204 | } 205 | 206 | return exp10; 207 | } 208 | -------------------------------------------------------------------------------- /libc/stdio/feof.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2005 Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | #undef feof 35 | 36 | int feof(FILE *stream) 37 | { 38 | return stream->flags & __SEOF; 39 | } 40 | -------------------------------------------------------------------------------- /libc/stdio/ferror.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2005 Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | #undef ferror 35 | 36 | int ferror(FILE *stream) 37 | { 38 | return stream->flags & __SERR; 39 | } 40 | -------------------------------------------------------------------------------- /libc/stdio/fgetc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, 2005, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | int fgetc(FILE *stream) 35 | { 36 | int rv; 37 | 38 | if ((stream->flags & __SRD) == 0) 39 | return EOF; 40 | 41 | if ((stream->flags & __SUNGET) != 0) { 42 | stream->flags &= ~__SUNGET; 43 | stream->len++; 44 | return stream->unget; 45 | } 46 | 47 | if (stream->flags & __SSTR) { 48 | rv = *stream->u.mem.buf; 49 | if (rv == '\0') { 50 | stream->flags |= __SEOF; 51 | return EOF; 52 | } else { 53 | stream->u.mem.buf++; 54 | } 55 | } else { 56 | rv = stream->u.dev.get(stream->udata); 57 | if (rv < 0) { 58 | /* if != _FDEV_ERR, assume it's _FDEV_EOF */ 59 | stream->flags |= (rv == _FDEV_ERR)? __SERR: __SEOF; 60 | return EOF; 61 | } 62 | } 63 | 64 | stream->len++; 65 | return (unsigned char)rv; 66 | } 67 | 68 | -------------------------------------------------------------------------------- /libc/stdio/fgets.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | char * fgets(char *str, int size, FILE *stream) 35 | { 36 | char *cp; 37 | int c; 38 | 39 | if ((stream->flags & __SRD) == 0 || size <= 0) 40 | return NULL; 41 | 42 | size--; 43 | for (c = 0, cp = str; c != '\n' && size > 0; size--, cp++) { 44 | if ((c = getc(stream)) == EOF) 45 | return NULL; 46 | *cp = (char)c; 47 | } 48 | *cp = '\0'; 49 | 50 | return str; 51 | } 52 | -------------------------------------------------------------------------------- /libc/stdio/fprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int fprintf(FILE *stream, const char *fmt, ...) 36 | { 37 | va_list ap; 38 | int i; 39 | 40 | va_start(ap, fmt); 41 | i = vfprintf(stream, fmt, ap); 42 | va_end(ap); 43 | 44 | return i; 45 | } 46 | -------------------------------------------------------------------------------- /libc/stdio/fputc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | int fputc(int c, FILE *stream) 35 | { 36 | if ((stream->flags & __SWR) == 0) 37 | return EOF; 38 | 39 | // Turn LF into CRLF if flagged 40 | if ((stream->flags & __SCRLF ) && c == '\n') 41 | fputc('\r', stream); 42 | 43 | if (stream->flags & __SSTR) { 44 | if (stream->len < stream->u.mem.size) 45 | *stream->u.mem.buf++ = c; 46 | } else { 47 | stream->u.dev.put(stream->udata, c); 48 | } 49 | stream->len++; 50 | return c; 51 | } 52 | -------------------------------------------------------------------------------- /libc/stdio/fputs.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | int fputs(const char *str, FILE *stream) 35 | { 36 | char c; 37 | 38 | if ((stream->flags & __SWR) == 0) 39 | return EOF; 40 | 41 | while ((c = *str++) != '\0') 42 | fputc(c, stream); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /libc/stdio/fread.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 35 | { 36 | size_t i, j; 37 | uint8_t *cp; 38 | int c; 39 | 40 | if ((stream->flags & __SRD) == 0) 41 | return 0; 42 | 43 | for (i = 0, cp = (uint8_t *)ptr; i < nmemb; i++) 44 | for (j = 0; j < size; j++) { 45 | c = getc(stream); 46 | if (c == EOF) 47 | return i; 48 | *cp++ = (uint8_t)c; 49 | } 50 | 51 | return i; 52 | } 53 | -------------------------------------------------------------------------------- /libc/stdio/fscanf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int fscanf(FILE *stream, const char *fmt, ...) 36 | { 37 | va_list ap; 38 | int i; 39 | 40 | va_start(ap, fmt); 41 | i = vfscanf(stream, fmt, ap); 42 | va_end(ap); 43 | 44 | return i; 45 | } 46 | -------------------------------------------------------------------------------- /libc/stdio/ftoa.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ftoa.c 3 | * 4 | * Created: 4/24/2020 5:29:32 PM 5 | * Author: Tim 6 | */ 7 | 8 | #include 9 | #include "stdio_private.h" 10 | #include "convtoa.h" 11 | #include "strconv.h" 12 | 13 | 14 | //************************************************************************* 15 | 16 | // IEEE single 17 | #define EXP_BITS 8 18 | #define MANTISSA_BITS 23 19 | #define EXP_BIAS 127 20 | #define MAX_EXP ((1 << EXP_BITS) - 1) 21 | #define SIGN_BIT (1 << 31) 22 | 23 | typedef union 24 | { 25 | float f; 26 | int32_t l; 27 | struct 28 | { 29 | uint32_t mant:MANTISSA_BITS; 30 | uint32_t exp:EXP_BITS; 31 | uint32_t sign:1; 32 | } bits; 33 | } float_struct; 34 | 35 | #define Log10_2 0.30102999566398119521 36 | #define Log10_2_shift 18 37 | #define Log10_2_mask ((1 << (Log10_2_shift + 1)) - 1) 38 | #define Exp2toExp10 (lround(Log10_2 * (double)(1 << Log10_2_shift))) 39 | 40 | //************************************************************************* 41 | 42 | /* 43 | int __ftoa(float val, char *buf, int prec, int maxdgs) 44 | 45 | Input: 46 | val - value to convert 47 | buf - output buffer address 48 | prec - precision: number of decimal digits is 'prec + 1' 49 | maxdgs - (0 if unused) precision restriction for "%f" specification 50 | 51 | Output: 52 | return - decimal exponent of first digit 53 | buf[0] - flags (FTOA_***) 54 | buf[1],... - decimal digits 55 | Number of digits: 56 | maxdgs == 0 ? prec+1 : aver(1, maxdgs+exp, prec+1) 57 | 58 | Notes: 59 | * Output string is not 0-terminated. For possibility of user's buffer 60 | usage in any case. 61 | * If used, 'maxdgs' is a number of digits for value with zero exponent. 62 | */ 63 | 64 | int __ftoa(float val, char *buf, int prec, int maxdgs) 65 | { 66 | int exp; 67 | int exp10; 68 | char digit; 69 | char flags; 70 | uint32_t mant; 71 | float_struct flt; 72 | 73 | // Multiply the input by a power of 10 so that 0.04 <= val < 8. 74 | // The ideal would be 0.05 <= val < 10, but we can't be that precise 75 | // using only the binary exponent. This means the true binary 76 | // exponent will be -5 <= exp <= 2. 77 | // 78 | // By multiplying by log10(2), we can directly calculate the power 79 | // of 10. Specifically, 10^-(ceil((exp - 2) * log10(2))). 80 | // 81 | // We only use even powers of 10 (10^2, 10^4...) to cut table space 82 | // in half. This can result in an additional multiply by 10 to get 83 | // the first digit. But by that time it's an integer multiply, so 84 | // it's fast and without loss of precision. 85 | 86 | flt.f = val; 87 | if ((flt.l & ~SIGN_BIT) == 0) 88 | { 89 | *buf++ = FTOA_ZERO; 90 | for (; prec > -2; prec--) 91 | *buf++ = '0'; 92 | return 0; 93 | } 94 | 95 | exp = flt.bits.exp; 96 | flags = flt.bits.sign ? FTOA_MINUS : 0; 97 | if (exp == MAX_EXP) 98 | { 99 | // Infinity or NAN 100 | flags |= flt.bits.mant == 0 ? FTOA_INF : FTOA_NAN; 101 | *buf = flags; 102 | return 0; 103 | } 104 | *buf++ = flags; 105 | 106 | exp -= EXP_BIAS + 2; 107 | // Adding Log10_2_mask below is what performs the ceil() function. 108 | // Shift by Log10_2_shift + 1 because we only use even powers of 10. 109 | exp10 = (((long)exp * Exp2toExp10) + Log10_2_mask) >> (Log10_2_shift + 1); 110 | flt.f = __mulpower100f(flt.f, -exp10); 111 | exp10 *= 2; // back to actual power of 10 112 | mant = flt.bits.mant | (1 << MANTISSA_BITS); 113 | exp = flt.bits.exp - EXP_BIAS; 114 | // Shift mantissa for actual binary exponent. Target range was 115 | // -5 <= exp <= 2, but it could be less if original number was 116 | // denormal. We add 5 so in normal case we only shift one way. 117 | // If exp were zero, the shift would put mantissa MSB at 118 | // MANTISSA_BITS + 5, and the bit would represent the 119 | // value 1. So our decimal digit will be formed from that bit 120 | // and the bits above it. 121 | #define DIGIT_SHIFT (MANTISSA_BITS + 5) 122 | exp += 5; // exp <= 2, so max of 7 123 | if (exp < 0) 124 | { 125 | // Handle denormal case. 126 | uint32_t lsb, rnd; 127 | 128 | lsb = 1 << -exp; 129 | rnd = lsb >> 1; 130 | // If result LSB is zero (even) and all bits below rounding bits 131 | // are zero, skip round up. 132 | if ((mant & (lsb | (rnd - 1))) != 0) 133 | mant += rnd; 134 | mant >>= -exp; 135 | } 136 | else 137 | mant <<= exp; 138 | 139 | // Scan off leading zeros 140 | for(;;) 141 | { 142 | digit = mant >> DIGIT_SHIFT; 143 | if (digit != 0) 144 | break; 145 | 146 | mant *= 10; 147 | exp10--; 148 | } 149 | 150 | // Calculate the number of digits we want 151 | prec++; 152 | if (maxdgs == 0) 153 | maxdgs = prec; 154 | else 155 | { 156 | maxdgs += exp10; 157 | if (maxdgs < 1) 158 | maxdgs = 1; 159 | else if (maxdgs > prec) 160 | maxdgs = prec; 161 | } 162 | 163 | // pump out the digits 164 | prec = maxdgs; 165 | for(;;) 166 | { 167 | // upper four bits has digit 168 | digit += '0'; 169 | *buf = digit; 170 | mant &= (1 << DIGIT_SHIFT) - 1; 171 | if (--maxdgs == 0) 172 | break; 173 | mant *= 10; 174 | digit = mant >> DIGIT_SHIFT; 175 | buf++; 176 | } 177 | 178 | // Round up if mantissa is > 0.5 or == 0.5 and digit is odd 179 | #define ROUND_VALUE (1 << (DIGIT_SHIFT - 1)) 180 | if (mant > ROUND_VALUE || (mant == ROUND_VALUE && (digit & 1))) 181 | { 182 | // End with a zero in case we round from 9.9..9 to 10.0..0 183 | buf[1] = '0'; 184 | 185 | for (;;) 186 | { 187 | digit++; 188 | if (digit > '9') 189 | digit = '0'; 190 | *buf = digit; 191 | if (--prec == 0) 192 | { 193 | if (digit == '0') 194 | { 195 | *buf = '1'; 196 | exp10++; 197 | } 198 | buf[-1] |= FTOA_CARRY; 199 | break; 200 | } 201 | if (digit != '0') 202 | break; 203 | digit = *--buf; 204 | } 205 | } 206 | 207 | return exp10; 208 | } 209 | -------------------------------------------------------------------------------- /libc/stdio/fwrite.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 35 | { 36 | size_t i, j; 37 | const uint8_t *cp; 38 | 39 | if ((stream->flags & __SWR) == 0) 40 | return 0; 41 | 42 | for (i = 0, cp = (const uint8_t *)ptr; i < nmemb; i++) 43 | for (j = 0; j < size; j++) 44 | fputc(*cp++, stream); 45 | 46 | return i; 47 | } 48 | -------------------------------------------------------------------------------- /libc/stdio/getchar.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | #undef getchar 35 | 36 | int getchar(void) 37 | { 38 | return getc(stdin); 39 | } 40 | -------------------------------------------------------------------------------- /libc/stdio/gets.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | char *gets(char *str) 35 | { 36 | char *cp; 37 | int c; 38 | 39 | if ((stdin->flags & __SRD) == 0) 40 | return NULL; 41 | 42 | for (c = 0, cp = str; c != '\n'; cp++) { 43 | if ((c = getchar()) == EOF) 44 | return NULL; 45 | *cp = (char)c; 46 | } 47 | *--cp = '\0'; 48 | 49 | return str; 50 | } 51 | -------------------------------------------------------------------------------- /libc/stdio/mulpower100d.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mulpower10d.c 3 | * 4 | * Created: 5/25/2020 4:34:26 PM 5 | * Author: Tim 6 | */ 7 | 8 | #ifndef _countof 9 | #define _countof(array) (sizeof(array)/sizeof(array[0])) 10 | #endif 11 | 12 | static const double Power100tableSmall[] = 13 | { 14 | 1e+2, 1e+4, 1e+6, 1e+8, 1e+10, 1e+12, 1e+14, 1e+16, 15 | 1e+18, 1e+20, 1e+22, 1e+24, 1e+26, 1e+28, 1e+30 16 | }; 17 | 18 | static const double Power100tableLarge[] = 19 | { 20 | 1e-288, 1e-256, 1e-224, 1e-192, 1e-160, 1e-128, 1e-96, 1e-64, 1e-32, 21 | 1e+32, 1e+64, 1e+96, 1e+128, 1e+160, 1e+192, 1e+224, 1e+256, 1e+288 22 | }; 23 | 24 | #define SMALL_POWER_BITS 4 25 | #define SMALL_POWER_MASK ((1 << SMALL_POWER_BITS) - 1) 26 | #define MIN_TABLE_POWER -288 27 | #define MAX_TABLE_POWER 288 28 | #define LARGE_POWER_BASE ((MAX_TABLE_POWER >> SMALL_POWER_BITS) / 2) 29 | 30 | 31 | // Multiply by powers of 100. 32 | 33 | double __mulpower100d(double dbl, int power) 34 | { 35 | int expPart; 36 | 37 | if (power != 0) // skip 100^0 38 | { 39 | while (power <= MIN_TABLE_POWER/2) 40 | { 41 | dbl *= Power100tableLarge[0]; 42 | power -= MIN_TABLE_POWER/2; 43 | } 44 | 45 | while (power >= MAX_TABLE_POWER/2) 46 | { 47 | dbl *= Power100tableLarge[_countof(Power100tableLarge) - 1]; 48 | power -= MAX_TABLE_POWER/2; 49 | } 50 | 51 | expPart = power & SMALL_POWER_MASK; 52 | if (expPart != 0) 53 | dbl *= Power100tableSmall[expPart - 1]; 54 | 55 | expPart = power >> SMALL_POWER_BITS; 56 | if (expPart != 0) 57 | dbl *= Power100tableLarge[expPart + LARGE_POWER_BASE + (expPart < 0 ? 0 : -1)]; 58 | } 59 | 60 | return dbl; 61 | } 62 | -------------------------------------------------------------------------------- /libc/stdio/mulpower100f.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mulpower10f.c 3 | * 4 | * Created: 5/25/2020 3:18:52 PM 5 | * Author: Tim 6 | */ 7 | 8 | 9 | #include "strconv.h" 10 | 11 | #ifndef _countof 12 | #define _countof(array) (sizeof(array)/sizeof(array[0])) 13 | #endif 14 | 15 | 16 | static const float Power100table[] = 17 | { 18 | 1e-36f, 1e-34f, 1e-32f, 1e-30f, 1e-28f, 1e-26f, 1e-24f, 1e-22f, 19 | 1e-20f, 1e-18f, 1e-16f, 1e-14f, 1e-12f, 1e-10f, 1e-8f, 1e-6f, 1e-4f, 1e-2f, 20 | // 1e+0 skipped 21 | 1e+2f, 1e+4f, 1e+6f, 1e+8f, 1e+10f, 1e+12f, 1e+14f, 1e+16f, 1e+18f, 1e+20f, 22 | 1e+22f, 1e+24f, 1e+26f, 1e+28f, 1e+30f, 1e+32f, 1e+34f, 1e+36f, 1e+38f 23 | }; 24 | 25 | #define MIN_TABLE_POWER -36 26 | #define MAX_TABLE_POWER 38 27 | 28 | 29 | // Multiply by powers of 100. 30 | 31 | float __mulpower100f(float flt, int power) 32 | { 33 | if (power != 0) // skip 100^0 34 | { 35 | while (power < MIN_TABLE_POWER/2) 36 | { 37 | flt *= Power100table[0]; 38 | power -= MIN_TABLE_POWER/2; 39 | } 40 | 41 | while (power > MAX_TABLE_POWER/2) 42 | { 43 | flt *= Power100table[_countof(Power100table) - 1]; 44 | power -= MAX_TABLE_POWER/2; 45 | } 46 | 47 | flt *= Power100table[power - MIN_TABLE_POWER / 2 + (power < 0 ? 0 : -1)]; 48 | } 49 | return flt; 50 | } 51 | -------------------------------------------------------------------------------- /libc/stdio/printf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int printf(const char *fmt, ...) 36 | { 37 | va_list ap; 38 | int i; 39 | 40 | va_start(ap, fmt); 41 | i = vfprintf(stdout, fmt, ap); 42 | va_end(ap); 43 | 44 | return i; 45 | } 46 | -------------------------------------------------------------------------------- /libc/stdio/putchar.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | #undef putchar 35 | 36 | int putchar(int c) 37 | { 38 | return putc(c, stdout); 39 | } 40 | -------------------------------------------------------------------------------- /libc/stdio/puts.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | int puts(const char *str) 35 | { 36 | char c; 37 | 38 | if ((stdout->flags & __SWR) == 0) 39 | return EOF; 40 | 41 | while ((c = *str++) != '\0') 42 | fputc(c, stdout); 43 | 44 | fputc('\n', stdout); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /libc/stdio/scanf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int scanf(const char *fmt, ...) 36 | { 37 | va_list ap; 38 | int i; 39 | 40 | va_start(ap, fmt); 41 | i = vfscanf(stdin, fmt, ap); 42 | va_end(ap); 43 | 44 | return i; 45 | } 46 | -------------------------------------------------------------------------------- /libc/stdio/snprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include 34 | #include "stdio_private.h" 35 | 36 | int snprintf(char *s, size_t n, const char *fmt, ...) 37 | { 38 | va_list ap; 39 | FILE f; 40 | int i; 41 | 42 | f.flags = __SWR | __SSTR; 43 | f.u.mem.buf = s; 44 | /* Restrict max output length to INT_MAX, as snprintf() return 45 | signed int. The fputc() function uses a signed comparison 46 | between estimated len and f.u.mem.size field. So we can write a 47 | negative value into f.u.mem.size in the case of n was 0. Note, 48 | that f.u.mem.size will be a max number of nonzero symbols. */ 49 | if ((int)n < 0) 50 | n = (unsigned)INT_MAX + 1; 51 | f.u.mem.size = n - 1; /* -1,0,...INT_MAX */ 52 | 53 | va_start(ap, fmt); 54 | i = vfprintf(&f, fmt, ap); 55 | va_end(ap); 56 | 57 | if (f.u.mem.size >= 0) 58 | s[f.len < f.u.mem.size ? f.len : f.u.mem.size] = 0; 59 | 60 | return i; 61 | } 62 | -------------------------------------------------------------------------------- /libc/stdio/sprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include 34 | #include "stdio_private.h" 35 | 36 | int sprintf(char *s, const char *fmt, ...) 37 | { 38 | va_list ap; 39 | FILE f; 40 | int i; 41 | 42 | f.flags = __SWR | __SSTR; 43 | f.u.mem.buf = s; 44 | f.u.mem.size = INT_MAX; 45 | va_start(ap, fmt); 46 | i = vfprintf(&f, fmt, ap); 47 | va_end(ap); 48 | s[f.len] = 0; 49 | 50 | return i; 51 | } 52 | -------------------------------------------------------------------------------- /libc/stdio/sscanf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int sscanf(const char *s, const char *fmt, ...) 36 | { 37 | va_list ap; 38 | FILE f; 39 | int i; 40 | 41 | f.flags = __SRD | __SSTR; 42 | /* 43 | * It is OK to discard the "const" qualifier here. The buffer is 44 | * really only be read (by getc()), and as this our FILE f we 45 | * be discarded upon exiting sscanf(), nobody will ever get 46 | * a chance to get write access to it again. 47 | */ 48 | f.u.mem.buf = (char *)s; 49 | va_start(ap, fmt); 50 | i = vfscanf(&f, fmt, ap); 51 | va_end(ap); 52 | 53 | return i; 54 | } 55 | -------------------------------------------------------------------------------- /libc/stdio/stdio_private.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2005, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #ifndef __GNUC__ 33 | #define __extension__ 34 | #define __attribute__(...) 35 | #endif 36 | 37 | #include 38 | #include "..\include\stdio.h" 39 | 40 | // This library can be compiled with different levels of math support. 41 | // The minimum is level is long int. The following levels can be added: 42 | // 43 | // long long; float/double not required 44 | // float; long long not required (no double) 45 | // double; requires long long (no float) 46 | // both float & double (scanf family only; double for printf family) 47 | // 48 | // WARNING FOR FLOAT!!: Variadic functions like printf cannot be 49 | // directly passed a float; the compiler will automatically promote it 50 | // to double. Since the objective of using float is to avoid 51 | // pulling in the presumably larger runtime library for double, this 52 | // would defeat the purpose. Instructions for passing a float 53 | // are in stdio.h. 54 | // 55 | // Because of the overlap with integer and floating point, two 56 | // compile-time variables are used, with these possible values: 57 | 58 | // Values for INT_MATH_LEVEL 59 | #define INT_MATH_MIN 0 60 | #define INT_MATH_LONG_LONG 1 61 | 62 | // Values for FP_MATH_LEVEL 63 | #define FP_MATH_NONE 0 64 | #define FP_MATH_FLT 1 65 | #define FP_MATH_DBL 2 66 | #define FP_MATH_FLT_DBL 3 67 | 68 | // If not set on the compiler command line, set defaults here: 69 | #ifndef INT_MATH_LEVEL 70 | #define INT_MATH_LEVEL INT_MATH_MIN 71 | #endif 72 | 73 | #ifndef FP_MATH_LEVEL 74 | #define FP_MATH_LEVEL FP_MATH_FLT 75 | #endif 76 | 77 | // And double always includes long long 78 | #if FP_MATH_LEVEL >= FP_MATH_DBL 79 | #undef INT_MATH_LEVEL 80 | #define INT_MATH_LEVEL INT_MATH_LONG_LONG 81 | #endif 82 | 83 | // Bonus functions declared in stdlib.h 84 | extern double strtod(const char *psz, char **ppend); 85 | extern float strtof(const char *psz, char **ppend); 86 | extern double atof(const char *psz); 87 | -------------------------------------------------------------------------------- /libc/stdio/strconv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * strconv.h 3 | * 4 | * Created: 5/24/2020 3:31:06 PM 5 | * Author: Tim 6 | */ 7 | 8 | 9 | #ifndef STRCONV_H_ 10 | #define STRCONV_H_ 11 | 12 | #include 13 | #include "stdio_private.h" 14 | 15 | // "h" & "hh" means FL_SHORT is set if FL_CHAR is set, so test FL_CHAR first 16 | // "l" & "ll" means FL_LONG is set if FL_LL is set, so test FL_LL first 17 | #define FL_STAR 0x01 /* '*': skip assignment */ 18 | #define FL_WIDTH 0x02 /* width is present */ 19 | #define FL_LONG 0x04 /* 'long' type modifier */ 20 | #define FL_CHAR 0x08 /* 'char' type modifier */ 21 | #define FL_OCT 0x10 /* octal number */ 22 | #define FL_DEC 0x20 /* decimal number */ 23 | #define FL_HEX 0x40 /* hexadecimal number */ 24 | #define FL_MINUS 0x80 /* minus flag (field or value) */ 25 | #define FL_SHORT 0x100 // 'short' type modifier 26 | #define FL_LL 0x200 /* 'long long' type modifier */ 27 | 28 | // Flags as used in __conv_flt and __conv_dbl only 29 | #define FL_ERR 0x01 // input not valid 30 | #define FL_ANY 0x02 /* any digit was readed */ 31 | #define FL_OVFL 0x04 /* overflow was */ 32 | #define FL_DOT 0x08 /* decimal '.' was */ 33 | #define FL_MEXP 0x10 /* exponent 'e' is neg. */ 34 | #define FL_NAN 0x20 // "NAN" 35 | #define FL_INF 0x40 // "INF" or "INFINITY" 36 | #define FL_SIGN 0x100 // sign present, count in width 37 | 38 | int __begin_fp(FILE* stream, int width); 39 | int __skip_spaces(FILE* stream); 40 | bool __conv_flt(FILE* stream, int width, float* addr); 41 | bool __conv_dbl(FILE* stream, int width, double* addr); 42 | float __mulpower100f(float flt, int power); 43 | double __mulpower100d(double dbl, int power); 44 | 45 | #endif /* STRCONV_H_ */ -------------------------------------------------------------------------------- /libc/stdio/strconvd.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2004,2005 Joerg Wunsch 2 | Copyright (c) 2008 Dmitry Xmelkov 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | * Neither the name of the copyright holders nor the names of 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* $Id$ */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include "stdio_private.h" 39 | #include "strconv.h" 40 | 41 | 42 | bool __conv_dbl(FILE* stream, int width, double* addr) 43 | { 44 | unsigned long long ull; 45 | double dbl; 46 | int i; 47 | int exp; 48 | int flag; 49 | 50 | flag = __begin_fp(stream, width); 51 | if (flag & FL_ERR) 52 | return false; 53 | 54 | if (flag & FL_NAN) 55 | dbl = NAN; 56 | else if (flag & FL_INF) 57 | dbl = INFINITY; 58 | else 59 | { 60 | if (flag & FL_SIGN) 61 | width--; 62 | 63 | i = getc(stream); 64 | 65 | exp = 0; 66 | ull = 0; 67 | do 68 | { 69 | unsigned int c = i - '0'; 70 | 71 | if (c <= 9) 72 | { 73 | flag |= FL_ANY; 74 | if (flag & FL_OVFL) 75 | { 76 | if (!(flag & FL_DOT)) 77 | exp += 1; 78 | if (c != 0) 79 | ull |= 1; // sticky bit 80 | } 81 | else 82 | { 83 | if (flag & FL_DOT) 84 | exp -= 1; 85 | ull = ull * 10 + c; 86 | // Keep room for extra multiply by 10 if power is odd. 87 | // Still gives 58 bits, plenty for round & sticky bits. 88 | if (ull >= (ULLONG_MAX / 10 - 9) / 10) 89 | flag |= FL_OVFL; 90 | } 91 | } 92 | else if (c == ('.' - '0') && !(flag & FL_DOT)) 93 | { 94 | flag |= FL_DOT; 95 | } 96 | else 97 | break; 98 | 99 | } while (--width && (i = getc(stream)) >= 0); 100 | 101 | if (!(flag & FL_ANY)) 102 | return false; 103 | 104 | if ((unsigned char)i == 'e' || (unsigned char)i == 'E') 105 | { 106 | int expacc; 107 | 108 | if (!--width || (i = getc(stream)) < 0) 109 | return false; 110 | 111 | switch ((unsigned char)i) 112 | { 113 | case '-': 114 | flag |= FL_MEXP; 115 | /* FALLTHROUGH */ 116 | case '+': 117 | if (!--width) 118 | return false; 119 | i = getc(stream); /* test EOF will below */ 120 | } 121 | 122 | if (!isdigit(i)) 123 | return false; 124 | 125 | expacc = 0; 126 | do 127 | { 128 | expacc = expacc * 10 + i - '0'; 129 | } while (--width && isdigit(i = getc(stream))); 130 | if (flag & FL_MEXP) 131 | expacc = -expacc; 132 | exp += expacc; 133 | } 134 | 135 | if (width && i >= 0) 136 | ungetc(i, stream); 137 | 138 | // Can only multiply by even powers of 10 139 | if (exp & 1) 140 | ull *= 10; 141 | dbl = (double)ull; 142 | if (ull != 0) 143 | dbl = __mulpower100d(dbl, exp >> 1); 144 | } 145 | 146 | if (flag & FL_MINUS) 147 | dbl = -dbl; 148 | if (addr) 149 | *addr = dbl; 150 | return true; 151 | } 152 | -------------------------------------------------------------------------------- /libc/stdio/strconvf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2004,2005 Joerg Wunsch 2 | Copyright (c) 2008 Dmitry Xmelkov 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | * Neither the name of the copyright holders nor the names of 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* $Id$ */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include "stdio_private.h" 39 | #include "strconv.h" 40 | 41 | 42 | bool __conv_flt(FILE* stream, int width, float* addr) 43 | { 44 | unsigned long ul; 45 | float flt; 46 | int i; 47 | int exp; 48 | int flag; 49 | 50 | flag = __begin_fp(stream, width); 51 | if (flag & FL_ERR) 52 | return false; 53 | 54 | if (flag & FL_NAN) 55 | flt = NAN; 56 | else if (flag & FL_INF) 57 | flt = INFINITY; 58 | else 59 | { 60 | if (flag & FL_SIGN) 61 | width--; 62 | 63 | i = getc(stream); 64 | 65 | exp = 0; 66 | ul = 0; 67 | do 68 | { 69 | unsigned int c = i - '0'; 70 | 71 | if (c <= 9) 72 | { 73 | flag |= FL_ANY; 74 | if (flag & FL_OVFL) 75 | { 76 | if (!(flag & FL_DOT)) 77 | exp += 1; 78 | } 79 | else 80 | { 81 | if (flag & FL_DOT) 82 | exp -= 1; 83 | ul = ul * 10 + c; 84 | // Keep room for extra multiply by 10 if power is odd. 85 | if (ul >= (ULONG_MAX / 10 - 9) / 10) 86 | flag |= FL_OVFL; 87 | } 88 | } 89 | else if (c == ('.' - '0') && !(flag & FL_DOT)) 90 | { 91 | flag |= FL_DOT; 92 | } 93 | else 94 | break; 95 | 96 | } while (--width && (i = getc(stream)) >= 0); 97 | 98 | if (!(flag & FL_ANY)) 99 | return false; 100 | 101 | if ((unsigned char)i == 'e' || (unsigned char)i == 'E') 102 | { 103 | int expacc; 104 | 105 | if (!--width || (i = getc(stream)) < 0) 106 | return false; 107 | 108 | switch ((unsigned char)i) 109 | { 110 | case '-': 111 | flag |= FL_MEXP; 112 | /* FALLTHROUGH */ 113 | case '+': 114 | if (!--width) 115 | return false; 116 | i = getc(stream); /* test EOF will below */ 117 | } 118 | 119 | if (!isdigit(i)) 120 | return false; 121 | 122 | expacc = 0; 123 | do 124 | { 125 | expacc = expacc * 10 + i - '0'; 126 | } while (--width && isdigit(i = getc(stream))); 127 | if (flag & FL_MEXP) 128 | expacc = -expacc; 129 | exp += expacc; 130 | } 131 | 132 | if (width && i >= 0) 133 | ungetc(i, stream); 134 | 135 | // Can only multiply by even powers of 10 136 | if (exp & 1) 137 | ul *= 10; 138 | flt = (float)ul; 139 | if (ul != 0) 140 | flt = __mulpower100f(flt, exp >> 1); 141 | } 142 | 143 | if (flag & FL_MINUS) 144 | flt = -flt; 145 | if (addr) 146 | *addr = flt; 147 | return true; 148 | } 149 | -------------------------------------------------------------------------------- /libc/stdio/strconvhelp.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2004,2005 Joerg Wunsch 2 | Copyright (c) 2008 Dmitry Xmelkov 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | * Neither the name of the copyright holders nor the names of 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* $Id$ */ 34 | 35 | #include 36 | #include "stdio_private.h" 37 | #include "strconv.h" 38 | 39 | 40 | static const char pstr_nfinity[] = "nfinity"; 41 | static const char pstr_an[] = "an"; 42 | 43 | int __begin_fp(FILE* stream, int width) 44 | { 45 | const char* p; 46 | int flag; 47 | int ch; 48 | 49 | ch = getc(stream); 50 | 51 | flag = 0; 52 | switch (ch) 53 | { 54 | case '-': 55 | flag = FL_MINUS; 56 | /* FALLTHROUGH */ 57 | case '+': 58 | flag |= FL_SIGN; 59 | if (!--width || (ch = getc(stream)) < 0) 60 | return FL_ERR; 61 | } 62 | 63 | switch (tolower(ch)) 64 | { 65 | 66 | case 'n': 67 | p = pstr_an; 68 | goto operate_pstr; 69 | 70 | case 'i': 71 | p = pstr_nfinity; 72 | operate_pstr: 73 | { 74 | unsigned char c; 75 | 76 | while ((c = *p++) != 0) 77 | { 78 | if (!--width 79 | || (ch = getc(stream)) < 0 80 | || ((unsigned char)tolower(ch) != c 81 | && (ungetc(ch, stream), 1))) 82 | { 83 | if (p == pstr_nfinity + 3) // allow "inf" 84 | break; 85 | return FL_ERR; 86 | } 87 | } 88 | } 89 | flag |= (p == pstr_an + 3) ? FL_NAN : FL_INF; 90 | break; 91 | 92 | default: 93 | ungetc(ch, stream); 94 | } 95 | return flag; 96 | } 97 | 98 | int __skip_spaces(FILE* stream) 99 | { 100 | int i; 101 | do 102 | { 103 | if ((i = getc(stream)) < 0) 104 | return i; 105 | } while (isspace(i)); 106 | ungetc(i, stream); 107 | return i; 108 | } 109 | -------------------------------------------------------------------------------- /libc/stdio/strtod.c: -------------------------------------------------------------------------------- 1 | /* 2 | * strtod.c 3 | * 4 | * Created: 5/27/2020 11:59:20 AM 5 | * Author: Tim 6 | */ 7 | 8 | 9 | #include 10 | #include "strconv.h" 11 | 12 | 13 | double strtod(const char *psz, char **ppend) 14 | { 15 | FILE f; 16 | double dbl; 17 | char* pend; 18 | 19 | f.flags = __SRD | __SSTR; 20 | f.u.mem.buf = (char *)psz; 21 | 22 | if (__skip_spaces(&f) >= 0 && __conv_dbl(&f, INT_MAX, &dbl)) 23 | { 24 | pend = f.u.mem.buf - (f.flags & __SUNGET ? 1 : 0); 25 | } 26 | else 27 | { 28 | dbl = 0.0; 29 | pend = (char *)psz; 30 | } 31 | if (ppend != NULL) 32 | *ppend = pend; 33 | return dbl; 34 | } 35 | -------------------------------------------------------------------------------- /libc/stdio/strtof.c: -------------------------------------------------------------------------------- 1 | /* 2 | * strtof.c 3 | * 4 | * Created: 5/27/2020 3:33:46 PM 5 | * Author: Tim 6 | */ 7 | 8 | 9 | #include 10 | #include "strconv.h" 11 | 12 | 13 | float strtof(const char *psz, char **ppend) 14 | { 15 | FILE f; 16 | float flt; 17 | char* pend; 18 | 19 | f.flags = __SRD | __SSTR; 20 | f.u.mem.buf = (char *)psz; 21 | 22 | if (__skip_spaces(&f) >= 0 && __conv_flt(&f, INT_MAX, &flt)) 23 | { 24 | pend = f.u.mem.buf - (f.flags & __SUNGET ? 1 : 0); 25 | } 26 | else 27 | { 28 | flt = 0.0; 29 | pend = (char *)psz; 30 | } 31 | if (ppend != NULL) 32 | *ppend = pend; 33 | return flt; 34 | } 35 | -------------------------------------------------------------------------------- /libc/stdio/ungetc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include "stdio_private.h" 33 | 34 | int ungetc(int c, FILE *stream) 35 | { 36 | /* 37 | * Streams that are not readable, or streams that already had 38 | * had an ungetc() before will cause an error. 39 | * 40 | * ungetc(EOF, ...) causes an error per definition. 41 | */ 42 | if ((stream->flags & __SRD) == 0 || 43 | (stream->flags & __SUNGET) != 0 || 44 | c == EOF) 45 | return EOF; 46 | 47 | stream->unget = c; 48 | stream->flags |= __SUNGET; 49 | stream->flags &= ~__SEOF; 50 | stream->len--; 51 | 52 | return stream->unget; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /libc/stdio/vfprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Alexander Popov (sasho@vip.bg) 2 | Copyright (c) 2002,2004,2005 Joerg Wunsch 3 | Copyright (c) 2005, Helmut Wallner 4 | Copyright (c) 2007, Dmitry Xmelkov 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in 14 | the documentation and/or other materials provided with the 15 | distribution. 16 | * Neither the name of the copyright holders nor the names of 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* From: Id: printf_p_new.c,v 1.1.1.9 2002/10/15 20:10:28 joerg_wunsch Exp */ 34 | /* $Id$ */ 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include "stdio_private.h" 41 | #include "convtoa.h" 42 | 43 | /* 44 | * This file can be compiled into more than one flavor. 45 | * See stdio_private.h. 46 | */ 47 | 48 | #if INT_MATH_LEVEL == INT_MATH_MIN || INT_MATH_LEVEL == INT_MATH_LONG_LONG 49 | /* OK */ 50 | #else 51 | # error "Not a known integer math level." 52 | #endif 53 | 54 | #if FP_MATH_LEVEL == FP_MATH_NONE || FP_MATH_LEVEL == FP_MATH_FLT || FP_MATH_LEVEL == FP_MATH_DBL || FP_MATH_LEVEL == FP_MATH_FLT_DBL 55 | /* OK */ 56 | #else 57 | # error "Not a known floating-point math level." 58 | #endif 59 | 60 | /* -------------------------------------------------------------------- */ 61 | 62 | #ifdef __arm__ 63 | 64 | uint64_t __aeabi_uidivmod(uint32_t numerator, uint32_t denominator); 65 | uint64_t __aeabi_uldivmod(uint64_t numerator, uint64_t denominator); 66 | 67 | #define UL_DIVMOD(val, rem, base) \ 68 | do { \ 69 | unsigned long long ull = __aeabi_uidivmod(val, base); \ 70 | val = ull; \ 71 | rem = (int)(ull >> 32); \ 72 | } while(0) 73 | 74 | #define ULL_DIVMOD(val, rem, base) \ 75 | do { \ 76 | val = __aeabi_uldivmod(val, base); \ 77 | asm volatile \ 78 | ( "movs %[r],r2\n\t" \ 79 | : [r] "=r" (rem)); \ 80 | } while(0) 81 | 82 | #else 83 | 84 | #define UL_DIVMOD(val, rem, base) do { rem = (val % base); val /= base; } while(0) 85 | #define ULL_DIVMOD(val, rem, base) do { rem = (val % base); val /= base; } while(0) 86 | 87 | #endif 88 | 89 | /* -------------------------------------------------------------------- */ 90 | 91 | // Values for cap argument of __ultoa_rev 92 | #define CONV_UPPER ('A' - '9' - 1) 93 | #define CONV_LOWER ('a' - '9' - 1) 94 | 95 | static inline unsigned char* __ultoa_rev(unsigned long val, unsigned char* str, unsigned base, int cap) 96 | { 97 | unsigned ch; 98 | 99 | do 100 | { 101 | UL_DIVMOD(val, ch, base); 102 | if (ch >= 10) 103 | ch += cap; 104 | *str++ = ch + '0'; 105 | } while (val != 0); 106 | 107 | return str; 108 | } 109 | 110 | /* -------------------------------------------------------------------- */ 111 | #define FL_ZFILL 0x01 112 | #define FL_PLUS 0x02 113 | #define FL_SPACE 0x04 114 | #define FL_LPAD 0x08 115 | #define FL_ALT 0x10 116 | #define FL_PREC 0x20 117 | #define FL_H 0x40 118 | #define FL_LONG 0x80 119 | #define FL_LL 0x100 120 | #define FL_NEGATIVE 0x200 121 | 122 | #define FL_ALTUPP FL_PLUS 123 | #define FL_ALTHEX FL_SPACE 124 | 125 | #define FL_FLTUPP FL_ALT 126 | #define FL_FLTEXP FL_PREC 127 | #define FL_FLTFIX FL_LONG 128 | 129 | // Size of conversion buffer on stack 130 | #if INT_MATH_LEVEL == INT_MATH_MIN 131 | #define MAX_OCT_DIGITS ((sizeof(long) * 8 + 2) / 3) 132 | #else 133 | #define MAX_OCT_DIGITS ((sizeof(long long) * 8 + 2) / 3) 134 | #endif 135 | 136 | #if FP_MATH_LEVEL >= FP_MATH_DBL 137 | #define MAX_FP_DIGITS 17 138 | #define MIN_EXP_WIDTH 6 /* 1e+000 */ 139 | #else 140 | #define MAX_FP_DIGITS 7 141 | #define MIN_EXP_WIDTH 5 /* 1e+00 */ 142 | #endif 143 | 144 | #define BUF_SIZE (MAX_FP_DIGITS > MAX_OCT_DIGITS ? MAX_FP_DIGITS : MAX_OCT_DIGITS) 145 | 146 | int vfprintf(FILE * stream, const char* fmt, va_list ap) 147 | { 148 | unsigned char c; /* holds a char from the format string */ 149 | unsigned flags; 150 | int width; 151 | int prec; 152 | unsigned char buf[BUF_SIZE]; 153 | unsigned char *str; 154 | long x; 155 | bool fStar; 156 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 157 | long long ll; 158 | #endif 159 | 160 | stream->len = 0; 161 | 162 | if ((stream->flags & __SWR) == 0) 163 | return EOF; 164 | 165 | for (;;) 166 | { 167 | // Pass through non-format characters 168 | for (;;) 169 | { 170 | c = *fmt++; 171 | if (!c) goto ret; 172 | if (c == '%') 173 | { 174 | c = *fmt++; 175 | if (c != '%') break; 176 | } 177 | putc(c, stream); 178 | } 179 | 180 | flags = 0; 181 | width = 0; 182 | prec = 0; 183 | fStar = false; 184 | 185 | // Read format flags 186 | for (;; c = *fmt++) 187 | { 188 | switch (c) 189 | { 190 | case '0': 191 | flags |= FL_ZFILL; 192 | continue; 193 | case '+': 194 | flags |= FL_PLUS; 195 | /* FALLTHROUGH */ 196 | case ' ': 197 | flags |= FL_SPACE; 198 | continue; 199 | case '-': 200 | flags |= FL_LPAD; 201 | continue; 202 | case '#': 203 | flags |= FL_ALT; 204 | continue; 205 | } 206 | break; 207 | } 208 | 209 | // Read format width & precision 210 | do 211 | { 212 | if (c >= '0' && c <= '9') 213 | { 214 | if (fStar) 215 | goto ret; 216 | c -= '0'; 217 | if (flags & FL_PREC) 218 | { 219 | prec = 10 * prec + c; 220 | continue; 221 | } 222 | width = 10 * width + c; 223 | continue; 224 | } 225 | if (c == '.') 226 | { 227 | if (flags & FL_PREC) 228 | goto ret; 229 | fStar = 0; 230 | flags |= FL_PREC; 231 | continue; 232 | } 233 | if (c == '*') 234 | { 235 | if (fStar) 236 | goto ret; 237 | if (flags & FL_PREC) 238 | { 239 | if (prec != 0) 240 | goto ret; 241 | prec = va_arg(ap, int); 242 | } 243 | else 244 | { 245 | if (width != 0) 246 | goto ret; 247 | width = va_arg(ap, int); 248 | } 249 | fStar = true; 250 | continue; 251 | } 252 | break; 253 | } while ((c = *fmt++) != 0); 254 | 255 | // Read format length 256 | if (c == 'l') 257 | { 258 | flags |= FL_LONG; 259 | c = *fmt++; 260 | if (c == 'l') 261 | { 262 | flags |= FL_LL; 263 | c = *fmt++; 264 | } 265 | } 266 | else if (c == 'h') 267 | { 268 | flags |= FL_H; 269 | c = *fmt++; 270 | } 271 | 272 | /* Only a format character is valid. */ 273 | 274 | #if 'F' != 'E'+1 || 'G' != 'F'+1 || 'f' != 'e'+1 || 'g' != 'f'+1 275 | # error 276 | #endif 277 | 278 | #if FP_MATH_LEVEL > FP_MATH_NONE 279 | if (c >= 'E' && c <= 'G') 280 | { 281 | flags |= FL_FLTUPP; 282 | c += 'e' - 'E'; 283 | goto flt_oper; 284 | 285 | } 286 | else if (c >= 'e' && c <= 'g') 287 | { 288 | 289 | int exp; /* exponent of master decimal digit */ 290 | int n; 291 | unsigned char vtype; /* result of float value parse */ 292 | unsigned char sign; /* sign character (or 0) */ 293 | # define ndigs c /* only for this block, undef is below */ 294 | 295 | flags &= ~FL_FLTUPP; 296 | 297 | flt_oper: 298 | if (!(flags & FL_PREC)) 299 | prec = 6; 300 | flags &= ~(FL_FLTEXP | FL_FLTFIX); 301 | if (c == 'e') 302 | flags |= FL_FLTEXP; 303 | else if (c == 'f') 304 | flags |= FL_FLTFIX; 305 | else if (prec > 0) // c == 'g' 306 | prec -= 1; 307 | 308 | if (flags & FL_FLTFIX) 309 | { 310 | vtype = MAX_FP_DIGITS; /* 'prec' arg for 'ftoa_engine' */ 311 | ndigs = prec < 60 ? prec + 1 : 60; 312 | } 313 | else 314 | { 315 | if (prec > MAX_FP_DIGITS) 316 | prec = MAX_FP_DIGITS; 317 | vtype = prec; 318 | ndigs = 0; 319 | } 320 | #if FP_MATH_LEVEL == FP_MATH_FLT 321 | union { uint32_t l; float f; } u; 322 | u.l = va_arg(ap, uint32_t); 323 | exp = __ftoa(u.f, (char*)buf, vtype, ndigs); 324 | #else 325 | exp = __dtoa(va_arg(ap, double), (char*)buf, vtype, ndigs); 326 | #endif 327 | vtype = buf[0]; 328 | 329 | sign = 0; 330 | if ((vtype & FTOA_MINUS) && !(vtype & FTOA_NAN)) 331 | sign = '-'; 332 | else if (flags & FL_PLUS) 333 | sign = '+'; 334 | else if (flags & FL_SPACE) 335 | sign = ' '; 336 | 337 | if (vtype & (FTOA_NAN | FTOA_INF)) 338 | { 339 | const char* p; 340 | ndigs = sign ? 4 : 3; 341 | if (width > ndigs) 342 | { 343 | width -= ndigs; 344 | if (!(flags & FL_LPAD)) 345 | { 346 | do 347 | { 348 | putc(' ', stream); 349 | } while (--width); 350 | } 351 | } 352 | else 353 | { 354 | width = 0; 355 | } 356 | if (sign) 357 | putc(sign, stream); 358 | p = "inf"; 359 | if (vtype & FTOA_NAN) 360 | p = "nan"; 361 | # if ('I'-'i' != 'N'-'n') || ('I'-'i' != 'F'-'f') || ('I'-'i' != 'A'-'a') 362 | # error 363 | # endif 364 | while ((ndigs = *p) != 0) 365 | { 366 | if (flags & FL_FLTUPP) 367 | ndigs += 'I' - 'i'; 368 | putc(ndigs, stream); 369 | p++; 370 | } 371 | goto tail; 372 | } 373 | 374 | /* Output format adjustment, number of decimal digits in buf[] */ 375 | if (flags & FL_FLTFIX) 376 | { 377 | ndigs += exp; 378 | if ((vtype & FTOA_CARRY) && buf[1] == '1') 379 | ndigs -= 1; 380 | if ((signed char)ndigs < 1) 381 | ndigs = 1; 382 | else if (ndigs > MAX_FP_DIGITS + 1) 383 | ndigs = MAX_FP_DIGITS + 1; 384 | } 385 | else if (!(flags & FL_FLTEXP)) 386 | { /* 'g(G)' format */ 387 | if (exp <= prec && exp >= -4) 388 | flags |= FL_FLTFIX; 389 | while (prec && buf[1 + prec] == '0') 390 | prec--; 391 | if (flags & FL_FLTFIX) 392 | { 393 | ndigs = prec + 1; /* number of digits in buf */ 394 | prec = prec > exp 395 | ? prec - exp : 0; /* fractional part length */ 396 | } 397 | } 398 | 399 | /* Conversion result length, width := free space length */ 400 | if (flags & FL_FLTFIX) 401 | n = (exp > 0 ? exp + 1 : 1); 402 | else 403 | n = MIN_EXP_WIDTH; 404 | if (sign) n += 1; 405 | if (prec) n += prec + 1; 406 | width = width > n ? width - n : 0; 407 | 408 | /* Output before first digit */ 409 | if (!(flags & (FL_LPAD | FL_ZFILL))) 410 | { 411 | while (width) 412 | { 413 | putc(' ', stream); 414 | width--; 415 | } 416 | } 417 | if (sign) putc(sign, stream); 418 | if (!(flags & FL_LPAD)) 419 | { 420 | while (width) 421 | { 422 | putc('0', stream); 423 | width--; 424 | } 425 | } 426 | 427 | if (flags & FL_FLTFIX) 428 | { /* 'f' format */ 429 | 430 | n = exp > 0 ? exp : 0; /* exponent of left digit */ 431 | do 432 | { 433 | if (n == -1) 434 | putc('.', stream); 435 | flags = (n <= exp && n > exp - ndigs) 436 | ? buf[exp - n + 1] : '0'; 437 | if (--n < -prec) 438 | break; 439 | putc(flags, stream); 440 | } while (1); 441 | if (n == exp 442 | && (buf[1] > '5' 443 | || (buf[1] == '5' && !(vtype & FTOA_CARRY)))) 444 | { 445 | flags = '1'; 446 | } 447 | putc(flags, stream); 448 | 449 | } 450 | else 451 | { /* 'e(E)' format */ 452 | 453 | /* mantissa */ 454 | if (buf[1] != '1') 455 | vtype &= ~FTOA_CARRY; 456 | putc(buf[1], stream); 457 | if (prec) 458 | { 459 | putc('.', stream); 460 | sign = 2; 461 | do 462 | { 463 | putc(buf[sign++], stream); 464 | } while (--prec); 465 | } 466 | 467 | /* exponent */ 468 | putc(flags & FL_FLTUPP ? 'E' : 'e', stream); 469 | ndigs = '+'; 470 | if (exp < 0 || (exp == 0 && (vtype & FTOA_CARRY) != 0)) 471 | { 472 | exp = -exp; 473 | ndigs = '-'; 474 | } 475 | putc(ndigs, stream); 476 | #if FP_MATH_LEVEL >= FP_MATH_DBL 477 | for (ndigs = '0'; exp >= 100; exp -= 100) 478 | ndigs += 1; 479 | putc(ndigs, stream); 480 | #endif 481 | for (ndigs = '0'; exp >= 10; exp -= 10) 482 | ndigs += 1; 483 | putc(ndigs, stream); 484 | putc('0' + exp, stream); 485 | } 486 | 487 | goto tail; 488 | # undef ndigs 489 | } 490 | 491 | #else /* to: FP_MATH_LEVEL > FP_MATH_NONE */ 492 | if ((c >= 'E' && c <= 'G') || (c >= 'e' && c <= 'g')) 493 | { 494 | (void)va_arg(ap, double); 495 | buf[0] = '?'; 496 | goto buf_addr; 497 | } 498 | 499 | #endif 500 | 501 | { 502 | const char* pnt; 503 | int size; 504 | 505 | switch (c) 506 | { 507 | 508 | case 'c': 509 | buf[0] = va_arg(ap, int); 510 | #if FP_MATH_LEVEL == FP_MATH_NONE || INT_MATH_LEVEL == INT_MATH_MIN 511 | buf_addr: 512 | #endif 513 | pnt = (char*)buf; 514 | size = 1; 515 | goto str_lpad; 516 | 517 | case 's': 518 | pnt = va_arg(ap, char*); 519 | size = strnlen(pnt, (flags & FL_PREC) ? prec : ~0); 520 | goto str_lpad; 521 | 522 | str_lpad: 523 | if (!(flags & FL_LPAD)) 524 | { 525 | while (size < width) 526 | { 527 | putc(' ', stream); 528 | width--; 529 | } 530 | } 531 | while (size) 532 | { 533 | putc(*pnt++, stream); 534 | if (width) width -= 1; 535 | size -= 1; 536 | } 537 | goto tail; 538 | } 539 | } 540 | 541 | unsigned base = 10; 542 | int cap = CONV_LOWER; 543 | 544 | if (c == 'd' || c == 'i') 545 | { 546 | flags &= ~FL_ALT; 547 | 548 | if (flags & FL_LL) 549 | { 550 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 551 | ll = va_arg(ap, long long); 552 | if (ll < 0) 553 | { 554 | ll = -ll; 555 | flags |= FL_NEGATIVE; 556 | } 557 | goto ulltoa; 558 | #else 559 | x = va_arg(ap, long); 560 | buf[0] = '?'; 561 | goto buf_addr; 562 | #endif 563 | } 564 | if (flags & FL_LONG) 565 | x = va_arg(ap, long); 566 | else 567 | x = va_arg(ap, int); 568 | 569 | if (x < 0) 570 | { 571 | x = -x; 572 | flags |= FL_NEGATIVE; 573 | } 574 | goto ultoa; 575 | } 576 | else if (c == 'u') 577 | { 578 | flags &= ~FL_ALT; 579 | } 580 | else 581 | { 582 | base = 16; 583 | flags &= ~(FL_PLUS | FL_SPACE); 584 | 585 | switch (c) 586 | { 587 | case 'o': 588 | base = 8; 589 | break; 590 | case 'P': 591 | if (flags & FL_ALT) 592 | flags |= (FL_ALTHEX | FL_ALTUPP); 593 | cap = CONV_UPPER; 594 | // Fall into 'p' 595 | case 'p': 596 | if (flags & FL_ALT) 597 | flags |= FL_ALTHEX; 598 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 599 | if (sizeof(void *) > sizeof(long)) 600 | goto ullRead; 601 | #endif 602 | if (sizeof(void *) > sizeof(int)) 603 | x = va_arg(ap, unsigned long); 604 | else 605 | x = va_arg(ap, unsigned int); 606 | goto ultoa; 607 | case 'x': 608 | if (flags & FL_ALT) 609 | flags |= FL_ALTHEX; 610 | break; 611 | case 'X': 612 | if (flags & FL_ALT) 613 | flags |= (FL_ALTHEX | FL_ALTUPP); 614 | cap = CONV_UPPER; 615 | break; 616 | 617 | default: 618 | goto ret; 619 | } 620 | } 621 | 622 | if (flags & FL_LL) 623 | { 624 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 625 | int ch; 626 | unsigned long long ull; 627 | ullRead: 628 | ll = va_arg(ap, unsigned long long); 629 | ulltoa: 630 | // Compute digits until shrunk to ulong 631 | ull = ll; 632 | str = buf; 633 | while (ull > ULONG_MAX) 634 | { 635 | ULL_DIVMOD(ull, ch, base); 636 | if (ch >= 10) 637 | ch += cap; 638 | *str++ = ch + '0'; 639 | } 640 | x = (unsigned long)ull; 641 | goto ultoaStr; 642 | #else 643 | x = va_arg(ap, unsigned long); 644 | buf[0] = '?'; 645 | goto buf_addr; 646 | #endif 647 | } 648 | if (sizeof(long) > sizeof(int) && (flags & FL_LONG)) 649 | x = va_arg(ap, unsigned long); 650 | else 651 | { 652 | x = va_arg(ap, unsigned int); 653 | if (flags & FL_H) 654 | x = (unsigned short)x; 655 | } 656 | 657 | ultoa: 658 | str = buf; 659 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 660 | ultoaStr: 661 | #endif 662 | c = __ultoa_rev(x, str, base, cap) - buf; 663 | 664 | { 665 | unsigned char len; 666 | 667 | len = c; 668 | if (flags & FL_PREC) 669 | { 670 | flags &= ~FL_ZFILL; 671 | if (len < prec) 672 | { 673 | len = prec; 674 | if ((flags & FL_ALT) && !(flags & FL_ALTHEX)) 675 | flags &= ~FL_ALT; 676 | } 677 | } 678 | if (flags & FL_ALT) 679 | { 680 | if (buf[c - 1] == '0') 681 | { 682 | flags &= ~(FL_ALT | FL_ALTHEX | FL_ALTUPP); 683 | } 684 | else 685 | { 686 | len += 1; 687 | if (flags & FL_ALTHEX) 688 | len += 1; 689 | } 690 | } 691 | else if (flags & (FL_NEGATIVE | FL_PLUS | FL_SPACE)) 692 | { 693 | len += 1; 694 | } 695 | 696 | if (!(flags & FL_LPAD)) 697 | { 698 | if (flags & FL_ZFILL) 699 | { 700 | prec = c; 701 | if (len < width) 702 | { 703 | prec += width - len; 704 | len = width; 705 | } 706 | } 707 | while (len < width) 708 | { 709 | putc(' ', stream); 710 | len++; 711 | } 712 | } 713 | 714 | width = (len < width) ? width - len : 0; 715 | 716 | if (flags & FL_ALT) 717 | { 718 | putc('0', stream); 719 | if (flags & FL_ALTHEX) 720 | putc(flags & FL_ALTUPP ? 'X' : 'x', stream); 721 | } 722 | else if (flags & (FL_NEGATIVE | FL_PLUS | FL_SPACE)) 723 | { 724 | unsigned char z = ' '; 725 | if (flags & FL_PLUS) z = '+'; 726 | if (flags & FL_NEGATIVE) z = '-'; 727 | putc(z, stream); 728 | } 729 | 730 | while (prec > c) 731 | { 732 | putc('0', stream); 733 | prec--; 734 | } 735 | 736 | do 737 | { 738 | putc(buf[--c], stream); 739 | } while (c); 740 | } 741 | 742 | tail: 743 | /* Tail is possible. */ 744 | while (width) 745 | { 746 | putc(' ', stream); 747 | width--; 748 | } 749 | } /* for (;;) */ 750 | 751 | ret: 752 | return stream->len; 753 | } 754 | -------------------------------------------------------------------------------- /libc/stdio/vfscanf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002,2004,2005 Joerg Wunsch 2 | Copyright (c) 2008 Dmitry Xmelkov 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | * Neither the name of the copyright holders nor the names of 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* $Id$ */ 34 | 35 | #include 36 | #include 37 | #include 38 | #include "stdio_private.h" 39 | #include "strconv.h" 40 | 41 | /* 42 | * This file can be compiled into more than one flavor. 43 | * See stdio_private.h. 44 | */ 45 | 46 | #if INT_MATH_LEVEL == INT_MATH_MIN || INT_MATH_LEVEL == INT_MATH_LONG_LONG 47 | /* OK */ 48 | #else 49 | # error "Not a known integer math level." 50 | #endif 51 | 52 | #if FP_MATH_LEVEL == FP_MATH_NONE || FP_MATH_LEVEL == FP_MATH_FLT || FP_MATH_LEVEL == FP_MATH_DBL || FP_MATH_LEVEL == FP_MATH_FLT_DBL 53 | /* OK */ 54 | #else 55 | # error "Not a known floating-point math level." 56 | #endif 57 | 58 | 59 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 60 | typedef unsigned long long val_t; 61 | #else 62 | typedef unsigned long val_t; 63 | #endif 64 | 65 | // "h" & "hh" means FL_SHORT is set if FL_CHAR is set, so test FL_CHAR first 66 | // "l" & "ll" means FL_LONG is set if FL_LL is set, so test FL_LL first 67 | static void putval(void* addr, unsigned flags, val_t val) 68 | { 69 | if (!(flags & FL_STAR)) 70 | { 71 | if (flags & FL_CHAR) 72 | *(char*)addr = (char)val; 73 | #if INT_MATH_LEVEL >= INT_MATH_LONG_LONG 74 | else if (flags & FL_LL) 75 | *(val_t*)addr = val; 76 | #endif 77 | else if (flags & FL_LONG) 78 | *(long*)addr = (long)val; 79 | else if (flags & FL_SHORT) 80 | *(short*)addr = (short)val; 81 | else 82 | *(int*)addr = (int)val; 83 | } 84 | } 85 | 86 | __attribute__((noinline)) 87 | static unsigned char 88 | conv_int(FILE* stream, int width, void* addr, unsigned flags) 89 | { 90 | val_t val; 91 | int i; 92 | 93 | i = getc(stream); /* after ungetc() */ 94 | 95 | switch ((unsigned char)i) 96 | { 97 | case '-': 98 | flags |= FL_MINUS; 99 | /* FALLTHROUGH */ 100 | case '+': 101 | if (!--width || (i = getc(stream)) < 0) 102 | goto err; 103 | } 104 | 105 | val = 0; 106 | flags &= ~FL_WIDTH; 107 | 108 | if (!(flags & (FL_DEC | FL_OCT)) && i == '0') 109 | { 110 | if (!--width || (i = getc(stream)) < 0) 111 | goto putval; 112 | flags |= FL_WIDTH; 113 | if (i == 'x' || i == 'X') 114 | { 115 | flags |= FL_HEX; 116 | if (!--width || (i = getc(stream)) < 0) 117 | goto putval; 118 | } 119 | else 120 | { 121 | if (!(flags & FL_HEX)) 122 | flags |= FL_OCT; 123 | } 124 | } 125 | 126 | /* This fact is used below to parse hexadecimal digit. */ 127 | #if ('A' - '0') != (('a' - '0') & ~('A' ^ 'a')) 128 | # error 129 | #endif 130 | 131 | do 132 | { 133 | unsigned u = i; 134 | u -= '0'; 135 | if (flags & FL_OCT) 136 | { 137 | if (u > 7) goto unget; 138 | val = val * 8 + u; 139 | } 140 | else if (flags & FL_HEX) 141 | { 142 | if (u > 9) 143 | { 144 | u &= ~('A' ^ 'a'); 145 | u += '0' - 'A'; 146 | if (u > 5) goto unget; 147 | u += 10; 148 | } 149 | val = val * 16 + u; 150 | } 151 | else 152 | { 153 | if (u > 9) 154 | { 155 | unget: 156 | ungetc(i, stream); 157 | break; 158 | } 159 | val = val * 10 + u; 160 | } 161 | flags |= FL_WIDTH; 162 | if (!--width) goto putval; 163 | } while ((i = getc(stream)) >= 0); 164 | if (!(flags & FL_WIDTH)) 165 | goto err; 166 | 167 | putval: 168 | if (flags & FL_MINUS) val = -(long)val; 169 | putval(addr, flags, val); 170 | return 1; 171 | 172 | err: 173 | return 0; 174 | } 175 | 176 | static const char* 177 | conv_brk(FILE* stream, int width, char* addr, const char* fmt) 178 | { 179 | unsigned char msk[32]; 180 | unsigned char fnegate; 181 | unsigned char frange; 182 | unsigned char cabove; 183 | int i; 184 | 185 | memset(msk, 0, sizeof(msk)); 186 | fnegate = 0; 187 | frange = 0; 188 | cabove = 0; /* init to avoid compiler warning */ 189 | 190 | for (i = 0; ; i++) 191 | { 192 | unsigned char c = *fmt++; 193 | 194 | if (c == 0) 195 | { 196 | return 0; 197 | } 198 | else if (c == '^' && !i) 199 | { 200 | fnegate = 1; 201 | continue; 202 | } 203 | else if (i > fnegate) 204 | { 205 | if (c == ']') break; 206 | if (c == '-' && !frange) 207 | { 208 | frange = 1; 209 | continue; 210 | } 211 | } 212 | 213 | if (!frange) cabove = c; 214 | 215 | for (;;) 216 | { 217 | msk[c >> 3] |= 1 << (c & 7); 218 | if (c == cabove) break; 219 | if (c < cabove) 220 | c++; 221 | else 222 | c--; 223 | } 224 | 225 | frange = 0; 226 | } 227 | if (frange) 228 | msk['-' / 8] |= 1 << ('-' & 7); 229 | 230 | if (fnegate) 231 | { 232 | unsigned char* p = msk; 233 | do 234 | { 235 | unsigned char c = *p; 236 | *p++ = ~c; 237 | } while (p != msk + sizeof(msk)); 238 | } 239 | 240 | /* And now it is a flag of fault. */ 241 | fnegate = 1; 242 | 243 | /* NUL ('\0') is considered as normal character. This is match to Glibc. 244 | Note, there is no method to include NUL into symbol list. */ 245 | do 246 | { 247 | i = getc(stream); 248 | if (i < 0) break; 249 | if (!((msk[(unsigned char)i >> 3] >> (i & 7)) & 1)) 250 | { 251 | ungetc(i, stream); 252 | break; 253 | } 254 | if (addr) *addr++ = i; 255 | fnegate = 0; 256 | } while (--width); 257 | 258 | if (fnegate) 259 | { 260 | return 0; 261 | } 262 | else 263 | { 264 | if (addr) *addr = 0; 265 | return fmt; 266 | } 267 | } 268 | 269 | 270 | /** 271 | Formatted input. This function is the heart of the \b scanf family of 272 | functions. 273 | 274 | Characters are read from \a stream and processed in a way described by 275 | \a fmt. Conversion results will be assigned to the parameters passed 276 | via \a ap. 277 | 278 | The format string \a fmt is scanned for conversion specifications. 279 | Anything that doesn't comprise a conversion specification is taken as 280 | text that is matched literally against the input. White space in the 281 | format string will match any white space in the data (including none), 282 | all other characters match only itself. Processing is aborted as soon 283 | as the data and format string no longer match, or there is an error or 284 | end-of-file condition on \a stream. 285 | 286 | Most conversions skip leading white space before starting the actual 287 | conversion. 288 | 289 | Conversions are introduced with the character \b %. Possible options 290 | can follow the \b %: 291 | 292 | - a \c * indicating that the conversion should be performed but 293 | the conversion result is to be discarded; no parameters will 294 | be processed from \c ap, 295 | - the character \c h indicating that the argument is a pointer 296 | to short int (rather than int), 297 | - the 2 characters \c hh indicating that the argument is a pointer 298 | to char (rather than int). 299 | - the character \c l indicating that the argument is a pointer 300 | to long int (rather than int, for integer 301 | type conversions), or a pointer to \c double (for floating 302 | point conversions), 303 | 304 | In addition, a maximal field width may be specified as a nonzero 305 | positive decimal integer, which will restrict the conversion to at 306 | most this many characters from the input stream. This field width is 307 | limited to at most 255 characters which is also the default value 308 | (except for the %c conversion that defaults to 1). 309 | 310 | The following conversion flags are supported: 311 | 312 | - \c % Matches a literal \c % character. This is not a conversion. 313 | - \c d Matches an optionally signed decimal integer; the next 314 | pointer must be a pointer to \c int. 315 | - \c i Matches an optionally signed integer; the next pointer must 316 | be a pointer to \c int. The integer is read in base 16 if it 317 | begins with \b 0x or \b 0X, in base 8 if it begins with \b 0, and 318 | in base 10 otherwise. Only characters that correspond to the 319 | base are used. 320 | - \c o Matches an octal integer; the next pointer must be a pointer to 321 | unsigned int. 322 | - \c u Matches an optionally signed decimal integer; the next 323 | pointer must be a pointer to unsigned int. 324 | - \c x Matches an optionally signed hexadecimal integer; the next 325 | pointer must be a pointer to unsigned int. 326 | - \c f Matches an optionally signed floating-point number; the next 327 | pointer must be a pointer to \c float. 328 | - e, g, F, E, G Equivalent to \c f. 329 | - \c s 330 | Matches a sequence of non-white-space characters; the next pointer 331 | must be a pointer to \c char, and the array must be large enough to 332 | accept all the sequence and the terminating \c NUL character. The 333 | input string stops at white space or at the maximum field width, 334 | whichever occurs first. 335 | - \c c 336 | Matches a sequence of width count characters (default 1); the next 337 | pointer must be a pointer to \c char, and there must be enough room 338 | for all the characters (no terminating \c NUL is added). The usual 339 | skip of leading white space is suppressed. To skip white space 340 | first, use an explicit space in the format. 341 | - \c [ 342 | Matches a nonempty sequence of characters from the specified set 343 | of accepted characters; the next pointer must be a pointer to \c 344 | char, and there must be enough room for all the characters in the 345 | string, plus a terminating \c NUL character. The usual skip of 346 | leading white space is suppressed. The string is to be made up 347 | of characters in (or not in) a particular set; the set is defined 348 | by the characters between the open bracket \c [ character and a 349 | close bracket \c ] character. The set excludes those characters 350 | if the first character after the open bracket is a circumflex 351 | \c ^. To include a close bracket in the set, make it the first 352 | character after the open bracket or the circumflex; any other 353 | position will end the set. The hyphen character \c - is also 354 | special; when placed between two other characters, it adds all 355 | intervening characters to the set. To include a hyphen, make it 356 | the last character before the final close bracket. For instance, 357 | [^]0-9-] means the set of everything except close 358 | bracket, zero through nine, and hyphen. The string ends 359 | with the appearance of a character not in the (or, with a 360 | circumflex, in) set or when the field width runs out. Note that 361 | usage of this conversion enlarges the stack expense. 362 | - \c p 363 | Matches a pointer value (as printed by %p in printf()); the 364 | next pointer must be a pointer to \c void. 365 | - \c n 366 | Nothing is expected; instead, the number of characters consumed 367 | thus far from the input is stored through the next pointer, which 368 | must be a pointer to \c int. This is not a conversion, although it 369 | can be suppressed with the \c * flag. 370 | 371 | These functions return the number of input items assigned, which can 372 | be fewer than provided for, or even zero, in the event of a matching 373 | failure. Zero indicates that, while there was input available, no 374 | conversions were assigned; typically this is due to an invalid input 375 | character, such as an alphabetic character for a %d 376 | conversion. The value \c EOF is returned if an input failure occurs 377 | before any conversion such as an end-of-file occurs. If an error or 378 | end-of-file occurs after conversion has begun, the number of 379 | conversions which were successfully completed is returned. 380 | 381 | By default, all the conversions described above are available except 382 | the floating-point conversions and the width is limited to 255 383 | characters. The float-point conversion will be available in the 384 | extended version provided by the library \c libscanf_flt.a. Also in 385 | this case the width is not limited (exactly, it is limited to 65535 386 | characters). To link a program against the extended version, use the 387 | following compiler flags in the link stage: 388 | 389 | \code 390 | -Wl,-u,vfscanf -lscanf_flt -lm 391 | \endcode 392 | 393 | A third version is available for environments that are tight on 394 | space. In addition to the restrictions of the standard one, this 395 | version implements no %[ specification. This version is 396 | provided in the library \c libscanf_min.a, and can be requested using 397 | the following options in the link stage: 398 | 399 | \code 400 | -Wl,-u,vfscanf -lscanf_min -lm 401 | \endcode 402 | */ 403 | int vfscanf(FILE* stream, const char* fmt, va_list ap) 404 | { 405 | int nconvs; 406 | unsigned char c; 407 | int width; 408 | char* addr; 409 | unsigned flags; 410 | int i; 411 | 412 | nconvs = 0; 413 | stream->len = 0; 414 | 415 | while ((c = *fmt++) != 0) 416 | { 417 | 418 | if (isspace(c)) 419 | { 420 | __skip_spaces(stream); 421 | 422 | } 423 | else if (c != '%' 424 | || (c = *fmt++) == '%') 425 | { 426 | /* Ordinary character. */ 427 | if ((i = getc(stream)) < 0) 428 | goto eof; 429 | if ((unsigned char)i != c) 430 | { 431 | ungetc(i, stream); 432 | break; 433 | } 434 | 435 | } 436 | else 437 | { 438 | flags = 0; 439 | 440 | if (c == '*') 441 | { 442 | flags = FL_STAR; 443 | c = *fmt++; 444 | } 445 | 446 | width = 0; 447 | while ((c -= '0') < 10) 448 | { 449 | flags |= FL_WIDTH; 450 | width = 10 * width + c; 451 | c = *fmt++; 452 | } 453 | c += '0'; 454 | if (flags & FL_WIDTH) 455 | { 456 | /* C99 says that width must be greater than zero. 457 | To simplify program do treat 0 as error in format. */ 458 | if (!width) break; 459 | } 460 | else 461 | { 462 | width = ~0; 463 | } 464 | 465 | switch (c) 466 | { 467 | case 'h': 468 | flags |= FL_SHORT; 469 | if ((c = *fmt++) != 'h') 470 | break; 471 | flags |= FL_CHAR; 472 | c = *fmt++; 473 | break; 474 | 475 | case 'l': 476 | flags |= FL_LONG; 477 | if ((c = *fmt++) != 'l') 478 | break; 479 | flags |= FL_LL; 480 | c = *fmt++; 481 | } 482 | 483 | #define CNV_BASE "cdinopsuxX[" 484 | #if FP_MATH_LEVEL >= FP_MATH_FLT 485 | # define CNV_FLOAT "efgEFG" 486 | #else 487 | # define CNV_FLOAT "" 488 | #endif 489 | #define CNV_LIST CNV_BASE CNV_FLOAT 490 | if (!c || !strchr(CNV_LIST, c)) 491 | break; 492 | 493 | addr = (flags & FL_STAR) ? 0 : va_arg(ap, char*); 494 | 495 | if (c == 'n') 496 | { 497 | putval(addr, flags, (unsigned)(stream->len)); 498 | continue; 499 | } 500 | 501 | if (c == 'c') 502 | { 503 | if (!(flags & FL_WIDTH)) width = 1; 504 | do 505 | { 506 | if ((i = getc(stream)) < 0) 507 | goto eof; 508 | if (addr) *addr++ = i; 509 | } while (--width); 510 | c = 1; /* no matter with smart GCC */ 511 | 512 | } 513 | else if (c == '[') 514 | { 515 | fmt = conv_brk(stream, width, addr, fmt); 516 | c = (fmt != 0); 517 | } 518 | else 519 | { 520 | if (__skip_spaces(stream) < 0) 521 | goto eof; 522 | 523 | switch (c) 524 | { 525 | 526 | case 's': 527 | /* Now we have 1 nospace symbol. */ 528 | do 529 | { 530 | if ((i = getc(stream)) < 0) 531 | break; 532 | if (isspace(i)) 533 | { 534 | ungetc(i, stream); 535 | break; 536 | } 537 | if (addr) *addr++ = i; 538 | } while (--width); 539 | if (addr) *addr = 0; 540 | c = 1; /* no matter with smart GCC */ 541 | break; 542 | 543 | #if FP_MATH_LEVEL >= FP_MATH_FLT 544 | case 'p': 545 | case 'x': 546 | case 'X': 547 | flags |= FL_HEX; 548 | goto conv_int; 549 | 550 | case 'd': 551 | case 'u': 552 | flags |= FL_DEC; 553 | goto conv_int; 554 | 555 | case 'o': 556 | flags |= FL_OCT; 557 | /* FALLTHROUGH */ 558 | case 'i': 559 | conv_int: 560 | c = conv_int(stream, width, addr, flags); 561 | break; 562 | 563 | default: /* e,E,f,F,g,G */ 564 | if (flags & FL_LONG) 565 | { 566 | #if FP_MATH_LEVEL >= FP_MATH_DBL 567 | c = __conv_dbl(stream, width, (double*)addr); 568 | #else 569 | goto eof; 570 | #endif 571 | } 572 | else 573 | { 574 | #if FP_MATH_LEVEL != FP_MATH_DBL 575 | c = __conv_flt(stream, width, (float*)addr); 576 | #else 577 | goto eof; 578 | #endif 579 | } 580 | 581 | #else // FP_MATH_LEVEL >= FP_MATH_FLT 582 | case 'd': 583 | case 'u': 584 | flags |= FL_DEC; 585 | goto conv_int; 586 | 587 | case 'o': 588 | flags |= FL_OCT; 589 | /* FALLTHROUGH */ 590 | case 'i': 591 | goto conv_int; 592 | 593 | default: /* p,x,X */ 594 | flags |= FL_HEX; 595 | conv_int: 596 | c = conv_int(stream, width, addr, flags); 597 | #endif // #else FP_MATH_LEVEL >= FP_MATH_FLT 598 | } 599 | } /* else */ 600 | 601 | if (!c) 602 | { 603 | if (stream->flags & (__SERR | __SEOF)) 604 | goto eof; 605 | break; 606 | } 607 | if (!(flags & FL_STAR)) nconvs += 1; 608 | } /* else */ 609 | } /* while */ 610 | return nconvs; 611 | 612 | eof: 613 | return nconvs ? nconvs : EOF; 614 | } 615 | -------------------------------------------------------------------------------- /libc/stdio/vprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2005, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int vprintf(const char *fmt, va_list ap) 36 | { 37 | return vfprintf(stdout, fmt, ap); 38 | } 39 | -------------------------------------------------------------------------------- /libc/stdio/vscanf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2005, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int vscanf(const char *fmt, va_list ap) 36 | { 37 | return vfscanf(stdin, fmt, ap); 38 | } 39 | -------------------------------------------------------------------------------- /libc/stdio/vsnprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2003, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include 34 | #include "stdio_private.h" 35 | 36 | int vsnprintf(char *s, size_t n, const char *fmt, va_list ap) 37 | { 38 | FILE f; 39 | int i; 40 | 41 | f.flags = __SWR | __SSTR; 42 | f.u.mem.buf = s; 43 | /* Restrict max output length to INT_MAX, as snprintf() return 44 | signed int. The fputc() function uses a signed comparison 45 | between estimated len and f.u.mem.size field. So we can write a 46 | negative value into f.u.mem.size in the case of n was 0. Note, 47 | that f.u.mem.size will be a max number of nonzero symbols. */ 48 | if ((int)n < 0) 49 | n = (unsigned)INT_MAX + 1; 50 | f.u.mem.size = n - 1; /* -1,0,...INT_MAX */ 51 | 52 | i = vfprintf(&f, fmt, ap); 53 | 54 | if (f.u.mem.size >= 0) 55 | s[f.len < f.u.mem.size ? f.len : f.u.mem.size] = 0; 56 | 57 | return i; 58 | } 59 | -------------------------------------------------------------------------------- /libc/stdio/vsprintf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2003, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include 34 | #include "stdio_private.h" 35 | 36 | int vsprintf(char *s, const char *fmt, va_list ap) 37 | { 38 | FILE f; 39 | int i; 40 | 41 | f.flags = __SWR | __SSTR; 42 | f.u.mem.buf = s; 43 | f.u.mem.size = INT_MAX; 44 | i = vfprintf(&f, fmt, ap); 45 | s[f.len] = 0; 46 | 47 | return i; 48 | } 49 | -------------------------------------------------------------------------------- /libc/stdio/vsscanf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id$ */ 31 | 32 | #include 33 | #include "stdio_private.h" 34 | 35 | int vsscanf(const char *s, const char *fmt, va_list ap) 36 | { 37 | FILE f; 38 | int i; 39 | 40 | f.flags = __SRD | __SSTR; 41 | /* 42 | * It is OK to discard the "const" qualifier here. The buffer is 43 | * really only be read (by getc()), and as this our FILE f we 44 | * be discarded upon exiting sscanf(), nobody will ever get 45 | * a chance to get write access to it again. 46 | */ 47 | f.u.mem.buf = (char *)s; 48 | i = vfscanf(&f, fmt, ap); 49 | 50 | return i; 51 | } 52 | -------------------------------------------------------------------------------- /stdio-mini.atsln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Atmel Studio Solution File, Format Version 11.00 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{54F91283-7BC4-4236-8FF9-10F437C3AD48}") = "stdio-mini", "stdio-mini.cproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}" 7 | EndProject 8 | Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "ArmTest", "Test Drivers\ArmTest\ArmTest.cppproj", "{DB4E3593-F4C2-4841-953B-721E203F0F92}" 9 | ProjectSection(ProjectDependencies) = postProject 10 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897} = {DCE6C7E3-EE26-4D79-826B-08594B9AD897} 11 | EndProjectSection 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | stdio-l|ARM = stdio-l|ARM 16 | stdio-l-flt|ARM = stdio-l-flt|ARM 17 | stdio-ll|ARM = stdio-ll|ARM 18 | stdio-ll-dbl|ARM = stdio-ll-dbl|ARM 19 | stdio-ll-flt|ARM = stdio-ll-flt|ARM 20 | stdio-ll-flt-dbl|ARM = stdio-ll-flt-dbl|ARM 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-l|ARM.ActiveCfg = stdio-l|ARM 24 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-l|ARM.Build.0 = stdio-l|ARM 25 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-l-flt|ARM.ActiveCfg = stdio-l-flt|ARM 26 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-l-flt|ARM.Build.0 = stdio-l-flt|ARM 27 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll|ARM.ActiveCfg = stdio-ll|ARM 28 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll|ARM.Build.0 = stdio-ll|ARM 29 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll-dbl|ARM.ActiveCfg = stdio-ll-dbl|ARM 30 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll-dbl|ARM.Build.0 = stdio-ll-dbl|ARM 31 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll-flt|ARM.ActiveCfg = stdio-ll-flt|ARM 32 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll-flt|ARM.Build.0 = stdio-ll-flt|ARM 33 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll-flt-dbl|ARM.ActiveCfg = stdio-ll-flt-dbl|ARM 34 | {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.stdio-ll-flt-dbl|ARM.Build.0 = stdio-ll-flt-dbl|ARM 35 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-l|ARM.ActiveCfg = stdio-l|ARM 36 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-l|ARM.Build.0 = stdio-l|ARM 37 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-l-flt|ARM.ActiveCfg = stdio-l-flt|ARM 38 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-l-flt|ARM.Build.0 = stdio-l-flt|ARM 39 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll|ARM.ActiveCfg = stdio-ll|ARM 40 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll|ARM.Build.0 = stdio-ll|ARM 41 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll-dbl|ARM.ActiveCfg = stdio-ll-dbl|ARM 42 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll-dbl|ARM.Build.0 = stdio-ll-dbl|ARM 43 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll-flt|ARM.ActiveCfg = stdio-ll-flt|ARM 44 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll-flt|ARM.Build.0 = stdio-ll-flt|ARM 45 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll-flt-dbl|ARM.ActiveCfg = stdio-ll-flt-dbl|ARM 46 | {DB4E3593-F4C2-4841-953B-721E203F0F92}.stdio-ll-flt-dbl|ARM.Build.0 = stdio-ll-flt-dbl|ARM 47 | EndGlobalSection 48 | GlobalSection(SolutionProperties) = preSolution 49 | HideSolutionNode = FALSE 50 | EndGlobalSection 51 | EndGlobal 52 | -------------------------------------------------------------------------------- /stdio-mini.componentinfo.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | CMSIS 8 | CORE 9 | 10 | 11 | ARM 12 | 5.1.2 13 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs 14 | 15 | 16 | 17 | 18 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\arm\CMSIS\5.4.0\CMSIS\Documentation\Core\html\index.html 19 | 20 | doc 21 | 22 | 23 | 24 | CMSIS/Documentation/Core/html/index.html 25 | 26 | 27 | 28 | 29 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\arm\CMSIS\5.4.0\CMSIS\Core\Include\ 30 | 31 | include 32 | 33 | 34 | 35 | CMSIS/Core/Include/ 36 | 37 | 38 | 39 | 40 | CMSIS 41 | C:/Program Files (x86)/Atmel/Studio/7.0/Packs/arm/CMSIS/5.4.0/ARM.CMSIS.pdsc 42 | 5.4.0 43 | true 44 | ARMv6_7_8-M Device 45 | 46 | 47 | 48 | Resolved 49 | Fixed 50 | true 51 | 52 | 53 | 54 | 55 | Device 56 | Startup 57 | 58 | 59 | Atmel 60 | 1.2.0 61 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs 62 | 63 | 64 | 65 | 66 | 67 | 68 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\include 69 | 70 | include 71 | C 72 | 73 | 74 | samc21/include 75 | 76 | 77 | 78 | 79 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\include\sam.h 80 | 81 | header 82 | C 83 | 2Rk75rgdSdFbgPkrL7+Wow== 84 | 85 | samc21/include/sam.h 86 | 87 | 88 | 89 | 90 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\templates\library.c 91 | template 92 | source 93 | C Lib 94 | WyKffHzAKIxQcCS/NYqKcQ== 95 | 96 | samc21/templates/library.c 97 | Main file (.c) 98 | 99 | 100 | 101 | C:/Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\SAMC21_DFP\1.2.176\samc21\templates\library.cpp 102 | template 103 | source 104 | C Lib 105 | A6CtkScmRPo5iQcWfSbq4Q== 106 | 107 | samc21/templates/library.cpp 108 | Main file (.cpp) 109 | 110 | 111 | 112 | SAMC21_DFP 113 | C:/Program Files (x86)/Atmel/Studio/7.0/Packs/atmel/SAMC21_DFP/1.2.176/Atmel.SAMC21_DFP.pdsc 114 | 1.2.176 115 | true 116 | ATSAMC21G17A 117 | 118 | 119 | 120 | Resolved 121 | Fixed 122 | true 123 | 124 | 125 | --------------------------------------------------------------------------------