diff --git a/rootfs.manifest b/rootfs.manifest index f3d8d2f..ab93532 100644 --- a/rootfs.manifest +++ b/rootfs.manifest @@ -731,6 +731,22 @@ file /share/smallc/test2.c file /share/smallc/test3.c file /share/smallc/webserver.c +# +# Files: /share/smallerc +# +dir /share/smallerc +file /share/smallerc/adc.c +file /share/smallerc/gpio.c +file /share/smallerc/hello.c +file /share/smallerc/Makefile +file /share/smallerc/primelist.c +file /share/smallerc/primesum.c +file /share/smallerc/q8.c +file /share/smallerc/rain.c +file /share/smallerc/test1.c +file /share/smallerc/test2.c +file /share/smallerc/test3.c + # # Files: /var # diff --git a/share/smallerc/Makefile b/share/smallerc/Makefile new file mode 100644 index 0000000..977a786 --- /dev/null +++ b/share/smallerc/Makefile @@ -0,0 +1,38 @@ +CC = cc +PROG = hello primelist primesum test1 test2 test3 gpio adc rain \ + q8 + +all: $(PROG) + +hello: hello.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) hello.c + +primelist: primelist.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) primelist.c + +primesum: primesum.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) primesum.c + +gpio: gpio.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) gpio.c + +adc: adc.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) adc.c + +rain: rain.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) rain.c + +q8: q8.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) q8.c + +test1: test1.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) test1.c + +test2: test2.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) test2.c + +test3: test3.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) test3.c + +clean: + rm -f *.o $(PROG) diff --git a/share/smallerc/adc.c b/share/smallerc/adc.c new file mode 100644 index 0000000..af0efe3 --- /dev/null +++ b/share/smallerc/adc.c @@ -0,0 +1,29 @@ +/* + * Example of reading ADC data. + */ +#include +#include +#include +#include + +char buf[100]; + +int main(void) +{ + int i, fd, value; + + for (i=0; i<16; i++) { + sprintf(buf, "/dev/adc%d", i); + fd = open(buf, O_RDWR); + if (fd < 0) { + printf("Error: unable to open %s\n", buf); + } else { + if (read(fd, buf, 20) > 0) { + value = strtol (buf, 0, 0); + printf("adc%-2d = %d\n", i, value); + } + close(fd); + } + } + return 0; +} diff --git a/share/smallerc/gpio.c b/share/smallerc/gpio.c new file mode 100644 index 0000000..f6b6ba2 --- /dev/null +++ b/share/smallerc/gpio.c @@ -0,0 +1,26 @@ +/* + * Example of polling general purpose i/o pins. + */ +#include +#include +#include +#include + +int main(void) +{ + int fd, pnum, value; + + fd = open ("/dev/porta", O_RDWR); + if (fd < 0) { + perror ("/dev/porta"); + return -1; + } + + for (pnum=0; pnum<7; pnum++) { + value = ioctl (fd, GPIO_POLL | GPIO_PORT (pnum), 0); + if (value < 0) + perror ("GPIO_POLL"); + printf ("port%c = 0x%04x\n", pnum + 'A', value); + } + return 0; +} diff --git a/share/smallerc/hello.c b/share/smallerc/hello.c new file mode 100644 index 0000000..c72c91b --- /dev/null +++ b/share/smallerc/hello.c @@ -0,0 +1,6 @@ +#include + +int main(void) +{ + printf("Hello, Smaller C World!\n"); +} diff --git a/share/smallerc/primelist.c b/share/smallerc/primelist.c new file mode 100644 index 0000000..036b02f --- /dev/null +++ b/share/smallerc/primelist.c @@ -0,0 +1,35 @@ +/* + * Print the list of prime numbers up to 100. + */ +#include + +int isprime(int); + +int main(void) +{ + int n; + + for (n=2; n<100; ++n) { + if (isprime(n)) { + printf("%d ", n); + } + } + printf("\n"); +} + +int isprime(int n) +{ + int j; + + if (n == 2) + return 1; + + if (n % 2 == 0) + return 0; + + for (j=3; j*j<=n; j+=2) + if (n % j == 0) + return 0; + return 1; +} + diff --git a/share/smallerc/primesum.c b/share/smallerc/primesum.c new file mode 100644 index 0000000..7f37a59 --- /dev/null +++ b/share/smallerc/primesum.c @@ -0,0 +1,35 @@ +/* + * Compute the sum of prime numbers up to 10000. + */ +#include + +int isprime(int); + +int main(void) +{ + int sum, n; + + sum = 0; + for (n=2; n<10000; ++n) { + if (isprime(n)) { + sum += n; + } + } + printf("Sum of primes less than 10000: %d\n", sum); +} + +int isprime(int n) +{ + int j; + + if (n == 2) + return 1; + + if (n % 2 == 0) + return 0; + + for (j=3; j*j<=n; j+=2) + if (n % j == 0) + return 0; + return 1; +} diff --git a/share/smallerc/q8.c b/share/smallerc/q8.c new file mode 100644 index 0000000..9cc3b12 --- /dev/null +++ b/share/smallerc/q8.c @@ -0,0 +1,209 @@ +/* + * Eight Queens puzzle + * + * (C) 2010 by Mark Sproul + * Open source as per standard Arduino code + * Modified by Pito 12/2012 for SmallC and then by Alexey Frunze for Smaller C + */ +#include +#define TRUE 1 +#define FALSE 0 + +unsigned int gChessBoard[8]; +unsigned int gLoopCounter; +int gValidCount; + +int CheckCurrentBoard(void) +{ + int ii; + int jj; + int theRow; + int theLongRow; + int theLongColumns; + int bitCount; + + //* we know we have 1 in each row, + //* Check for 1 in each column + theRow = 0; + for (ii=0; ii<8; ii++) { + theRow |= gChessBoard[ii]; + } + if (theRow != 0x0ff) { + return FALSE; + } + + //* we have 1 in each column, now check the diagonals + theLongColumns = 0; + for (ii=0; ii<8; ii++) { + theLongRow = gChessBoard[ii] & 0x0ff; + theLongRow = theLongRow << ii; + + theLongColumns |= theLongRow; + } + + //* now count the bits + bitCount = 0; + for (ii=0; ii<16; ii++) { + if ((theLongColumns & 0x01) == 0x01) { + bitCount++; + } + theLongColumns = theLongColumns >> 1; + } + + if (bitCount != 8) { + return FALSE; + } + + //* we now have to check the other diagonal + theLongColumns = 0; + for (ii=0; ii<8; ii++) { + theLongRow = gChessBoard[ii] & 0x0ff; + theLongRow = theLongRow << 8; + theLongRow = theLongRow >> ii; + + theLongColumns |= theLongRow; + } + + //* now count the bits + bitCount = 0; + for (ii=0; ii<16; ii++) { + if ((theLongColumns & 0x01) == 0x01) { + bitCount++; + } + theLongColumns = theLongColumns >> 1; + } + + if (bitCount != 8) { + return FALSE; + } + return TRUE; +} + +int CheckForDone(void) +{ + int ii; + int weAreDone; + int theRow; + + weAreDone = FALSE; + + //* we know we have 1 in each row, + //* Check for 1 in each column + theRow = 0; + for (ii=0; ii<8; ii++) { + theRow |= gChessBoard[ii]; + } + + if (theRow == 0x01) { + weAreDone = TRUE; + } + return weAreDone; +} + +void RotateQueens(void) +{ + int ii; + int keepGoing; + int theRow; + + ii = 0; + keepGoing = TRUE; + while (keepGoing && (ii < 8)) { + theRow = gChessBoard[ii] & 0x0ff; + theRow = (theRow >> 1) & 0x0ff; + if (theRow != 0) { + gChessBoard[ii] = theRow; + keepGoing = FALSE; + } else { + gChessBoard[ii] = 0x080; + } + ii++; + } +} + +void PrintChessBoard(void) +{ + int ii; + int jj; + int theRow; + char textString[32]; + + printf("\nLoop= %d\n", gLoopCounter); + printf("Solution count= %d\n", gValidCount); + + printf("+----------------+\n"); + for (ii=0; ii<8; ii++) { + theRow = gChessBoard[ii]; + + printf("|"); + for (jj=0; jj<8; jj++) { + if (theRow & 0x080) { + printf("Q "); + } else { + printf(". "); + } + theRow = theRow << 1; + } + printf("|\n"); + } + printf("+----------------+\n"); +} + +int main(void) +{ + int ii; + + printf("\nEight Queens brute force"); + printf("\n************************\n"); + //* put the 8 queens on the board, 1 in each row + for (ii=0; ii<8; ii++) { + gChessBoard[ii] = 0x080; + } + PrintChessBoard(); + + gLoopCounter = 0; + gValidCount = 0; + + + while (1) { + gLoopCounter++; + + if (CheckCurrentBoard()) { + gValidCount++; + PrintChessBoard(); + } else if ((gLoopCounter % 1000) == 0) { + //PrintChessBoard(); + } + + RotateQueens(); + if (CheckForDone()) { + //int elapsedSeconds; + //int elapsedMinutes; + //int elapsedHours; + + //elapsedSeconds = millis() / 1000; + //elapsedMinutes = elapsedSeconds / 60; + //elapsedHours = elapsedMinutes / 60; + + printf("----------------------------------\n"); + printf("All done\n"); + + PrintChessBoard(); + printf("----------------------------------\n"); + + //Serial.print("total seconds="); + //Serial.println(elapsedSeconds); + + //Serial.print("hours="); + //Serial.println(elapsedHours); + + //Serial.print("minutes="); + //Serial.println(elapsedMinutes % 60); + + //Serial.print("seconds="); + //Serial.println(elapsedSeconds % 60); + + return (1); + } + } +} diff --git a/share/smallerc/rain.c b/share/smallerc/rain.c new file mode 100644 index 0000000..fc385ed --- /dev/null +++ b/share/smallerc/rain.c @@ -0,0 +1,129 @@ +/* + * Example of using termcap library for SmallC. + * 11/3/1980 EPS/CITHEP + * + * Copyright (c) 1980 Regents of the University of California. + * All rights reserved. The Berkeley software License Agreement + * specifies the terms and conditions for redistribution. + */ +#include +#include +#include +#include + +#define CO 80 /* number of columns */ +#define LI 24 /* number of lines */ + +#define CL "\33[H\33[J" /* clear the screen */ +#define CM "\33[%u;%uH" /* move the cursor to row, column */ +#define BC "\b" /* move cursor left */ +#define DN "\33[B" /* move cursor down */ +#define ND " " /* move cursor right */ + +int xpos[5], ypos[5]; +char outbuf[BUFSIZ]; + +void moveto(int col, int row) +{ + printf(CM, row, col); +} + +void onsig(int n) +{ + moveto(0, LI - 1); + fflush(stdout); + _exit(0); +} + +int main(void) +{ + int x, y, j; + + setbuf(stdout, outbuf); + for (j = SIGHUP; j <= SIGTERM; j++) + if (signal(j, SIG_IGN) != SIG_IGN) + signal(j, onsig); + + fputs(CL, stdout); + fflush(stdout); + for (j = 5; --j >= 0; ) { + xpos[j] = 2 + rand() % (CO - 4); + ypos[j] = 2 + rand() % (LI - 4); + } + for (j = 0; ; ) { + x = 2 + rand() % (CO - 4); + y = 2 + rand() % (LI - 4); + moveto(x, y); + putchar('.'); + moveto(xpos[j], ypos[j]); + putchar('o'); + if (j == 0) + j = 4; + else + --j; + moveto(xpos[j], ypos[j]); + putchar('O'); + if (j == 0) + j = 4; + else + --j; + moveto(xpos[j], ypos[j]-1); + putchar('-'); + fputs(DN, stdout); + fputs(BC, stdout); + fputs(BC, stdout); + fputs("|.|", stdout); + fputs(DN, stdout); + fputs(BC, stdout); + fputs(BC, stdout); + putchar('-'); + if (j == 0) + j = 4; + else + --j; + moveto(xpos[j], ypos[j]-2); + putchar('-'); + fputs(DN, stdout); + fputs(BC, stdout); + fputs(BC, stdout); + fputs("/ \\", stdout); + moveto(xpos[j]-2, ypos[j]); + fputs("| O |", stdout); + moveto(xpos[j]-1, ypos[j]+1); + fputs("\\ /", stdout); + fputs(DN, stdout); + fputs(BC, stdout); + fputs(BC, stdout); + putchar('-'); + if (j == 0) + j = 4; + else + --j; + moveto(xpos[j], ypos[j]-2); + putchar(' '); + fputs(DN, stdout); + fputs(BC, stdout); + fputs(BC, stdout); + putchar(' '); + fputs(ND, stdout); + putchar(' '); + moveto(xpos[j]-2, ypos[j]); + putchar(' '); + fputs(ND, stdout); + putchar(' '); + fputs(ND, stdout); + putchar(' '); + moveto(xpos[j]-1, ypos[j]+1); + putchar(' '); + fputs(ND, stdout); + putchar(' '); + fputs(DN, stdout); + fputs(BC, stdout); + fputs(BC, stdout); + putchar(' '); + xpos[j] = x; + ypos[j] = y; + fflush(stdout); + usleep(100000); + } +} diff --git a/share/smallerc/test1.c b/share/smallerc/test1.c new file mode 100644 index 0000000..10e3843 --- /dev/null +++ b/share/smallerc/test1.c @@ -0,0 +1,214 @@ +#include + +int ga[5]; + +int main(void) +{ + int a, b, c, d; + int arr[5]; + int *pi; + char arrc[5]; + char *pic; + int s1, s2; + int z; + int t; + int *pip; + int *picp; + int e1, e2; + + ga[0] = 10; + ga[1] = 20; + ga[2] = 30; + ga[3] = 40; + ga[4] = 50; + + a = 21; + b = 31; + c = 71; + d = 82; + + arr[0] = 10; + arr[1] = 20; + arr[2] = 30; + arr[3] = 40; + arr[4] = 50; + pi = &arr[0]; + + arrc[0] = 13; + arrc[1] = 23; + arrc[2] = 33; + arrc[3] = 43; + arrc[4] = 53; + pic = &arrc[0]; + + printf(" 21 + 31 = %d (52)\n", a + b); + printf(" 21 - 31 = %d (-10)\n", a - b); + printf(" 21 & 71 = %d (5)\n", a & c); + printf(" 21 | 82 = %d (87)\n", a | d); + printf(" 21 ^ 82 = %d (71)\n", a ^ d); + printf(" 21 * 82 = %d (1722)\n", a * d); + printf(" 82 %% 21 = %d (19)\n", d % a); + printf(" 82 / 21 = %d (3)\n", d / a); + printf(" *pi = %d (10)\n", *pi); + printf(" *pi + 1 = %d (11)\n", *pi + 1); + printf(" *(pi + 1) = %d (20)\n", *(pi + 1)); + printf("&arr[3] - &arr[0] = %d (3)\n", &arr[3] - &arr[0]); + printf(" arr[3]-arr[0] = %d (30)\n", arr[3] - arr[0]); + printf(" arr[3]+arr[0] = %d (50)\n", arr[3] + arr[0]); + printf(" &ga[3] - &ga[0] = %d (3)\n", &ga[3] - &ga[0]); + printf(" ga[3]-ga[0] = %d (30)\n", ga[3] - ga[0]); + printf(" ga[3]+ga[0] = %d (50)\n", ga[3] + ga[0]); + printf("\n"); + + printf(" *pic = %d (13)\n", *pic); + printf(" *pic + 1 = %d (14)\n", *pic+1); + printf(" *(pic + 1) = %d (23)\n", *(pic+1)); + printf("&arrc[3] - &arrc[0] = %d (3)\n", &arrc[3]-&arrc[0]); + printf("\n"); + + s1 = 3; + s2 = -200; + printf(" 82 << 3 = %d (656)\n", d << s1); + printf(" 82 >> 3 = %d (10)\n", d >> s1); + printf("-200 >> 3 = %d (-25)\n", s2 >> s1); + printf("-200 << 3 = %d (-1600)\n", s2 << s1); + printf("\n"); + + printf("-s1 = %d (-3)\n", -s1); + printf("-s2 = %d (200)\n", -s2); + printf("\n"); + + printf("~82 = %d (-83)\n", ~d); + printf("\n"); + + z = 0; + printf("!82 = %d (0)\n", !d); + printf(" !0 = %d (1)\n", !z); + printf("\n"); + + printf(" 0 && 0 = %d (0)\n", z && z); + printf(" 0 && 21 = %d (0)\n", z && a); + printf(" 3 && 21 = %d (1)\n", s1 && a); + printf("21 && 3 = %d (1)\n", a && s1); + printf("\n"); + + printf(" 0 || 0 = %d (0)\n", z || z); + printf(" 0 || 21 = %d (1)\n", z || a); + printf(" 3 || 21 = %d (1)\n", s1 || a); + printf("21 || 3 = %d (1)\n", a || s1); + printf("\n"); + + pi = 4; + printf("pi++ = %d (4)\n", pi++); + printf(" pi = %d (8)\n", pi); + printf("++pi = %d (12)\n", ++pi); + printf("pi-- = %d (12)\n", pi--); + printf(" pi = %d (8)\n", pi); // TBD!!! can't be -8; bug in tokPostSub: operands reversed + printf("--pi = %d (4)\n", --pi); // TBD!!! can't be -12 + printf("\n"); + + pic = 4; + printf("pic++ = %d (4)\n", pic++); + printf(" pic = %d (5)\n", pic); + printf("++pic = %d (6)\n", ++pic); + printf("pic-- = %d (6)\n", pic--); + printf(" pic = %d (5)\n", pic); + printf("--pic = %d (4)\n", --pic); + printf("\n"); + + t = 4; + printf("t++ = %d (4)\n", t++); + printf(" t = %d (5)\n", t); + printf("++t = %d (6)\n", ++t); + printf("t-- = %d (6)\n", t--); + printf(" t = %d (5)\n", t); + printf("--t = %d (4)\n", --t); + printf("\n"); + + t = 4; + printf(" t==4 = %d (1)\n", t == 4); + printf(" t==3 = %d (0)\n", t == 3); + printf(" t==5 = %d (0)\n", t == 5); + t = -4; + printf("t==-4 = %d (1)\n", t == -4); + printf("t==-3 = %d (0)\n", t == -3); + printf("t==-5 = %d (0)\n", t == -5); + printf(" t==4 = %d (0)\n", t == 4); + printf(" t==3 = %d (0)\n", t == 3); + printf(" t==5 = %d (0)\n", t == 5); + printf("\n"); + + t = 4; + printf(" t!=4 = %d (0)\n", t != 4); + printf(" t!=3 = %d (1)\n", t != 3); + printf(" t!=5 = %d (1)\n", t != 5); + t = -4; + printf("t!=-4 = %d (0)\n", t != -4); + printf("t!=-3 = %d (1)\n", t != -3); + printf("t!=-5 = %d (1)\n", t != -5); + printf(" t!=4 = %d (1)\n", t != 4); + printf(" t!=3 = %d (1)\n", t != 3); + printf(" t!=5 = %d (1)\n", t != 5); + printf("\n"); + + t = 4; + printf(" t<4 = %d (0)\n", t < 4); + printf(" t<3 = %d (0)\n", t < 3); + printf(" t<5 = %d (1)\n", t < 5); + printf("t<-1 = %d (0)\n", t < -1); + printf("\n"); + + printf(" t<=4 = %d (1)\n", t <= 4); + printf(" t<=3 = %d (0)\n", t <= 3); + printf(" t<=5 = %d (1)\n", t <= 5); + printf("t<=-1 = %d (0)\n", t <= -1); + printf("\n"); + + t = 4; + printf(" t>4 = %d (0)\n", t > 4); + printf(" t>3 = %d (1)\n", t > 3); + printf(" t>5 = %d (0)\n", t > 5); + printf("t>-1 = %d (1)\n", t > -1); + printf("\n"); + + printf(" t>=4 = %d (1)\n", t >= 4); + printf(" t>=3 = %d (1)\n", t >= 3); + printf(" t>=5 = %d (0)\n", t >= 5); + printf("t>=-1 = %d (1)\n", t >= -1); + printf("\n"); + + pi = -100; + printf(" pi<4 = %d (0)\n", pi < (int*)4); + printf(" pi<3 = %d (0)\n", pi < (int*)3); + printf("pi<-100 = %d (0)\n", pi < (int*)-100); + printf(" pi<-1 = %d (1)\n", pi < (int*)-1); + printf("\n"); + + printf(" pi<=4 = %d (0)\n", pi <= (int*)4); + printf(" pi<=3 = %d (0)\n", pi <= (int*)3); + printf("pi<=-100 = %d (1)\n", pi <= (int*)-100); + printf(" pi<=-1 = %d (1)\n", pi <= (int*)-1); + printf("\n"); + + pi = -100; + printf(" pi>4 = %d (1)\n", pi > (int*)4); + printf(" pi>3 = %d (1)\n", pi > (int*)3); + printf("pi>-100 = %d (0)\n", pi > (int*)-100); + printf(" pi>-1 = %d (0)\n", pi > (int*)-1); + printf("\n"); + + printf(" pi>=4 = %d (1)\n", pi >= (int*)4); + printf(" pi>=3 = %d (1)\n", pi >= (int*)3); + printf("pi>=-100 = %d (1)\n", pi >= (int*)-100); + printf(" pi>=-1 = %d (0)\n", pi >= (int*)-1); + printf("\n"); + + pi = &arr[0]; + pip = &arr[3]; + printf(" *pip - *pi: %d (30)\n", *pip - *pi); + printf(" pip - pi: %d (3)\n", pip - pi); + printf(" *pip: %d (40)\n", *pip); + printf(" *(pip - 3): %d (10)\n", *(pip - 3)); + printf(" *&arr[3]: %d (40)\n", *&arr[3]); + printf("*(&arr[3] - 3): %d (10)\n", *(&arr[3]-3)); +} diff --git a/share/smallerc/test2.c b/share/smallerc/test2.c new file mode 100644 index 0000000..e15b3f9 --- /dev/null +++ b/share/smallerc/test2.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include + +int aaa; +int bbb; +int ccc; +char gc; +char gbuffer[3]; +int gibuffer[4]; + +int main(void) +{ + char b; + int la; + unsigned int u1, u2; + int s1, s2; + unsigned char uc1, uc2; + char sc1, sc2; + int fd; + char buffer[6]; + int ibuffer[7]; + + printf(" sizeof(uc1): %d 1\n", sizeof(uc1)); + printf(" sizeof(sc1): %d 1\n", sizeof(sc1)); + printf(" sizeof(u1): %d 4\n", sizeof(u1)); + printf(" sizeof(s1): %d 4\n", sizeof(s1)); + printf(" sizeof(aaa): %d 4\n", sizeof(aaa)); + printf(" sizeof(bbb): %d 4\n", sizeof(bbb)); + printf(" sizeof(gc): %d 1\n", sizeof(gc)); + printf(" sizeof(buffer): %d 6\n", sizeof(buffer)); + printf(" sizeof(ibuffer): %d 28\n", sizeof(ibuffer)); + printf(" sizeof(char): %d 1\n", sizeof(char)); + printf(" sizeof(int): %d 4\n", sizeof(int)); + printf(" sizeof(gbuffer): %d 3\n", sizeof(gbuffer)); + printf(" sizeof(gibuffer): %d 16\n", sizeof(gibuffer)); + // sizeof(ibuffer[0]) is not supported, so the following can be used... + printf("sizeof(ibuffer)/sizeof(int): %d 7\n", sizeof(ibuffer)/sizeof(int)); + + aaa = 1; + bbb = 2; + la = 4; + printf("%d 1\n", aaa); + printf("%d 2\n", bbb); + printf("%d 4\n", la); + + uc1 = 0x80; + sc1 = 0x80; + s1 = uc1; + s2 = sc1; + printf("unsigned char (0x80) -> int: %d 128\n", s1); + printf(" signed char (0x80) -> int: %d -128\n", s2); + + u1 = uc1; + u2 = sc1; + printf("unsigned char (0x80) -> unsigned: %d 128\n", u1); + printf(" signed char (0x80) -> unsigned: %d -128\n", u2); + + la = errno; + printf("errno: %d 0\n", la); + + write(1, "abcd ", 5); + la = errno; + printf("errno after good write call: %d 0\n", la); + + write(10, "abcde", 5); + la = errno; + printf("errno after bad write call: %d 9\n", la); + + write(1, "abcd ", 5); + la = errno; + printf("good write after failed should not overwrite errno: %d 9\n", la); + + errno = 0; + write(1, "abcd ", 5); + la = errno; + printf("good write after errno set to zero: %d 0\n", la); + + la = write(1, "abcd ", 5); + printf("write() return: %d 5\n", la); + + la = write(10, "abcd ", 5); + printf("write(bad fd) return: %d -1\n", la); + + fd = open("/a.txt", O_WRONLY | O_CREAT, 0666); + if (fd != -1) { + printf("open success\n"); + la = write(fd, "abcd\n", 5); + if (la == 5) printf("write success\n"); else printf("write failed\n"); + la = close(fd); + if (la != -1) printf("close success\n"); else printf("close failed\n"); + } else { + printf("open failed\n"); + } + + buffer[0] = 0; + buffer[1] = 0; + buffer[2] = 0; + buffer[3] = 0; + buffer[4] = 0; + buffer[5] = 0; + + fd = open("/a.txt", O_RDONLY, 0666); + if (fd != -1) { + printf("open success\n"); + la = read(fd, buffer, 5); + printf(buffer); + if (la == 5) printf("read success\n"); else printf("read failed\n"); + la = close(fd); + if (la != -1) printf("close success\n"); else printf("close failed\n"); + } else { + printf("open failed\n"); + } + + if (buffer[0] != 'a') printf("data0 readback from file MISMATCH\n"); + if (buffer[1] != 'b') printf("data1 readback from file MISMATCH\n"); + if (buffer[2] != 'c') printf("data2 readback from file MISMATCH\n"); + if (buffer[3] != 'd') printf("data3 readback from file MISMATCH\n"); + if (buffer[4] != '\n') printf("data4 readback from file MISMATCH\n"); + + if (buffer[0] != 'a' || buffer[1] != 'b' || buffer[2] != 'c' || + buffer[3] != 'd' || buffer[4] != '\n') { + printf("data readback from file MISMATCH\n"); + } else { + printf("data readback from file OK\n"); + } +} diff --git a/share/smallerc/test3.c b/share/smallerc/test3.c new file mode 100644 index 0000000..0155783 --- /dev/null +++ b/share/smallerc/test3.c @@ -0,0 +1,51 @@ +#include + +void printt(int t, char* str); + +int main(void) +{ + int t; + + t = 0; + if (t) printt(t, "failure"); else printt(t, "success"); + t = 1; + if (t) printt(t, "success"); else printt(t, "failure"); + t = 8; + if (t) printt(t, "success"); else printt(t, "failure"); + t = -2; + if (t) printt(t, "success"); else printt(t, "failure"); + printf("\n"); + + t = 4; + printf("switch test: "); + switch (t) { + case 3: + printf("failure"); + break; + case 4: + printf("success"); + break; + case 5: + printf("failure"); + break; + } + printf("\n"); + + printf("switch fallthrough test: "); + switch (t) { + case 3: + printf("failure"); + break; + case 4: + printf("OKSOFAR: "); + case 5: + printf("success if oksofar printed before this in caps"); + break; + } + printf("\n"); +} + +void printt(int t, char* str) +{ + printf("bool test on value %d %s\n", t, str); +} diff --git a/src/cmd/smlrc/cgmips.c b/src/cmd/smlrc/cgmips.c index 300c8f3..f4cda05 100644 --- a/src/cmd/smlrc/cgmips.c +++ b/src/cmd/smlrc/cgmips.c @@ -1265,8 +1265,8 @@ void GenExpr0(void) GenReadIndirect(MipsOpRegV0, reg, v); GenPrintInstr3Operands(instr, 0, MipsOpRegT1, 0, - MipsOpRegT1, 0, - MipsOpRegV0, 0); + MipsOpRegV0, 0, + MipsOpRegT1, 0); GenWriteIndirect(reg, MipsOpRegT1, v); } break;