1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
diff --git a/apps/alarm_menu.c b/apps/alarm_menu.c
index 3a44fa4..1cd0bc4 100644
--- a/apps/alarm_menu.c
+++ b/apps/alarm_menu.c
@@ -25,7 +25,6 @@
 #include "lcd.h"
 #include "action.h"
 #include "kernel.h"
-#include "sprintf.h"
 #include <string.h>
 #include "settings.h"
 #include "power.h"
diff --git a/apps/apps.make b/apps/apps.make
index 3717c5b..0248589 100644
--- a/apps/apps.make
+++ b/apps/apps.make
@@ -17,7 +17,8 @@ SRC += $(call preprocess, $(APPSDIR)/SOURCES)
 #
 # Kludge: depends on ctype.o only to depend on config-*.h ...
 #
-features $(BUILDDIR)/apps/features $(BUILDDIR)/apps/genlang-features: $(APPSDIR)/features.txt $(BUILDDIR)/firmware/common/ctype.o
+# $(BUILDDIR)/firmware/libc/ctype.o
+features $(BUILDDIR)/apps/features $(BUILDDIR)/apps/genlang-features: $(APPSDIR)/features.txt
 	$(SILENT)mkdir -p $(BUILDDIR)/apps
 	$(SILENT)mkdir -p $(BUILDDIR)/lang
 	$(call PRINTS,PP $(<F))
diff --git a/apps/bookmark.c b/apps/bookmark.c
index f02ac25..a3559be 100644
--- a/apps/bookmark.c
+++ b/apps/bookmark.c
@@ -342,7 +342,7 @@ static char* create_bookmark()
              /* new optional bookmark token descriptors should be inserted
                 just before the "%s;%s" in this line... */
 #if CONFIG_CODEC == SWCODEC
-             ">%d;%d;%ld;%d;%ld;%d;%d;%d;%d;%s;%s",
+             ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
 #else
              ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
 #endif
@@ -360,8 +360,8 @@ static char* create_bookmark()
              global_settings.playlist_shuffle,
              /* ...and their values should go here */
 #if CONFIG_CODEC == SWCODEC
-             sound_get_pitch(),
-             dsp_get_timestretch(),
+             (long)sound_get_pitch(),
+             (long)dsp_get_timestretch(),
 #endif
              /* more mandatory tokens */
              playlist_get_name(NULL, global_temp_buffer,
diff --git a/apps/buffering.c b/apps/buffering.c
index afc7c7a..2c26c66 100644
--- a/apps/buffering.c
+++ b/apps/buffering.c
@@ -38,7 +38,6 @@
 #include "kernel.h"
 #include "tree.h"
 #include "debug.h"
-#include "sprintf.h"
 #include "settings.h"
 #include "codecs.h"
 #include "audio.h"
diff --git a/apps/buffering.h b/apps/buffering.h
index 6e17b65..b44713f 100644
--- a/apps/buffering.h
+++ b/apps/buffering.h
@@ -22,7 +22,7 @@
 #ifndef _BUFFERING_H_
 #define _BUFFERING_H_

-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdbool.h>
 #include "appevents.h"

diff --git a/apps/codecs.c b/apps/codecs.c
index 8976871..a978639 100644
--- a/apps/codecs.c
+++ b/apps/codecs.c
@@ -26,12 +26,12 @@
 #include <stdlib.h>
 #include <timefuncs.h>
 #include <ctype.h>
+#include "string-extra.h"
 #include "debug.h"
 #include "button.h"
 #include "dir.h"
 #include "file.h"
 #include "kernel.h"
-#include "sprintf.h"
 #include "screens.h"
 #include "misc.h"
 #include "mas.h"
diff --git a/apps/codecs.h b/apps/codecs.h
index 6f28f0e..54af462 100644
--- a/apps/codecs.h
+++ b/apps/codecs.h
@@ -31,8 +31,10 @@
 #define MEM 2
 #endif

+#include <_ansi.h>
 #include <stdbool.h>
 #include <stdlib.h>
+#include "strlcpy.h"
 #include "config.h"
 #include "system.h"
 #include "metadata.h"
diff --git a/apps/codecs/adx.c b/apps/codecs/adx.c
index fe32653..dd5bba1 100644
--- a/apps/codecs/adx.c
+++ b/apps/codecs/adx.c
@@ -18,6 +18,8 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
+
+#include <limits.h>
 #include "codeclib.h"
 #include "inttypes.h"
 #include "math.h"
diff --git a/apps/codecs/demac/demac.c b/apps/codecs/demac/demac.c
index 5e98936..82c649f 100644
--- a/apps/codecs/demac/demac.c
+++ b/apps/codecs/demac/demac.c
@@ -46,7 +46,7 @@ avoided by writing the decoded data one sample at a time.
 #include <stdio.h>
 #include <inttypes.h>
 #include <stdlib.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/apps/codecs/demac/libdemac/parser.c b/apps/codecs/demac/libdemac/parser.c
index ebde36d..2af4a29 100644
--- a/apps/codecs/demac/libdemac/parser.c
+++ b/apps/codecs/demac/libdemac/parser.c
@@ -27,7 +27,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 #ifndef ROCKBOX
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/apps/codecs/demac/wavwrite.c b/apps/codecs/demac/wavwrite.c
index 72c7950..71d2b7b 100644
--- a/apps/codecs/demac/wavwrite.c
+++ b/apps/codecs/demac/wavwrite.c
@@ -25,7 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 #include <stdio.h>
 #include <inttypes.h>
 #include <stdlib.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/apps/codecs/lib/codeclib.h b/apps/codecs/lib/codeclib.h
index 817d86a..9c663f3 100644
--- a/apps/codecs/lib/codeclib.h
+++ b/apps/codecs/lib/codeclib.h
@@ -24,7 +24,8 @@

 #include "config.h"
 #include "codecs.h"
-#include <sys/types.h>
+#include "inttypes.h"
+#include <string.h>
 #include "mdct.h"
 #include "fft.h"

@@ -44,6 +45,7 @@ extern unsigned char* filebuf;    /* The rest of the MP3 buffer
 #define calloc(x,y) codec_calloc(x,y)
 #define realloc(x,y) codec_realloc(x,y)
 #define free(x) codec_free(x)
+#undef alloca
 #define alloca(x) __builtin_alloca(x)

 void* codec_malloc(size_t size);
@@ -59,7 +61,11 @@ void *memmove(void *s1, const void *s2, size_t n);
 size_t strlen(const char *s);
 char *strcpy(char *dest, const char *src);
 char *strcat(char *dest, const char *src);
-int strcmp(const char *, const char *);
+
+/* on some platforms strcmp() seems to be a tricky define which
+ * breaks if we write down strcmp's prototype */
+#undef strcmp
+int strcmp(const char *s1, const char *s2);

 void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));

diff --git a/apps/codecs/lib/codeclib_misc.h b/apps/codecs/lib/codeclib_misc.h
index 0d56075..6632e80 100644
--- a/apps/codecs/lib/codeclib_misc.h
+++ b/apps/codecs/lib/codeclib_misc.h
@@ -40,7 +40,7 @@

 #ifndef  _LOW_ACCURACY_
 /* 64 bit multiply */
-/* #include <sys/types.h> */
+/* #include "inttypes.h" */

 #if ROCKBOX_LITTLE_ENDIAN == 1
 union magic {
diff --git a/apps/codecs/lib/tlsf/src/tlsf.c b/apps/codecs/lib/tlsf/src/tlsf.c
index 6d15c3a..77551f9 100644
--- a/apps/codecs/lib/tlsf/src/tlsf.c
+++ b/apps/codecs/lib/tlsf/src/tlsf.c
@@ -165,7 +165,6 @@
 #endif

 #if defined(ROCKBOX) && defined(SIMULATOR) || !defined(ROCKBOX)
-int printf(char*, ...);
 #define PRINT_MSG(fmt, args...) printf(fmt, ## args)
 #define ERROR_MSG(fmt, args...) printf(fmt, ## args)
 #else
diff --git a/apps/codecs/lib/tlsf/src/tlsf.h b/apps/codecs/lib/tlsf/src/tlsf.h
index 5d016f4..9f9ee01 100644
--- a/apps/codecs/lib/tlsf/src/tlsf.h
+++ b/apps/codecs/lib/tlsf/src/tlsf.h
@@ -19,7 +19,7 @@
 #ifndef _TLSF_H_
 #define _TLSF_H_

-#include <sys/types.h>
+#include "inttypes.h"

 extern size_t init_memory_pool(size_t, void *);
 extern size_t get_used_size(void *);
diff --git a/apps/codecs/libatrac/main.c b/apps/codecs/libatrac/main.c
index e0a3f85..4551c56 100644
--- a/apps/codecs/libatrac/main.c
+++ b/apps/codecs/libatrac/main.c
@@ -1,6 +1,6 @@
 #include <stdio.h>
 #include <stdint.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/apps/codecs/libcook/main.c b/apps/codecs/libcook/main.c
index 71d02fb..5330592 100644
--- a/apps/codecs/libcook/main.c
+++ b/apps/codecs/libcook/main.c
@@ -19,7 +19,7 @@
  *
  ****************************************************************************/
 #include <stdint.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/apps/codecs/libfaad/common.h b/apps/codecs/libfaad/common.h
index 658e92f..01164e3 100644
--- a/apps/codecs/libfaad/common.h
+++ b/apps/codecs/libfaad/common.h
@@ -185,7 +185,7 @@ typedef float float32_t;

 #include <stdio.h>
 #ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
+# include "inttypes.h"
 #endif
 #ifdef HAVE_SYS_STAT_H
 # include <sys/stat.h>
diff --git a/apps/codecs/libfaad/faad_config.h b/apps/codecs/libfaad/faad_config.h
index 163b556..31ffe9a 100644
--- a/apps/codecs/libfaad/faad_config.h
+++ b/apps/codecs/libfaad/faad_config.h
@@ -71,7 +71,7 @@
 /* Define to 1 if you have the <sys/time.h> header file. */
 //#define HAVE_SYS_TIME_H 1

-/* Define to 1 if you have the <sys/types.h> header file. */
+/* Define to 1 if you have the "inttypes.h" header file. */
 //#define HAVE_SYS_TYPES_H 1

 /* Define to 1 if you have the <unistd.h> header file. */
@@ -116,5 +116,5 @@
 /* #undef inline */
 #endif

-/* Define to `long' if <sys/types.h> does not define. */
+/* Define to `long' if "inttypes.h" does not define. */
 /* #undef off_t */
diff --git a/apps/codecs/libffmpegFLAC/main.c b/apps/codecs/libffmpegFLAC/main.c
index 5d232a8..11e4e71 100644
--- a/apps/codecs/libffmpegFLAC/main.c
+++ b/apps/codecs/libffmpegFLAC/main.c
@@ -38,7 +38,7 @@
 #include <stdbool.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>

 #include "decoder.h"
diff --git a/apps/codecs/libmad/decoder.c b/apps/codecs/libmad/decoder.c
index 46f4dfe..8eb8c2d 100644
--- a/apps/codecs/libmad/decoder.c
+++ b/apps/codecs/libmad/decoder.c
@@ -26,7 +26,7 @@
 # include "global.h"

 # ifdef HAVE_SYS_TYPES_H
-#  include <sys/types.h>
+#  include "inttypes.h"
 # endif

 # ifdef HAVE_SYS_WAIT_H
diff --git a/apps/codecs/libpcm/adpcm_seek.h b/apps/codecs/libpcm/adpcm_seek.h
index 66ec390..696df97 100644
--- a/apps/codecs/libpcm/adpcm_seek.h
+++ b/apps/codecs/libpcm/adpcm_seek.h
@@ -21,7 +21,7 @@
 #ifndef CODEC_LIBPCM_ADPCM_SEEK_H
 #define CODEC_LIBPCM_ADPCM_SEEK_H

-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdbool.h>
 #include <inttypes.h>

diff --git a/apps/codecs/libpcm/ima_adpcm_common.h b/apps/codecs/libpcm/ima_adpcm_common.h
index 0a2129b..a391e4d 100644
--- a/apps/codecs/libpcm/ima_adpcm_common.h
+++ b/apps/codecs/libpcm/ima_adpcm_common.h
@@ -21,7 +21,7 @@
 #ifndef CODEC_LIBPCM_IMA_ADPCM_COMMON_H
 #define CODEC_LIBPCM_IMA_ADPCM_COMMON_H

-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdbool.h>
 #include <inttypes.h>

diff --git a/apps/codecs/libpcm/pcm_common.h b/apps/codecs/libpcm/pcm_common.h
index 91c3ab7..f21854f 100644
--- a/apps/codecs/libpcm/pcm_common.h
+++ b/apps/codecs/libpcm/pcm_common.h
@@ -21,7 +21,7 @@
 #ifndef CODEC_LIBPCM_PCM_COMMON_H
 #define CODEC_LIBPCM_PCM_COMMON_H

-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdbool.h>
 #include <inttypes.h>

diff --git a/apps/codecs/librm/rm.c b/apps/codecs/librm/rm.c
index a179688..b543da5 100644
--- a/apps/codecs/librm/rm.c
+++ b/apps/codecs/librm/rm.c
@@ -32,7 +32,7 @@
 #ifdef TEST
 #include <fcntl.h>
 #include <unistd.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>

 int filesize(int fd)
diff --git a/apps/codecs/libspeex/config-speex.h b/apps/codecs/libspeex/config-speex.h
index f3b397f..f1b303a 100644
--- a/apps/codecs/libspeex/config-speex.h
+++ b/apps/codecs/libspeex/config-speex.h
@@ -97,7 +97,7 @@
 /* Define to 1 if you have the <sys/stat.h> header file. */
 #define HAVE_SYS_STAT_H 1

-/* Define to 1 if you have the <sys/types.h> header file. */
+/* Define to 1 if you have the "inttypes.h" header file. */
 #define HAVE_SYS_TYPES_H 1

 /* Define to 1 if you have the <unistd.h> header file. */
diff --git a/apps/codecs/libspeex/speex/speex_types.h b/apps/codecs/libspeex/speex/speex_types.h
index a2ff1d6..fe8fb72 100644
--- a/apps/codecs/libspeex/speex/speex_types.h
+++ b/apps/codecs/libspeex/speex/speex_types.h
@@ -50,7 +50,7 @@

 #elif defined(__MACOS__)

-#  include <sys/types.h>
+#  include "inttypes.h"
    typedef SInt16 spx_int16_t;
    typedef UInt16 spx_uint16_t;
    typedef SInt32 spx_int32_t;
@@ -58,7 +58,7 @@

 #elif defined(__MACOSX__) /* MacOS X Framework build */

-#  include <sys/types.h>
+#  include "inttypes.h"
    typedef int16_t spx_int16_t;
    typedef u_int16_t spx_uint16_t;
    typedef int32_t spx_int32_t;
diff --git a/apps/codecs/libtremor/config-tremor.h b/apps/codecs/libtremor/config-tremor.h
index 8a68ce3..abed965 100644
--- a/apps/codecs/libtremor/config-tremor.h
+++ b/apps/codecs/libtremor/config-tremor.h
@@ -7,6 +7,7 @@
 #define _ARM_ASSEM_
 #endif

+#ifndef BYTE_ORDER
 #ifdef ROCKBOX_BIG_ENDIAN
 #define BIG_ENDIAN 1
 #define LITTLE_ENDIAN 0
@@ -16,6 +17,7 @@
 #define LITTLE_ENDIAN 1
 #define BIG_ENDIAN 0
 #endif
+#endif

 #ifndef ICODE_ATTR_TREMOR_MDCT
 #define ICODE_ATTR_TREMOR_MDCT ICODE_ATTR
diff --git a/apps/codecs/libtremor/ctype.c b/apps/codecs/libtremor/ctype.c
index 10468bc..359be61 100644
--- a/apps/codecs/libtremor/ctype.c
+++ b/apps/codecs/libtremor/ctype.c
@@ -1 +1,3 @@
-#include "common/ctype.c"
+#ifndef SIMULATOR
+#include "libc/ctype.c"
+#endif
diff --git a/apps/codecs/libtremor/misc.h b/apps/codecs/libtremor/misc.h
index a64a95d..2ea1d9e 100644
--- a/apps/codecs/libtremor/misc.h
+++ b/apps/codecs/libtremor/misc.h
@@ -36,7 +36,7 @@ extern int _ilog(unsigned int v);

 #ifndef  _LOW_ACCURACY_
 /* 64 bit multiply */
-/* #include <sys/types.h> */
+/* #include "inttypes.h" */

 #if BYTE_ORDER==LITTLE_ENDIAN
 union magic {
diff --git a/apps/codecs/libwavpack/words.c b/apps/codecs/libwavpack/words.c
index 6da7161..c3ae50d 100644
--- a/apps/codecs/libwavpack/words.c
+++ b/apps/codecs/libwavpack/words.c
@@ -141,7 +141,7 @@ void init_words (WavpackStream *wps)
     CLEAR (wps->w);
 }

-static int mylog2 (unsigned int32_t avalue);
+static int mylog2 (uint32_t avalue);

 // Read the median log2 values from the specifed metadata structure, convert
 // them back to 32-bit unsigned values and store them. If length is not
@@ -553,7 +553,7 @@ void send_words (int32_t *buffer, int nsamples, uint32_t flags,
         if (sign)
             value = ~value;

-        if ((unsigned int32_t) value < GET_MED (0)) {
+        if ((uint32_t) value < GET_MED (0)) {
             ones_count = low = 0;
             high = GET_MED (0) - 1;
             DEC_MED0 ();
@@ -709,7 +709,7 @@ void flush_word (struct words_data *w, Bitstream *bs)
 // This function returns the log2 for the specified 32-bit unsigned value.
 // The maximum value allowed is about 0xff800000 and returns 8447.

-static int mylog2 (unsigned int32_t avalue)
+static int mylog2 (uint32_t avalue)
 {
     int dbits;

diff --git a/apps/cuesheet.c b/apps/cuesheet.c
index cb3ba65..c75ea65 100644
--- a/apps/cuesheet.c
+++ b/apps/cuesheet.c
@@ -28,7 +28,6 @@
 #include "audio.h"
 #include "kernel.h"
 #include "logf.h"
-#include "sprintf.h"
 #include "misc.h"
 #include "screens.h"
 #include "list.h"
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index 867ac1f..1c37c57 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -28,7 +28,6 @@
 #include "menu.h"
 #include "debug_menu.h"
 #include "kernel.h"
-#include "sprintf.h"
 #include "structec.h"
 #include "action.h"
 #include "debug.h"
diff --git a/apps/enc_config.c b/apps/enc_config.c
index 48ad18f..ca8aebb 100644
--- a/apps/enc_config.c
+++ b/apps/enc_config.c
@@ -19,7 +19,6 @@
  *
  ****************************************************************************/
 #include <stdio.h>
-#include <sprintf.h>
 #include <string.h>
 #include <stdlib.h>
 #include "config.h"
diff --git a/apps/filetree.c b/apps/filetree.c
index e3977e5..9ff48dc 100644
--- a/apps/filetree.c
+++ b/apps/filetree.c
@@ -38,7 +38,6 @@
 #include "screens.h"
 #include "plugin.h"
 #include "rolo.h"
-#include "sprintf.h"
 #include "splash.h"
 #include "cuesheet.h"
 #include "filetree.h"
diff --git a/apps/filetypes.c b/apps/filetypes.c
index 4aefef1..3c8f4b9 100644
--- a/apps/filetypes.c
+++ b/apps/filetypes.c
@@ -26,7 +26,6 @@
 #include "string.h"
 #include <ctype.h>

-#include "sprintf.h"
 #include "settings.h"
 #include "debug.h"
 #include "lang.h"
diff --git a/apps/gui/bitmap/list.c b/apps/gui/bitmap/list.c
index 10616c9..0da67c7 100644
--- a/apps/gui/bitmap/list.c
+++ b/apps/gui/bitmap/list.c
@@ -25,7 +25,6 @@
 #include "lcd.h"
 #include "font.h"
 #include "button.h"
-#include "sprintf.h"
 #include "string.h"
 #include "settings.h"
 #include "kernel.h"
diff --git a/apps/gui/charcell/list.c b/apps/gui/charcell/list.c
index f36d3c9..8eebfe7 100644
--- a/apps/gui/charcell/list.c
+++ b/apps/gui/charcell/list.c
@@ -25,7 +25,6 @@
 #include "lcd.h"
 #include "font.h"
 #include "button.h"
-#include "sprintf.h"
 #include "string.h"
 #include "settings.h"
 #include "kernel.h"
diff --git a/apps/gui/list.c b/apps/gui/list.c
index 11a4c36..de76310 100644
--- a/apps/gui/list.c
+++ b/apps/gui/list.c
@@ -19,11 +19,11 @@
  *
  ****************************************************************************/

+#include <stdarg.h>
 #include "config.h"
 #include "lcd.h"
 #include "font.h"
 #include "button.h"
-#include "sprintf.h"
 #include "string.h"
 #include "settings.h"
 #include "kernel.h"
diff --git a/apps/gui/option_select.c b/apps/gui/option_select.c
index 71a28cb..feed1dc 100644
--- a/apps/gui/option_select.c
+++ b/apps/gui/option_select.c
@@ -20,10 +20,9 @@
  *
  ****************************************************************************/
 #include <stdlib.h>
-#include <string.h>
+#include "string-extra.h"
 #include "config.h"
 #include "option_select.h"
-#include "sprintf.h"
 #include "kernel.h"
 #include "lang.h"
 #include "talk.h"
diff --git a/apps/gui/pitchscreen.c b/apps/gui/pitchscreen.c
index 77b5739..a005872 100644
--- a/apps/gui/pitchscreen.c
+++ b/apps/gui/pitchscreen.c
@@ -23,9 +23,8 @@
 #include <string.h>
 #include <stdio.h>
 #include <math.h>
-#include <stdlib.h> /* for ABS() */
+#include <stdlib.h> /* for abs() */
 #include "config.h"
-#include "sprintf.h"
 #include "action.h"
 #include "dsp.h"
 #include "sound.h"
@@ -248,10 +247,10 @@ static void pitchscreen_draw(struct screen *display, int max_lines,
             /* Pitch:XXX.X% */
             if(global_settings.pitch_mode_semitone)
             {
-                snprintf(buf, sizeof(buf), "%s: %s%ld.%02ld", str(LANG_PITCH),
+                snprintf(buf, sizeof(buf), "%s: %s%d.%02d", str(LANG_PITCH),
                          semitone >= 0 ? "+" : "-",
-                         ABS(semitone / PITCH_SPEED_PRECISION), 
-                         ABS((semitone % PITCH_SPEED_PRECISION) / 
+                         abs(semitone / PITCH_SPEED_PRECISION), 
+                         abs((semitone % PITCH_SPEED_PRECISION) / 
                                          (PITCH_SPEED_PRECISION / 100))
                         );
             }
@@ -290,10 +289,10 @@ static void pitchscreen_draw(struct screen *display, int max_lines,
     {
         if(global_settings.pitch_mode_semitone)
         {
-            snprintf(buf, sizeof(buf), "%s%ld.%02ld",
+            snprintf(buf, sizeof(buf), "%s%d.%02d",
                      semitone >= 0 ? "+" : "-",
-                     ABS(semitone / PITCH_SPEED_PRECISION), 
-                     ABS((semitone % PITCH_SPEED_PRECISION) / 
+                     abs(semitone / PITCH_SPEED_PRECISION), 
+                     abs((semitone % PITCH_SPEED_PRECISION) / 
                                      (PITCH_SPEED_PRECISION / 100))
                     );
         }
diff --git a/apps/gui/skin_engine/skin_backdrops.c b/apps/gui/skin_engine/skin_backdrops.c
index fd786a7..77de8bd 100644
--- a/apps/gui/skin_engine/skin_backdrops.c
+++ b/apps/gui/skin_engine/skin_backdrops.c
@@ -21,9 +21,8 @@

 #include "config.h"
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
-
+#include "string-extra.h"
 #include "settings.h"
 #include "skin_buffer.h"
 #include "wps_internals.h"
diff --git a/apps/gui/skin_engine/skin_display.c b/apps/gui/skin_engine/skin_display.c
index a0b3497..71e04ff 100644
--- a/apps/gui/skin_engine/skin_display.c
+++ b/apps/gui/skin_engine/skin_display.c
@@ -21,7 +21,7 @@
  ****************************************************************************/
 #include "config.h"
 #include <stdio.h>
-#include <string.h>
+#include "string-extra.h"
 #include "misc.h"
 #include "font.h"
 #include "system.h"
diff --git a/apps/gui/skin_engine/skin_tokens.c b/apps/gui/skin_engine/skin_tokens.c
index 4ee8c74..6e5a2ea 100644
--- a/apps/gui/skin_engine/skin_tokens.c
+++ b/apps/gui/skin_engine/skin_tokens.c
@@ -21,7 +21,7 @@
  ****************************************************************************/
 #include "font.h"
 #include <stdio.h>
-#include <string.h>
+#include "string-extra.h"
 #include <stdlib.h>
 #include "action.h"
 #include "system.h"
@@ -247,7 +247,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
                 {
                     *intval = limit * elapsed / length + 1;
                 }
-                snprintf(buf, buf_size, "%d", 100 * elapsed / length);
+                snprintf(buf, buf_size, "%lu", 100 * elapsed / length);
                 return buf;


@@ -269,7 +269,7 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
                 if ((id3->frequency % 1000) < 100)
                     snprintf(buf, buf_size, "%ld", id3->frequency / 1000);
                 else
-                    snprintf(buf, buf_size, "%ld.%d",
+                    snprintf(buf, buf_size, "%ld.%lu",
                             id3->frequency / 1000,
                             (id3->frequency % 1000) / 100);
                 return buf;
@@ -302,12 +302,12 @@ const char *get_id3_token(struct wps_token *token, struct mp3entry *id3,
         case WPS_TOKEN_DATABASE_RATING:
             if (intval)
                 *intval = id3->rating + 1;
-            snprintf(buf, buf_size, "%ld", id3->rating);
+            snprintf(buf, buf_size, "%d", id3->rating);
             return buf;
         case WPS_TOKEN_DATABASE_AUTOSCORE:
             if (intval)
                 *intval = id3->score + 1;
-            snprintf(buf, buf_size, "%ld", id3->score);
+            snprintf(buf, buf_size, "%d", id3->score);
             return buf;
 #endif

@@ -1064,7 +1064,7 @@ const char *get_token_value(struct gui_wps *gwps,
                         break;)
                 }
             }
-            snprintf(buf, buf_size, "%d.%1d", samprk/1000,samprk%1000);
+            snprintf(buf, buf_size, "%lu.%1lu", samprk/1000,samprk%1000);
 #else /* HWCODEC */

             static const char * const freq_strings[] =
@@ -1164,7 +1164,7 @@ const char *get_token_value(struct gui_wps *gwps,
                     #endif
                     *intval = global_settings.mp3_enc_config.bitrate+1;
                 }
-                snprintf(buf, buf_size, "%d", global_settings.mp3_enc_config.bitrate+1);
+                snprintf(buf, buf_size, "%lu", global_settings.mp3_enc_config.bitrate+1);
                 return buf;
             }
             else
diff --git a/apps/gui/statusbar.c b/apps/gui/statusbar.c
index bc5570e..901615d 100644
--- a/apps/gui/statusbar.c
+++ b/apps/gui/statusbar.c
@@ -19,11 +19,11 @@
  *
  ****************************************************************************/

+#include <stdio.h>
 #include "config.h"
 #include "font.h"
 #include "kernel.h"
 #include "string.h" /* for memcmp oO*/
-#include "sprintf.h"
 #include "sound.h"
 #include "settings.h"
 #include "viewport.h"
diff --git a/apps/gui/usb_screen.c b/apps/gui/usb_screen.c
index 019aec6..9895def 100644
--- a/apps/gui/usb_screen.c
+++ b/apps/gui/usb_screen.c
@@ -30,7 +30,6 @@
 #include "usb_core.h"
 #ifdef USB_ENABLE_HID
 #include "usb_keymaps.h"
-#include "sprintf.h"
 #endif
 #endif
 #include "settings.h"
diff --git a/apps/gui/viewport.c b/apps/gui/viewport.c
index 03f004b..7d12818 100644
--- a/apps/gui/viewport.c
+++ b/apps/gui/viewport.c
@@ -41,7 +41,6 @@
 /* all below isn't needed for pc tools (i.e. checkwps/wps editor)
  * only viewport_parse_viewport() is */
 #ifndef __PCTOOL__
-#include "sprintf.h"
 #include "string.h"
 #include "kernel.h"
 #include "system.h"
diff --git a/apps/gui/wps.c b/apps/gui/wps.c
index afd7f3f..916ca2c 100644
--- a/apps/gui/wps.c
+++ b/apps/gui/wps.c
@@ -31,7 +31,6 @@
 #include "action.h"
 #include "kernel.h"
 #include "filetypes.h"
-#include "sprintf.h"
 #include "settings.h"
 #include "skin_engine/skin_engine.h"
 #include "mp3_playback.h"
diff --git a/apps/main.c b/apps/main.c
index 5b9ecda..b043dac 100644
--- a/apps/main.c
+++ b/apps/main.c
@@ -49,7 +49,6 @@
 #include "status.h"
 #include "debug_menu.h"
 #include "version.h"
-#include "sprintf.h"
 #include "font.h"
 #include "language.h"
 #include "wps.h"
diff --git a/apps/menus/eq_menu.c b/apps/menus/eq_menu.c
index 16d5b82..066787a 100644
--- a/apps/menus/eq_menu.c
+++ b/apps/menus/eq_menu.c
@@ -35,7 +35,6 @@
 #include "icons.h"
 #include "font.h"
 #include "lang.h"
-#include "sprintf.h"
 #include "talk.h"
 #include "misc.h"
 #include "sound.h"
diff --git a/apps/menus/recording_menu.c b/apps/menus/recording_menu.c
index 800918b..daea1ea 100644
--- a/apps/menus/recording_menu.c
+++ b/apps/menus/recording_menu.c
@@ -38,7 +38,6 @@
 #include "scrollbar.h"
 #endif
 #include "lang.h"
-#include "sprintf.h"
 #include "talk.h"
 #include "misc.h"
 #include "sound.h"
diff --git a/apps/menus/settings_menu.c b/apps/menus/settings_menu.c
index db2ad78..1007d2a 100644
--- a/apps/menus/settings_menu.c
+++ b/apps/menus/settings_menu.c
@@ -35,7 +35,6 @@
 #include "usb.h"
 #include "splash.h"
 #include "talk.h"
-#include "sprintf.h"
 #include "powermgmt.h"
 #if CONFIG_CODEC == SWCODEC
 #include "playback.h"
diff --git a/apps/menus/time_menu.c b/apps/menus/time_menu.c
index 597f7e0..a529837 100644
--- a/apps/menus/time_menu.c
+++ b/apps/menus/time_menu.c
@@ -22,9 +22,9 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <limits.h>
+#include <stdio.h>
 #include "config.h"
 #include "string.h"
-#include "sprintf.h"
 #include "lang.h"
 #include "action.h"
 #include "settings.h"
@@ -201,7 +201,7 @@ static void draw_timedate(struct viewport *vp, struct screen *display)
 }


-static struct viewport clock[NB_SCREENS], menu[NB_SCREENS];
+static struct viewport clock_vps[NB_SCREENS], menu[NB_SCREENS];
 static bool menu_was_pressed;
 static int time_menu_callback(int action,
                        const struct menu_item_ex *this_item)
@@ -233,7 +233,7 @@ static int time_menu_callback(int action,
     {
         last_redraw = current_tick;
         FOR_NB_SCREENS(i)
-            draw_timedate(&clock[i], &screens[i]);
+            draw_timedate(&clock_vps[i], &screens[i]);
     }
     return action;
 }
@@ -257,30 +257,30 @@ int time_screen(void* ignored)

     FOR_NB_SCREENS(i)
     {
-        viewport_set_defaults(&clock[i], i);
+        viewport_set_defaults(&clock_vps[i], i);
 #ifdef HAVE_BUTTONBAR
         if (global_settings.buttonbar)
         {
-            clock[i].height -= BUTTONBAR_HEIGHT;
+            clock_vps[i].height -= BUTTONBAR_HEIGHT;
         }
 #endif
-        nb_lines = viewport_get_nb_lines(&clock[i]);
+        nb_lines = viewport_get_nb_lines(&clock_vps[i]);

-        menu[i] = clock[i];
+        menu[i] = clock_vps[i];
         /* force time to be drawn centered */
-        clock[i].flags |= VP_FLAG_ALIGN_CENTER;
+        clock_vps[i].flags |= VP_FLAG_ALIGN_CENTER;

-        font_h = font_get(clock[i].font)->height;
+        font_h = font_get(clock_vps[i].font)->height;
         nb_lines -= 2; /* at least 2 lines for menu */
         if (nb_lines > 4)
             nb_lines = 4;
         if (nb_lines >= 2)
-            clock[i].height = nb_lines*font_h;
-        else /* disable the clock drawing */
-            clock[i].height = 0;
-        menu[i].y += clock[i].height;
-        menu[i].height -= clock[i].height;
-        draw_timedate(&clock[i], &screens[i]);
+            clock_vps[i].height = nb_lines*font_h;
+        else /* disable the clock_vps drawing */
+            clock_vps[i].height = 0;
+        menu[i].y += clock_vps[i].height;
+        menu[i].height -= clock_vps[i].height;
+        draw_timedate(&clock_vps[i], &screens[i]);
     }

     ret = do_menu(&time_menu, NULL, menu, false);
diff --git a/apps/metadata.c b/apps/metadata.c
index 1dfc376..d66bc6e 100644
--- a/apps/metadata.c
+++ b/apps/metadata.c
@@ -19,10 +19,10 @@
  *
  ****************************************************************************/
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <inttypes.h>
+#include "inttypes.h"
+#include "string-extra.h"

 #include "debug.h"
 #include "logf.h"
diff --git a/apps/metadata/metadata_common.c b/apps/metadata/metadata_common.c
index fd1cd55..e1ef9a0 100644
--- a/apps/metadata/metadata_common.c
+++ b/apps/metadata/metadata_common.c
@@ -19,7 +19,7 @@
  *
  ****************************************************************************/
 #include <stdio.h>
-#include <string.h>
+#include "string-extra.h"
 #include <stdlib.h>
 #include <ctype.h>
 #include <inttypes.h>
diff --git a/apps/metadata/mp3.c b/apps/metadata/mp3.c
index 84c565b..49d5c73 100644
--- a/apps/metadata/mp3.c
+++ b/apps/metadata/mp3.c
@@ -30,11 +30,11 @@

 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <errno.h>
 #include <stdbool.h>
 #include <stddef.h>
-#include <ctype.h>
+#include <ctype.h> 
+#include "string-extra.h"
 #include "config.h"
 #include "file.h"
 #include "logf.h"
diff --git a/apps/metadata/smaf.c b/apps/metadata/smaf.c
index ca5b690..fa12c99 100644
--- a/apps/metadata/smaf.c
+++ b/apps/metadata/smaf.c
@@ -18,9 +18,9 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <string.h>
 #include <inttypes.h>

+#include "string-extra.h"
 #include "system.h"
 #include "metadata.h"
 #include "metadata_common.h"
diff --git a/apps/misc.c b/apps/misc.c
index f9c6116..0cdd71d 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -20,7 +20,9 @@
  ****************************************************************************/
 #include <stdlib.h>
 #include <ctype.h>
-#include <string.h>
+#include "string-extra.h"
+#include <stdio.h>
+#include <stdarg.h>
 #include "config.h"
 #include "misc.h"
 #include "lcd.h"
@@ -29,7 +31,6 @@
 #include <stdarg.h>
 #include <stdio.h>
 #else
-#include "sprintf.h"
 #include "lang.h"
 #include "dir.h"
 #include "lcd-remote.h"
diff --git a/apps/onplay.c b/apps/onplay.c
index e2037ad..feca281 100644
--- a/apps/onplay.c
+++ b/apps/onplay.c
@@ -25,7 +25,6 @@
 #include <stdbool.h>

 #include "debug.h"
-#include "sprintf.h"
 #include "lcd.h"
 #include "dir.h"
 #include "file.h"
diff --git a/apps/player/keyboard.c b/apps/player/keyboard.c
index e657f36..1c6dc9b 100644
--- a/apps/player/keyboard.c
+++ b/apps/player/keyboard.c
@@ -23,7 +23,6 @@
 #include "kernel.h"
 #include "system.h"
 #include "version.h"
-#include "sprintf.h"
 #include <string.h>
 #include "settings.h"
 #include "statusbar.h"
diff --git a/apps/playlist.c b/apps/playlist.c
index d90de38..2b16f0a 100644
--- a/apps/playlist.c
+++ b/apps/playlist.c
@@ -68,15 +68,14 @@
  */

 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
+#include <stdlib.h>      
+#include <ctype.h>                      
+#include "string-extra.h"
 #include "playlist.h"
 #include "ata_idle_notify.h"
 #include "file.h"
 #include "action.h"
 #include "dir.h"
-#include "sprintf.h"
 #include "debug.h"
 #include "audio.h"
 #include "lcd.h"
diff --git a/apps/playlist_catalog.c b/apps/playlist_catalog.c
index 44fd42f..113c3f6 100644
--- a/apps/playlist_catalog.c
+++ b/apps/playlist_catalog.c
@@ -22,7 +22,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
+#include "string-extra.h"
 #include "action.h"
 #include "dir.h"
 #include "file.h"
@@ -36,7 +36,6 @@
 #include "playlist.h"
 #include "settings.h"
 #include "splash.h"
-#include "sprintf.h"
 #include "tree.h"
 #include "yesno.h"
 #include "filetypes.h"
diff --git a/apps/playlist_viewer.c b/apps/playlist_viewer.c
index e2be36c..e0ab3bb 100644
--- a/apps/playlist_viewer.c
+++ b/apps/playlist_viewer.c
@@ -24,7 +24,6 @@
  * multi-screen support, rewrote a lot of code
  */
 #include <string.h>
-#include <sprintf.h>
 #include "playlist.h"
 #include "audio.h"
 #include "screens.h"
diff --git a/apps/plugin.c b/apps/plugin.c
index 2f54c0d..78a178f 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -21,7 +21,6 @@
 #include "plugin.h"
 #include <ctype.h>
 #include <string.h>
-#include <sprintf.h>
 #include <stdlib.h>
 #include "debug.h"
 #include "i2c.h"
@@ -436,7 +435,9 @@ static const struct plugin_api rockbox_api = {
     memset,
     memcpy,
     memmove,
+#ifndef SIMULATOR
     _ctype_,
+#endif
     atoi,
     strchr,
     strcat,
diff --git a/apps/plugin.h b/apps/plugin.h
index 31c8743..465ddd1 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -32,11 +32,12 @@
 #endif

 #include <stdbool.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "string-extra.h"

 char* strncpy(char *, const char *, size_t);
 void* plugin_get_buffer(size_t *buffer_size);
@@ -111,6 +112,11 @@ void* plugin_get_buffer(size_t *buffer_size);
 #include "usbstack/usb_hid_usage_tables.h"
 #endif

+
+/* on some platforms strcmp() seems to be a tricky define which
+ * breaks if we write down strcmp's prototype */
+#undef strcmp
+
 #ifdef PLUGIN

 #if defined(DEBUG) || defined(SIMULATOR)
@@ -529,7 +535,7 @@ struct plugin_api {
     /* strings and memory */
     int (*snprintf)(char *buf, size_t size, const char *fmt, ...)
                     ATTRIBUTE_PRINTF(3, 4);
-    int (*vsnprintf)(char *buf, int size, const char *fmt, va_list ap);
+    int (*vsnprintf)(char *buf, size_t size, const char *fmt, va_list ap);
     char* (*strcpy)(char *dst, const char *src);
     size_t (*strlcpy)(char *dst, const char *src, size_t length);
     size_t (*strlen)(const char *str);
@@ -541,7 +547,9 @@ struct plugin_api {
     void* (*memset)(void *dst, int c, size_t length);
     void* (*memcpy)(void *out, const void *in, size_t n);
     void* (*memmove)(void *out, const void *in, size_t n);
+#ifndef SIMULATOR
     const unsigned char *_rbctype_;
+#endif
     int (*atoi)(const char *str);
     char *(*strchr)(const char *s, int c);
     char *(*strcat)(char *s1, const char *s2);
diff --git a/apps/plugins/calculator.c b/apps/plugins/calculator.c
index 942f031..c19d6bb 100644
--- a/apps/plugins/calculator.c
+++ b/apps/plugins/calculator.c
@@ -102,6 +102,9 @@ PLUGIN_HEADER
 #define X_5_POS (X_4_POS + REC_WIDTH)  /* x5 = 110, column 111 left blank */

 #define SIGN(x) ((x)<0?-1:1)
+#ifndef ABS
+#define ABS(a) (((a) < 0) ? -(a) : (a))
+#endif

 /* variable button definitions */
 #if CONFIG_KEYPAD == RECORDER_PAD
diff --git a/apps/plugins/doom/d_deh.c b/apps/plugins/doom/d_deh.c
index d3f7496..82426c1 100644
--- a/apps/plugins/doom/d_deh.c
+++ b/apps/plugins/doom/d_deh.c
@@ -44,7 +44,7 @@
 #include "d_think.h"
 #include "w_wad.h"

-#include "sscanf.h"
+#include <stdio.h> /* sscanf */
 #include "rockmacros.h"

 #define TRUE 1
diff --git a/apps/plugins/doom/doom.make b/apps/plugins/doom/doom.make
index bdc3223..54a63eb 100644
--- a/apps/plugins/doom/doom.make
+++ b/apps/plugins/doom/doom.make
@@ -13,7 +13,7 @@ DOOMBUILDDIR := $(BUILDDIR)/apps/plugins/doom
 ROCKS += $(DOOMBUILDDIR)/doom.rock

 DOOM_SRC := $(call preprocess, $(DOOMSRCDIR)/SOURCES)
-DOOM_SRC += $(ROOTDIR)/firmware/common/sscanf.c
+DOOM_SRC += $(ROOTDIR)/firmware/libc/sscanf.c
 DOOM_OBJ := $(call c2obj, $(DOOM_SRC))

 # add source files to OTHER_SRC to get automatic dependencies
diff --git a/apps/plugins/doom/rockmacros.h b/apps/plugins/doom/rockmacros.h
index 98f908d..2f405a7 100644
--- a/apps/plugins/doom/rockmacros.h
+++ b/apps/plugins/doom/rockmacros.h
@@ -33,9 +33,13 @@ extern bool doomexit;
 int printf(const char *fmt, ...);
 int fileexists(const char * fname);
 char *my_strtok( char * s, const char * delim );
+#undef  alloca
 #define alloca             __builtin_alloca
-#define fdprintf(...)       rb->fdprintf(__VA_ARGS__)
+#undef  fdprintf
+#define fdprintf(...)      rb->fdprintf(__VA_ARGS__)
+#undef  vsnprintf
 #define vsnprintf(...)     rb->vsnprintf(__VA_ARGS__)
+#undef  read_line
 #define read_line(a,b,c)   rb->read_line((a),(b),(c))

 #ifdef SIMULATOR
@@ -52,19 +56,33 @@ int my_close(int id);
 #define filesize(a)        rb->filesize((a))
 #define read(a,b,c)        rb->read((a),(b),(c))
 #define write(a,b,c)       rb->write((a),(b),(c))
+#undef  strtok
 #define strtok(a,b)        my_strtok((a),(b))
+#undef  strcat
 #define strcat(a,b)        rb->strcat((a),(b))
+#undef  memset
 #define memset(a,b,c)      rb->memset((a),(b),(c))
+#undef  memmove
 #define memmove(a,b,c)     rb->memmove((a),(b),(c))
+#undef  memcmp
 #define memcmp(a,b,c)      rb->memcmp((a),(b),(c))
+#undef  memchr
 #define memchr(a,b,c)      rb->memchr((a),(b),(c))
+#undef  strcpy
 #define strcpy(a,b)        rb->strcpy((a),(b))
+#undef  strlen
 #define strlen(a)          rb->strlen((a))
+#undef  strcmp
 #define strcmp(a,b)        rb->strcmp((a),(b))
+#undef  strncmp
 #define strncmp(a,b,c)     rb->strncmp((a),(b),(c))
+#undef  strchr
 #define strchr(a,b)        rb->strchr((a),(b))
+#undef  strrchr
 #define strrchr(a,b)       rb->strrchr((a),(b))
+#undef  strcasecmp
 #define strcasecmp(a,b)    rb->strcasecmp((a),(b))
+#undef  strncasecmp
 #define strncasecmp(a,b,c) rb->strncasecmp((a),(b),(c))
 #define srand(a)           rb->srand((a))
 #define rand()             rb->rand()
diff --git a/apps/plugins/frotz/frotz.c b/apps/plugins/frotz/frotz.c
index 96029b8..f64431f 100644
--- a/apps/plugins/frotz/frotz.c
+++ b/apps/plugins/frotz/frotz.c
@@ -285,14 +285,14 @@ void os_beep(int volume)
 static unsigned char unget_buf;
 static int unget_file;

-int ungetc(int c, int f)
+int frotz_ungetc(int c, int f)
 {
     unget_file = f;
     unget_buf = c;
     return c;
 }

-int fgetc(int f)
+int frotz_fgetc(int f)
 {
     unsigned char cb;
     if (unget_file == f)
@@ -305,7 +305,7 @@ int fgetc(int f)
     return cb;
 }

-int fputc(int c, int f)
+int frotz_fputc(int c, int f)
 {
     unsigned char cb = c;
     if (rb->write(f, &cb, 1) != 1)
diff --git a/apps/plugins/frotz/frotzplugin.h b/apps/plugins/frotz/frotzplugin.h
index 8caddb4..1a52bdb 100644
--- a/apps/plugins/frotz/frotzplugin.h
+++ b/apps/plugins/frotz/frotzplugin.h
@@ -37,9 +37,12 @@
 /*
  * we need functions for character io
  */
-extern int ungetc(int c, int f);
-extern int fgetc(int f);
-extern int fputc(int c, int f);
+extern int frotz_ungetc(int c, int f);
+#define ungetc frotz_ungetc
+extern int frotz_fgetc(int f);
+#define fgetc frotz_fgetc
+extern int frotz_fputc(int c, int f);
+#define fputc frotz_fputc

 /*
  * this is used instead of os_read_key for more prompts and the like
diff --git a/apps/plugins/imageviewer/png/zconf.h b/apps/plugins/imageviewer/png/zconf.h
index 03a9431..b3ca88c 100644
--- a/apps/plugins/imageviewer/png/zconf.h
+++ b/apps/plugins/imageviewer/png/zconf.h
@@ -285,7 +285,7 @@ typedef uLong FAR uLongf;
 #endif

 #if 0           /* HAVE_UNISTD_H -- this line is updated by ./configure */
-#  include <sys/types.h> /* for off_t */
+#  include "inttypes.h" /* for off_t */
 #  include <unistd.h>    /* for SEEK_* and off_t */
 #  ifdef VMS
 #    include <unixio.h>   /* for off_t */
diff --git a/apps/plugins/invadrox.c b/apps/plugins/invadrox.c
index 3e5c98e..851487c 100644
--- a/apps/plugins/invadrox.c
+++ b/apps/plugins/invadrox.c
@@ -81,6 +81,10 @@ PLUGIN_HEADER
 #define DBG(format, arg...) {}
 #endif

+#ifndef ABS
+#define ABS(a) (((a) < 0) ? -(a) : (a))
+#endif
+
 #if CONFIG_KEYPAD == IRIVER_H100_PAD

 #define QUIT BUTTON_OFF
diff --git a/apps/plugins/lib/buflib.c b/apps/plugins/lib/buflib.c
index 5d03ca4..930e49d 100644
--- a/apps/plugins/lib/buflib.c
+++ b/apps/plugins/lib/buflib.c
@@ -23,8 +23,8 @@
 *
 ****************************************************************************/

+#include <stdlib.h> /* for abs() */
 #include "buflib.h"
-
 /* The main goal of this design is fast fetching of the pointer for a handle.
  * For that reason, the handles are stored in a table at the end of the buffer
  * with a fixed address, so that returning the pointer for a handle is a simple
@@ -311,7 +311,7 @@ buflib_free(struct buflib_context *ctx, int handle_num)
     while (next_block < freed_block)
     {
         block = next_block;
-        next_block += ABS(block->val);
+        next_block += abs(block->val);
     }
     /* If next_block == block, the above loop didn't go anywhere. If it did,
      * and the block before this one is empty, we can combine them.
diff --git a/apps/plugins/lib/jhash.h b/apps/plugins/lib/jhash.h
index 97d1ac3..bee45b0 100644
--- a/apps/plugins/lib/jhash.h
+++ b/apps/plugins/lib/jhash.h
@@ -24,7 +24,7 @@
 #ifndef _LIB_JHASH_H_
 #define _LIB_JHASH_H_
 #include <inttypes.h>     /* defines uint32_t etc */
-#include <sys/types.h>
+#include "inttypes.h"
 #include <plugin.h>

 /*
diff --git a/apps/plugins/lib/strncpy.c b/apps/plugins/lib/strncpy.c
index 7c1973b..f80e759 100644
--- a/apps/plugins/lib/strncpy.c
+++ b/apps/plugins/lib/strncpy.c
@@ -39,6 +39,7 @@ QUICKREF

 #include <string.h>
 #include <limits.h>
+#include "plugin.h"

 /*SUPPRESS 560*/
 /*SUPPRESS 530*/
diff --git a/apps/plugins/lua/gmtime.c b/apps/plugins/lua/gmtime.c
index f13c855..c096597 100644
--- a/apps/plugins/lua/gmtime.c
+++ b/apps/plugins/lua/gmtime.c
@@ -20,13 +20,6 @@ const short __spm[13] =
     (31+28+31+30+31+30+31+31+30+31+30+31),
   };

-int __isleap(int year) {
-  /* every fourth year is a leap year except for century years that are
-   * not divisible by 400. */
-/*  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
-  return (!(year%4) && ((year%100) || !(year%400)));
-}
-
 struct tm *gmtime(const time_t *timep) {
   static struct tm r;
   time_t i;
diff --git a/apps/plugins/lua/malloc.c b/apps/plugins/lua/malloc.c
index 6dc00c4..8770586 100644
--- a/apps/plugins/lua/malloc.c
+++ b/apps/plugins/lua/malloc.c
@@ -468,7 +468,7 @@ DEFAULT_MMAP_THRESHOLD       default: 256K
 #endif  /* DARWIN */

 #ifndef LACKS_SYS_TYPES_H
-#include <sys/types.h>  /* For size_t */
+#include "inttypes.h"  /* For size_t */
 #endif  /* LACKS_SYS_TYPES_H */

 /* The maximum possible size_t value has all bits set */
diff --git a/apps/plugins/lua/strftime.c b/apps/plugins/lua/strftime.c
index 681d4be..feac0b9 100644
--- a/apps/plugins/lua/strftime.c
+++ b/apps/plugins/lua/strftime.c
@@ -1,4 +1,4 @@
-#include <sys/types.h>
+#include "inttypes.h"
 #include <time.h>
 #include "plugin.h"

diff --git a/apps/plugins/pdbox/PDa/extra/dumpOSC.c b/apps/plugins/pdbox/PDa/extra/dumpOSC.c
index b4ef97d..3c1867c 100644
--- a/apps/plugins/pdbox/PDa/extra/dumpOSC.c
+++ b/apps/plugins/pdbox/PDa/extra/dumpOSC.c
@@ -88,7 +88,7 @@ void sys_addpollfn(int fd, t_fdpollfn fn, void *ptr);
 	#include <string.h>
 	#include <stdlib.h>
 	#include <fcntl.h>
-	#include <sys/types.h>
+	#include "inttypes.h"
 	#include <sys/stat.h>
 	#include <ctype.h>
 	#include <signal.h>
@@ -98,7 +98,7 @@ void sys_addpollfn(int fd, t_fdpollfn fn, void *ptr);
 	#include <stdlib.h>
 	#include <unistd.h>
 	#include <fcntl.h>
-	#include <sys/types.h>
+	#include "inttypes.h"
 	#include <sys/stat.h>
 	#include <netinet/in.h>
 	#include <rpc/rpc.h>
diff --git a/apps/plugins/pdbox/PDa/extra/sendOSC.c b/apps/plugins/pdbox/PDa/extra/sendOSC.c
index 6bb809d..d9420aa 100644
--- a/apps/plugins/pdbox/PDa/extra/sendOSC.c
+++ b/apps/plugins/pdbox/PDa/extra/sendOSC.c
@@ -51,11 +51,11 @@ The OSC webpage is http://cnmat.cnmat.berkeley.edu/OpenSoundControl
 #include "OSC-client.h"

 #include <string.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/stat.h>
-#include <sys/types.h>
+#include "inttypes.h"

 #ifdef WIN32
 #include <winsock2.h>	
@@ -1055,12 +1055,12 @@ void SendData(void *htmsocket, int size, char *data) {
 	#include 	<stdio.h>    
 	#include 	<errno.h>
 	#include 	<fcntl.h>
-	#include 	<sys/types.h>
+	#include 	"inttypes.h"
 	#include 	<sys/stat.h>
 #endif

 #ifdef __APPLE__
-  #include <sys/types.h>
+  #include "inttypes.h"
 #endif

 #ifdef unix
diff --git a/apps/plugins/pdbox/PDa/extra/shell.c b/apps/plugins/pdbox/PDa/extra/shell.c
index 8653c63..861bf94 100644
--- a/apps/plugins/pdbox/PDa/extra/shell.c
+++ b/apps/plugins/pdbox/PDa/extra/shell.c
@@ -10,7 +10,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/wait.h>
 #include <signal.h>
 #include <sched.h>
diff --git a/apps/plugins/pdbox/PDa/src/d_array.c b/apps/plugins/pdbox/PDa/src/d_array.c
index 7139e4d..8314e19 100644
--- a/apps/plugins/pdbox/PDa/src/d_array.c
+++ b/apps/plugins/pdbox/PDa/src/d_array.c
@@ -516,7 +516,7 @@ static void tabread4_tilde_setup(void)
 #define HIOFFSET 0    /* word offset to find MSB */
 #define LOWOFFSET 1    /* word offset to find LSB */
 #endif /* BYTE_ORDER */
-#include <sys/types.h>
+#include "inttypes.h"
 #define int32 int32_t
 #endif

@@ -534,7 +534,7 @@ static void tabread4_tilde_setup(void)
 #define LOWOFFSET 1    /* word offset to find LSB */                            
 #endif /* __BYTE_ORDER */                                                       

-#include <sys/types.h>
+#include "inttypes.h"
 #define int32 int32_t

 #else
diff --git a/apps/plugins/pdbox/PDa/src/d_osc.c b/apps/plugins/pdbox/PDa/src/d_osc.c
index 35b43b8..7ee9cae 100644
--- a/apps/plugins/pdbox/PDa/src/d_osc.c
+++ b/apps/plugins/pdbox/PDa/src/d_osc.c
@@ -33,7 +33,7 @@
 #define HIOFFSET 0    /* word offset to find MSB */
 #define LOWOFFSET 1    /* word offset to find LSB */
 #endif /* BYTE_ORDER */
-#include <sys/types.h>
+#include "inttypes.h"
 #define int32 int32_t
 #endif
 #ifdef __linux__
@@ -52,7 +52,7 @@
 #define LOWOFFSET 1    /* word offset to find LSB */                            
 #endif /* __BYTE_ORDER */                                                       

-#include <sys/types.h>
+#include "inttypes.h"
 #define int32 int32_t

 #else
diff --git a/apps/plugins/pdbox/PDa/src/d_soundfile.c b/apps/plugins/pdbox/PDa/src/d_soundfile.c
index 479655f..47174f0 100644
--- a/apps/plugins/pdbox/PDa/src/d_soundfile.c
+++ b/apps/plugins/pdbox/PDa/src/d_soundfile.c
@@ -1436,7 +1436,7 @@ static void pute(char *s)   /* debug routine */
 #define sfread_cond_signal pthread_cond_signal
 #else
 #include <sys/time.h>    /* debugging version... */
-#include <sys/types.h>
+#include "inttypes.h"
 static void readsf_fakewait(pthread_mutex_t *b)
 {
     struct timeval timout;
diff --git a/apps/plugins/pdbox/PDa/src/s_audio_alsa.c b/apps/plugins/pdbox/PDa/src/s_audio_alsa.c
index b8e8535..8ff5584 100644
--- a/apps/plugins/pdbox/PDa/src/s_audio_alsa.c
+++ b/apps/plugins/pdbox/PDa/src/s_audio_alsa.c
@@ -14,7 +14,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
diff --git a/apps/plugins/pdbox/PDa/src/s_audio_oss.c b/apps/plugins/pdbox/PDa/src/s_audio_oss.c
index de11f66..fd863ae 100644
--- a/apps/plugins/pdbox/PDa/src/s_audio_oss.c
+++ b/apps/plugins/pdbox/PDa/src/s_audio_oss.c
@@ -16,7 +16,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/time.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
diff --git a/apps/plugins/pdbox/PDa/src/s_file.c b/apps/plugins/pdbox/PDa/src/s_file.c
index 4995e87..c7e70ca 100644
--- a/apps/plugins/pdbox/PDa/src/s_file.c
+++ b/apps/plugins/pdbox/PDa/src/s_file.c
@@ -14,7 +14,7 @@
 #else /* ROCKBOX */
 #include "m_pd.h"
 #include "s_stuff.h"
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #endif /* ROCKBOX */

diff --git a/apps/plugins/pdbox/PDa/src/s_inter.c b/apps/plugins/pdbox/PDa/src/s_inter.c
index 9df9c82..07e304a 100644
--- a/apps/plugins/pdbox/PDa/src/s_inter.c
+++ b/apps/plugins/pdbox/PDa/src/s_inter.c
@@ -37,7 +37,7 @@ typedef int pid_t;
 #include <stdio.h>

 #ifdef MACOSX
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <pthread.h>
 #else
diff --git a/apps/plugins/pdbox/PDa/src/s_main.c b/apps/plugins/pdbox/PDa/src/s_main.c
index b77f804..0b77b44 100644
--- a/apps/plugins/pdbox/PDa/src/s_main.c
+++ b/apps/plugins/pdbox/PDa/src/s_main.c
@@ -14,7 +14,7 @@ char pd_compiledate[] = __DATE__;
 #include "m_pd.h"
 #include "m_imp.h"
 #include "s_stuff.h"
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <limits.h>
 #include <string.h>
diff --git a/apps/plugins/pdbox/PDa/src/s_midi.c b/apps/plugins/pdbox/PDa/src/s_midi.c
index 72a8792..c24ba9e 100644
--- a/apps/plugins/pdbox/PDa/src/s_midi.c
+++ b/apps/plugins/pdbox/PDa/src/s_midi.c
@@ -16,7 +16,7 @@
 #endif
 #ifdef MSW
 #include <winsock.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/timeb.h> 
 #include <wtypes.h>
 #endif
diff --git a/apps/plugins/pdbox/PDa/src/s_midi_oss.c b/apps/plugins/pdbox/PDa/src/s_midi_oss.c
index fab1d84..c035390 100644
--- a/apps/plugins/pdbox/PDa/src/s_midi_oss.c
+++ b/apps/plugins/pdbox/PDa/src/s_midi_oss.c
@@ -10,7 +10,7 @@
 #include <unistd.h>
 #endif
 #include <stdlib.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
diff --git a/apps/plugins/pdbox/PDa/src/s_midi_sgi.c b/apps/plugins/pdbox/PDa/src/s_midi_sgi.c
index 105a812..fcfd0c0 100644
--- a/apps/plugins/pdbox/PDa/src/s_midi_sgi.c
+++ b/apps/plugins/pdbox/PDa/src/s_midi_sgi.c
@@ -10,7 +10,7 @@
 #ifdef HAVE_BSTRING_H
 #include <bstring.h>
 #endif
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/time.h>

 #include <dmedia/audio.h>
diff --git a/apps/plugins/pdbox/PDa/src/s_watchdog.c b/apps/plugins/pdbox/PDa/src/s_watchdog.c
index e8653e9..6a9060f 100644
--- a/apps/plugins/pdbox/PDa/src/s_watchdog.c
+++ b/apps/plugins/pdbox/PDa/src/s_watchdog.c
@@ -7,7 +7,7 @@ tries to prevent Pd from locking up the processor if it's at realtime
 priority.  Linux only.  Invoked from s_inter.c. */

 #include <sys/time.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include <unistd.h>
 #include <signal.h>
 #include <stdio.h>
diff --git a/apps/plugins/pdbox/PDa/src/t_tkcmd.c b/apps/plugins/pdbox/PDa/src/t_tkcmd.c
index ff12a28..79a3270 100644
--- a/apps/plugins/pdbox/PDa/src/t_tkcmd.c
+++ b/apps/plugins/pdbox/PDa/src/t_tkcmd.c
@@ -11,7 +11,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #ifdef UNIX
 #include <unistd.h>
 #include <sys/socket.h>
diff --git a/apps/plugins/pdbox/PDa/src/u_pdreceive.c b/apps/plugins/pdbox/PDa/src/u_pdreceive.c
index 51e60f7..204e20f 100644
--- a/apps/plugins/pdbox/PDa/src/u_pdreceive.c
+++ b/apps/plugins/pdbox/PDa/src/u_pdreceive.c
@@ -6,7 +6,7 @@
 from Pd via the netsend/netreceive ("FUDI") protocol, and copies them to
 standard output. */

-#include <sys/types.h>
+#include "inttypes.h"
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
diff --git a/apps/plugins/pdbox/PDa/src/u_pdsend.c b/apps/plugins/pdbox/PDa/src/u_pdsend.c
index 4fe714d..cc9d668 100644
--- a/apps/plugins/pdbox/PDa/src/u_pdsend.c
+++ b/apps/plugins/pdbox/PDa/src/u_pdsend.c
@@ -5,7 +5,7 @@
 /* the "pdsend" command.  This is a standalone program that forwards messages
 from its standard input to Pd via the netsend/netreceive ("FUDI") protocol. */

-#include <sys/types.h>
+#include "inttypes.h"
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
diff --git a/apps/plugins/pdbox/PDa/src/x_misc.c b/apps/plugins/pdbox/PDa/src/x_misc.c
index 10d872f..1bfce49 100644
--- a/apps/plugins/pdbox/PDa/src/x_misc.c
+++ b/apps/plugins/pdbox/PDa/src/x_misc.c
@@ -17,7 +17,7 @@ extern uint64_t runningtime;
 #include <stdio.h>
 #include <string.h>
 #ifdef UNIX
-#include <sys/types.h>
+#include "inttypes.h"
 #include <sys/time.h>
 #include <sys/times.h>
 //#include <sys/param.h>
diff --git a/apps/plugins/pdbox/PDa/src/x_net.c b/apps/plugins/pdbox/PDa/src/x_net.c
index ecd02d6..1cb9655 100644
--- a/apps/plugins/pdbox/PDa/src/x_net.c
+++ b/apps/plugins/pdbox/PDa/src/x_net.c
@@ -13,7 +13,7 @@
 #include "s_stuff.h"

 #ifndef ROCKBOX
-#include <sys/types.h>
+#include "inttypes.h"
 #include <string.h>
 #ifdef UNIX
 #include <sys/socket.h>
diff --git a/apps/plugins/random_folder_advance_config.c b/apps/plugins/random_folder_advance_config.c
index 5f83798..1995315 100644
--- a/apps/plugins/random_folder_advance_config.c
+++ b/apps/plugins/random_folder_advance_config.c
@@ -23,7 +23,7 @@

 PLUGIN_HEADER

-static bool abort;
+static bool cancel;
 static int fd;
 static int dirs_count;
 static int lasttick;
@@ -70,7 +70,7 @@ void traversedir(char* location, char* name)
     if (dir) {
         entry = rb->readdir(dir);
         while (entry) {
-            if (abort)
+            if (cancel)
                 break;
             /* Skip .. and . */
             if (entry->d_name[0] == '.')
@@ -113,7 +113,7 @@ void traversedir(char* location, char* name)
                 lasttick = *rb->current_tick;
                 if (rb->action_userabort(TIMEOUT_NOBLOCK))
                 {
-                    abort = true;
+                    cancel = true;
                     break;
                 }
             }
@@ -213,7 +213,7 @@ bool custom_dir(void)
 void generate(void)
 {
     dirs_count = 0;
-    abort = false;
+    cancel = false;
     fd = rb->open(RFA_FILE,O_CREAT|O_WRONLY);
     rb->write(fd,&dirs_count,sizeof(int));
     if (fd < 0)
@@ -621,7 +621,7 @@ enum plugin_status plugin_start(const void* parameter)
 {
     (void)parameter;

-    abort = false;
+    cancel = false;

     return main_menu();
 }
diff --git a/apps/plugins/rockboy/rockboy.make b/apps/plugins/rockboy/rockboy.make
index ca0ed17..81e9a1f 100644
--- a/apps/plugins/rockboy/rockboy.make
+++ b/apps/plugins/rockboy/rockboy.make
@@ -11,7 +11,7 @@ ROCKBOY_SRCDIR = $(APPSDIR)/plugins/rockboy
 ROCKBOY_OBJDIR = $(BUILDDIR)/apps/plugins/rockboy

 ROCKBOY_SRC := $(call preprocess, $(ROCKBOY_SRCDIR)/SOURCES)
-ROCKBOY_SRC += $(ROOTDIR)/firmware/common/sscanf.c
+ROCKBOY_SRC += $(ROOTDIR)/firmware/libc/sscanf.c
 ROCKBOY_OBJ := $(call c2obj, $(ROCKBOY_SRC))

 OTHER_SRC += $(ROCKBOY_SRC)
diff --git a/apps/plugins/rockboy/rtc.c b/apps/plugins/rockboy/rtc.c
index 6f33bc4..248ebc0 100644
--- a/apps/plugins/rockboy/rtc.c
+++ b/apps/plugins/rockboy/rtc.c
@@ -7,7 +7,6 @@
 #include "defs.h"
 #include "mem.h"
 #include "rtc-gb.h"
-#include "sscanf.h"

 struct rtc rtc;

diff --git a/apps/plugins/search.c b/apps/plugins/search.c
index 4f60c82..36eda39 100644
--- a/apps/plugins/search.c
+++ b/apps/plugins/search.c
@@ -20,7 +20,7 @@
  *
  ****************************************************************************/
 #include "plugin.h"
-#include "ctype.h"
+#include <ctype.h>

 PLUGIN_HEADER

diff --git a/apps/plugins/stats.c b/apps/plugins/stats.c
index eaa1055..3420579 100644
--- a/apps/plugins/stats.c
+++ b/apps/plugins/stats.c
@@ -24,7 +24,7 @@ PLUGIN_HEADER

 static int files, dirs, musicfiles, largestdir;
 static int lasttick;
-static bool abort;
+static bool cancel;

 #if CONFIG_KEYPAD == PLAYER_PAD 
 #define STATS_STOP BUTTON_STOP
@@ -173,7 +173,7 @@ void traversedir(char* location, char* name)
     if (dir) {
         entry = rb->readdir(dir);
         while (entry) {
-            if (abort)
+            if (cancel)
                 break;
             /* Skip .. and . */
             if (rb->strcmp(entry->d_name, ".") && rb->strcmp(entry->d_name, ".."))
@@ -206,7 +206,7 @@ void traversedir(char* location, char* name)
                     || button == STATS_STOP_REMOTE
 #endif
                     ) {
-                    abort = true;
+                    cancel = true;
                     break;
                 }
             }
@@ -229,14 +229,14 @@ enum plugin_status plugin_start(const void* parameter)
     dirs = 0;
     musicfiles = 0;
     largestdir = 0;
-    abort = false;
+    cancel = false;

     rb->splash(HZ, "Counting...");
     update_screen();
     lasttick = *rb->current_tick;

     traversedir("", "");
-    if (abort) {
+    if (cancel) {
         rb->splash(HZ, "Aborted");
         return PLUGIN_OK;
     }
diff --git a/apps/plugins/wav2wv.c b/apps/plugins/wav2wv.c
index 1102d13..ed5f58e 100644
--- a/apps/plugins/wav2wv.c
+++ b/apps/plugins/wav2wv.c
@@ -97,8 +97,8 @@ static int32_t temp_buffer [TEMP_SAMPLES] IDATA_ATTR;
 static int wav2wv(const char *infile)
 {
     int in_fd, out_fd, num_chans, error = false, last_buttons;
-    unsigned int32_t total_bytes_read = 0, total_bytes_written = 0;
-    unsigned int32_t total_samples, samples_remaining;
+    uint32_t total_bytes_read = 0, total_bytes_written = 0;
+    uint32_t total_samples, samples_remaining;
     int32_t *input_buffer = (int32_t *) audiobuf;
     unsigned char *output_buffer = (unsigned char *)(audiobuf + 0x100000);
     char outfile[MAX_PATH];
@@ -180,7 +180,7 @@ static int wav2wv(const char *infile)
     wvupdate (start_tick, native_header.SampleRate, total_samples, 0, 0, 0);

     for (samples_remaining = total_samples; samples_remaining;) {
-        unsigned int32_t samples_count, samples_to_pack, bytes_count;
+        uint32_t samples_count, samples_to_pack, bytes_count;
         int cnt, buttons;
         int32_t value, *lp;
         signed char *cp;
@@ -204,7 +204,7 @@ static int wav2wv(const char *infile)
         cp = (signed char *) input_buffer;

         while (samples_to_pack) {
-            unsigned int32_t samples_this_pass = TEMP_SAMPLES / num_chans;
+            uint32_t samples_this_pass = TEMP_SAMPLES / num_chans;

             if (samples_this_pass > samples_to_pack)
                 samples_this_pass = samples_to_pack;
diff --git a/apps/plugins/zxbox/helpers.h b/apps/plugins/zxbox/helpers.h
index ba8607d..c8cb9c1 100644
--- a/apps/plugins/zxbox/helpers.h
+++ b/apps/plugins/zxbox/helpers.h
@@ -7,9 +7,12 @@ extern off_t my_ftell(int);
 extern void *my_malloc(size_t size);


+#undef getc
 #define getc my_getc
-#define malloc my_malloc
-#define ftell my_ftell
+#undef putc
 #define putc my_putc
+#undef ftell
+#define ftell my_ftell
+#define malloc my_malloc

 #endif /* HELPERS_H */
diff --git a/apps/plugins/zxbox/snapshot.c b/apps/plugins/zxbox/snapshot.c
index 6efb29f..d3ae9d3 100644
--- a/apps/plugins/zxbox/snapshot.c
+++ b/apps/plugins/zxbox/snapshot.c
@@ -18,8 +18,9 @@
  *
  */

+#include <stdlib.h>
+#include "inttypes.h"
 #include "zxmisc.h"
-#include "helpers.h"
 #include "spperif.h"
 #include "z80.h"

@@ -29,9 +30,8 @@

 #include "spconf.h"

-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
+/* include this at last to avoid clashes with stdio functions */
+#include "helpers.h"

 #define COMPRESS_SAVE 1

diff --git a/apps/plugins/zxbox/spconf.c b/apps/plugins/zxbox/spconf.c
index 728f786..057631f 100644
--- a/apps/plugins/zxbox/spconf.c
+++ b/apps/plugins/zxbox/spconf.c
@@ -28,7 +28,7 @@
 #include "zxconfig.h"
 #include "stdio.h"
 #include "string.h"
-#include "sys/types.h"
+#include "inttypes.h"
 #include "helpers.h"
 #include "ctype.h"

diff --git a/apps/plugins/zxbox/sptape.c b/apps/plugins/zxbox/sptape.c
index 21f962e..4dcbadf 100644
--- a/apps/plugins/zxbox/sptape.c
+++ b/apps/plugins/zxbox/sptape.c
@@ -31,7 +31,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <sys/types.h>
+#include "inttypes.h"

 #define MAXLINELEN 256

diff --git a/apps/plugins/zxbox/tapefile.c b/apps/plugins/zxbox/tapefile.c
index 0e262aa..92f4ea6 100644
--- a/apps/plugins/zxbox/tapefile.c
+++ b/apps/plugins/zxbox/tapefile.c
@@ -29,7 +29,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include "zxconfig.h"
 #include "helpers.h"
 #define max(x, y) ((x) > (y) ? (x) : (y))
diff --git a/apps/plugins/zxbox/zxmisc.h b/apps/plugins/zxbox/zxmisc.h
index 4ad25a0..d9d7dba 100644
--- a/apps/plugins/zxbox/zxmisc.h
+++ b/apps/plugins/zxbox/zxmisc.h
@@ -21,7 +21,7 @@
 #ifndef ZXMISC_H
 #define ZXMISC_H

-#include <sys/types.h>
+#include "inttypes.h"

 extern char *get_base_name(char *fname);
 extern int   check_ext(const char *filename, const char *ext);
diff --git a/apps/recorder/albumart.c b/apps/recorder/albumart.c
index be78753..5eca713 100644
--- a/apps/recorder/albumart.c
+++ b/apps/recorder/albumart.c
@@ -19,8 +19,7 @@
  *
  ****************************************************************************/

-#include <string.h>
-#include "sprintf.h"
+#include "string-extra.h"
 #include "system.h"
 #include "albumart.h"
 #include "metadata.h"
diff --git a/apps/recorder/icons.c b/apps/recorder/icons.c
index 0b48c12..e6c3926 100644
--- a/apps/recorder/icons.c
+++ b/apps/recorder/icons.c
@@ -22,7 +22,6 @@
 #include "lcd.h"
 #include "font.h"
 #include "kernel.h"
-#include "sprintf.h"
 #include "rtc.h"
 #include "powermgmt.h"

diff --git a/apps/recorder/jpeg_load.c b/apps/recorder/jpeg_load.c
index f75176d..1af65fa 100644
--- a/apps/recorder/jpeg_load.c
+++ b/apps/recorder/jpeg_load.c
@@ -856,7 +856,7 @@ static const struct idct_entry idct_tbl[] = {
 /* JPEG decoder implementation */

 #ifdef JPEG_FROM_MEM
-INLINE unsigned char *getc(struct jpeg* p_jpeg)
+INLINE unsigned char *jpeg_getc(struct jpeg* p_jpeg)
 {
     if (LIKELY(p_jpeg->len))
     {
@@ -880,7 +880,7 @@ INLINE bool skip_bytes(struct jpeg* p_jpeg, int count)
     }
 }

-INLINE void putc(struct jpeg* p_jpeg)
+INLINE void jpeg_putc(struct jpeg* p_jpeg)
 {
     p_jpeg->len++;
     p_jpeg->data--;
@@ -892,7 +892,7 @@ INLINE void fill_buf(struct jpeg* p_jpeg)
         p_jpeg->buf_index = 0;
 }

-static unsigned char *getc(struct jpeg* p_jpeg)
+static unsigned char *jpeg_getc(struct jpeg* p_jpeg)
 {
     if (UNLIKELY(p_jpeg->buf_left < 1))
         fill_buf(p_jpeg);
@@ -917,7 +917,7 @@ static bool skip_bytes(struct jpeg* p_jpeg, int count)
     return p_jpeg->buf_left >= 0 || skip_bytes_seek(p_jpeg);
 }

-static void putc(struct jpeg* p_jpeg)
+static void jpeg_putc(struct jpeg* p_jpeg)
 {
     p_jpeg->buf_left++;
     p_jpeg->buf_index--;
@@ -933,14 +933,14 @@ do {\
 #define e_getc(jpeg, code) \
 ({ \
     unsigned char *c; \
-    if (UNLIKELY(!(c = getc(jpeg)))) \
+    if (UNLIKELY(!(c = jpeg_getc(jpeg)))) \
         return (code); \
     *c; \
 })

 #define d_getc(jpeg, def) \
 ({ \
-    unsigned char *cp = getc(jpeg); \
+    unsigned char *cp = jpeg_getc(jpeg); \
     unsigned char c = LIKELY(cp) ? *cp : (def); \
     c; \
 })
@@ -958,7 +958,7 @@ static int process_markers(struct jpeg* p_jpeg)
         if (c != 0xFF) /* no marker? */
         {
             JDEBUGF("Non-marker data\n");
-            putc(p_jpeg);
+            jpeg_putc(p_jpeg);
             break; /* exit marker processing */
         }

@@ -969,7 +969,7 @@ static int process_markers(struct jpeg* p_jpeg)
         case 0xFF: /* Fill byte */
             ret |= FILL_FF;
         case 0x00: /* Zero stuffed byte - entropy data */
-            putc(p_jpeg);
+            jpeg_putc(p_jpeg);
             continue;

         case 0xC0: /* SOF Huff  - Baseline DCT */
@@ -1669,7 +1669,7 @@ static void search_restart(struct jpeg *p_jpeg)
                 return;
             }
             else
-                putc(p_jpeg);
+                jpeg_putc(p_jpeg);
         }
     }
 }
diff --git a/apps/recorder/keyboard.c b/apps/recorder/keyboard.c
index 3df3142..f8b390d 100644
--- a/apps/recorder/keyboard.c
+++ b/apps/recorder/keyboard.c
@@ -20,7 +20,7 @@
  ****************************************************************************/
 #include "kernel.h"
 #include "system.h"
-#include <string.h>
+#include "string-extra.h"
 #include "font.h"
 #include "screens.h"
 #include "talk.h"
diff --git a/apps/recorder/peakmeter.c b/apps/recorder/peakmeter.c
index 8f32a83..154f605 100644
--- a/apps/recorder/peakmeter.c
+++ b/apps/recorder/peakmeter.c
@@ -29,7 +29,6 @@
 #include "storage.h"
 #include "lcd.h"
 #include "scrollbar.h"
-#include "sprintf.h"
 #include "button.h"
 #include "system.h"
 #include "font.h"
diff --git a/apps/recorder/radio.c b/apps/recorder/radio.c
index 0523f1e..c46f660 100644
--- a/apps/recorder/radio.c
+++ b/apps/recorder/radio.c
@@ -23,7 +23,6 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <stdlib.h>
-#include "sprintf.h"
 #include "mas.h"
 #include "settings.h"
 #include "button.h"
@@ -35,7 +34,7 @@
 #include "file.h"
 #include "general.h"
 #include "errno.h"
-#include "string.h"
+#include "string-extra.h"
 #include "system.h"
 #include "radio.h"
 #include "menu.h"
diff --git a/apps/recorder/recording.c b/apps/recorder/recording.c
index fc52792..0f1ff30 100644
--- a/apps/recorder/recording.c
+++ b/apps/recorder/recording.c
@@ -1762,7 +1762,7 @@ bool recording_screen(bool no_source)
                    draw attention */
                 /* Don't use language string unless agreed upon to make this
                    method permanent - could do something in the statusbar */
-                snprintf(buf, sizeof(buf), "Warning: %08X",
+                snprintf(buf, sizeof(buf), "Warning: %08lX",
                          pcm_rec_get_warnings());
             }
             else
@@ -1771,7 +1771,7 @@ bool recording_screen(bool no_source)
                 (global_settings.rec_split_method))
             {
                 dmb = dsize/1024/1024;
-                snprintf(buf, sizeof(buf), "%s %dMB",
+                snprintf(buf, sizeof(buf), "%s %luMB",
                          str(LANG_SPLIT_SIZE), dmb);
             }
             else
diff --git a/apps/replaygain.c b/apps/replaygain.c
index 6ece110..87d01c9 100644
--- a/apps/replaygain.c
+++ b/apps/replaygain.c
@@ -20,13 +20,14 @@
  ****************************************************************************/

 #include <ctype.h>
-#include <inttypes.h>
 #include <math.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
+#include "strlcpy.h"
+#include "strcasecmp.h"
 #include <system.h>
+#include "inttypes.h"
 #include "metadata.h"
 #include "debug.h"
 #include "replaygain.h"
@@ -244,7 +245,7 @@ long parse_replaygain_int(bool album, long gain, long peak,

     if (buffer != NULL)
     {
-        len = snprintf(buffer, length, "%d.%02d dB", gain / 512,
+        len = snprintf(buffer, length, "%ld.%02d dB", gain / 512,
             ((abs(gain) & 0x01ff) * 100 + 256) / 512);
         len++;
     }
diff --git a/apps/root_menu.c b/apps/root_menu.c
index 5124375..e533c8c 100644
--- a/apps/root_menu.c
+++ b/apps/root_menu.c
@@ -19,9 +19,9 @@
  *
  ****************************************************************************/
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include "string-extra.h"
 #include "config.h"
 #include "appevents.h"
 #include "menu.h"
diff --git a/apps/screen_access.c b/apps/screen_access.c
index 871b1f8..df07102 100644
--- a/apps/screen_access.c
+++ b/apps/screen_access.c
@@ -19,17 +19,18 @@
  *
  ****************************************************************************/

+#include <stdio.h>
+#include "config.h"
 #include <lcd.h>
 #include <lcd-remote.h>
 #include <scroll_engine.h>
-#include "backlight.h"
 #include <font.h>
 #include <button.h>
-#include <sprintf.h>
 #include <settings.h>
 #include <kernel.h>
 #include <icons.h>

+#include "backlight.h"
 #include "screen_access.h"
 #include "backdrop.h"

diff --git a/apps/screens.c b/apps/screens.c
index 65ab109..8ae8211 100644
--- a/apps/screens.c
+++ b/apps/screens.c
@@ -34,7 +34,6 @@
 #include "settings.h"
 #include "status.h"
 #include "playlist.h"
-#include "sprintf.h"
 #include "kernel.h"
 #include "power.h"
 #include "system.h"
diff --git a/apps/scrobbler.c b/apps/scrobbler.c
index d7eebee..c040d0c 100644
--- a/apps/scrobbler.c
+++ b/apps/scrobbler.c
@@ -23,8 +23,8 @@ Audioscrobbler spec at:
 http://www.audioscrobbler.net/wiki/Portable_Player_Logging
 */

+#include <stdio.h>
 #include "file.h"
-#include "sprintf.h"
 #include "logf.h"
 #include "metadata.h"
 #include "kernel.h"
diff --git a/apps/settings.c b/apps/settings.c
index 0c5560a..9768e3a 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -33,7 +33,8 @@
 #include "backlight.h"
 #include "audio.h"
 #include "talk.h"
-#include "string.h"
+#include "strlcpy.h"
+#include "strcasestr.h"
 #include "rtc.h"
 #include "power.h"
 #include "ata_idle_notify.h"
@@ -52,7 +53,6 @@
 #include "lang.h"
 #include "language.h"
 #include "powermgmt.h"
-#include "sprintf.h"
 #include "keyboard.h"
 #include "version.h"
 #include "sound.h"
diff --git a/apps/settings_list.c b/apps/settings_list.c
index 044f9e8..34d4fc5 100644
--- a/apps/settings_list.c
+++ b/apps/settings_list.c
@@ -21,7 +21,7 @@

 #include "config.h"
 #include <stdbool.h>
-#include <string.h>
+#include "string-extra.h"
 #include "system.h"
 #include "storage.h"
 #include "lang.h"
diff --git a/apps/tagcache.c b/apps/tagcache.c
index 0d06d8f..c97f08f 100644
--- a/apps/tagcache.c
+++ b/apps/tagcache.c
@@ -66,7 +66,7 @@
 #include "kernel.h"
 #include "system.h"
 #include "logf.h"
-#include "string.h"
+#include "string-extra.h"
 #include "usb.h"
 #include "metadata.h"
 #include "tagcache.h"
@@ -1392,7 +1392,7 @@ static bool get_next(struct tagcache_search *tcs)

     if (TAGCACHE_IS_NUMERIC(tcs->type))
     {
-        snprintf(buf, sizeof(buf), "%d", tcs->position);
+        snprintf(buf, sizeof(buf), "%ld", tcs->position);
         tcs->result = buf;
         tcs->result_len = strlen(buf) + 1;
         return true;
@@ -3488,7 +3488,7 @@ bool tagcache_create_changelog(struct tagcache_search *tcs)
         {
             if (TAGCACHE_IS_NUMERIC(j))
             {
-                snprintf(temp, sizeof temp, "%d", idx.tag_seek[j]);
+                snprintf(temp, sizeof temp, "%d", (int)idx.tag_seek[j]);
                 write_tag(clfd, tagcache_tag_to_str(j), temp);
                 continue;
             }
diff --git a/apps/tagtree.c b/apps/tagtree.c
index ef9c9d2..3b13df0 100644
--- a/apps/tagtree.c
+++ b/apps/tagtree.c
@@ -27,8 +27,8 @@
 /*#define LOGF_ENABLE*/

 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
+#include "string-extra.h"
 #include "config.h"
 #include "system.h"
 #include "kernel.h"
diff --git a/apps/talk.c b/apps/talk.c
index b816d8e..8c72b4b 100644
--- a/apps/talk.c
+++ b/apps/talk.c
@@ -25,7 +25,7 @@

 #include <stdio.h>
 #include <stddef.h>
-#include <string.h>
+#include "string-extra.h"
 #include "file.h"
 #include "buffer.h"
 #include "system.h"
diff --git a/apps/tree.c b/apps/tree.c
index db8f507..8fbc793 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -19,9 +19,9 @@
  *
  ****************************************************************************/
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include "string-extra.h"

 #include "applimits.h"
 #include "dir.h"
@@ -32,7 +32,6 @@
 #include "kernel.h"
 #include "usb.h"
 #include "tree.h"
-#include "sprintf.h"
 #include "audio.h"
 #include "playlist.h"
 #include "menu.h"
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 48360d7..7ad0e2f 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -22,12 +22,31 @@ panic.c
 debug.c

 /* Common */
-common/atoi.c
+#if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
+libc/errno.c
+#endif /* !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__) */
+#ifndef SIMULATOR
+libc/atoi.c
+libc/ctype.c
+libc/memcmp.c
+libc/memchr.c
+libc/qsort.c
+libc/random.c
+libc/sprintf.c
+libc/strcat.c
+libc/strchr.c
+libc/strcmp.c
+libc/strcpy.c
+libc/strncmp.c
+libc/strrchr.c
+libc/strtok.c
+libc/strstr.c
+libc/mktime.c
+#endif
 common/crc32.c
 #ifdef MI4_FORMAT
 common/crc32-mi4.c
 #endif
-common/ctype.c
 #ifndef SIMULATOR
 common/dir_uncached.c
 common/file.c
@@ -36,28 +55,13 @@ common/file.c
 common/dircache.c
 #endif /* HAVE_DIRCACHE */
 common/disk.c
-#if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
-common/errno.c
-#endif /* !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__) */
 common/filefuncs.c
-common/memcmp.c
-common/memchr.c
-common/qsort.c
-common/random.c
-common/sprintf.c
+common/format.c
 common/strcasecmp.c
 common/strcasestr.c
-common/strcat.c
-common/strchr.c
-common/strcmp.c
 common/strnatcmp.c
-common/strcpy.c
-common/strncmp.c
 common/strlcat.c
 common/strlcpy.c
-common/strrchr.c
-common/strtok.c
-common/strstr.c
 common/structec.c
 common/timefuncs.c
 common/unicode.c
@@ -377,7 +381,7 @@ target/coldfire/i2c-coldfire.c
 target/arm/support-arm.S
 target/arm/memcpy-arm.S
 target/arm/memmove-arm.S
-common/strlen.c
+libc/strlen.c
 #ifndef SIMULATOR
 target/arm/memset-arm.S
 target/arm/memset16-arm.S
@@ -472,9 +476,9 @@ target/arm/crt0.S
 #elif defined(CPU_MIPS)
 #undef mips
 /*target/mips/strlen.S*/
-common/memmove.c
+libc/memmove.c
 common/memset16.c
-common/strlen.c
+libc/strlen.c
 target/mips/ffs-mips.S
 target/mips/memcpy-mips.S
 target/mips/memset-mips.S
@@ -488,11 +492,11 @@ target/mips/ingenic_jz47xx/crt0.S
 #ifdef HAVE_PRIORITY_SCHEDULING
 common/ffs.c
 #endif
-common/memcpy.c
-common/memmove.c
-common/memset.c
+libc/memcpy.c
+libc/memmove.c
+libc/memset.c
 common/memset16.c
-common/strlen.c
+libc/strlen.c
 #ifndef SIMULATOR
 crt0.S
 drivers/i2c.c
@@ -752,7 +756,7 @@ target/coldfire/iaudio/audio-iaudio.c
 #ifdef IRIVER_IFP7XX_SERIES
 #ifdef STUB
 ifp_usb_serial.c
-common/sscanf.c
+libc/sscanf.c
 #endif /* STUB */
 #endif /* IRIVER_IFP7XX_SERIES */

diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c
index 495366f..9eb73ef 100644
--- a/firmware/common/dircache.c
+++ b/firmware/common/dircache.c
@@ -27,7 +27,7 @@

 #include <stdio.h>
 #include <errno.h>
-#include <string.h>
+#include "string-extra.h"
 #include <stdbool.h>
 #include <stdlib.h>
 #include "debug.h"
diff --git a/firmware/common/strlcat.c b/firmware/common/strlcat.c
index da0d253..d04f8f3 100644
--- a/firmware/common/strlcat.c
+++ b/firmware/common/strlcat.c
@@ -16,7 +16,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */

-#include <sys/types.h>
+#include "inttypes.h"
 #include <string.h>

 /*
diff --git a/firmware/common/strlcpy.c b/firmware/common/strlcpy.c
index 6e06eb8..aca8276 100644
--- a/firmware/common/strlcpy.c
+++ b/firmware/common/strlcpy.c
@@ -16,7 +16,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */

-#include <sys/types.h>
+#include "inttypes.h"
 #include <string.h>

 /*
diff --git a/firmware/common/timefuncs.c b/firmware/common/timefuncs.c
index e423e01..42babbd 100644
--- a/firmware/common/timefuncs.c
+++ b/firmware/common/timefuncs.c
@@ -103,43 +103,6 @@ int set_time(const struct tm *tm)
 #endif /* RTC */
 }

-#if CONFIG_RTC
-/* mktime() code taken from lynx-2.8.5 source, written
- by Philippe De Muyter <[email protected]> */
-time_t mktime(struct tm *t)
-{
-    short month, year;
-    time_t result;
-    static int m_to_d[12] =
-        {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
-
-    month = t->tm_mon;
-    year = t->tm_year + month / 12 + 1900;
-    month %= 12;
-    if (month < 0)
-    {
-        year -= 1;
-        month += 12;
-    }
-    result = (year - 1970) * 365 + (year - 1969) / 4 + m_to_d[month];
-    result = (year - 1970) * 365 + m_to_d[month];
-    if (month <= 1)
-        year -= 1;
-    result += (year - 1968) / 4;
-    result -= (year - 1900) / 100;
-    result += (year - 1600) / 400;
-    result += t->tm_mday;
-    result -= 1;
-    result *= 24;
-    result += t->tm_hour;
-    result *= 60;
-    result += t->tm_min;
-    result *= 60;
-    result += t->tm_sec;
-    return(result);
-}
-#endif
-
 void set_day_of_week(struct tm *tm)
 {
     int y=tm->tm_year+1900;
diff --git a/firmware/drivers/lcd-bitmap-common.c b/firmware/drivers/lcd-bitmap-common.c
index 8f0b61e..1b31ee8 100644
--- a/firmware/drivers/lcd-bitmap-common.c
+++ b/firmware/drivers/lcd-bitmap-common.c
@@ -27,8 +27,9 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include "stdarg.h"
-#include "sprintf.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include "string-extra.h"
 #include "diacritic.h"

 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
diff --git a/firmware/drivers/lcd-charcell.c b/firmware/drivers/lcd-charcell.c
index 8af08e6..0186c26 100644
--- a/firmware/drivers/lcd-charcell.c
+++ b/firmware/drivers/lcd-charcell.c
@@ -22,7 +22,6 @@
 #include "config.h"
 #include "hwcompat.h"
 #include "stdarg.h"
-#include "sprintf.h"
 #include "lcd.h"
 #include "kernel.h"
 #include "thread.h"
diff --git a/firmware/drivers/tuner/lv24020lp.c b/firmware/drivers/tuner/lv24020lp.c
index d70eef8..aaf8aaa 100644
--- a/firmware/drivers/tuner/lv24020lp.c
+++ b/firmware/drivers/tuner/lv24020lp.c
@@ -40,7 +40,6 @@ static struct mutex tuner_mtx;
 #undef SANYO_TUNER_LOGF

 #ifdef SANYO_TUNER_LOG_FILE
-#include "sprintf.h"
 #include "file.h"

 static int fd_log = -1;
diff --git a/firmware/export/audio.h b/firmware/export/audio.h
index 6833761..f61ca56 100644
--- a/firmware/export/audio.h
+++ b/firmware/export/audio.h
@@ -22,7 +22,7 @@
 #define AUDIO_H

 #include <stdbool.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include "config.h"
 /* These must always be included with audio.h for this to compile under
    cetain conditions. Do it here or else spread the complication around to
diff --git a/firmware/export/config/sim.h b/firmware/export/config/sim.h
index 56c3e18..5b42394 100644
--- a/firmware/export/config/sim.h
+++ b/firmware/export/config/sim.h
@@ -95,3 +95,5 @@
 /* default for 100% in the sim */
 #define DEFAULT_BRIGHTNESS_SETTING MAX_BRIGHTNESS_SETTING
 #endif
+
+#define _ISOC99_SOURCE 1
diff --git a/firmware/export/pcm.h b/firmware/export/pcm.h
index e67d459..b3468c5 100644
--- a/firmware/export/pcm.h
+++ b/firmware/export/pcm.h
@@ -21,7 +21,7 @@
 #ifndef PCM_PLAYBACK_H
 #define PCM_PLAYBACK_H

-#include <sys/types.h>
+#include "inttypes.h"

 #define DMA_REC_ERROR_DMA       (-1)
 #ifdef HAVE_SPDIF_REC
diff --git a/firmware/export/structec.h b/firmware/export/structec.h
index b3e7d69..8cf658c 100644
--- a/firmware/export/structec.h
+++ b/firmware/export/structec.h
@@ -22,7 +22,7 @@
 #ifndef _STRUCTEC_H
 #define _STRUCTEC_H

-#include <sys/types.h>
+#include "inttypes.h"
 #include <stdbool.h>

 void structec_convert(void *structure, const char *ecinst, 
diff --git a/firmware/firmware.make b/firmware/firmware.make
index ec24b4e..52b5a19 100644
--- a/firmware/firmware.make
+++ b/firmware/firmware.make
@@ -7,7 +7,10 @@
 # $Id$
 #

-INCLUDES += -I$(FIRMDIR) -I$(FIRMDIR)/include -I$(FIRMDIR)/export -I$(FIRMDIR)/common -I$(FIRMDIR)/drivers
+INCLUDES += -I$(FIRMDIR) -I$(FIRMDIR)/export -I$(FIRMDIR)/drivers -I$(FIRMDIR)/include
+ifndef SIMVER
+INCLUDES += -I$(FIRMDIR)/libc/include
+endif

 FIRMLIB_SRC += $(call preprocess, $(FIRMDIR)/SOURCES)
 FIRMLIB_OBJ := $(call c2obj, $(FIRMLIB_SRC))
diff --git a/firmware/general.c b/firmware/general.c
index 6f7238e..fa10254 100644
--- a/firmware/general.c
+++ b/firmware/general.c
@@ -20,13 +20,13 @@
  ****************************************************************************/

 #include "config.h"
+#include <stdio.h>
 #include "general.h"

 #include "dir.h"
 #include "limits.h"
-#include "sprintf.h"
 #include "stdlib.h"
-#include "string.h"
+#include "string-extra.h"
 #include "system.h"
 #include "time.h"
 #include "timefuncs.h"
diff --git a/firmware/ifp_usb_serial.c b/firmware/ifp_usb_serial.c
index 06b286a..530f2c1 100644
--- a/firmware/ifp_usb_serial.c
+++ b/firmware/ifp_usb_serial.c
@@ -37,7 +37,6 @@

 #ifdef LCD_DEBUG
 #include "lcd.h"
-#include "sprintf.h"
 #endif


diff --git a/firmware/include/file.h b/firmware/include/file.h
index 9a9548f..9729290 100644
--- a/firmware/include/file.h
+++ b/firmware/include/file.h
@@ -22,7 +22,8 @@
 #ifndef _FILE_H_
 #define _FILE_H_

-#include <sys/types.h>
+#include "inttypes.h"
+#include "_ansi.h"

 #undef MAX_PATH /* this avoids problems when building simulator */
 #define MAX_PATH 260
@@ -81,5 +82,5 @@ extern int rename(const char* path, const char* newname);
 extern int ftruncate(int fd, off_t length);
 extern off_t filesize(int fd);
 extern int release_files(int volume);
-
+int fdprintf (int fd, const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
 #endif
diff --git a/firmware/include/inttypes.h b/firmware/include/inttypes.h
index f7f5099..813222c 100644
--- a/firmware/include/inttypes.h
+++ b/firmware/include/inttypes.h
@@ -22,8 +22,10 @@
 #ifndef __INTTYPES_H__
 #define __INTTYPES_H__

-#ifndef WPSEDITOR
-
+#if defined(WPSEDITOR) || defined(SIMULATOR)
+#include <stdint.h>
+#include <sys/types.h>
+#else
 #include <limits.h>

 /* 8 bit */
@@ -105,8 +107,37 @@
 #define uint64_t    unsigned long long

 #endif
-#else
-#include <stdint.h>
-#endif /* !WPSEDITOR*/
+
+#if !defined(__ssize_t_defined) && !defined(_SSIZE_T_) && !defined(ssize_t) && !defined(_SSIZE_T_DECLARED)
+#define __ssize_t_defined
+#define _SSIZE_T_
+#define ssize_t ssize_t
+typedef signed long ssize_t;
+#endif
+
+#if !defined(__off_t_defined) && !defined(_OFF_T_) && !defined(off_t) && !defined(_OFF_T_DECLARED)
+#define __off_t_defined
+#define _OFF_T_
+#define off_t off_t
+typedef signed long off_t;
+#endif
+
+#if !defined(__mode_t_defined) && !defined(_MODE_T_) && !defined(mode_t) && !defined(_MODE_T_DECLARED)
+#define __mode_t_defined
+#define _MODE_T_
+#define mode_t mode_t
+typedef unsigned int mode_t;
+#endif
+
+#if !defined(__size_t_defined)&& !defined(_SIZE_T_) && !defined(size_t) && !defined(_SIZE_T_DECLARED)
+#define __size_t_defined
+#define _SIZE_T
+#define _SIZE_T_
+#define _SIZE_T_DECLARED
+#define size_t size_t
+typedef unsigned long size_t;
+#endif
+
+#endif /* WPSEDITOR || SIMULATOR */

 #endif /* __INTTYPES_H__ */
diff --git a/firmware/include/memory.h b/firmware/include/memory.h
index 0b12629..d025bce 100644
--- a/firmware/include/memory.h
+++ b/firmware/include/memory.h
@@ -22,7 +22,7 @@
 #ifndef _MEMORY_H_
 #define _MEMORY_H_

-#include <sys/types.h>
+#include "inttypes.h"

 void memset16(void *dst, int val, size_t len);

diff --git a/firmware/include/string-extra.h b/firmware/include/string-extra.h
index ad9a1de..5fe5ab8 100644
--- a/firmware/include/string-extra.h
+++ b/firmware/include/string-extra.h
@@ -20,6 +20,7 @@
  ****************************************************************************/


+#include <string.h>
 #include "strlcpy.h"
 #include "strlcat.h"
 #include "strcasecmp.h"
diff --git a/firmware/include/strlcpy.h b/firmware/include/strlcpy.h
index d9f05f4..f94ed52 100644
--- a/firmware/include/strlcpy.h
+++ b/firmware/include/strlcpy.h
@@ -20,6 +20,7 @@
  ****************************************************************************/


-
+#ifndef __STRLCPY_H__
+#define __STRLCPY_H__
 size_t  strlcpy(char *dst, const char *src, size_t siz);
-size_t  strlcat(char *dst, const char *src, size_t siz);
+#endif
diff --git a/firmware/include/timefuncs.h b/firmware/include/timefuncs.h
index f51fa99..86a41a5 100644
--- a/firmware/include/timefuncs.h
+++ b/firmware/include/timefuncs.h
@@ -30,9 +30,6 @@ struct tm *get_time(void);
 int set_time(const struct tm *tm);
 bool valid_time(const struct tm *tm);
 void set_day_of_week(struct tm *tm);
-#if CONFIG_RTC
-time_t mktime(struct tm *t);
-#endif

 #endif /* _TIMEFUNCS_H_ */

diff --git a/firmware/libc/include/stdio.h b/firmware/libc/include/stdio.h
index 6ae2ff6..d9a6dce 100644
--- a/firmware/libc/include/stdio.h
+++ b/firmware/libc/include/stdio.h
@@ -33,8 +33,20 @@
 #define __VALIST char*
 #endif

-int snprintf (char *buf, size_t size, const char *fmt, ...);
-int vsnprintf (char *buf, int size, const char *fmt, __VALIST ap);
+int vsnprintf (char *buf, size_t size, const char *fmt, __VALIST ap);
+
+int sprintf  (char *buf, const char *fmt, ...) ATTRIBUTE_PRINTF(2, 3);
+
+int snprintf (char *buf, size_t size, const char *fmt, ...)
+              ATTRIBUTE_PRINTF(3, 4);
+
+/* callback function is called for every output character (byte) with userp and
+ * should return 0 when ch is a char other than '\0' that should stop printing */
+int vuprintf(int (*push)(void *userp, unsigned char data),
+              void *userp, const char *fmt, __VALIST ap);
+
+int sscanf(const char *s, const char *fmt, ...)
+    ATTRIBUTE_SCANF(2, 3);

 #ifdef SIMULATOR
 typedef void FILE;
diff --git a/firmware/libc/include/stdlib.h b/firmware/libc/include/stdlib.h
index 6de00c8..5f6db6d 100644
--- a/firmware/libc/include/stdlib.h
+++ b/firmware/libc/include/stdlib.h
@@ -43,8 +43,8 @@ int rand(void);
 #endif /* __GNUC__ */
 #endif

-#define abs(x) (ABS(x))
-#define labs(x) abs(x)
+#define abs(x) ((int)ABS(x))
+#define labs(x) ((long)abs(x))

 #ifdef SIMULATOR
 void exit(int status);
diff --git a/firmware/libc/include/string.h b/firmware/libc/include/string.h
index 1a2e056..846aa45 100644
--- a/firmware/libc/include/string.h
+++ b/firmware/libc/include/string.h
@@ -15,6 +15,7 @@ extern "C" {

 #define __need_size_t
 #include <stddef.h>
+#include "inttypes.h"

 #ifndef NULL
 #define NULL ((void*)0)
diff --git a/firmware/libc/include/time.h b/firmware/libc/include/time.h
index 2868049..5266d09 100644
--- a/firmware/libc/include/time.h
+++ b/firmware/libc/include/time.h
@@ -8,7 +8,7 @@
 #define _TIME_H_

 #ifdef WPSEDITOR
-#include <sys/types.h>
+#include "inttypes.h"
 #include <time.h>
 #endif

@@ -34,7 +34,16 @@ typedef long time_t;
 #define _TIME_T_DECLARED
 time_t time(time_t *t);
 struct tm *localtime(const time_t *timep);
+time_t mktime(struct tm *t);

+
+static inline int __isleap(int year) {
+  /* every fourth year is a leap year except for century years that are
+   * not divisible by 400. */
+/*  return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
+  return (!(year%4) && ((year%100) || !(year%400)));
+}
+#define __isleap __isleap
 #endif /* SIMULATOR */

 #ifdef __PCTOOL__
@@ -43,7 +52,6 @@ struct tm *localtime(const time_t *timep);
 #undef __USE_MISC  
 #endif

-
 #endif /* _TIME_H_ */


diff --git a/firmware/libc/memset.c b/firmware/libc/memset.c
index 6c4a66b..7b8d213 100644
--- a/firmware/libc/memset.c
+++ b/firmware/libc/memset.c
@@ -34,6 +34,7 @@ QUICKREF
 */

 #include <string.h>
+#include "_ansi.h"

 #define LBLOCKSIZE (sizeof(long))
 #define UNALIGNED(X)   ((long)X & (LBLOCKSIZE - 1))
diff --git a/firmware/libc/sprintf.c b/firmware/libc/sprintf.c
index 35f977a..71aab59 100644
--- a/firmware/libc/sprintf.c
+++ b/firmware/libc/sprintf.c
@@ -31,168 +31,7 @@
 #include <stdbool.h>
 #include <limits.h>

-#include "file.h" /* for write(), used in fprintf() */
-#include "sprintf.h" /* to allow the simulator magic */
-
-static const char hexdigit[] = "0123456789ABCDEF";
-
-static int format(
-    /* call 'push()' for each output letter */
-    int (*push)(void *userp, unsigned char data),
-    void *userp,
-    const char *fmt,
-    va_list ap)
-{
-    char *str;
-    char tmpbuf[12], pad;
-    int ch, width, val, sign, precision;
-    long lval, lsign;
-    unsigned int uval;
-    unsigned long ulval;
-    bool ok = true;
-
-    tmpbuf[sizeof tmpbuf - 1] = '\0';
-
-    while ((ch = *fmt++) != '\0' && ok)
-    {
-    if (ch == '%')
-    {
-        ch = *fmt++;
-        pad = ' ';
-        if (ch == '0')
-        pad = '0';
-
-        width = 0;
-        while (ch >= '0' && ch <= '9')
-        {
-        width = 10*width + ch - '0';
-        ch = *fmt++;
-        }
-        
-        precision = 0;
-        if(ch == '.')
-        {
-            ch = *fmt++;
-            while (ch >= '0' && ch <= '9')
-            {
-                precision = 10*precision + ch - '0';
-                ch = *fmt++;
-            }
-        } else {
-            precision = INT_MAX;
-        }
-
-        str = tmpbuf + sizeof tmpbuf - 1;
-        switch (ch)
-        {
-        case 'c':
-            *--str = va_arg (ap, int);
-            break;
-
-        case 's':
-            str = va_arg (ap, char*);
-            break;
-
-        case 'd':
-            val = sign = va_arg (ap, int);
-            if (val < 0)
-            val = -val;
-            do
-            {
-            *--str = (val % 10) + '0';
-            val /= 10;
-            }
-            while (val > 0);
-            if (sign < 0)
-            *--str = '-';
-            break;
-
-        case 'u':
-            uval = va_arg(ap, unsigned int);
-            do
-            {
-            *--str = (uval % 10) + '0';
-            uval /= 10;
-            }
-            while (uval > 0);
-            break;
-
-        case 'x':
-        case 'X':
-            pad='0';
-            uval = va_arg (ap, int);
-            do
-            {
-            *--str = hexdigit[uval & 0xf];
-            uval >>= 4;
-            }
-            while (uval);
-            break;
-
-        case 'l':
-            ch = *fmt++;
-            switch(ch) {
-                case 'x':
-                case 'X':
-                    pad='0';
-                    ulval = va_arg (ap, long);
-                    do
-                    {
-                        *--str = hexdigit[ulval & 0xf];
-                        ulval >>= 4;
-                    }
-                    while (ulval);
-                    break;
-                case 'd':
-                    lval = lsign = va_arg (ap, long);
-                    if (lval < 0)
-                        lval = -lval;
-                    do
-                    {
-                        *--str = (lval % 10) + '0';
-                        lval /= 10;
-                    }
-                    while (lval > 0);
-                    if (lsign < 0)
-                        *--str = '-';
-                    break;
-
-                case 'u':
-                    ulval = va_arg(ap, unsigned long);
-                    do
-                    {
-                        *--str = (ulval % 10) + '0';
-                        ulval /= 10;
-                    }
-                    while (ulval > 0);
-                    break;
-
-                default:
-                    *--str = 'l';
-                    *--str = ch;
-            }
-
-            break;
-
-        default:
-            *--str = ch;
-            break;
-        }
-
-        if (width > 0)
-        {
-        width -= strlen (str);
-        while (width-- > 0 && ok)
-            ok=push(userp, pad);
-        }
-        while (*str != '\0' && ok && precision--)
-                ok=push(userp, *str++);
-    }
-    else
-        ok=push(userp, ch);
-    }
-    return ok; /* true means good */
-}
+#include "format.h"

 #if !defined(SIMULATOR) || !defined(linux)
 /* ALSA library requires a more advanced snprintf, so let's not
@@ -201,8 +40,8 @@ static int format(

 struct for_snprintf {
     unsigned char *ptr; /* where to store it */
-    int bytes; /* amount already stored */
-    int max;   /* max amount to store */
+    size_t bytes; /* amount already stored */
+    size_t max;   /* max amount to store */
 };

 static int sprfunc(void *ptr, unsigned char letter)
@@ -238,7 +77,7 @@ int snprintf(char *buf, size_t size, const char *fmt, ...)
     return pr.bytes;
 }

-int vsnprintf(char *buf, int size, const char *fmt, va_list ap)
+int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
 {
     bool ok;
     struct for_snprintf pr;
@@ -257,43 +96,3 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list ap)

 #endif /* Linux SIMULATOR */

-struct for_fprintf {
-    int fd;    /* where to store it */
-    int bytes; /* amount stored */
-};
-
-static int fprfunc(void *pr, unsigned char letter)
-{
-    struct for_fprintf *fpr  = (struct for_fprintf *)pr;
-    int rc = write(fpr->fd, &letter, 1);
-    
-    if(rc > 0) {
-        fpr->bytes++; /* count them */
-        return true;  /* we are ok */
-    }
-
-    return false; /* failure */
-}
-
-
-int fdprintf(int fd, const char *fmt, ...)
-{
-    bool ok;
-    va_list ap;
-    struct for_fprintf fpr;
-
-    fpr.fd=fd;
-    fpr.bytes=0;
-
-    va_start(ap, fmt);
-    ok = format(fprfunc, &fpr, fmt, ap);
-    va_end(ap);
-
-    return fpr.bytes; /* return 0 on error */
-}
-
-int vuprintf(int (*push)(void *userp, unsigned char data), void *userp, const char *fmt, va_list ap)
-{
-    return format(push, userp, fmt, ap);
-}
-
diff --git a/firmware/profile.c b/firmware/profile.c
index 6700eca..f712146 100644
--- a/firmware/profile.c
+++ b/firmware/profile.c
@@ -59,7 +59,7 @@
 #include <system.h>
 #include <string.h>
 #include <timer.h>
-#include <sys/types.h>
+#include "inttypes.h"
 #include "profile.h"

 /* PFD is Profiled Function Data */
diff --git a/firmware/rolo.c b/firmware/rolo.c
index 87e6958..7b8c83e 100644
--- a/firmware/rolo.c
+++ b/firmware/rolo.c
@@ -24,7 +24,6 @@
 #include "lcd-remote.h"
 #include "thread.h"
 #include "kernel.h"
-#include "sprintf.h"
 #include "button.h"
 #include "file.h"
 #include "audio.h"
diff --git a/firmware/target/arm/as3525/debug-as3525.c b/firmware/target/arm/as3525/debug-as3525.c
index cfd7c37..75cce72 100644
--- a/firmware/target/arm/as3525/debug-as3525.c
+++ b/firmware/target/arm/as3525/debug-as3525.c
@@ -25,7 +25,6 @@
 #include "lcd.h"
 #include "font.h"
 #include "system.h"
-#include "sprintf.h"
 #include "cpu.h"
 #include "pl180.h"
 #include "ascodec-target.h"
diff --git a/firmware/target/arm/imx31/debug-imx31.c b/firmware/target/arm/imx31/debug-imx31.c
index d854c9b..52bbabf 100644
--- a/firmware/target/arm/imx31/debug-imx31.c
+++ b/firmware/target/arm/imx31/debug-imx31.c
@@ -23,7 +23,6 @@
 #include "string.h"
 #include "button.h"
 #include "lcd.h"
-#include "sprintf.h"
 #include "font.h"
 #include "debug-target.h"
 #include "mc13783.h"
diff --git a/firmware/target/arm/imx31/gigabeat-s/lcd-gigabeat-s.c b/firmware/target/arm/imx31/gigabeat-s/lcd-gigabeat-s.c
index 71d8e4b..5a0f813 100644
--- a/firmware/target/arm/imx31/gigabeat-s/lcd-gigabeat-s.c
+++ b/firmware/target/arm/imx31/gigabeat-s/lcd-gigabeat-s.c
@@ -18,7 +18,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "cpu.h"
diff --git a/firmware/target/arm/ipod/video/lcd-video.c b/firmware/target/arm/ipod/video/lcd-video.c
index ea9738b..f51a53e 100644
--- a/firmware/target/arm/ipod/video/lcd-video.c
+++ b/firmware/target/arm/ipod/video/lcd-video.c
@@ -25,7 +25,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "cpu.h"
diff --git a/firmware/target/arm/lcd-c200_c200v2.c b/firmware/target/arm/lcd-c200_c200v2.c
index 45a37d9..14749aa 100644
--- a/firmware/target/arm/lcd-c200_c200v2.c
+++ b/firmware/target/arm/lcd-c200_c200v2.c
@@ -18,7 +18,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "cpu.h"
diff --git a/firmware/target/arm/philips/hdd1630/lcd-hdd1630.c b/firmware/target/arm/philips/hdd1630/lcd-hdd1630.c
index 500120a..93abd30 100644
--- a/firmware/target/arm/philips/hdd1630/lcd-hdd1630.c
+++ b/firmware/target/arm/philips/hdd1630/lcd-hdd1630.c
@@ -18,7 +18,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "cpu.h"
diff --git a/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c b/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c
index 7e0594b..2f3c418 100644
--- a/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c
+++ b/firmware/target/arm/pnx0101/iriver-ifp7xx/usb-ifp7xx.c
@@ -31,7 +31,6 @@
 #include "lcd.h"
 #include "usb.h"
 #include "button.h"
-#include "sprintf.h"
 #include "string.h"

 void usb_init_device(void)
diff --git a/firmware/target/arm/s3c2440/debug-s3c2440.c b/firmware/target/arm/s3c2440/debug-s3c2440.c
index 064cca9..196a0b4 100644
--- a/firmware/target/arm/s3c2440/debug-s3c2440.c
+++ b/firmware/target/arm/s3c2440/debug-s3c2440.c
@@ -26,7 +26,6 @@
 #include <stdbool.h>
 #include "button.h"
 #include "lcd.h"
-#include "sprintf.h"
 #include "font.h"
 #include "debug-target.h"

diff --git a/firmware/target/arm/s3c2440/lcd-s3c2440.c b/firmware/target/arm/s3c2440/lcd-s3c2440.c
index 8b24ace..4fbef58 100644
--- a/firmware/target/arm/s3c2440/lcd-s3c2440.c
+++ b/firmware/target/arm/s3c2440/lcd-s3c2440.c
@@ -19,7 +19,7 @@
 * KIND, either express or implied.
 *
 ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "system.h"
diff --git a/firmware/target/arm/s5l8700/debug-s5l8700.c b/firmware/target/arm/s5l8700/debug-s5l8700.c
index 0ad7603..96df154 100644
--- a/firmware/target/arm/s5l8700/debug-s5l8700.c
+++ b/firmware/target/arm/s5l8700/debug-s5l8700.c
@@ -26,7 +26,6 @@
 #include "button.h"
 #include "lcd.h"
 #include "font.h"
-#include "sprintf.h"
 #include "storage.h"
 #ifdef IPOD_NANO2G
 #include "power.h"
diff --git a/firmware/target/arm/samsung/yh820/lcd-yh820.c b/firmware/target/arm/samsung/yh820/lcd-yh820.c
index f7d971a..dfff25b 100644
--- a/firmware/target/arm/samsung/yh820/lcd-yh820.c
+++ b/firmware/target/arm/samsung/yh820/lcd-yh820.c
@@ -18,7 +18,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "cpu.h"
diff --git a/firmware/target/arm/sandisk/sansa-e200/lcd-e200.c b/firmware/target/arm/sandisk/sansa-e200/lcd-e200.c
index 03f6a1b..2fc2131 100644
--- a/firmware/target/arm/sandisk/sansa-e200/lcd-e200.c
+++ b/firmware/target/arm/sandisk/sansa-e200/lcd-e200.c
@@ -22,7 +22,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"
 #include <string.h>
 #include "cpu.h"
 #include "system.h"
diff --git a/firmware/target/arm/tcc77x/debug-tcc77x.c b/firmware/target/arm/tcc77x/debug-tcc77x.c
index 203a601..bf322f5 100644
--- a/firmware/target/arm/tcc77x/debug-tcc77x.c
+++ b/firmware/target/arm/tcc77x/debug-tcc77x.c
@@ -26,7 +26,6 @@
 #include <stdbool.h>
 #include "button.h"
 #include "lcd.h"
-#include "sprintf.h"
 #include "font.h"
 #include "debug-target.h"
 #include "adc.h"
diff --git a/firmware/target/arm/tcc77x/iaudio7/ata2501.c b/firmware/target/arm/tcc77x/iaudio7/ata2501.c
index 3d78599..f7526b2 100644
--- a/firmware/target/arm/tcc77x/iaudio7/ata2501.c
+++ b/firmware/target/arm/tcc77x/iaudio7/ata2501.c
@@ -76,7 +76,6 @@ unsigned short ata2501_read(void)
 //#define ATA2501_TEST
 #ifdef ATA2501_TEST
 #include "lcd.h"
-#include "sprintf.h"

 static
 void bits(char *str, unsigned short val)
diff --git a/firmware/target/arm/tcc780x/cowond2/lcd-cowond2.c b/firmware/target/arm/tcc780x/cowond2/lcd-cowond2.c
index d5a7e2f..d42a5d3 100644
--- a/firmware/target/arm/tcc780x/cowond2/lcd-cowond2.c
+++ b/firmware/target/arm/tcc780x/cowond2/lcd-cowond2.c
@@ -18,7 +18,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "hwcompat.h"
diff --git a/firmware/target/arm/tcc780x/debug-tcc780x.c b/firmware/target/arm/tcc780x/debug-tcc780x.c
index ef6415e..11d6d2a 100644
--- a/firmware/target/arm/tcc780x/debug-tcc780x.c
+++ b/firmware/target/arm/tcc780x/debug-tcc780x.c
@@ -26,7 +26,6 @@
 #include <stdbool.h>
 #include "button.h"
 #include "lcd.h"
-#include "sprintf.h"
 #include "font.h"
 #include "debug-target.h"
 #include "adc.h"
diff --git a/firmware/target/arm/tms320dm320/creative-zvm/pic-creativezvm.c b/firmware/target/arm/tms320dm320/creative-zvm/pic-creativezvm.c
index 98a4b57..52c500f 100644
--- a/firmware/target/arm/tms320dm320/creative-zvm/pic-creativezvm.c
+++ b/firmware/target/arm/tms320dm320/creative-zvm/pic-creativezvm.c
@@ -26,7 +26,6 @@
 #include "kernel.h"
 #include "button-target.h"
 #include "i2c-dm320.h"
-#include "sprintf.h"
 #include "logf.h"

 #ifdef BUTTON_DEBUG
diff --git a/firmware/target/arm/tms320dm320/debug-dm320.c b/firmware/target/arm/tms320dm320/debug-dm320.c
index 8be2106..8bf1700 100644
--- a/firmware/target/arm/tms320dm320/debug-dm320.c
+++ b/firmware/target/arm/tms320dm320/debug-dm320.c
@@ -27,7 +27,6 @@
 #include "button.h"
 #include "lcd.h"
 #include "debug.h"
-#include "sprintf.h"
 #include "font.h"
 #include "lcd-target.h"

diff --git a/firmware/target/arm/usb-s3c6400x.c b/firmware/target/arm/usb-s3c6400x.c
index 2fbb1a9..3f28e7f 100644
--- a/firmware/target/arm/usb-s3c6400x.c
+++ b/firmware/target/arm/usb-s3c6400x.c
@@ -35,7 +35,6 @@
 #include "usb_ch9.h"
 #include "usb_core.h"
 #include <inttypes.h>
-#include "sprintf.h"
 #include "power.h"

 struct ep_type
diff --git a/firmware/target/arm/usb-tcc.c b/firmware/target/arm/usb-tcc.c
index 9d5ae2d..1b5f16c 100644
--- a/firmware/target/arm/usb-tcc.c
+++ b/firmware/target/arm/usb-tcc.c
@@ -42,7 +42,6 @@ static int global_ep_irq_mask = 0x1;
 #include <inttypes.h>


-#include "sprintf.h"
 #include "power.h"

 #ifndef BOOTLOADER
diff --git a/firmware/target/mips/ingenic_jz47xx/lcd-jz4740.c b/firmware/target/mips/ingenic_jz47xx/lcd-jz4740.c
index 29f337b..89b7d51 100644
--- a/firmware/target/mips/ingenic_jz47xx/lcd-jz4740.c
+++ b/firmware/target/mips/ingenic_jz47xx/lcd-jz4740.c
@@ -18,7 +18,7 @@
  * KIND, either express or implied.
  *
  ****************************************************************************/
-#include <sys/types.h>
+#include "inttypes.h"

 #include "config.h"
 #include "jz4740.h"
diff --git a/firmware/target/mips/ingenic_jz47xx/system-jz4740.c b/firmware/target/mips/ingenic_jz47xx/system-jz4740.c
index ce02a25..45fe0de 100644
--- a/firmware/target/mips/ingenic_jz47xx/system-jz4740.c
+++ b/firmware/target/mips/ingenic_jz47xx/system-jz4740.c
@@ -32,7 +32,6 @@
 #include "backlight-target.h"
 #include "font.h"
 #include "lcd.h"
-#include "sprintf.h"
 #endif

 #define NUM_DMA  6
diff --git a/firmware/test/snprintf/test.c b/firmware/test/snprintf/test.c
index 8923c9c..049be4d 100644
--- a/firmware/test/snprintf/test.c
+++ b/firmware/test/snprintf/test.c
@@ -1,7 +1,6 @@

 #include <stdio.h>

-#include "sprintf.h"

 int main(int argc, char **argv)
 {
diff --git a/firmware/thread.c b/firmware/thread.c
index 13d568e..d3031d5 100644
--- a/firmware/thread.c
+++ b/firmware/thread.c
@@ -20,9 +20,9 @@
  ****************************************************************************/
 #include "config.h"
 #include <stdbool.h>
+#include <stdio.h>
 #include "thread.h"
 #include "panic.h"
-#include "sprintf.h"
 #include "system.h"
 #include "kernel.h"
 #include "cpu.h"
diff --git a/firmware/usb.c b/firmware/usb.c
index ccf12c1..c615e97 100644
--- a/firmware/usb.c
+++ b/firmware/usb.c
@@ -37,7 +37,6 @@
 #include "usb-target.h"
 #include "usb.h"
 #include "button.h"
-#include "sprintf.h"
 #include "string.h"
 #ifdef HAVE_USBSTACK
 #include "usb_core.h"
diff --git a/tools/configure b/tools/configure
index 186e0d5..27609ca 100755
--- a/tools/configure
+++ b/tools/configure
@@ -165,7 +165,8 @@ simcc () {
         exit 2
     else 
         # generic sdl-config checker
-        GCCOPTS="$GCCOPTS `$sdl --cflags`"
+        #ignore the -D_GNU_SOURCE=1 inserted by sdl
+        GCCOPTS="$GCCOPTS `$sdl --cflags` -U_GNU_SOURCE"
         LDOPTS="$LDOPTS `$sdl --libs`"
     fi
  fi
diff --git a/uisimulator/sdl/sprintf.h b/uisimulator/sdl/sprintf.h
index 129a618..861d272 100644
--- a/uisimulator/sdl/sprintf.h
+++ b/uisimulator/sdl/sprintf.h
@@ -29,10 +29,12 @@
 int snprintf (char *buf, size_t size, const char *fmt, ...);
 char *strtok_r (char *, const char *, char **);

+/*
 int rockbox_fprintf (int fd, const char *fmt, ...);
 #define fprintf rockbox_fprintf

 int rockbox_vsnprintf (char *buf, int size, const char *fmt, va_list ap);
 #define vsnprintf rockbox_vsnprintf
+*/

 #endif /* __SPRINTF_H__ */