The tests are taken almost verbatim from the open source project PicoC. It can be found at https://code.google.com/p/picoc/. The tests range from very simple/trivial ones to more complicated. My view is that the more tests the better. Without tests like this I was very reluctant to make any changes to tcc for the fear of breaking things. The tests pass on Win32, OSX, Linux x86 and x86_64. One or two tests fail on each platform due to differences in the runtime library.
Unified · Split
| @@ -358,10 +358,12 @@ tar: · 357 unmodified lines | ||
| 358 | 358 | export LIBTCC1 |
| 359 | 359 | |
| 360 | 360 | %est: |
| 361 | + $(MAKE) -C tests2 $@ | |
| 361 | 362 | $(MAKE) -C tests $@ |
| 362 | 363 | |
| 363 | 364 | clean: |
| 364 | 365 | rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF) |
| 366 | + $(MAKE) -C tests2 $@ | |
| 365 | 367 | $(MAKE) -C tests $@ |
| 366 | 368 | ifneq ($(LIBTCC1),) |
| 367 | 369 | $(MAKE) -C lib $@ |
| @@ -0,0 +1,18 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + a = 42; | |
| 7 | + printf("%d\n", a); | |
| 8 | + | |
| 9 | + int b = 64; | |
| 10 | + printf("%d\n", b); | |
| 11 | + | |
| 12 | + int c = 12, d = 34; | |
| 13 | + printf("%d, %d\n", c, d); | |
| 14 | + | |
| 15 | + return 0; | |
| 16 | +} | |
| 17 | + | |
| 18 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +42 | |
| 2 | +64 | |
| 3 | +12, 34 | |
| @@ -0,0 +1,14 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + printf("Hello\n"); | |
| 6 | + printf("Hello\n"); /* this is a comment */ printf("Hello\n"); | |
| 7 | + printf("Hello\n"); | |
| 8 | + // this is also a comment sayhello(); | |
| 9 | + printf("Hello\n"); | |
| 10 | + | |
| 11 | + return 0; | |
| 12 | +} | |
| 13 | + | |
| 14 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,5 @@ | ||
| 1 | +Hello | |
| 2 | +Hello | |
| 3 | +Hello | |
| 4 | +Hello | |
| 5 | +Hello | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + printf("Hello world\n"); | |
| 6 | + | |
| 7 | + int Count; | |
| 8 | + for (Count = -5; Count <= 5; Count++) | |
| 9 | + printf("Count = %d\n", Count); | |
| 10 | + | |
| 11 | + printf("String 'hello', 'there' is '%s', '%s'\n", "hello", "there"); | |
| 12 | + printf("Character 'A' is '%c'\n", 65); | |
| 13 | + printf("Character 'a' is '%c'\n", 'a'); | |
| 14 | + | |
| 15 | + return 0; | |
| 16 | +} | |
| 17 | + | |
| 18 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,15 @@ | ||
| 1 | +Hello world | |
| 2 | +Count = -5 | |
| 3 | +Count = -4 | |
| 4 | +Count = -3 | |
| 5 | +Count = -2 | |
| 6 | +Count = -1 | |
| 7 | +Count = 0 | |
| 8 | +Count = 1 | |
| 9 | +Count = 2 | |
| 10 | +Count = 3 | |
| 11 | +Count = 4 | |
| 12 | +Count = 5 | |
| 13 | +String 'hello', 'there' is 'hello', 'there' | |
| 14 | +Character 'A' is 'A' | |
| 15 | +Character 'a' is 'a' | |
| @@ -0,0 +1,31 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +struct fred | |
| 4 | +{ | |
| 5 | + int boris; | |
| 6 | + int natasha; | |
| 7 | +}; | |
| 8 | + | |
| 9 | +void main() | |
| 10 | +{ | |
| 11 | + struct fred bloggs; | |
| 12 | + | |
| 13 | + bloggs.boris = 12; | |
| 14 | + bloggs.natasha = 34; | |
| 15 | + | |
| 16 | + printf("%d\n", bloggs.boris); | |
| 17 | + printf("%d\n", bloggs.natasha); | |
| 18 | + | |
| 19 | + struct fred jones[2]; | |
| 20 | + jones[0].boris = 12; | |
| 21 | + jones[0].natasha = 34; | |
| 22 | + jones[1].boris = 56; | |
| 23 | + jones[1].natasha = 78; | |
| 24 | + | |
| 25 | + printf("%d\n", jones[0].boris); | |
| 26 | + printf("%d\n", jones[0].natasha); | |
| 27 | + printf("%d\n", jones[1].boris); | |
| 28 | + printf("%d\n", jones[1].natasha); | |
| 29 | + | |
| 30 | + return 0; | |
| 31 | +} | |
| @@ -0,0 +1,6 @@ | ||
| 1 | +12 | |
| 2 | +34 | |
| 3 | +12 | |
| 4 | +34 | |
| 5 | +56 | |
| 6 | +78 | |
| @@ -0,0 +1,15 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int Count; | |
| 6 | + | |
| 7 | + for (Count = 1; Count <= 10; Count++) | |
| 8 | + { | |
| 9 | + printf("%d\n", Count); | |
| 10 | + } | |
| 11 | + | |
| 12 | + return 0; | |
| 13 | +} | |
| 14 | + | |
| 15 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,10 @@ | ||
| 1 | +1 | |
| 2 | +2 | |
| 3 | +3 | |
| 4 | +4 | |
| 5 | +5 | |
| 6 | +6 | |
| 7 | +7 | |
| 8 | +8 | |
| 9 | +9 | |
| 10 | +10 | |
| @@ -0,0 +1,21 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int Count; | |
| 6 | + int Array[10]; | |
| 7 | + | |
| 8 | + for (Count = 1; Count <= 10; Count++) | |
| 9 | + { | |
| 10 | + Array[Count-1] = Count * Count; | |
| 11 | + } | |
| 12 | + | |
| 13 | + for (Count = 0; Count < 10; Count++) | |
| 14 | + { | |
| 15 | + printf("%d\n", Array[Count]); | |
| 16 | + } | |
| 17 | + | |
| 18 | + return 0; | |
| 19 | +} | |
| 20 | + | |
| 21 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,10 @@ | ||
| 1 | +1 | |
| 2 | +4 | |
| 3 | +9 | |
| 4 | +16 | |
| 5 | +25 | |
| 6 | +36 | |
| 7 | +49 | |
| 8 | +64 | |
| 9 | +81 | |
| 10 | +100 | |
| @@ -0,0 +1,29 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int Count; | |
| 6 | + | |
| 7 | + for (Count = 0; Count < 4; Count++) | |
| 8 | + { | |
| 9 | + printf("%d\n", Count); | |
| 10 | + switch (Count) | |
| 11 | + { | |
| 12 | + case 1: | |
| 13 | + printf("%d\n", 1); | |
| 14 | + break; | |
| 15 | + | |
| 16 | + case 2: | |
| 17 | + printf("%d\n", 2); | |
| 18 | + break; | |
| 19 | + | |
| 20 | + default: | |
| 21 | + printf("%d\n", 0); | |
| 22 | + break; | |
| 23 | + } | |
| 24 | + } | |
| 25 | + | |
| 26 | + return 0; | |
| 27 | +} | |
| 28 | + | |
| 29 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,8 @@ | ||
| 1 | +0 | |
| 2 | +0 | |
| 3 | +1 | |
| 4 | +1 | |
| 5 | +2 | |
| 6 | +2 | |
| 7 | +3 | |
| 8 | +0 | |
| @@ -0,0 +1,30 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int myfunc(int x) | |
| 4 | +{ | |
| 5 | + return x * x; | |
| 6 | +} | |
| 7 | + | |
| 8 | +void vfunc(int a) | |
| 9 | +{ | |
| 10 | + printf("a=%d\n", a); | |
| 11 | +} | |
| 12 | + | |
| 13 | +void qfunc() | |
| 14 | +{ | |
| 15 | + printf("qfunc()\n"); | |
| 16 | +} | |
| 17 | + | |
| 18 | +int main() | |
| 19 | +{ | |
| 20 | + printf("%d\n", myfunc(3)); | |
| 21 | + printf("%d\n", myfunc(4)); | |
| 22 | + | |
| 23 | + vfunc(1234); | |
| 24 | + | |
| 25 | + qfunc(); | |
| 26 | + | |
| 27 | + return 0; | |
| 28 | +} | |
| 29 | + | |
| 30 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,4 @@ | ||
| 1 | +9 | |
| 2 | +16 | |
| 3 | +a=1234 | |
| 4 | +qfunc() | |
| @@ -0,0 +1,24 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + int p; | |
| 7 | + int t; | |
| 8 | + | |
| 9 | + a = 1; | |
| 10 | + p = 0; | |
| 11 | + t = 0; | |
| 12 | + | |
| 13 | + while (a < 100) | |
| 14 | + { | |
| 15 | + printf("%d\n", a); | |
| 16 | + t = a; | |
| 17 | + a = t + p; | |
| 18 | + p = t; | |
| 19 | + } | |
| 20 | + | |
| 21 | + return 0; | |
| 22 | +} | |
| 23 | + | |
| 24 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,11 @@ | ||
| 1 | +1 | |
| 2 | +1 | |
| 3 | +2 | |
| 4 | +3 | |
| 5 | +5 | |
| 6 | +8 | |
| 7 | +13 | |
| 8 | +21 | |
| 9 | +34 | |
| 10 | +55 | |
| 11 | +89 | |
| @@ -0,0 +1,24 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + int p; | |
| 7 | + int t; | |
| 8 | + | |
| 9 | + a = 1; | |
| 10 | + p = 0; | |
| 11 | + t = 0; | |
| 12 | + | |
| 13 | + do | |
| 14 | + { | |
| 15 | + printf("%d\n", a); | |
| 16 | + t = a; | |
| 17 | + a = t + p; | |
| 18 | + p = t; | |
| 19 | + } while (a < 100); | |
| 20 | + | |
| 21 | + return 0; | |
| 22 | +} | |
| 23 | + | |
| 24 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,11 @@ | ||
| 1 | +1 | |
| 2 | +1 | |
| 3 | +2 | |
| 4 | +3 | |
| 5 | +5 | |
| 6 | +8 | |
| 7 | +13 | |
| 8 | +21 | |
| 9 | +34 | |
| 10 | +55 | |
| 11 | +89 | |
| @@ -0,0 +1,40 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +struct ziggy | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + int b; | |
| 7 | + int c; | |
| 8 | +} bolshevic; | |
| 9 | + | |
| 10 | +int main() | |
| 11 | +{ | |
| 12 | + int a; | |
| 13 | + int *b; | |
| 14 | + int c; | |
| 15 | + | |
| 16 | + a = 42; | |
| 17 | + b = &a; | |
| 18 | + printf("a = %d\n", *b); | |
| 19 | + | |
| 20 | + bolshevic.a = 12; | |
| 21 | + bolshevic.b = 34; | |
| 22 | + bolshevic.c = 56; | |
| 23 | + | |
| 24 | + printf("bolshevic.a = %d\n", bolshevic.a); | |
| 25 | + printf("bolshevic.b = %d\n", bolshevic.b); | |
| 26 | + printf("bolshevic.c = %d\n", bolshevic.c); | |
| 27 | + | |
| 28 | + struct ziggy *tsar = &bolshevic; | |
| 29 | + | |
| 30 | + printf("tsar->a = %d\n", tsar->a); | |
| 31 | + printf("tsar->b = %d\n", tsar->b); | |
| 32 | + printf("tsar->c = %d\n", tsar->c); | |
| 33 | + | |
| 34 | + b = &(bolshevic.b); | |
| 35 | + printf("bolshevic.b = %d\n", *b); | |
| 36 | + | |
| 37 | + return 0; | |
| 38 | +} | |
| 39 | + | |
| 40 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,8 @@ | ||
| 1 | +a = 42 | |
| 2 | +bolshevic.a = 12 | |
| 3 | +bolshevic.b = 34 | |
| 4 | +bolshevic.c = 56 | |
| 5 | +tsar->a = 12 | |
| 6 | +tsar->b = 34 | |
| 7 | +tsar->c = 56 | |
| 8 | +bolshevic.b = 34 | |
| @@ -0,0 +1,40 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + int b; | |
| 7 | + int c; | |
| 8 | + int d; | |
| 9 | + int e; | |
| 10 | + int f; | |
| 11 | + int x; | |
| 12 | + int y; | |
| 13 | + | |
| 14 | + a = 12; | |
| 15 | + b = 34; | |
| 16 | + c = 56; | |
| 17 | + d = 78; | |
| 18 | + e = 0; | |
| 19 | + f = 1; | |
| 20 | + | |
| 21 | + printf("%d\n", c + d); | |
| 22 | + printf("%d\n", (y = c + d)); | |
| 23 | + printf("%d\n", e || e && f); | |
| 24 | + printf("%d\n", e || f && f); | |
| 25 | + printf("%d\n", e && e || f); | |
| 26 | + printf("%d\n", e && f || f); | |
| 27 | + printf("%d\n", a && f | f); | |
| 28 | + printf("%d\n", a | b ^ c & d); | |
| 29 | + printf("%d, %d\n", a == a, a == b); | |
| 30 | + printf("%d, %d\n", a != a, a != b); | |
| 31 | + printf("%d\n", a != b && c != d); | |
| 32 | + printf("%d\n", a + b * c / f); | |
| 33 | + printf("%d\n", a + b * c / f); | |
| 34 | + printf("%d\n", (4 << 4)); | |
| 35 | + printf("%d\n", (64 >> 4)); | |
| 36 | + | |
| 37 | + return 0; | |
| 38 | +} | |
| 39 | + | |
| 40 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,15 @@ | ||
| 1 | +134 | |
| 2 | +134 | |
| 3 | +0 | |
| 4 | +1 | |
| 5 | +1 | |
| 6 | +1 | |
| 7 | +1 | |
| 8 | +46 | |
| 9 | +1, 0 | |
| 10 | +0, 1 | |
| 11 | +1 | |
| 12 | +1916 | |
| 13 | +1916 | |
| 14 | +64 | |
| 15 | +4 | |
| @@ -0,0 +1,14 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +#define FRED 12 | |
| 4 | +#define BLOGGS(x) (12*(x)) | |
| 5 | + | |
| 6 | +int main() | |
| 7 | +{ | |
| 8 | + printf("%d\n", FRED); | |
| 9 | + printf("%d, %d, %d\n", BLOGGS(1), BLOGGS(2), BLOGGS(3)); | |
| 10 | + | |
| 11 | + return 0; | |
| 12 | +} | |
| 13 | + | |
| 14 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +12 | |
| 2 | +12, 24, 36 | |
| @@ -0,0 +1,20 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a = 24680; | |
| 6 | + int b = 01234567; | |
| 7 | + int c = 0x2468ac; | |
| 8 | + int d = 0x2468AC; | |
| 9 | + int e = 0b010101010101; | |
| 10 | + | |
| 11 | + printf("%d\n", a); | |
| 12 | + printf("%d\n", b); | |
| 13 | + printf("%d\n", c); | |
| 14 | + printf("%d\n", d); | |
| 15 | + printf("%d\n", e); | |
| 16 | + | |
| 17 | + return 0; | |
| 18 | +} | |
| 19 | + | |
| 20 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,5 @@ | ||
| 1 | +24680 | |
| 2 | +342391 | |
| 3 | +2386092 | |
| 4 | +2386092 | |
| 5 | +1365 | |
| @@ -0,0 +1,21 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a = 1; | |
| 6 | + | |
| 7 | + if (a) | |
| 8 | + printf("a is true\n"); | |
| 9 | + else | |
| 10 | + printf("a is false\n"); | |
| 11 | + | |
| 12 | + int b = 0; | |
| 13 | + if (b) | |
| 14 | + printf("b is true\n"); | |
| 15 | + else | |
| 16 | + printf("b is false\n"); | |
| 17 | + | |
| 18 | + return 0; | |
| 19 | +} | |
| 20 | + | |
| 21 | +// vim: set expandtab ts=4 sw=3 sts=3 tw=80 : | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +a is true | |
| 2 | +b is false | |
| @@ -0,0 +1,21 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int factorial(int i) | |
| 4 | +{ | |
| 5 | + if (i < 2) | |
| 6 | + return i; | |
| 7 | + else | |
| 8 | + return i * factorial(i - 1); | |
| 9 | +} | |
| 10 | + | |
| 11 | +int main() | |
| 12 | +{ | |
| 13 | + int Count; | |
| 14 | + | |
| 15 | + for (Count = 1; Count <= 10; Count++) | |
| 16 | + printf("%d\n", factorial(Count)); | |
| 17 | + | |
| 18 | + return 0; | |
| 19 | +} | |
| 20 | + | |
| 21 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,10 @@ | ||
| 1 | +1 | |
| 2 | +2 | |
| 3 | +6 | |
| 4 | +24 | |
| 5 | +120 | |
| 6 | +720 | |
| 7 | +5040 | |
| 8 | +40320 | |
| 9 | +362880 | |
| 10 | +3628800 | |
| @@ -0,0 +1,21 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int x, y, z; | |
| 6 | + | |
| 7 | + for (x = 0; x < 2; x++) | |
| 8 | + { | |
| 9 | + for (y = 0; y < 3; y++) | |
| 10 | + { | |
| 11 | + for (z = 0; z < 3; z++) | |
| 12 | + { | |
| 13 | + printf("%d %d %d\n", x, y, z); | |
| 14 | + } | |
| 15 | + } | |
| 16 | + } | |
| 17 | + | |
| 18 | + return 0; | |
| 19 | +} | |
| 20 | + | |
| 21 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +0 0 0 | |
| 2 | +0 0 1 | |
| 3 | +0 0 2 | |
| 4 | +0 1 0 | |
| 5 | +0 1 1 | |
| 6 | +0 1 2 | |
| 7 | +0 2 0 | |
| 8 | +0 2 1 | |
| 9 | +0 2 2 | |
| 10 | +1 0 0 | |
| 11 | +1 0 1 | |
| 12 | +1 0 2 | |
| 13 | +1 1 0 | |
| 14 | +1 1 1 | |
| 15 | +1 1 2 | |
| 16 | +1 2 0 | |
| 17 | +1 2 1 | |
| 18 | +1 2 2 | |
| @@ -0,0 +1,29 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +enum fred | |
| 4 | +{ | |
| 5 | + a, | |
| 6 | + b, | |
| 7 | + c, | |
| 8 | + d, | |
| 9 | + e = 54, | |
| 10 | + f = 73, | |
| 11 | + g, | |
| 12 | + h | |
| 13 | +}; | |
| 14 | + | |
| 15 | +int main() | |
| 16 | +{ | |
| 17 | + enum fred frod; | |
| 18 | + | |
| 19 | + printf("%d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h); | |
| 20 | + /* printf("%d\n", frod); */ | |
| 21 | + frod = 12; | |
| 22 | + printf("%d\n", frod); | |
| 23 | + frod = e; | |
| 24 | + printf("%d\n", frod); | |
| 25 | + | |
| 26 | + return 0; | |
| 27 | +} | |
| 28 | + | |
| 29 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +0 1 2 3 54 73 74 75 | |
| 2 | +12 | |
| 3 | +54 | |
| @@ -0,0 +1,12 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + printf("including\n"); | |
| 6 | +#include "18_include.h" | |
| 7 | + printf("done\n"); | |
| 8 | + | |
| 9 | + return 0; | |
| 10 | +} | |
| 11 | + | |
| 12 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +including | |
| 2 | +included | |
| 3 | +done | |
| @@ -0,0 +1 @@ | ||
| 1 | +printf("included\n"); | |
| @@ -0,0 +1,28 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + int *b; | |
| 7 | + int *c; | |
| 8 | + | |
| 9 | + a = 42; | |
| 10 | + b = &a; | |
| 11 | + c = NULL; | |
| 12 | + | |
| 13 | + printf("%d\n", *b); | |
| 14 | + | |
| 15 | + if (b == NULL) | |
| 16 | + printf("b is NULL\n"); | |
| 17 | + else | |
| 18 | + printf("b is not NULL\n"); | |
| 19 | + | |
| 20 | + if (c == NULL) | |
| 21 | + printf("c is NULL\n"); | |
| 22 | + else | |
| 23 | + printf("c is not NULL\n"); | |
| 24 | + | |
| 25 | + return 0; | |
| 26 | +} | |
| 27 | + | |
| 28 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +42 | |
| 2 | +b is not NULL | |
| 3 | +c is NULL | |
| @@ -0,0 +1,24 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + int b; | |
| 7 | + int *d; | |
| 8 | + int *e; | |
| 9 | + d = &a; | |
| 10 | + e = &b; | |
| 11 | + a = 12; | |
| 12 | + b = 34; | |
| 13 | + printf("%d\n", *d); | |
| 14 | + printf("%d\n", *e); | |
| 15 | + printf("%d\n", d == e); | |
| 16 | + printf("%d\n", d != e); | |
| 17 | + d = e; | |
| 18 | + printf("%d\n", d == e); | |
| 19 | + printf("%d\n", d != e); | |
| 20 | + | |
| 21 | + return 0; | |
| 22 | +} | |
| 23 | + | |
| 24 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,6 @@ | ||
| 1 | +12 | |
| 2 | +34 | |
| 3 | +0 | |
| 4 | +1 | |
| 5 | +1 | |
| 6 | +0 | |
| @@ -0,0 +1,33 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int x = 'a'; | |
| 6 | + char y = x; | |
| 7 | + | |
| 8 | + char *a = "hello"; | |
| 9 | + | |
| 10 | + printf("%s\n", a); | |
| 11 | + | |
| 12 | + int c; | |
| 13 | + c = *a; | |
| 14 | + | |
| 15 | + char *b; | |
| 16 | + for (b = a; *b != 0; b++) | |
| 17 | + printf("%c: %d\n", *b, *b); | |
| 18 | + | |
| 19 | + char destarray[10]; | |
| 20 | + char *dest = &destarray[0]; | |
| 21 | + char *src = a; | |
| 22 | + | |
| 23 | + while (*src != 0) | |
| 24 | + *dest++ = *src++; | |
| 25 | + | |
| 26 | + *dest = 0; | |
| 27 | + | |
| 28 | + printf("copied string is %s\n", destarray); | |
| 29 | + | |
| 30 | + return 0; | |
| 31 | +} | |
| 32 | + | |
| 33 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,7 @@ | ||
| 1 | +hello | |
| 2 | +h: 104 | |
| 3 | +e: 101 | |
| 4 | +l: 108 | |
| 5 | +l: 108 | |
| 6 | +o: 111 | |
| 7 | +copied string is hello | |
| @@ -0,0 +1,50 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <math.h> | |
| 3 | + | |
| 4 | +int main() | |
| 5 | +{ | |
| 6 | + // variables | |
| 7 | + float a = 12.34 + 56.78; | |
| 8 | + printf("%f\n", a); | |
| 9 | + | |
| 10 | + // infix operators | |
| 11 | + printf("%f\n", 12.34 + 56.78); | |
| 12 | + printf("%f\n", 12.34 - 56.78); | |
| 13 | + printf("%f\n", 12.34 * 56.78); | |
| 14 | + printf("%f\n", 12.34 / 56.78); | |
| 15 | + | |
| 16 | + // comparison operators | |
| 17 | + printf("%d %d %d %d %d %d\n", 12.34 < 56.78, 12.34 <= 56.78, 12.34 == 56.78, 12.34 >= 56.78, 12.34 > 56.78, 12.34 != 56.78); | |
| 18 | + printf("%d %d %d %d %d %d\n", 12.34 < 12.34, 12.34 <= 12.34, 12.34 == 12.34, 12.34 >= 12.34, 12.34 > 12.34, 12.34 != 12.34); | |
| 19 | + printf("%d %d %d %d %d %d\n", 56.78 < 12.34, 56.78 <= 12.34, 56.78 == 12.34, 56.78 >= 12.34, 56.78 > 12.34, 56.78 != 12.34); | |
| 20 | + | |
| 21 | + // assignment operators | |
| 22 | + a = 12.34; | |
| 23 | + a += 56.78; | |
| 24 | + printf("%f\n", a); | |
| 25 | + | |
| 26 | + a = 12.34; | |
| 27 | + a -= 56.78; | |
| 28 | + printf("%f\n", a); | |
| 29 | + | |
| 30 | + a = 12.34; | |
| 31 | + a *= 56.78; | |
| 32 | + printf("%f\n", a); | |
| 33 | + | |
| 34 | + a = 12.34; | |
| 35 | + a /= 56.78; | |
| 36 | + printf("%f\n", a); | |
| 37 | + | |
| 38 | + // prefix operators | |
| 39 | + printf("%f\n", +12.34); | |
| 40 | + printf("%f\n", -12.34); | |
| 41 | + | |
| 42 | + // type coercion | |
| 43 | + a = 2; | |
| 44 | + printf("%f\n", a); | |
| 45 | + printf("%f\n", sin(2)); | |
| 46 | + | |
| 47 | + return 0; | |
| 48 | +} | |
| 49 | + | |
| 50 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,16 @@ | ||
| 1 | +69.120003 | |
| 2 | +69.120000 | |
| 3 | +-44.440000 | |
| 4 | +700.665200 | |
| 5 | +0.217330 | |
| 6 | +1 1 0 0 0 1 | |
| 7 | +0 1 1 1 0 0 | |
| 8 | +0 0 0 1 1 1 | |
| 9 | +69.120003 | |
| 10 | +-44.439999 | |
| 11 | +700.665222 | |
| 12 | +0.217330 | |
| 13 | +12.340000 | |
| 14 | +-12.340000 | |
| 15 | +2.000000 | |
| 16 | +0.909297 | |
| @@ -0,0 +1,54 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +void charfunc(char a) | |
| 4 | +{ | |
| 5 | + printf("char: %c\n", a); | |
| 6 | +} | |
| 7 | + | |
| 8 | +void intfunc(int a) | |
| 9 | +{ | |
| 10 | + printf("int: %d\n", a); | |
| 11 | +} | |
| 12 | + | |
| 13 | +void floatfunc(float a) | |
| 14 | +{ | |
| 15 | + printf("float: %f\n", a); | |
| 16 | +} | |
| 17 | + | |
| 18 | +int main() | |
| 19 | +{ | |
| 20 | + charfunc('a'); | |
| 21 | + charfunc(98); | |
| 22 | + charfunc(99.0); | |
| 23 | + | |
| 24 | + intfunc('a'); | |
| 25 | + intfunc(98); | |
| 26 | + intfunc(99.0); | |
| 27 | + | |
| 28 | + floatfunc('a'); | |
| 29 | + floatfunc(98); | |
| 30 | + floatfunc(99.0); | |
| 31 | + | |
| 32 | + /* printf("%c %d %f\n", 'a', 'b', 'c'); */ | |
| 33 | + /* printf("%c %d %f\n", 97, 98, 99); */ | |
| 34 | + /* printf("%c %d %f\n", 97.0, 98.0, 99.0); */ | |
| 35 | + | |
| 36 | + char b = 97; | |
| 37 | + char c = 97.0; | |
| 38 | + | |
| 39 | + printf("%d %d\n", b, c); | |
| 40 | + | |
| 41 | + int d = 'a'; | |
| 42 | + int e = 97.0; | |
| 43 | + | |
| 44 | + printf("%d %d\n", d, e); | |
| 45 | + | |
| 46 | + float f = 'a'; | |
| 47 | + float g = 97; | |
| 48 | + | |
| 49 | + printf("%f %f\n", f, g); | |
| 50 | + | |
| 51 | + return 0; | |
| 52 | +} | |
| 53 | + | |
| 54 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,12 @@ | ||
| 1 | +char: a | |
| 2 | +char: b | |
| 3 | +char: c | |
| 4 | +int: 97 | |
| 5 | +int: 98 | |
| 6 | +int: 99 | |
| 7 | +float: 97.000000 | |
| 8 | +float: 98.000000 | |
| 9 | +float: 99.000000 | |
| 10 | +97 97 | |
| 11 | +97 97 | |
| 12 | +97.000000 97.000000 | |
| @@ -0,0 +1,28 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <math.h> | |
| 3 | + | |
| 4 | +int main() | |
| 5 | +{ | |
| 6 | + printf("%f\n", sin(0.12)); | |
| 7 | + printf("%f\n", cos(0.12)); | |
| 8 | + printf("%f\n", tan(0.12)); | |
| 9 | + printf("%f\n", asin(0.12)); | |
| 10 | + printf("%f\n", acos(0.12)); | |
| 11 | + printf("%f\n", atan(0.12)); | |
| 12 | + printf("%f\n", sinh(0.12)); | |
| 13 | + printf("%f\n", cosh(0.12)); | |
| 14 | + printf("%f\n", tanh(0.12)); | |
| 15 | + printf("%f\n", exp(0.12)); | |
| 16 | + printf("%f\n", fabs(-0.12)); | |
| 17 | + printf("%f\n", log(0.12)); | |
| 18 | + printf("%f\n", log10(0.12)); | |
| 19 | + printf("%f\n", pow(0.12, 0.12)); | |
| 20 | + printf("%f\n", sqrt(0.12)); | |
| 21 | + printf("%f\n", round(12.34)); | |
| 22 | + printf("%f\n", ceil(12.34)); | |
| 23 | + printf("%f\n", floor(12.34)); | |
| 24 | + | |
| 25 | + return 0; | |
| 26 | +} | |
| 27 | + | |
| 28 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +0.119712 | |
| 2 | +0.992809 | |
| 3 | +0.120579 | |
| 4 | +0.120290 | |
| 5 | +1.450506 | |
| 6 | +0.119429 | |
| 7 | +0.120288 | |
| 8 | +1.007209 | |
| 9 | +0.119427 | |
| 10 | +1.127497 | |
| 11 | +0.120000 | |
| 12 | +-2.120264 | |
| 13 | +-0.920819 | |
| 14 | +0.775357 | |
| 15 | +0.346410 | |
| 16 | +12.000000 | |
| 17 | +13.000000 | |
| 18 | +12.000000 | |
| @@ -0,0 +1,83 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int array[16]; | |
| 4 | + | |
| 5 | +//Swap integer values by array indexes | |
| 6 | +void swap(int a, int b) | |
| 7 | +{ | |
| 8 | + int tmp = array[a]; | |
| 9 | + array[a] = array[b]; | |
| 10 | + array[b] = tmp; | |
| 11 | +} | |
| 12 | + | |
| 13 | +//Partition the array into two halves and return the | |
| 14 | +//index about which the array is partitioned | |
| 15 | +int partition(int left, int right) | |
| 16 | +{ | |
| 17 | + int pivotIndex = left; | |
| 18 | + int pivotValue = array[pivotIndex]; | |
| 19 | + int index = left; | |
| 20 | + int i; | |
| 21 | + | |
| 22 | + swap(pivotIndex, right); | |
| 23 | + for(i = left; i < right; i++) | |
| 24 | + { | |
| 25 | + if(array[i] < pivotValue) | |
| 26 | + { | |
| 27 | + swap(i, index); | |
| 28 | + index += 1; | |
| 29 | + } | |
| 30 | + } | |
| 31 | + swap(right, index); | |
| 32 | + | |
| 33 | + return index; | |
| 34 | +} | |
| 35 | + | |
| 36 | +//Quicksort the array | |
| 37 | +void quicksort(int left, int right) | |
| 38 | +{ | |
| 39 | + if(left >= right) | |
| 40 | + return; | |
| 41 | + | |
| 42 | + int index = partition(left, right); | |
| 43 | + quicksort(left, index - 1); | |
| 44 | + quicksort(index + 1, right); | |
| 45 | +} | |
| 46 | + | |
| 47 | +int main() | |
| 48 | +{ | |
| 49 | + int i; | |
| 50 | + | |
| 51 | + array[0] = 62; | |
| 52 | + array[1] = 83; | |
| 53 | + array[2] = 4; | |
| 54 | + array[3] = 89; | |
| 55 | + array[4] = 36; | |
| 56 | + array[5] = 21; | |
| 57 | + array[6] = 74; | |
| 58 | + array[7] = 37; | |
| 59 | + array[8] = 65; | |
| 60 | + array[9] = 33; | |
| 61 | + array[10] = 96; | |
| 62 | + array[11] = 38; | |
| 63 | + array[12] = 53; | |
| 64 | + array[13] = 16; | |
| 65 | + array[14] = 74; | |
| 66 | + array[15] = 55; | |
| 67 | + | |
| 68 | + for (i = 0; i < 16; i++) | |
| 69 | + printf("%d ", array[i]); | |
| 70 | + | |
| 71 | + printf("\n"); | |
| 72 | + | |
| 73 | + quicksort(0, 15); | |
| 74 | + | |
| 75 | + for (i = 0; i < 16; i++) | |
| 76 | + printf("%d ", array[i]); | |
| 77 | + | |
| 78 | + printf("\n"); | |
| 79 | + | |
| 80 | + return 0; | |
| 81 | +} | |
| 82 | + | |
| 83 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +62 83 4 89 36 21 74 37 65 33 96 38 53 16 74 55 | |
| 2 | +4 16 21 33 36 37 38 53 55 62 65 74 74 83 89 96 | |
| @@ -0,0 +1,17 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + printf("%d\n", '\1'); | |
| 6 | + printf("%d\n", '\10'); | |
| 7 | + printf("%d\n", '\100'); | |
| 8 | + printf("%d\n", '\x01'); | |
| 9 | + printf("%d\n", '\x0e'); | |
| 10 | + printf("%d\n", '\x10'); | |
| 11 | + printf("%d\n", '\x40'); | |
| 12 | + printf("test \x40\n"); | |
| 13 | + | |
| 14 | + return 0; | |
| 15 | +} | |
| 16 | + | |
| 17 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,8 @@ | ||
| 1 | +1 | |
| 2 | +8 | |
| 3 | +64 | |
| 4 | +1 | |
| 5 | +14 | |
| 6 | +16 | |
| 7 | +64 | |
| 8 | +test @ | |
| @@ -0,0 +1,16 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + char a; | |
| 6 | + int b; | |
| 7 | + double c; | |
| 8 | + | |
| 9 | + printf("%d\n", sizeof(a)); | |
| 10 | + printf("%d\n", sizeof(b)); | |
| 11 | + printf("%d\n", sizeof(c)); | |
| 12 | + | |
| 13 | + return 0; | |
| 14 | +} | |
| 15 | + | |
| 16 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +1 | |
| 2 | +4 | |
| 3 | +8 | |
| @@ -0,0 +1,46 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <string.h> | |
| 3 | +#include <strings.h> | |
| 4 | + | |
| 5 | +int main() | |
| 6 | +{ | |
| 7 | + char a[10]; | |
| 8 | + | |
| 9 | + strcpy(a, "hello"); | |
| 10 | + printf("%s\n", a); | |
| 11 | + | |
| 12 | + strncpy(a, "gosh", 2); | |
| 13 | + printf("%s\n", a); | |
| 14 | + | |
| 15 | + printf("%d\n", strcmp(a, "apple") > 0); | |
| 16 | + printf("%d\n", strcmp(a, "goere") > 0); | |
| 17 | + printf("%d\n", strcmp(a, "zebra") < 0); | |
| 18 | + | |
| 19 | + printf("%d\n", strlen(a)); | |
| 20 | + | |
| 21 | + strcat(a, "!"); | |
| 22 | + printf("%s\n", a); | |
| 23 | + | |
| 24 | + printf("%d\n", strncmp(a, "apple", 2) > 0); | |
| 25 | + printf("%d\n", strncmp(a, "goere", 2) == 0); | |
| 26 | + printf("%d\n", strncmp(a, "goerg", 2) == 0); | |
| 27 | + printf("%d\n", strncmp(a, "zebra", 2) < 0); | |
| 28 | + | |
| 29 | + printf("%s\n", index(a, 'o')); | |
| 30 | + printf("%s\n", rindex(a, 'l')); | |
| 31 | + printf("%d\n", rindex(a, 'x') == NULL); | |
| 32 | + | |
| 33 | + memset(&a[1], 'r', 4); | |
| 34 | + printf("%s\n", a); | |
| 35 | + | |
| 36 | + memcpy(&a[2], a, 2); | |
| 37 | + printf("%s\n", a); | |
| 38 | + | |
| 39 | + printf("%d\n", memcmp(a, "apple", 4) > 0); | |
| 40 | + printf("%d\n", memcmp(a, "grgr", 4) == 0); | |
| 41 | + printf("%d\n", memcmp(a, "zebra", 4) < 0); | |
| 42 | + | |
| 43 | + return 0; | |
| 44 | +} | |
| 45 | + | |
| 46 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,19 @@ | ||
| 1 | +hello | |
| 2 | +gollo | |
| 3 | +1 | |
| 4 | +1 | |
| 5 | +1 | |
| 6 | +5 | |
| 7 | +gollo! | |
| 8 | +1 | |
| 9 | +1 | |
| 10 | +1 | |
| 11 | +1 | |
| 12 | +ollo! | |
| 13 | +lo! | |
| 14 | +1 | |
| 15 | +grrrr! | |
| 16 | +grgrr! | |
| 17 | +1 | |
| 18 | +1 | |
| 19 | +1 | |
| @@ -0,0 +1,13 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | +#include <string.h> | |
| 3 | + | |
| 4 | +int main() | |
| 5 | +{ | |
| 6 | + char a[10]; | |
| 7 | + strcpy(a, "abcdef"); | |
| 8 | + printf("%s\n", &a[1]); | |
| 9 | + | |
| 10 | + return 0; | |
| 11 | +} | |
| 12 | + | |
| 13 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1 @@ | ||
| 1 | +bcdef | |
| @@ -0,0 +1,122 @@ | ||
| 1 | +/* example from http://barnyard.syr.edu/quickies/hanoi.c */ | |
| 2 | + | |
| 3 | +/* hanoi.c: solves the tower of hanoi problem. (Programming exercise.) */ | |
| 4 | +/* By Terry R. McConnell (12/2/97) */ | |
| 5 | +/* Compile: cc -o hanoi hanoi.c */ | |
| 6 | + | |
| 7 | +/* This program does no error checking. But then, if it's right, | |
| 8 | + it's right ... right ? */ | |
| 9 | + | |
| 10 | + | |
| 11 | +/* The original towers of hanoi problem seems to have been originally posed | |
| 12 | + by one M. Claus in 1883. There is a popular legend that goes along with | |
| 13 | + it that has been often repeated and paraphrased. It goes something like this: | |
| 14 | + In the great temple at Benares there are 3 golden spikes. On one of them, | |
| 15 | + God placed 64 disks increasing in size from bottom to top, at the beginning | |
| 16 | + of time. Since then, and to this day, the priest on duty constantly transfers | |
| 17 | + disks, one at a time, in such a way that no larger disk is ever put on top | |
| 18 | + of a smaller one. When the disks have been transferred entirely to another | |
| 19 | + spike the Universe will come to an end in a large thunderclap. | |
| 20 | + | |
| 21 | + This paraphrases the original legend due to DeParville, La Nature, Paris 1884, | |
| 22 | + Part I, 285-286. For this and further information see: Mathematical | |
| 23 | + Recreations & Essays, W.W. Rouse Ball, MacMillan, NewYork, 11th Ed. 1967, | |
| 24 | + 303-305. | |
| 25 | + * | |
| 26 | + * | |
| 27 | + */ | |
| 28 | + | |
| 29 | +#include <stdio.h> | |
| 30 | +#include <stdlib.h> | |
| 31 | + | |
| 32 | +#define TRUE 1 | |
| 33 | +#define FALSE 0 | |
| 34 | + | |
| 35 | +/* This is the number of "disks" on tower A initially. Taken to be 64 in the | |
| 36 | + * legend. The number of moves required, in general, is 2^N - 1. For N = 64, | |
| 37 | + * this is 18,446,744,073,709,551,615 */ | |
| 38 | +#define N 4 | |
| 39 | + | |
| 40 | +/* These are the three towers. For example if the state of A is 0,1,3,4, that | |
| 41 | + * means that there are three discs on A of sizes 1, 3, and 4. (Think of right | |
| 42 | + * as being the "down" direction.) */ | |
| 43 | +int A[N], B[N], C[N]; | |
| 44 | + | |
| 45 | +void Hanoi(int,int*,int*,int*); | |
| 46 | + | |
| 47 | +/* Print the current configuration of A, B, and C to the screen */ | |
| 48 | +void PrintAll() | |
| 49 | +{ | |
| 50 | + int i; | |
| 51 | + | |
| 52 | + printf("A: "); | |
| 53 | + for(i=0;i<N;i++)printf(" %d ",A[i]); | |
| 54 | + printf("\n"); | |
| 55 | + | |
| 56 | + printf("B: "); | |
| 57 | + for(i=0;i<N;i++)printf(" %d ",B[i]); | |
| 58 | + printf("\n"); | |
| 59 | + | |
| 60 | + printf("C: "); | |
| 61 | + for(i=0;i<N;i++)printf(" %d ",C[i]); | |
| 62 | + printf("\n"); | |
| 63 | + printf("------------------------------------------\n"); | |
| 64 | + return; | |
| 65 | +} | |
| 66 | + | |
| 67 | +/* Move the leftmost nonzero element of source to dest, leave behind 0. */ | |
| 68 | +/* Returns the value moved (not used.) */ | |
| 69 | +int Move(int *source, int *dest) | |
| 70 | +{ | |
| 71 | + int i,j; | |
| 72 | + | |
| 73 | + while (i<N && (source[i])==0) i++; | |
| 74 | + while (j<N && (dest[j])==0) j++; | |
| 75 | + | |
| 76 | + dest[j-1] = source[i]; | |
| 77 | + source[i] = 0; | |
| 78 | + PrintAll(); /* Print configuration after each move. */ | |
| 79 | + return dest[j-1]; | |
| 80 | +} | |
| 81 | + | |
| 82 | + | |
| 83 | +/* Moves first n nonzero numbers from source to dest using the rules of Hanoi. | |
| 84 | + Calls itself recursively. | |
| 85 | + */ | |
| 86 | +void Hanoi(int n,int *source, int *dest, int *spare) | |
| 87 | +{ | |
| 88 | + int i; | |
| 89 | + if(n==1){ | |
| 90 | + Move(source,dest); | |
| 91 | + return; | |
| 92 | + } | |
| 93 | + | |
| 94 | + Hanoi(n-1,source,spare,dest); | |
| 95 | + Move(source,dest); | |
| 96 | + Hanoi(n-1,spare,dest,source); | |
| 97 | + return; | |
| 98 | +} | |
| 99 | + | |
| 100 | +int main() | |
| 101 | +{ | |
| 102 | + int i; | |
| 103 | + | |
| 104 | + /* initialize the towers */ | |
| 105 | + for(i=0;i<N;i++)A[i]=i+1; | |
| 106 | + for(i=0;i<N;i++)B[i]=0; | |
| 107 | + for(i=0;i<N;i++)C[i]=0; | |
| 108 | + | |
| 109 | + printf("Solution of Tower of Hanoi Problem with %d Disks\n\n",N); | |
| 110 | + | |
| 111 | + /* Print the starting state */ | |
| 112 | + printf("Starting state:\n"); | |
| 113 | + PrintAll(); | |
| 114 | + printf("\n\nSubsequent states:\n\n"); | |
| 115 | + | |
| 116 | + /* Do it! Use A = Source, B = Destination, C = Spare */ | |
| 117 | + Hanoi(N,A,B,C); | |
| 118 | + | |
| 119 | + return 0; | |
| 120 | +} | |
| 121 | + | |
| 122 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,71 @@ | ||
| 1 | +Solution of Tower of Hanoi Problem with 4 Disks | |
| 2 | + | |
| 3 | +Starting state: | |
| 4 | +A: 1 2 3 4 | |
| 5 | +B: 0 0 0 0 | |
| 6 | +C: 0 0 0 0 | |
| 7 | +------------------------------------------ | |
| 8 | + | |
| 9 | + | |
| 10 | +Subsequent states: | |
| 11 | + | |
| 12 | +A: 0 2 3 4 | |
| 13 | +B: 0 0 0 0 | |
| 14 | +C: 0 0 0 1 | |
| 15 | +------------------------------------------ | |
| 16 | +A: 0 0 3 4 | |
| 17 | +B: 0 0 0 2 | |
| 18 | +C: 0 0 0 1 | |
| 19 | +------------------------------------------ | |
| 20 | +A: 0 0 3 4 | |
| 21 | +B: 0 0 1 2 | |
| 22 | +C: 0 0 0 0 | |
| 23 | +------------------------------------------ | |
| 24 | +A: 0 0 0 4 | |
| 25 | +B: 0 0 1 2 | |
| 26 | +C: 0 0 0 3 | |
| 27 | +------------------------------------------ | |
| 28 | +A: 0 0 1 4 | |
| 29 | +B: 0 0 0 2 | |
| 30 | +C: 0 0 0 3 | |
| 31 | +------------------------------------------ | |
| 32 | +A: 0 0 1 4 | |
| 33 | +B: 0 0 0 0 | |
| 34 | +C: 0 0 2 3 | |
| 35 | +------------------------------------------ | |
| 36 | +A: 0 0 0 4 | |
| 37 | +B: 0 0 0 0 | |
| 38 | +C: 0 1 2 3 | |
| 39 | +------------------------------------------ | |
| 40 | +A: 0 0 0 0 | |
| 41 | +B: 0 0 0 4 | |
| 42 | +C: 0 1 2 3 | |
| 43 | +------------------------------------------ | |
| 44 | +A: 0 0 0 0 | |
| 45 | +B: 0 0 1 4 | |
| 46 | +C: 0 0 2 3 | |
| 47 | +------------------------------------------ | |
| 48 | +A: 0 0 0 2 | |
| 49 | +B: 0 0 1 4 | |
| 50 | +C: 0 0 0 3 | |
| 51 | +------------------------------------------ | |
| 52 | +A: 0 0 1 2 | |
| 53 | +B: 0 0 0 4 | |
| 54 | +C: 0 0 0 3 | |
| 55 | +------------------------------------------ | |
| 56 | +A: 0 0 1 2 | |
| 57 | +B: 0 0 3 4 | |
| 58 | +C: 0 0 0 0 | |
| 59 | +------------------------------------------ | |
| 60 | +A: 0 0 0 2 | |
| 61 | +B: 0 0 3 4 | |
| 62 | +C: 0 0 0 1 | |
| 63 | +------------------------------------------ | |
| 64 | +A: 0 0 0 0 | |
| 65 | +B: 0 2 3 4 | |
| 66 | +C: 0 0 0 1 | |
| 67 | +------------------------------------------ | |
| 68 | +A: 0 0 0 0 | |
| 69 | +B: 1 2 3 4 | |
| 70 | +C: 0 0 0 0 | |
| 71 | +------------------------------------------ | |
| @@ -0,0 +1,14 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main(int argc, char **argv) | |
| 4 | +{ | |
| 5 | + int Count; | |
| 6 | + | |
| 7 | + printf("hello world %d\n", argc); | |
| 8 | + for (Count = 0; Count < argc; Count++) | |
| 9 | + printf("arg %d: %s\n", Count, argv[Count]); | |
| 10 | + | |
| 11 | + return 0; | |
| 12 | +} | |
| 13 | + | |
| 14 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,7 @@ | ||
| 1 | +hello world 6 | |
| 2 | +arg 0: 31_args.c | |
| 3 | +arg 1: - | |
| 4 | +arg 2: arg1 | |
| 5 | +arg 3: arg2 | |
| 6 | +arg 4: arg3 | |
| 7 | +arg 5: arg4 | |
| @@ -0,0 +1,266 @@ | ||
| 1 | +/* example from http://barnyard.syr.edu/quickies/led.c */ | |
| 2 | + | |
| 3 | +/* led.c: print out number as if on 7 line led display. I.e., write integer | |
| 4 | + given on command line like this: | |
| 5 | + _ _ _ | |
| 6 | + | _| _| |_| |_ | |
| 7 | + | |_ _| | _| etc. | |
| 8 | + | |
| 9 | + We assume the terminal behaves like a classical teletype. So the top | |
| 10 | + lines of all digits have to be printed first, then the middle lines of | |
| 11 | + all digits, etc. | |
| 12 | + | |
| 13 | + By Terry R. McConnell | |
| 14 | + | |
| 15 | +compile: cc -o led led.c | |
| 16 | + | |
| 17 | +If you just want to link in the subroutine print_led that does all the | |
| 18 | +work, compile with -DNO_MAIN, and declare the following in any source file | |
| 19 | +that uses the call: | |
| 20 | + | |
| 21 | +extern void print_led(unsigned long x, char *buf); | |
| 22 | + | |
| 23 | +Bug: you cannot call repeatedly to print more than one number to a line. | |
| 24 | +That would require curses or some other terminal API that allows moving the | |
| 25 | +cursor to a previous line. | |
| 26 | + | |
| 27 | +*/ | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | +#include <stdlib.h> | |
| 32 | +#include <stdio.h> | |
| 33 | + | |
| 34 | +#define MAX_DIGITS 32 | |
| 35 | +#define NO_MAIN | |
| 36 | + | |
| 37 | + | |
| 38 | +/* Print the top line of the digit d into buffer. | |
| 39 | + Does not null terminate buffer. */ | |
| 40 | + | |
| 41 | +void topline(int d, char *p){ | |
| 42 | + | |
| 43 | + *p++ = ' '; | |
| 44 | + switch(d){ | |
| 45 | + | |
| 46 | + /* all these have _ on top line */ | |
| 47 | + | |
| 48 | + case 0: | |
| 49 | + case 2: | |
| 50 | + case 3: | |
| 51 | + case 5: | |
| 52 | + case 7: | |
| 53 | + case 8: | |
| 54 | + case 9: | |
| 55 | + *p++ = '_'; | |
| 56 | + break; | |
| 57 | + default: | |
| 58 | + *p++=' '; | |
| 59 | + | |
| 60 | + } | |
| 61 | + *p++=' '; | |
| 62 | +} | |
| 63 | + | |
| 64 | +/* Print the middle line of the digit d into the buffer. | |
| 65 | + Does not null terminate. */ | |
| 66 | + | |
| 67 | +void midline(int d, char *p){ | |
| 68 | + | |
| 69 | + switch(d){ | |
| 70 | + | |
| 71 | + /* those that have leading | on middle line */ | |
| 72 | + | |
| 73 | + case 0: | |
| 74 | + case 4: | |
| 75 | + case 5: | |
| 76 | + case 6: | |
| 77 | + case 8: | |
| 78 | + case 9: | |
| 79 | + *p++='|'; | |
| 80 | + break; | |
| 81 | + default: | |
| 82 | + *p++=' '; | |
| 83 | + } | |
| 84 | + switch(d){ | |
| 85 | + | |
| 86 | + /* those that have _ on middle line */ | |
| 87 | + | |
| 88 | + case 2: | |
| 89 | + case 3: | |
| 90 | + case 4: | |
| 91 | + case 5: | |
| 92 | + case 6: | |
| 93 | + case 8: | |
| 94 | + case 9: | |
| 95 | + *p++='_'; | |
| 96 | + break; | |
| 97 | + default: | |
| 98 | + *p++=' '; | |
| 99 | + | |
| 100 | + } | |
| 101 | + switch(d){ | |
| 102 | + | |
| 103 | + /* those that have closing | on middle line */ | |
| 104 | + | |
| 105 | + case 0: | |
| 106 | + case 1: | |
| 107 | + case 2: | |
| 108 | + case 3: | |
| 109 | + case 4: | |
| 110 | + case 7: | |
| 111 | + case 8: | |
| 112 | + case 9: | |
| 113 | + *p++='|'; | |
| 114 | + break; | |
| 115 | + default: | |
| 116 | + *p++=' '; | |
| 117 | + | |
| 118 | + } | |
| 119 | +} | |
| 120 | + | |
| 121 | +/* Print the bottom line of the digit d. Does not null terminate. */ | |
| 122 | + | |
| 123 | +void botline(int d, char *p){ | |
| 124 | + | |
| 125 | + | |
| 126 | + switch(d){ | |
| 127 | + | |
| 128 | + /* those that have leading | on bottom line */ | |
| 129 | + | |
| 130 | + case 0: | |
| 131 | + case 2: | |
| 132 | + case 6: | |
| 133 | + case 8: | |
| 134 | + *p++='|'; | |
| 135 | + break; | |
| 136 | + default: | |
| 137 | + *p++=' '; | |
| 138 | + } | |
| 139 | + switch(d){ | |
| 140 | + | |
| 141 | + /* those that have _ on bottom line */ | |
| 142 | + | |
| 143 | + case 0: | |
| 144 | + case 2: | |
| 145 | + case 3: | |
| 146 | + case 5: | |
| 147 | + case 6: | |
| 148 | + case 8: | |
| 149 | + *p++='_'; | |
| 150 | + break; | |
| 151 | + default: | |
| 152 | + *p++=' '; | |
| 153 | + | |
| 154 | + } | |
| 155 | + switch(d){ | |
| 156 | + | |
| 157 | + /* those that have closing | on bottom line */ | |
| 158 | + | |
| 159 | + case 0: | |
| 160 | + case 1: | |
| 161 | + case 3: | |
| 162 | + case 4: | |
| 163 | + case 5: | |
| 164 | + case 6: | |
| 165 | + case 7: | |
| 166 | + case 8: | |
| 167 | + case 9: | |
| 168 | + *p++='|'; | |
| 169 | + break; | |
| 170 | + default: | |
| 171 | + *p++=' '; | |
| 172 | + | |
| 173 | + } | |
| 174 | +} | |
| 175 | + | |
| 176 | +/* Write the led representation of integer to string buffer. */ | |
| 177 | + | |
| 178 | +void print_led(unsigned long x, char *buf) | |
| 179 | +{ | |
| 180 | + | |
| 181 | + int i=0,n; | |
| 182 | + static int d[MAX_DIGITS]; | |
| 183 | + | |
| 184 | + | |
| 185 | + /* extract digits from x */ | |
| 186 | + | |
| 187 | + n = ( x == 0L ? 1 : 0 ); /* 0 is a digit, hence a special case */ | |
| 188 | + | |
| 189 | + while(x){ | |
| 190 | + d[n++] = (int)(x%10L); | |
| 191 | + if(n >= MAX_DIGITS)break; | |
| 192 | + x = x/10L; | |
| 193 | + } | |
| 194 | + | |
| 195 | + /* print top lines of all digits */ | |
| 196 | + | |
| 197 | + for(i=n-1;i>=0;i--){ | |
| 198 | + topline(d[i],buf); | |
| 199 | + buf += 3; | |
| 200 | + *buf++=' '; | |
| 201 | + } | |
| 202 | + *buf++='\n'; /* move teletype to next line */ | |
| 203 | + | |
| 204 | + /* print middle lines of all digits */ | |
| 205 | + | |
| 206 | + for(i=n-1;i>=0;i--){ | |
| 207 | + midline(d[i],buf); | |
| 208 | + buf += 3; | |
| 209 | + *buf++=' '; | |
| 210 | + } | |
| 211 | + *buf++='\n'; | |
| 212 | + | |
| 213 | + /* print bottom lines of all digits */ | |
| 214 | + | |
| 215 | + for(i=n-1;i>=0;i--){ | |
| 216 | + botline(d[i],buf); | |
| 217 | + buf += 3; | |
| 218 | + *buf++=' '; | |
| 219 | + } | |
| 220 | + *buf++='\n'; | |
| 221 | + *buf='\0'; | |
| 222 | +} | |
| 223 | + | |
| 224 | +int main() | |
| 225 | +{ | |
| 226 | + char buf[5*MAX_DIGITS]; | |
| 227 | + print_led(1234567, buf); | |
| 228 | + printf("%s\n",buf); | |
| 229 | + | |
| 230 | + return 0; | |
| 231 | +} | |
| 232 | + | |
| 233 | +#ifndef NO_MAIN | |
| 234 | +int main(int argc, char **argv) | |
| 235 | +{ | |
| 236 | + | |
| 237 | + int i=0,n; | |
| 238 | + long x; | |
| 239 | + static int d[MAX_DIGITS]; | |
| 240 | + char buf[5*MAX_DIGITS]; | |
| 241 | + | |
| 242 | + if(argc != 2){ | |
| 243 | + fprintf(stderr,"led: usage: led integer\n"); | |
| 244 | + return 1; | |
| 245 | + } | |
| 246 | + | |
| 247 | + /* fetch argument from command line */ | |
| 248 | + | |
| 249 | + x = atol(argv[1]); | |
| 250 | + | |
| 251 | + /* sanity check */ | |
| 252 | + | |
| 253 | + if(x<0){ | |
| 254 | + fprintf(stderr,"led: %d must be non-negative\n",x); | |
| 255 | + return 1; | |
| 256 | + } | |
| 257 | + | |
| 258 | + print_led(x,buf); | |
| 259 | + printf("%s\n",buf); | |
| 260 | + | |
| 261 | + return 0; | |
| 262 | + | |
| 263 | +} | |
| 264 | +#endif | |
| 265 | + | |
| 266 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,4 @@ | ||
| 1 | + _ _ _ _ | |
| 2 | + | _| _| |_| |_ |_ | | |
| 3 | + | |_ _| | _| |_| | | |
| 4 | + | |
| @@ -0,0 +1,15 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int Count; | |
| 6 | + | |
| 7 | + for (Count = 0; Count < 10; Count++) | |
| 8 | + { | |
| 9 | + printf("%d\n", (Count < 5) ? (Count*Count) : (Count * 3)); | |
| 10 | + } | |
| 11 | + | |
| 12 | + return 0; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,10 @@ | ||
| 1 | +0 | |
| 2 | +1 | |
| 3 | +4 | |
| 4 | +9 | |
| 5 | +16 | |
| 6 | +15 | |
| 7 | +18 | |
| 8 | +21 | |
| 9 | +24 | |
| 10 | +27 | |
| @@ -0,0 +1,23 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a[4]; | |
| 6 | + | |
| 7 | + a[0] = 12; | |
| 8 | + a[1] = 23; | |
| 9 | + a[2] = 34; | |
| 10 | + a[3] = 45; | |
| 11 | + | |
| 12 | + printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]); | |
| 13 | + | |
| 14 | + int b[4]; | |
| 15 | + | |
| 16 | + b = a; | |
| 17 | + | |
| 18 | + printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]); | |
| 19 | + | |
| 20 | + return 0; | |
| 21 | +} | |
| 22 | + | |
| 23 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +12 23 34 45 | |
| 2 | +12 23 34 45 | |
| @@ -0,0 +1,14 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + char a; | |
| 6 | + short b; | |
| 7 | + | |
| 8 | + printf("%d %d\n", sizeof(char), sizeof(a)); | |
| 9 | + printf("%d %d\n", sizeof(short), sizeof(b)); | |
| 10 | + | |
| 11 | + return 0; | |
| 12 | +} | |
| 13 | + | |
| 14 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +1 1 | |
| 2 | +2 2 | |
| @@ -0,0 +1,21 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int Count; | |
| 6 | + | |
| 7 | + int Array[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753 }; | |
| 8 | + | |
| 9 | + for (Count = 0; Count < 10; Count++) | |
| 10 | + printf("%d: %d\n", Count, Array[Count]); | |
| 11 | + | |
| 12 | + int Array2[10] = { 12, 34, 56, 78, 90, 123, 456, 789, 8642, 9753, }; | |
| 13 | + | |
| 14 | + for (Count = 0; Count < 10; Count++) | |
| 15 | + printf("%d: %d\n", Count, Array2[Count]); | |
| 16 | + | |
| 17 | + | |
| 18 | + return 0; | |
| 19 | +} | |
| 20 | + | |
| 21 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,20 @@ | ||
| 1 | +0: 12 | |
| 2 | +1: 34 | |
| 3 | +2: 56 | |
| 4 | +3: 78 | |
| 5 | +4: 90 | |
| 6 | +5: 123 | |
| 7 | +6: 456 | |
| 8 | +7: 789 | |
| 9 | +8: 8642 | |
| 10 | +9: 9753 | |
| 11 | +0: 12 | |
| 12 | +1: 34 | |
| 13 | +2: 56 | |
| 14 | +3: 78 | |
| 15 | +4: 90 | |
| 16 | +5: 123 | |
| 17 | +6: 456 | |
| 18 | +7: 789 | |
| 19 | +8: 8642 | |
| 20 | +9: 9753 | |
| @@ -0,0 +1,17 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + char Buf[100]; | |
| 6 | + int Count; | |
| 7 | + | |
| 8 | + for (Count = 1; Count <= 20; Count++) | |
| 9 | + { | |
| 10 | + sprintf(Buf, "->%02d<-\n", Count); | |
| 11 | + printf("%s", Buf); | |
| 12 | + } | |
| 13 | + | |
| 14 | + return 0; | |
| 15 | +} | |
| 16 | + | |
| 17 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,20 @@ | ||
| 1 | +->01<- | |
| 2 | +->02<- | |
| 3 | +->03<- | |
| 4 | +->04<- | |
| 5 | +->05<- | |
| 6 | +->06<- | |
| 7 | +->07<- | |
| 8 | +->08<- | |
| 9 | +->09<- | |
| 10 | +->10<- | |
| 11 | +->11<- | |
| 12 | +->12<- | |
| 13 | +->13<- | |
| 14 | +->14<- | |
| 15 | +->15<- | |
| 16 | +->16<- | |
| 17 | +->17<- | |
| 18 | +->18<- | |
| 19 | +->19<- | |
| 20 | +->20<- | |
| @@ -0,0 +1,32 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a[4][4]; | |
| 6 | + int b = 0; | |
| 7 | + int x; | |
| 8 | + int y; | |
| 9 | + | |
| 10 | + for (x = 0; x < 4; x++) | |
| 11 | + { | |
| 12 | + for (y = 0; y < 4; y++) | |
| 13 | + { | |
| 14 | + b++; | |
| 15 | + a[x][y] = b; | |
| 16 | + } | |
| 17 | + } | |
| 18 | + | |
| 19 | + for (x = 0; x < 4; x++) | |
| 20 | + { | |
| 21 | + printf("x=%d: ", x); | |
| 22 | + for (y = 0; y < 4; y++) | |
| 23 | + { | |
| 24 | + printf("%d ", a[x][y]); | |
| 25 | + } | |
| 26 | + printf("\n"); | |
| 27 | + } | |
| 28 | + | |
| 29 | + return 0; | |
| 30 | +} | |
| 31 | + | |
| 32 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,4 @@ | ||
| 1 | +x=0: 1 2 3 4 | |
| 2 | +x=1: 5 6 7 8 | |
| 3 | +x=2: 9 10 11 12 | |
| 4 | +x=3: 13 14 15 16 | |
| @@ -0,0 +1,31 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +typedef int MyInt; | |
| 4 | + | |
| 5 | +struct FunStruct | |
| 6 | +{ | |
| 7 | + int i; | |
| 8 | + int j; | |
| 9 | +}; | |
| 10 | + | |
| 11 | +typedef struct FunStruct MyFunStruct; | |
| 12 | + | |
| 13 | +typedef MyFunStruct *MoreFunThanEver; | |
| 14 | + | |
| 15 | +int main() | |
| 16 | +{ | |
| 17 | + MyInt a = 1; | |
| 18 | + printf("%d\n", a); | |
| 19 | + | |
| 20 | + MyFunStruct b; | |
| 21 | + b.i = 12; | |
| 22 | + b.j = 34; | |
| 23 | + printf("%d,%d\n", b.i, b.j); | |
| 24 | + | |
| 25 | + MoreFunThanEver c = &b; | |
| 26 | + printf("%d,%d\n", c->i, c->j); | |
| 27 | + | |
| 28 | + return 0; | |
| 29 | +} | |
| 30 | + | |
| 31 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,3 @@ | ||
| 1 | +1 | |
| 2 | +12,34 | |
| 3 | +12,34 | |
| @@ -0,0 +1,52 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + FILE *f = fopen("fred.txt", "w"); | |
| 6 | + fwrite("hello\nhello\n", 1, 12, f); | |
| 7 | + fclose(f); | |
| 8 | + | |
| 9 | + char freddy[7]; | |
| 10 | + f = fopen("fred.txt", "r"); | |
| 11 | + if (fread(freddy, 1, 6, f) != 6) | |
| 12 | + printf("couldn't read fred.txt\n"); | |
| 13 | + | |
| 14 | + freddy[6] = '\0'; | |
| 15 | + fclose(f); | |
| 16 | + | |
| 17 | + printf("%s", freddy); | |
| 18 | + | |
| 19 | + char InChar; | |
| 20 | + char ShowChar; | |
| 21 | + f = fopen("fred.txt", "r"); | |
| 22 | + while ( (InChar = fgetc(f)) != EOF) | |
| 23 | + { | |
| 24 | + ShowChar = InChar; | |
| 25 | + if (ShowChar < ' ') | |
| 26 | + ShowChar = '.'; | |
| 27 | + | |
| 28 | + printf("ch: %d '%c'\n", InChar, ShowChar); | |
| 29 | + } | |
| 30 | + fclose(f); | |
| 31 | + | |
| 32 | + f = fopen("fred.txt", "r"); | |
| 33 | + while ( (InChar = getc(f)) != EOF) | |
| 34 | + { | |
| 35 | + ShowChar = InChar; | |
| 36 | + if (ShowChar < ' ') | |
| 37 | + ShowChar = '.'; | |
| 38 | + | |
| 39 | + printf("ch: %d '%c'\n", InChar, ShowChar); | |
| 40 | + } | |
| 41 | + fclose(f); | |
| 42 | + | |
| 43 | + f = fopen("fred.txt", "r"); | |
| 44 | + while (fgets(freddy, sizeof(freddy), f) != NULL) | |
| 45 | + printf("x: %s", freddy); | |
| 46 | + | |
| 47 | + fclose(f); | |
| 48 | + | |
| 49 | + return 0; | |
| 50 | +} | |
| 51 | + | |
| 52 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,27 @@ | ||
| 1 | +hello | |
| 2 | +ch: 104 'h' | |
| 3 | +ch: 101 'e' | |
| 4 | +ch: 108 'l' | |
| 5 | +ch: 108 'l' | |
| 6 | +ch: 111 'o' | |
| 7 | +ch: 10 '.' | |
| 8 | +ch: 104 'h' | |
| 9 | +ch: 101 'e' | |
| 10 | +ch: 108 'l' | |
| 11 | +ch: 108 'l' | |
| 12 | +ch: 111 'o' | |
| 13 | +ch: 10 '.' | |
| 14 | +ch: 104 'h' | |
| 15 | +ch: 101 'e' | |
| 16 | +ch: 108 'l' | |
| 17 | +ch: 108 'l' | |
| 18 | +ch: 111 'o' | |
| 19 | +ch: 10 '.' | |
| 20 | +ch: 104 'h' | |
| 21 | +ch: 101 'e' | |
| 22 | +ch: 108 'l' | |
| 23 | +ch: 108 'l' | |
| 24 | +ch: 111 'o' | |
| 25 | +ch: 10 '.' | |
| 26 | +x: hello | |
| 27 | +x: hello | |
| @@ -0,0 +1,85 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + printf("#include test\n"); | |
| 6 | + | |
| 7 | +#if 1 | |
| 8 | +#if 0 | |
| 9 | + printf("a\n"); | |
| 10 | +#else | |
| 11 | + printf("b\n"); | |
| 12 | +#endif | |
| 13 | +#else | |
| 14 | +#if 0 | |
| 15 | + printf("c\n"); | |
| 16 | +#else | |
| 17 | + printf("d\n"); | |
| 18 | +#endif | |
| 19 | +#endif | |
| 20 | + | |
| 21 | +#if 0 | |
| 22 | +#if 1 | |
| 23 | + printf("e\n"); | |
| 24 | +#else | |
| 25 | + printf("f\n"); | |
| 26 | +#endif | |
| 27 | +#else | |
| 28 | +#if 1 | |
| 29 | + printf("g\n"); | |
| 30 | +#else | |
| 31 | + printf("h\n"); | |
| 32 | +#endif | |
| 33 | +#endif | |
| 34 | + | |
| 35 | +#define DEF | |
| 36 | + | |
| 37 | +#ifdef DEF | |
| 38 | +#ifdef DEF | |
| 39 | + printf("i\n"); | |
| 40 | +#else | |
| 41 | + printf("j\n"); | |
| 42 | +#endif | |
| 43 | +#else | |
| 44 | +#ifdef DEF | |
| 45 | + printf("k\n"); | |
| 46 | +#else | |
| 47 | + printf("l\n"); | |
| 48 | +#endif | |
| 49 | +#endif | |
| 50 | + | |
| 51 | +#ifndef DEF | |
| 52 | +#ifndef DEF | |
| 53 | + printf("m\n"); | |
| 54 | +#else | |
| 55 | + printf("n\n"); | |
| 56 | +#endif | |
| 57 | +#else | |
| 58 | +#ifndef DEF | |
| 59 | + printf("o\n"); | |
| 60 | +#else | |
| 61 | + printf("p\n"); | |
| 62 | +#endif | |
| 63 | +#endif | |
| 64 | + | |
| 65 | +#define ONE 1 | |
| 66 | +#define ZERO 0 | |
| 67 | + | |
| 68 | +#if ONE | |
| 69 | +#if ZERO | |
| 70 | + printf("q\n"); | |
| 71 | +#else | |
| 72 | + printf("r\n"); | |
| 73 | +#endif | |
| 74 | +#else | |
| 75 | +#if ZERO | |
| 76 | + printf("s\n"); | |
| 77 | +#else | |
| 78 | + printf("t\n"); | |
| 79 | +#endif | |
| 80 | +#endif | |
| 81 | + | |
| 82 | + return 0; | |
| 83 | +} | |
| 84 | + | |
| 85 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,6 @@ | ||
| 1 | +#include test | |
| 2 | +b | |
| 3 | +g | |
| 4 | +i | |
| 5 | +p | |
| 6 | +r | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int fred(int p) | |
| 4 | +{ | |
| 5 | + printf("yo %d\n", p); | |
| 6 | + return 42; | |
| 7 | +} | |
| 8 | + | |
| 9 | +int (*f)(int) = &fred; | |
| 10 | + | |
| 11 | +int main() | |
| 12 | +{ | |
| 13 | + printf("%d\n", (*f)(24)); | |
| 14 | + | |
| 15 | + return 0; | |
| 16 | +} | |
| 17 | + | |
| 18 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,2 @@ | ||
| 1 | +yo 24 | |
| 2 | +42 | |
| @@ -0,0 +1,15 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +void fred(void) | |
| 4 | +{ | |
| 5 | + printf("yo\n"); | |
| 6 | +} | |
| 7 | + | |
| 8 | +int main() | |
| 9 | +{ | |
| 10 | + fred(); | |
| 11 | + | |
| 12 | + return 0; | |
| 13 | +} | |
| 14 | + | |
| 15 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1 @@ | ||
| 1 | +yo | |
| @@ -0,0 +1,17 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + | |
| 7 | + for (a = 0; a < 2; a++) | |
| 8 | + { | |
| 9 | + int b = a; | |
| 10 | + } | |
| 11 | + | |
| 12 | + printf("it's all good\n"); | |
| 13 | + | |
| 14 | + return 0; | |
| 15 | +} | |
| 16 | + | |
| 17 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1 @@ | ||
| 1 | +it's all good | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int Count = 0; | |
| 6 | + | |
| 7 | + for (;;) | |
| 8 | + { | |
| 9 | + Count++; | |
| 10 | + printf("%d\n", Count); | |
| 11 | + if (Count >= 10) | |
| 12 | + break; | |
| 13 | + } | |
| 14 | + | |
| 15 | + return 0; | |
| 16 | +} | |
| 17 | + | |
| 18 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,10 @@ | ||
| 1 | +1 | |
| 2 | +2 | |
| 3 | +3 | |
| 4 | +4 | |
| 5 | +5 | |
| 6 | +6 | |
| 7 | +7 | |
| 8 | +8 | |
| 9 | +9 | |
| 10 | +10 | |
| @@ -0,0 +1,564 @@ | ||
| 1 | +/* | |
| 2 | + * The information in this document is subject to change | |
| 3 | + * without notice and should not be construed as a commitment | |
| 4 | + * by Digital Equipment Corporation or by DECUS. | |
| 5 | + * | |
| 6 | + * Neither Digital Equipment Corporation, DECUS, nor the authors | |
| 7 | + * assume any responsibility for the use or reliability of this | |
| 8 | + * document or the described software. | |
| 9 | + * | |
| 10 | + * Copyright (C) 1980, DECUS | |
| 11 | + * | |
| 12 | + * General permission to copy or modify, but not for profit, is | |
| 13 | + * hereby granted, provided that the above copyright notice is | |
| 14 | + * included and reference made to the fact that reproduction | |
| 15 | + * privileges were granted by DECUS. | |
| 16 | + */ | |
| 17 | +#include <stdio.h> | |
| 18 | + | |
| 19 | +/* | |
| 20 | + * grep | |
| 21 | + * | |
| 22 | + * Runs on the Decus compiler or on vms, On vms, define as: | |
| 23 | + * grep :== "$disk:[account]grep" (native) | |
| 24 | + * grep :== "$disk:[account]grep grep" (Decus) | |
| 25 | + * See below for more information. | |
| 26 | + */ | |
| 27 | + | |
| 28 | +#if 0 | |
| 29 | +char *documentation[] = { | |
| 30 | + "grep searches a file for a given pattern. Execute by", | |
| 31 | + " grep [flags] regular_expression file_list\n", | |
| 32 | + "Flags are single characters preceeded by '-':", | |
| 33 | + " -c Only a count of matching lines is printed", | |
| 34 | + " -f Print file name for matching lines switch, see below", | |
| 35 | + " -n Each line is preceeded by its line number", | |
| 36 | + " -v Only print non-matching lines\n", | |
| 37 | + "The file_list is a list of files (wildcards are acceptable on RSX modes).", | |
| 38 | + "\nThe file name is normally printed if there is a file given.", | |
| 39 | + "The -f flag reverses this action (print name no file, not if more).\n", | |
| 40 | + 0 }; | |
| 41 | + | |
| 42 | +char *patdoc[] = { | |
| 43 | + "The regular_expression defines the pattern to search for. Upper- and", | |
| 44 | + "lower-case are always ignored. Blank lines never match. The expression", | |
| 45 | + "should be quoted to prevent file-name translation.", | |
| 46 | + "x An ordinary character (not mentioned below) matches that character.", | |
| 47 | + "'\\' The backslash quotes any character. \"\\$\" matches a dollar-sign.", | |
| 48 | + "'^' A circumflex at the beginning of an expression matches the", | |
| 49 | + " beginning of a line.", | |
| 50 | + "'$' A dollar-sign at the end of an expression matches the end of a line.", | |
| 51 | + "'.' A period matches any character except \"new-line\".", | |
| 52 | + "':a' A colon matches a class of characters described by the following", | |
| 53 | + "':d' character. \":a\" matches any alphabetic, \":d\" matches digits,", | |
| 54 | + "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and", | |
| 55 | + "': ' other control characters, such as new-line.", | |
| 56 | + "'*' An expression followed by an asterisk matches zero or more", | |
| 57 | + " occurrances of that expression: \"fo*\" matches \"f\", \"fo\"", | |
| 58 | + " \"foo\", etc.", | |
| 59 | + "'+' An expression followed by a plus sign matches one or more", | |
| 60 | + " occurrances of that expression: \"fo+\" matches \"fo\", etc.", | |
| 61 | + "'-' An expression followed by a minus sign optionally matches", | |
| 62 | + " the expression.", | |
| 63 | + "'[]' A string enclosed in square brackets matches any character in", | |
| 64 | + " that string, but no others. If the first character in the", | |
| 65 | + " string is a circumflex, the expression matches any character", | |
| 66 | + " except \"new-line\" and the characters in the string. For", | |
| 67 | + " example, \"[xyz]\" matches \"xx\" and \"zyx\", while \"[^xyz]\"", | |
| 68 | + " matches \"abc\" but not \"axb\". A range of characters may be", | |
| 69 | + " specified by two characters separated by \"-\". Note that,", | |
| 70 | + " [a-z] matches alphabetics, while [z-a] never matches.", | |
| 71 | + "The concatenation of regular expressions is a regular expression.", | |
| 72 | + 0}; | |
| 73 | +#endif | |
| 74 | + | |
| 75 | +#define LMAX 512 | |
| 76 | +#define PMAX 256 | |
| 77 | + | |
| 78 | +#define CHAR 1 | |
| 79 | +#define BOL 2 | |
| 80 | +#define EOL 3 | |
| 81 | +#define ANY 4 | |
| 82 | +#define CLASS 5 | |
| 83 | +#define NCLASS 6 | |
| 84 | +#define STAR 7 | |
| 85 | +#define PLUS 8 | |
| 86 | +#define MINUS 9 | |
| 87 | +#define ALPHA 10 | |
| 88 | +#define DIGIT 11 | |
| 89 | +#define NALPHA 12 | |
| 90 | +#define PUNCT 13 | |
| 91 | +#define RANGE 14 | |
| 92 | +#define ENDPAT 15 | |
| 93 | + | |
| 94 | +int cflag=0, fflag=0, nflag=0, vflag=0, nfile=0, debug=0; | |
| 95 | + | |
| 96 | +char *pp, lbuf[LMAX], pbuf[PMAX]; | |
| 97 | + | |
| 98 | +char *cclass(); | |
| 99 | +char *pmatch(); | |
| 100 | + | |
| 101 | + | |
| 102 | +/*** Display a file name *******************************/ | |
| 103 | +void file(char *s) | |
| 104 | +{ | |
| 105 | + printf("File %s:\n", s); | |
| 106 | +} | |
| 107 | + | |
| 108 | +/*** Report unopenable file ****************************/ | |
| 109 | +void cant(char *s) | |
| 110 | +{ | |
| 111 | + fprintf(stderr, "%s: cannot open\n", s); | |
| 112 | +} | |
| 113 | + | |
| 114 | +/*** Give good help ************************************/ | |
| 115 | +void help(char **hp) | |
| 116 | +{ | |
| 117 | + char **dp; | |
| 118 | + | |
| 119 | + for (dp = hp; *dp; ++dp) | |
| 120 | + printf("%s\n", *dp); | |
| 121 | +} | |
| 122 | + | |
| 123 | +/*** Display usage summary *****************************/ | |
| 124 | +void usage(char *s) | |
| 125 | +{ | |
| 126 | + fprintf(stderr, "?GREP-E-%s\n", s); | |
| 127 | + fprintf(stderr, | |
| 128 | + "Usage: grep [-cfnv] pattern [file ...]. grep ? for help\n"); | |
| 129 | + exit(1); | |
| 130 | +} | |
| 131 | + | |
| 132 | +/*** Compile the pattern into global pbuf[] ************/ | |
| 133 | +void compile(char *source) | |
| 134 | +{ | |
| 135 | + char *s; /* Source string pointer */ | |
| 136 | + char *lp; /* Last pattern pointer */ | |
| 137 | + int c; /* Current character */ | |
| 138 | + int o; /* Temp */ | |
| 139 | + char *spp; /* Save beginning of pattern */ | |
| 140 | + | |
| 141 | + s = source; | |
| 142 | + if (debug) | |
| 143 | + printf("Pattern = \"%s\"\n", s); | |
| 144 | + pp = pbuf; | |
| 145 | + while (c = *s++) { | |
| 146 | + /* | |
| 147 | + * STAR, PLUS and MINUS are special. | |
| 148 | + */ | |
| 149 | + if (c == '*' || c == '+' || c == '-') { | |
| 150 | + if (pp == pbuf || | |
| 151 | + (o=pp[-1]) == BOL || | |
| 152 | + o == EOL || | |
| 153 | + o == STAR || | |
| 154 | + o == PLUS || | |
| 155 | + o == MINUS) | |
| 156 | + badpat("Illegal occurrance op.", source, s); | |
| 157 | + store(ENDPAT); | |
| 158 | + store(ENDPAT); | |
| 159 | + spp = pp; /* Save pattern end */ | |
| 160 | + while (--pp > lp) /* Move pattern down */ | |
| 161 | + *pp = pp[-1]; /* one byte */ | |
| 162 | + *pp = (c == '*') ? STAR : | |
| 163 | + (c == '-') ? MINUS : PLUS; | |
| 164 | + pp = spp; /* Restore pattern end */ | |
| 165 | + continue; | |
| 166 | + } | |
| 167 | + /* | |
| 168 | + * All the rest. | |
| 169 | + */ | |
| 170 | + lp = pp; /* Remember start */ | |
| 171 | + switch(c) { | |
| 172 | + | |
| 173 | + case '^': | |
| 174 | + store(BOL); | |
| 175 | + break; | |
| 176 | + | |
| 177 | + case '$': | |
| 178 | + store(EOL); | |
| 179 | + break; | |
| 180 | + | |
| 181 | + case '.': | |
| 182 | + store(ANY); | |
| 183 | + break; | |
| 184 | + | |
| 185 | + case '[': | |
| 186 | + s = cclass(source, s); | |
| 187 | + break; | |
| 188 | + | |
| 189 | + case ':': | |
| 190 | + if (*s) { | |
| 191 | + switch(tolower(c = *s++)) { | |
| 192 | + | |
| 193 | + case 'a': | |
| 194 | + case 'A': | |
| 195 | + store(ALPHA); | |
| 196 | + break; | |
| 197 | + | |
| 198 | + case 'd': | |
| 199 | + case 'D': | |
| 200 | + store(DIGIT); | |
| 201 | + break; | |
| 202 | + | |
| 203 | + case 'n': | |
| 204 | + case 'N': | |
| 205 | + store(NALPHA); | |
| 206 | + break; | |
| 207 | + | |
| 208 | + case ' ': | |
| 209 | + store(PUNCT); | |
| 210 | + break; | |
| 211 | + | |
| 212 | + default: | |
| 213 | + badpat("Unknown : type", source, s); | |
| 214 | + | |
| 215 | + } | |
| 216 | + break; | |
| 217 | + } | |
| 218 | + else badpat("No : type", source, s); | |
| 219 | + | |
| 220 | + case '\\': | |
| 221 | + if (*s) | |
| 222 | + c = *s++; | |
| 223 | + | |
| 224 | + default: | |
| 225 | + store(CHAR); | |
| 226 | + store(tolower(c)); | |
| 227 | + } | |
| 228 | + } | |
| 229 | + store(ENDPAT); | |
| 230 | + store(0); /* Terminate string */ | |
| 231 | + if (debug) { | |
| 232 | + for (lp = pbuf; lp < pp;) { | |
| 233 | + if ((c = (*lp++ & 0377)) < ' ') | |
| 234 | + printf("\\%o ", c); | |
| 235 | + else printf("%c ", c); | |
| 236 | + } | |
| 237 | + printf("\n"); | |
| 238 | + } | |
| 239 | +} | |
| 240 | + | |
| 241 | +/*** Compile a class (within []) ***********************/ | |
| 242 | +char *cclass(char *source, char *src) | |
| 243 | + /* char *source; // Pattern start -- for error msg. */ | |
| 244 | + /* char *src; // Class start */ | |
| 245 | +{ | |
| 246 | + char *s; /* Source pointer */ | |
| 247 | + char *cp; /* Pattern start */ | |
| 248 | + int c; /* Current character */ | |
| 249 | + int o; /* Temp */ | |
| 250 | + | |
| 251 | + s = src; | |
| 252 | + o = CLASS; | |
| 253 | + if (*s == '^') { | |
| 254 | + ++s; | |
| 255 | + o = NCLASS; | |
| 256 | + } | |
| 257 | + store(o); | |
| 258 | + cp = pp; | |
| 259 | + store(0); /* Byte count */ | |
| 260 | + while ((c = *s++) && c!=']') { | |
| 261 | + if (c == '\\') { /* Store quoted char */ | |
| 262 | + if ((c = *s++) == '\0') /* Gotta get something */ | |
| 263 | + badpat("Class terminates badly", source, s); | |
| 264 | + else store(tolower(c)); | |
| 265 | + } | |
| 266 | + else if (c == '-' && | |
| 267 | + (pp - cp) > 1 && *s != ']' && *s != '\0') { | |
| 268 | + c = pp[-1]; /* Range start */ | |
| 269 | + pp[-1] = RANGE; /* Range signal */ | |
| 270 | + store(c); /* Re-store start */ | |
| 271 | + c = *s++; /* Get end char and*/ | |
| 272 | + store(tolower(c)); /* Store it */ | |
| 273 | + } | |
| 274 | + else { | |
| 275 | + store(tolower(c)); /* Store normal char */ | |
| 276 | + } | |
| 277 | + } | |
| 278 | + if (c != ']') | |
| 279 | + badpat("Unterminated class", source, s); | |
| 280 | + if ((c = (pp - cp)) >= 256) | |
| 281 | + badpat("Class too large", source, s); | |
| 282 | + if (c == 0) | |
| 283 | + badpat("Empty class", source, s); | |
| 284 | + *cp = c; | |
| 285 | + return(s); | |
| 286 | +} | |
| 287 | + | |
| 288 | +/*** Store an entry in the pattern buffer **************/ | |
| 289 | +void store(int op) | |
| 290 | +{ | |
| 291 | + if (pp >= &pbuf[PMAX]) | |
| 292 | + error("Pattern too complex\n"); | |
| 293 | + *pp++ = op; | |
| 294 | +} | |
| 295 | + | |
| 296 | +/*** Report a bad pattern specification ****************/ | |
| 297 | +void badpat(char *message, char *source, char *stop) | |
| 298 | + /* char *message; // Error message */ | |
| 299 | + /* char *source; // Pattern start */ | |
| 300 | + /* char *stop; // Pattern end */ | |
| 301 | +{ | |
| 302 | + fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source); | |
| 303 | + fprintf(stderr, "-GREP-E-Stopped at byte %d, '%c'\n", | |
| 304 | + stop-source, stop[-1]); | |
| 305 | + error("?GREP-E-Bad pattern\n"); | |
| 306 | +} | |
| 307 | + | |
| 308 | +/*** Scan the file for the pattern in pbuf[] ***********/ | |
| 309 | +void grep(FILE *fp, char *fn) | |
| 310 | + /* FILE *fp; // File to process */ | |
| 311 | + /* char *fn; // File name (for -f option) */ | |
| 312 | +{ | |
| 313 | + int lno, count, m; | |
| 314 | + | |
| 315 | + lno = 0; | |
| 316 | + count = 0; | |
| 317 | + while (fgets(lbuf, LMAX, fp)) { | |
| 318 | + ++lno; | |
| 319 | + m = match(); | |
| 320 | + if ((m && !vflag) || (!m && vflag)) { | |
| 321 | + ++count; | |
| 322 | + if (!cflag) { | |
| 323 | + if (fflag && fn) { | |
| 324 | + file(fn); | |
| 325 | + fn = 0; | |
| 326 | + } | |
| 327 | + if (nflag) | |
| 328 | + printf("%d\t", lno); | |
| 329 | + printf("%s\n", lbuf); | |
| 330 | + } | |
| 331 | + } | |
| 332 | + } | |
| 333 | + if (cflag) { | |
| 334 | + if (fflag && fn) | |
| 335 | + file(fn); | |
| 336 | + printf("%d\n", count); | |
| 337 | + } | |
| 338 | +} | |
| 339 | + | |
| 340 | +/*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/ | |
| 341 | +void match() | |
| 342 | +{ | |
| 343 | + char *l; /* Line pointer */ | |
| 344 | + | |
| 345 | + for (l = lbuf; *l; ++l) { | |
| 346 | + if (pmatch(l, pbuf)) | |
| 347 | + return(1); | |
| 348 | + } | |
| 349 | + return(0); | |
| 350 | +} | |
| 351 | + | |
| 352 | +/*** Match partial line with pattern *******************/ | |
| 353 | +char *pmatch(char *line, char *pattern) | |
| 354 | + /* char *line; // (partial) line to match */ | |
| 355 | + /* char *pattern; // (partial) pattern to match */ | |
| 356 | +{ | |
| 357 | + char *l; /* Current line pointer */ | |
| 358 | + char *p; /* Current pattern pointer */ | |
| 359 | + char c; /* Current character */ | |
| 360 | + char *e; /* End for STAR and PLUS match */ | |
| 361 | + int op; /* Pattern operation */ | |
| 362 | + int n; /* Class counter */ | |
| 363 | + char *are; /* Start of STAR match */ | |
| 364 | + | |
| 365 | + l = line; | |
| 366 | + if (debug > 1) | |
| 367 | + printf("pmatch(\"%s\")\n", line); | |
| 368 | + p = pattern; | |
| 369 | + while ((op = *p++) != ENDPAT) { | |
| 370 | + if (debug > 1) | |
| 371 | + printf("byte[%d] = 0%o, '%c', op = 0%o\n", | |
| 372 | + l-line, *l, *l, op); | |
| 373 | + switch(op) { | |
| 374 | + | |
| 375 | + case CHAR: | |
| 376 | + if (tolower(*l++) != *p++) | |
| 377 | + return(0); | |
| 378 | + break; | |
| 379 | + | |
| 380 | + case BOL: | |
| 381 | + if (l != lbuf) | |
| 382 | + return(0); | |
| 383 | + break; | |
| 384 | + | |
| 385 | + case EOL: | |
| 386 | + if (*l != '\0') | |
| 387 | + return(0); | |
| 388 | + break; | |
| 389 | + | |
| 390 | + case ANY: | |
| 391 | + if (*l++ == '\0') | |
| 392 | + return(0); | |
| 393 | + break; | |
| 394 | + | |
| 395 | + case DIGIT: | |
| 396 | + if ((c = *l++) < '0' || (c > '9')) | |
| 397 | + return(0); | |
| 398 | + break; | |
| 399 | + | |
| 400 | + case ALPHA: | |
| 401 | + c = tolower(*l++); | |
| 402 | + if (c < 'a' || c > 'z') | |
| 403 | + return(0); | |
| 404 | + break; | |
| 405 | + | |
| 406 | + case NALPHA: | |
| 407 | + c = tolower(*l++); | |
| 408 | + if (c >= 'a' && c <= 'z') | |
| 409 | + break; | |
| 410 | + else if (c < '0' || c > '9') | |
| 411 | + return(0); | |
| 412 | + break; | |
| 413 | + | |
| 414 | + case PUNCT: | |
| 415 | + c = *l++; | |
| 416 | + if (c == 0 || c > ' ') | |
| 417 | + return(0); | |
| 418 | + break; | |
| 419 | + | |
| 420 | + case CLASS: | |
| 421 | + case NCLASS: | |
| 422 | + c = tolower(*l++); | |
| 423 | + n = *p++ & 0377; | |
| 424 | + do { | |
| 425 | + if (*p == RANGE) { | |
| 426 | + p += 3; | |
| 427 | + n -= 2; | |
| 428 | + if (c >= p[-2] && c <= p[-1]) | |
| 429 | + break; | |
| 430 | + } | |
| 431 | + else if (c == *p++) | |
| 432 | + break; | |
| 433 | + } while (--n > 1); | |
| 434 | + if ((op == CLASS) == (n <= 1)) | |
| 435 | + return(0); | |
| 436 | + if (op == CLASS) | |
| 437 | + p += n - 2; | |
| 438 | + break; | |
| 439 | + | |
| 440 | + case MINUS: | |
| 441 | + e = pmatch(l, p); /* Look for a match */ | |
| 442 | + while (*p++ != ENDPAT); /* Skip over pattern */ | |
| 443 | + if (e) /* Got a match? */ | |
| 444 | + l = e; /* Yes, update string */ | |
| 445 | + break; /* Always succeeds */ | |
| 446 | + | |
| 447 | + case PLUS: /* One or more ... */ | |
| 448 | + if ((l = pmatch(l, p)) == 0) | |
| 449 | + return(0); /* Gotta have a match */ | |
| 450 | + case STAR: /* Zero or more ... */ | |
| 451 | + are = l; /* Remember line start */ | |
| 452 | + while (*l && (e = pmatch(l, p))) | |
| 453 | + l = e; /* Get longest match */ | |
| 454 | + while (*p++ != ENDPAT); /* Skip over pattern */ | |
| 455 | + while (l >= are) { /* Try to match rest */ | |
| 456 | + if (e = pmatch(l, p)) | |
| 457 | + return(e); | |
| 458 | + --l; /* Nope, try earlier */ | |
| 459 | + } | |
| 460 | + return(0); /* Nothing else worked */ | |
| 461 | + | |
| 462 | + default: | |
| 463 | + printf("Bad op code %d\n", op); | |
| 464 | + error("Cannot happen -- match\n"); | |
| 465 | + } | |
| 466 | + } | |
| 467 | + return(l); | |
| 468 | +} | |
| 469 | + | |
| 470 | +/*** Report an error ***********************************/ | |
| 471 | +void error(char *s) | |
| 472 | +{ | |
| 473 | + fprintf(stderr, "%s", s); | |
| 474 | + exit(1); | |
| 475 | +} | |
| 476 | + | |
| 477 | +/*** Main program - parse arguments & grep *************/ | |
| 478 | +int main(int argc, char **argv) | |
| 479 | +{ | |
| 480 | + char *p; | |
| 481 | + int c, i; | |
| 482 | + int gotpattern; | |
| 483 | + | |
| 484 | + FILE *f; | |
| 485 | + | |
| 486 | + if (argc <= 1) | |
| 487 | + usage("No arguments"); | |
| 488 | + if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) { | |
| 489 | + help(documentation); | |
| 490 | + help(patdoc); | |
| 491 | + return 0; | |
| 492 | + } | |
| 493 | + nfile = argc-1; | |
| 494 | + gotpattern = 0; | |
| 495 | + for (i=1; i < argc; ++i) { | |
| 496 | + p = argv[i]; | |
| 497 | + if (*p == '-') { | |
| 498 | + ++p; | |
| 499 | + while (c = *p++) { | |
| 500 | + switch(tolower(c)) { | |
| 501 | + | |
| 502 | + case '?': | |
| 503 | + help(documentation); | |
| 504 | + break; | |
| 505 | + | |
| 506 | + case 'C': | |
| 507 | + case 'c': | |
| 508 | + ++cflag; | |
| 509 | + break; | |
| 510 | + | |
| 511 | + case 'D': | |
| 512 | + case 'd': | |
| 513 | + ++debug; | |
| 514 | + break; | |
| 515 | + | |
| 516 | + case 'F': | |
| 517 | + case 'f': | |
| 518 | + ++fflag; | |
| 519 | + break; | |
| 520 | + | |
| 521 | + case 'n': | |
| 522 | + case 'N': | |
| 523 | + ++nflag; | |
| 524 | + break; | |
| 525 | + | |
| 526 | + case 'v': | |
| 527 | + case 'V': | |
| 528 | + ++vflag; | |
| 529 | + break; | |
| 530 | + | |
| 531 | + default: | |
| 532 | + usage("Unknown flag"); | |
| 533 | + } | |
| 534 | + } | |
| 535 | + argv[i] = 0; | |
| 536 | + --nfile; | |
| 537 | + } else if (!gotpattern) { | |
| 538 | + compile(p); | |
| 539 | + argv[i] = 0; | |
| 540 | + ++gotpattern; | |
| 541 | + --nfile; | |
| 542 | + } | |
| 543 | + } | |
| 544 | + if (!gotpattern) | |
| 545 | + usage("No pattern"); | |
| 546 | + if (nfile == 0) | |
| 547 | + grep(stdin, 0); | |
| 548 | + else { | |
| 549 | + fflag = fflag ^ (nfile > 0); | |
| 550 | + for (i=1; i < argc; ++i) { | |
| 551 | + if (p = argv[i]) { | |
| 552 | + if ((f=fopen(p, "r")) == NULL) | |
| 553 | + cant(p); | |
| 554 | + else { | |
| 555 | + grep(f, p); | |
| 556 | + fclose(f); | |
| 557 | + } | |
| 558 | + } | |
| 559 | + } | |
| 560 | + } | |
| 561 | + return 0; | |
| 562 | +} | |
| 563 | + | |
| 564 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,24 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +void fred(int x) | |
| 4 | +{ | |
| 5 | + switch (x) | |
| 6 | + { | |
| 7 | + case 1: printf("1\n"); return; | |
| 8 | + case 2: printf("2\n"); break; | |
| 9 | + case 3: printf("3\n"); return; | |
| 10 | + } | |
| 11 | + | |
| 12 | + printf("out\n"); | |
| 13 | +} | |
| 14 | + | |
| 15 | +int main() | |
| 16 | +{ | |
| 17 | + fred(1); | |
| 18 | + fred(2); | |
| 19 | + fred(3); | |
| 20 | + | |
| 21 | + return 0; | |
| 22 | +} | |
| 23 | + | |
| 24 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,4 @@ | ||
| 1 | +1 | |
| 2 | +2 | |
| 3 | +out | |
| 4 | +3 | |
| @@ -0,0 +1,26 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int main() | |
| 4 | +{ | |
| 5 | + int a; | |
| 6 | + char b; | |
| 7 | + | |
| 8 | + a = 0; | |
| 9 | + while (a < 2) | |
| 10 | + { | |
| 11 | + printf("%d", a++); | |
| 12 | + break; | |
| 13 | + | |
| 14 | + b = 'A'; | |
| 15 | + while (b < 'C') | |
| 16 | + { | |
| 17 | + printf("%c", b++); | |
| 18 | + } | |
| 19 | + printf("e"); | |
| 20 | + } | |
| 21 | + printf("\n"); | |
| 22 | + | |
| 23 | + return 0; | |
| 24 | +} | |
| 25 | + | |
| 26 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1 @@ | ||
| 1 | +0 | |
| @@ -0,0 +1,23 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +struct point | |
| 4 | +{ | |
| 5 | + double x; | |
| 6 | + double y; | |
| 7 | +}; | |
| 8 | + | |
| 9 | +struct point point_array[100]; | |
| 10 | + | |
| 11 | +int main() | |
| 12 | +{ | |
| 13 | + int my_point = 10; | |
| 14 | + | |
| 15 | + point_array[my_point].x = 12.34; | |
| 16 | + point_array[my_point].y = 56.78; | |
| 17 | + | |
| 18 | + printf("%f, %f\n", point_array[my_point].x, point_array[my_point].y); | |
| 19 | + | |
| 20 | + return 0; | |
| 21 | +} | |
| 22 | + | |
| 23 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1 @@ | ||
| 1 | +12.340000, 56.780000 | |
| @@ -0,0 +1,29 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +int fred() | |
| 4 | +{ | |
| 5 | + printf("fred\n"); | |
| 6 | + return 0; | |
| 7 | +} | |
| 8 | + | |
| 9 | +int joe() | |
| 10 | +{ | |
| 11 | + printf("joe\n"); | |
| 12 | + return 1; | |
| 13 | +} | |
| 14 | + | |
| 15 | +int main() | |
| 16 | +{ | |
| 17 | + printf("%d\n", fred() && joe()); | |
| 18 | + printf("%d\n", fred() || joe()); | |
| 19 | + printf("%d\n", joe() && fred()); | |
| 20 | + printf("%d\n", joe() || fred()); | |
| 21 | + printf("%d\n", fred() && (1 + joe())); | |
| 22 | + printf("%d\n", fred() || (0 + joe())); | |
| 23 | + printf("%d\n", joe() && (0 + fred())); | |
| 24 | + printf("%d\n", joe() || (1 + fred())); | |
| 25 | + | |
| 26 | + return 0; | |
| 27 | +} | |
| 28 | + | |
| 29 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,20 @@ | ||
| 1 | +fred | |
| 2 | +0 | |
| 3 | +fred | |
| 4 | +joe | |
| 5 | +1 | |
| 6 | +joe | |
| 7 | +fred | |
| 8 | +0 | |
| 9 | +joe | |
| 10 | +1 | |
| 11 | +fred | |
| 12 | +0 | |
| 13 | +fred | |
| 14 | +joe | |
| 15 | +1 | |
| 16 | +joe | |
| 17 | +fred | |
| 18 | +0 | |
| 19 | +joe | |
| 20 | +1 | |
| @@ -0,0 +1,30 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +static int fred = 1234; | |
| 4 | +static int joe; | |
| 5 | + | |
| 6 | +void henry() | |
| 7 | +{ | |
| 8 | + static int fred = 4567; | |
| 9 | + | |
| 10 | + printf("%d\n", fred); | |
| 11 | + fred++; | |
| 12 | +} | |
| 13 | + | |
| 14 | +int main() | |
| 15 | +{ | |
| 16 | + printf("%d\n", fred); | |
| 17 | + henry(); | |
| 18 | + henry(); | |
| 19 | + henry(); | |
| 20 | + henry(); | |
| 21 | + printf("%d\n", fred); | |
| 22 | + fred = 8901; | |
| 23 | + joe = 2345; | |
| 24 | + printf("%d\n", fred); | |
| 25 | + printf("%d\n", joe); | |
| 26 | + | |
| 27 | + return 0; | |
| 28 | +} | |
| 29 | + | |
| 30 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,8 @@ | ||
| 1 | +1234 | |
| 2 | +4567 | |
| 3 | +4568 | |
| 4 | +4569 | |
| 5 | +4570 | |
| 6 | +1234 | |
| 7 | +8901 | |
| 8 | +2345 | |
| @@ -0,0 +1,27 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +enum fred { a, b, c }; | |
| 4 | + | |
| 5 | +int main() | |
| 6 | +{ | |
| 7 | + printf("a=%d\n", a); | |
| 8 | + printf("b=%d\n", b); | |
| 9 | + printf("c=%d\n", c); | |
| 10 | + | |
| 11 | + enum fred d; | |
| 12 | + | |
| 13 | + typedef enum { e, f, g } h; | |
| 14 | + typedef enum { i, j, k } m; | |
| 15 | + | |
| 16 | + printf("e=%d\n", e); | |
| 17 | + printf("f=%d\n", f); | |
| 18 | + printf("g=%d\n", g); | |
| 19 | + | |
| 20 | + printf("i=%d\n", i); | |
| 21 | + printf("j=%d\n", j); | |
| 22 | + printf("k=%d\n", k); | |
| 23 | + | |
| 24 | + return 0; | |
| 25 | +} | |
| 26 | + | |
| 27 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,9 @@ | ||
| 1 | +a=0 | |
| 2 | +b=1 | |
| 3 | +c=2 | |
| 4 | +e=0 | |
| 5 | +f=1 | |
| 6 | +g=2 | |
| 7 | +i=0 | |
| 8 | +j=1 | |
| 9 | +k=2 | |
| @@ -0,0 +1,56 @@ | ||
| 1 | +#include <stdio.h> | |
| 2 | + | |
| 3 | +void fred() | |
| 4 | +{ | |
| 5 | + printf("In fred()\n"); | |
| 6 | + goto done; | |
| 7 | + printf("In middle\n"); | |
| 8 | +done: | |
| 9 | + printf("At end\n"); | |
| 10 | +} | |
| 11 | + | |
| 12 | +void joe() | |
| 13 | +{ | |
| 14 | + int b = 5678; | |
| 15 | + | |
| 16 | + printf("In joe()\n"); | |
| 17 | + | |
| 18 | + { | |
| 19 | + int c = 1234; | |
| 20 | + printf("c = %d\n", c); | |
| 21 | + goto outer; | |
| 22 | + printf("uh-oh\n"); | |
| 23 | + } | |
| 24 | + | |
| 25 | +outer: | |
| 26 | + | |
| 27 | + printf("done\n"); | |
| 28 | +} | |
| 29 | + | |
| 30 | +void henry() | |
| 31 | +{ | |
| 32 | + int a; | |
| 33 | + | |
| 34 | + printf("In henry()\n"); | |
| 35 | + goto inner; | |
| 36 | + | |
| 37 | + { | |
| 38 | + int b; | |
| 39 | +inner: | |
| 40 | + b = 1234; | |
| 41 | + printf("b = %d\n", b); | |
| 42 | + } | |
| 43 | + | |
| 44 | + printf("done\n"); | |
| 45 | +} | |
| 46 | + | |
| 47 | +int main() | |
| 48 | +{ | |
| 49 | + fred(); | |
| 50 | + joe(); | |
| 51 | + henry(); | |
| 52 | + | |
| 53 | + return 0; | |
| 54 | +} | |
| 55 | + | |
| 56 | +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ | |
| @@ -0,0 +1,8 @@ | ||
| 1 | +In fred() | |
| 2 | +At end | |
| 3 | +In joe() | |
| 4 | +c = 1234 | |
| 5 | +done | |
| 6 | +In henry() | |
| 7 | +b = 1234 | |
| 8 | +done | |
| @@ -0,0 +1,37 @@ | ||
| 1 | +The tests in this directory are either directly copied from the picoc project or | |
| 2 | +are subsequently modified and added to for the purpose of TinyCC project. All | |
| 3 | +these modifications are licensed under the same terms as TinyCC as specified in | |
| 4 | +the file COPYING. | |
| 5 | + | |
| 6 | +=== picoc license === | |
| 7 | + | |
| 8 | +Copyright (c) 2009-2011, Zik Saleeba | |
| 9 | +All rights reserved. | |
| 10 | + | |
| 11 | +Redistribution and use in source and binary forms, with or without | |
| 12 | +modification, are permitted provided that the following conditions are | |
| 13 | +met: | |
| 14 | + | |
| 15 | + * Redistributions of source code must retain the above copyright | |
| 16 | + notice, this list of conditions and the following disclaimer. | |
| 17 | + | |
| 18 | + * Redistributions in binary form must reproduce the above copyright | |
| 19 | + notice, this list of conditions and the following disclaimer in | |
| 20 | + the documentation and/or other materials provided with the | |
| 21 | + distribution. | |
| 22 | + | |
| 23 | + * Neither the name of the Zik Saleeba nor the names of its | |
| 24 | + contributors may be used to endorse or promote products derived | |
| 25 | + from this software without specific prior written permission. | |
| 26 | + | |
| 27 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 28 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 29 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 30 | +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 31 | +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 32 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 33 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 34 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 35 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 36 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 37 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| @@ -0,0 +1,90 @@ | ||
| 1 | +TOP = .. | |
| 2 | +include $(TOP)/Makefile | |
| 3 | + | |
| 4 | +ifeq ($(TARGETOS),Darwin) | |
| 5 | + CFLAGS+=-Wl,-flat_namespace,-undefined,warning | |
| 6 | + TCCFLAGS=-D_ANSI_SOURCE | |
| 7 | + export MACOSX_DEPLOYMENT_TARGET:=10.2 | |
| 8 | +endif | |
| 9 | + | |
| 10 | +ifdef CONFIG_WIN32 | |
| 11 | + TCCFLAGS=-I $(TOP)/win32/include -L$(TOP) | |
| 12 | +endif | |
| 13 | + | |
| 14 | +TESTS= 00_assignment.test \ | |
| 15 | + 01_comment.test \ | |
| 16 | + 02_printf.test \ | |
| 17 | + 03_struct.test \ | |
| 18 | + 04_for.test \ | |
| 19 | + 05_array.test \ | |
| 20 | + 06_case.test \ | |
| 21 | + 07_function.test \ | |
| 22 | + 08_while.test \ | |
| 23 | + 09_do_while.test \ | |
| 24 | + 10_pointer.test \ | |
| 25 | + 11_precedence.test \ | |
| 26 | + 12_hashdefine.test \ | |
| 27 | + 13_integer_literals.test \ | |
| 28 | + 14_if.test \ | |
| 29 | + 15_recursion.test \ | |
| 30 | + 16_nesting.test \ | |
| 31 | + 17_enum.test \ | |
| 32 | + 18_include.test \ | |
| 33 | + 19_pointer_arithmetic.test \ | |
| 34 | + 20_pointer_comparison.test \ | |
| 35 | + 21_char_array.test \ | |
| 36 | + 22_floating_point.test \ | |
| 37 | + 23_type_coercion.test \ | |
| 38 | + 24_math_library.test \ | |
| 39 | + 25_quicksort.test \ | |
| 40 | + 26_character_constants.test \ | |
| 41 | + 27_sizeof.test \ | |
| 42 | + 28_strings.test \ | |
| 43 | + 29_array_address.test \ | |
| 44 | + 31_args.test \ | |
| 45 | + 32_led.test \ | |
| 46 | + 33_ternary_op.test \ | |
| 47 | + 35_sizeof.test \ | |
| 48 | + 36_array_initialisers.test \ | |
| 49 | + 37_sprintf.test \ | |
| 50 | + 38_multiple_array_index.test \ | |
| 51 | + 39_typedef.test \ | |
| 52 | + 40_stdio.test \ | |
| 53 | + 41_hashif.test \ | |
| 54 | + 42_function_pointer.test \ | |
| 55 | + 43_void_param.test \ | |
| 56 | + 44_scoped_declarations.test \ | |
| 57 | + 45_empty_for.test \ | |
| 58 | + 47_switch_return.test \ | |
| 59 | + 48_nested_break.test \ | |
| 60 | + 49_bracket_evaluation.test \ | |
| 61 | + 50_logical_second_arg.test \ | |
| 62 | + 51_static.test \ | |
| 63 | + 52_unnamed_enum.test \ | |
| 64 | + 54_goto.test | |
| 65 | + | |
| 66 | + # 30_hanoi.test \ # seg fault in the code, gcc as well | |
| 67 | + # 34_array_assignment.test \ # array assignment is not in C standard | |
| 68 | + # 46_grep.test \ # does not compile even with gcc | |
| 69 | + | |
| 70 | +%.test: %.expect %.c | |
| 71 | + @echo Test: $*... | |
| 72 | + -@if [ "x`echo $* | grep args`" != "x" ]; \ | |
| 73 | + then \ | |
| 74 | + ../tcc -B.. $(TCCFLAGS) -run $*.c - arg1 arg2 arg3 arg4 2>&1 >$*.output; \ | |
| 75 | + else \ | |
| 76 | + ../tcc -B.. $(TCCFLAGS) -run $*.c 2>&1 >$*.output; \ | |
| 77 | + fi | |
| 78 | + @if diff -bu $*.expect $*.output ; \ | |
| 79 | + then \ | |
| 80 | + rm -f $*.output \ | |
| 81 | + : \ | |
| 82 | + else \ | |
| 83 | + echo "ERROR: test $*"; \ | |
| 84 | + fi | |
| 85 | + | |
| 86 | +all: test | |
| 87 | + | |
| 88 | +test: $(TESTS) | |
| 89 | + | |
| 90 | +# vim: set expandtab ts=4 sw=4 sts=4 tw=80 : | |