/* This program illustrates the special formatting options for iosb's write function. They are similar to C's printf's options, but not exactly the same. The program's output appears after the program so you can be sure exactly what each option does without having to download and run it yourself. The formats are as follows. N represents any positive decimal integer that does not have any leading zeros, and 0 is a zero not a capital O. %c (that is a lower case c) Print a character accoring to its ASCII code %C (capital C) Prints characters in an unambiguous and always visible way. If the value is a normal visible ASCII character, it is printed mormally. If it is a space, then \s is printed instead. If it is a \, then \\ is printed instead. If it is a newline, then \n is printed instead. If it is a return (ASCII 13), then \r is printed instead. If it is a tab, then \t is printed instead. Anything else is printed as \AB where A and B are the two hexadecimal digits of the character code provided. %d Print an integer in decimal using exactly the number of characters it needs, no more, no less. %Nd Print as usual, but if the result does not take up N characters add spaces afterwards until it does. %-Nd The same as %Nd except the extra spaces are to the left before the number itself. %0Nd The same as %-Nd except that zeros are added to the left instead of spaces %x The same as %d except for two things: The number is printed in hexadecinmal with the letters a-f for digits 10-15, The number is always interpreted as positive, -2 comes out as fffffffe. %Nx %-Nx %0Nx The same changes to the behaviour of %x as they cause for %d. %X %NX %-NX %0NX The same as the %x versions except the letters A-F for the digits 10-15. %s (that is a lower case s) Print a string, showing all of its characters and nothing else. %Ns The same, but add enough spaces after the string to make it fill at least N character positions. N is the minimum length. %-Ns The string is printed as normal, except that no more than N characters will be printed. N is the maximum length. %0NX The string is printed so that it occupies exactly N character positions. If it is too short, spaces will be added after it. If it is too long it will be cut short %S %NS %-NS %0NS (all with capital S) Exactly the same as the lower case s formats, except that ever character is ptinted in the unambiguous and visible way described under %C (capital). %t Print a date and time (as returned by seconds() from io) in the format YYYY-MM-DD:HH:MM:SS. In every case the number represented by N may be replaced with a star *. This has the same effect as in C's printf(). A format such as %-*s uses the next two parameters instead of just the usual one. The first should be an integer value, it provides the N value that the * replaced. The second is the normal parameter, the string or whatever that is to be printed. write(iosb, "www %*d xxx %-*x yyy %0*s zzz", 8, x + y, 4, addr, 24, "abcdefgh"); has the same effect as write(iosb, "www %8d xxx %-4x yyy %024s zzz", x + y, addr, "abcdefgh"); */ import "io" import "iosb" import "heap0" let start() be { init(); write(tty, "It is now %t\n\n", seconds()); write(tty, "'%s' s abcde\n", "abcde"); write(tty, "'%s' s abcdefghi\n", "abcdefghi"); write(tty, "'%s' s abcdefghij\n", "abcdefghij"); write(tty, "'%s' s abcdefghijk\n", "abcdefghijk"); write(tty, "'%s' s abcdefghijklmnop\n", "abcdefghijklmnop"); writech(tty, '\n'); write(tty, "'%10s' 10s abcde\n", "abcde"); write(tty, "'%10s' 10s abcdefghi\n", "abcdefghi"); write(tty, "'%10s' 10s abcdefghij\n", "abcdefghij"); write(tty, "'%10s' 10s abcdefghijk\n", "abcdefghijk"); write(tty, "'%10s' 10s abcdefghijklmnop\n", "abcdefghijklmnop"); writech(tty, '\n'); write(tty, "'%-10s' -10s abcde\n", "abcde"); write(tty, "'%-10s' -10s abcdefghi\n", "abcdefghi"); write(tty, "'%-10s' -10s abcdefghij\n", "abcdefghij"); write(tty, "'%-10s' -10s abcdefghijk\n", "abcdefghijk"); write(tty, "'%-10s' -10s abcdefghijklmnop\n", "abcdefghijklmnop"); writech(tty, '\n'); write(tty, "'%010s' 010s abcde\n", "abcde"); write(tty, "'%010s' 010s abcdefghi\n", "abcdefghi"); write(tty, "'%010s' 010s abcdefghij\n", "abcdefghij"); write(tty, "'%010s' 010s abcdefghijk\n", "abcdefghijk"); write(tty, "'%010s' 010s abcdefghijklmnop\n", "abcdefghijklmnop"); writech(tty, '\n'); write(tty, "'%d' d -98765432\n", -98765432); write(tty, "'%d' d -98765\n", -98765); write(tty, "'%d' d -9876\n", -9876); write(tty, "'%d' d -987\n", -987); write(tty, "'%d' d -1\n", -1); write(tty, "'%d' d 0\n", 0); write(tty, "'%d' d 1\n", 1); write(tty, "'%d' d 123\n", 123); write(tty, "'%d' d 1234\n", 1234); write(tty, "'%d' d 12345\n", 12345); write(tty, "'%d' d 0x7FFFFFFF\n", 0x7FFFFFFF); write(tty, "'%d' d 0x80000000\n", 0x80000000); writech(tty, '\n'); write(tty, "'%5d' 5d -98765432\n", -98765432); write(tty, "'%5d' 5d -98765\n", -98765); write(tty, "'%5d' 5d -9876\n", -9876); write(tty, "'%5d' 5d -987\n", -987); write(tty, "'%5d' 5d -1\n", -1); write(tty, "'%5d' 5d 0\n", 0); write(tty, "'%5d' 5d 1\n", 1); write(tty, "'%5d' 5d 1234\n", 1234); write(tty, "'%5d' 5d 12345\n", 12345); write(tty, "'%5d' 5d 123456\n", 123456); write(tty, "'%5d' 5d 0x7FFFFFFF\n", 0x7FFFFFFF); write(tty, "'%5d' 5d 0x80000000\n", 0x80000000); writech(tty, '\n'); write(tty, "'%05d' 05d -98765432\n", -98765432); write(tty, "'%05d' 05d -98765\n", -98765); write(tty, "'%05d' 05d -9876\n", -9876); write(tty, "'%05d' 05d -987\n", -987); write(tty, "'%05d' 05d -1\n", -1); write(tty, "'%05d' 05d 0\n", 0); write(tty, "'%05d' 05d 1\n", 1); write(tty, "'%05d' 05d 1234\n", 1234); write(tty, "'%05d' 05d 12345\n", 12345); write(tty, "'%05d' 05d 123456\n", 123456); write(tty, "'%05d' 05d 0x7FFFFFFF\n", 0x7FFFFFFF); write(tty, "'%05d' 05d 0x80000000\n", 0x80000000); writech(tty, '\n'); write(tty, "'%-5d' -5d -98765432\n", -98765432); write(tty, "'%-5d' -5d -98765\n", -98765); write(tty, "'%-5d' -5d -9876\n", -9876); write(tty, "'%-5d' -5d -987\n", -987); write(tty, "'%-5d' -5d -1\n", -1); write(tty, "'%-5d' -5d 0\n", 0); write(tty, "'%-5d' -5d 1\n", 1); write(tty, "'%-5d' -5d 1234\n", 1234); write(tty, "'%-5d' -5d 12345\n", 12345); write(tty, "'%-5d' -5d 123456\n", 123456); write(tty, "'%-5d' -5d 0x7FFFFFFF\n", 0x7FFFFFFF); write(tty, "'%-5d' -5d 0x80000000\n", 0x80000000); writech(tty, '\n'); write(tty, "'%-05d' -05d -98765432\n", -98765432); write(tty, "'%-05d' -05d -98765\n", -98765); write(tty, "'%-05d' -05d -9876\n", -9876); write(tty, "'%-05d' -05d -987\n", -987); write(tty, "'%-05d' -05d -1\n", -1); write(tty, "'%-05d' -05d 0\n", 0); write(tty, "'%-05d' -05d 1\n", 1); write(tty, "'%-05d' -05d 1234\n", 1234); write(tty, "'%-05d' -05d 12345\n", 12345); write(tty, "'%-05d' -05d 123456\n", 123456); write(tty, "'%-05d' -05d 0x7FFFFFFF\n", 0x7FFFFFFF); write(tty, "'%-05d' -05d 0x80000000\n", 0x80000000); writech(tty, '\n'); write(tty, "'%*d' *d 7 123\n", 7, 123); write(tty, "'%*d' *d 7 -123\n", 7, -123); write(tty, "'%*d' *d 7 123456789\n", 7, 123456789); write(tty, "'%*d' *d 7 -123456789\n", 7, -123456789); writech(tty, '\n'); write(tty, "'%-*d' -*d 7 123\n", 7, 123); write(tty, "'%-*d' -*d 7 -123\n", 7, -123); write(tty, "'%-*d' -*d 7 123456789\n", 7, 123456789); write(tty, "'%-*d' -*d 7 -123456789\n", 7, -123456789); writech(tty, '\n'); write(tty, "'%0*d' 0*d 7 123\n", 7, 123); write(tty, "'%0*d' 0*d 7 -123\n", 7, -123); write(tty, "'%0*d' 0*d 7 123456789\n", 7, 123456789); write(tty, "'%0*d' 0*d 7 -123456789\n", 7, -123456789); writech(tty, '\n'); write(tty, "'%-0*d' -0*d 7 123\n", 7, 123); write(tty, "'%-0*d' -0*d 7 -123\n", 7, -123); write(tty, "'%-0*d' -0*d 7 123456789\n", 7, 123456789); write(tty, "'%-0*d' -0*d 7 -123456789\n", 7, -123456789); writech(tty, '\n'); write(tty, "'%*s' *s 5 abcd\n", 5, "abcd"); write(tty, "'%*s' *s 5 abcde\n", 5, "abcde"); write(tty, "'%*s' *s 5 abcdef\n", 5, "abcdef"); writech(tty, '\n'); write(tty, "'%-*s' -*s 5 abcd\n", 5, "abcd"); write(tty, "'%-*s' -*s 5 abcde\n", 5, "abcde"); write(tty, "'%-*s' -*s 5 abcdef\n", 5, "abcdef"); writech(tty, '\n'); write(tty, "'%0*s' 0*s 5 abcd\n", 5, "abcd"); write(tty, "'%0*s' 0*s 5 abcde\n", 5, "abcde"); write(tty, "'%0*s' 0*s 5 abcdef\n", 5, "abcdef"); writech(tty, '\n'); write(tty, "'%-0*s' -0*s 5 abcd\n", 5, "abcd"); write(tty, "'%-0*s' -0*s 5 abcde\n", 5, "abcde"); write(tty, "'%-0*s' -0*s 5 abcdef\n", 5, "abcdef"); writech(tty, '\n'); write(tty, "'%x' x 0x0\n", 0x0); write(tty, "'%x' x 0xA\n", 0xA); write(tty, "'%x' x 0x1234\n", 0x1234); write(tty, "'%x' x 0x12345\n", 0x12345); write(tty, "'%x' x 0x123456\n", 0x123456); write(tty, "'%x' x 0x12345678\n", 0x12345678); write(tty, "'%x' x 0xABCD4321\n", 0xABCD4321); write(tty, "'%x' x 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%5x' 5x 0x0\n", 0x0); write(tty, "'%5x' 5x 0xA\n", 0xA); write(tty, "'%5x' 5x 0x1234\n", 0x1234); write(tty, "'%5x' 5x 0x12345\n", 0x12345); write(tty, "'%5x' 5x 0x123456\n", 0x123456); write(tty, "'%5x' 5x 0x12345678\n", 0x12345678); write(tty, "'%5x' 5x 0xABCD4321\n", 0xABCD4321); write(tty, "'%5x' 5x 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%05x' 05x 0x0\n", 0x0); write(tty, "'%05x' 05x 0xA\n", 0xA); write(tty, "'%05x' 05x 0x1234\n", 0x1234); write(tty, "'%05x' 05x 0x12345\n", 0x12345); write(tty, "'%05x' 05x 0x123456\n", 0x123456); write(tty, "'%05x' 05x 0x12345678\n", 0x12345678); write(tty, "'%05x' 05x 0xABCD4321\n", 0xABCD4321); write(tty, "'%05x' 05x 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%-5x' -5x 0x0\n", 0x0); write(tty, "'%-5x' -5x 0xA\n", 0xA); write(tty, "'%-5x' -5x 0x1234\n", 0x1234); write(tty, "'%-5x' -5x 0x12345\n", 0x12345); write(tty, "'%-5x' -5x 0x123456\n", 0x123456); write(tty, "'%-5x' -5x 0x12345678\n", 0x12345678); write(tty, "'%-5x' -5x 0xABCD4321\n", 0xABCD4321); write(tty, "'%-5x' -5x 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%X' X 0x0\n", 0x0); write(tty, "'%X' X 0xA\n", 0xA); write(tty, "'%X' X 0x1234\n", 0x1234); write(tty, "'%X' X 0x12345\n", 0x12345); write(tty, "'%X' X 0x123456\n", 0x123456); write(tty, "'%X' X 0x12345678\n", 0x12345678); write(tty, "'%X' X 0xABCD4321\n", 0xABCD4321); write(tty, "'%X' X 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%5X' 5X 0x0\n", 0x0); write(tty, "'%5X' 5X 0xA\n", 0xA); write(tty, "'%5X' 5X 0x1234\n", 0x1234); write(tty, "'%5X' 5X 0x12345\n", 0x12345); write(tty, "'%5X' 5X 0x123456\n", 0x123456); write(tty, "'%5X' 5X 0x12345678\n", 0x12345678); write(tty, "'%5X' 5X 0xABCD4321\n", 0xABCD4321); write(tty, "'%5X' 5X 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%05X' 05X 0x0\n", 0x0); write(tty, "'%05X' 05X 0xA\n", 0xA); write(tty, "'%05X' 05X 0x1234\n", 0x1234); write(tty, "'%05X' 05X 0x12345\n", 0x12345); write(tty, "'%05X' 05X 0x123456\n", 0x123456); write(tty, "'%05X' 05X 0x12345678\n", 0x12345678); write(tty, "'%05X' 05X 0xABCD4321\n", 0xABCD4321); write(tty, "'%05X' 05X 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n'); write(tty, "'%-5X' -5X 0x0\n", 0x0); write(tty, "'%-5X' -5X 0xA\n", 0xA); write(tty, "'%-5X' -5X 0x1234\n", 0x1234); write(tty, "'%-5X' -5X 0x12345\n", 0x12345); write(tty, "'%-5X' -5X 0x123456\n", 0x123456); write(tty, "'%-5X' -5X 0x12345678\n", 0x12345678); write(tty, "'%-5X' -5X 0xABCD4321\n", 0xABCD4321); write(tty, "'%-5X' -5X 0xFFFFFFFF\n", 0xFFFFFFFF); writech(tty, '\n') } /* This is the ouput produced when that program runs It is now 2024-02-09:19:24:52 'abcde' s abcde 'abcdefghi' s abcdefghi 'abcdefghij' s abcdefghij 'abcdefghijk' s abcdefghijk 'abcdefghijklmnop' s abcdefghijklmnop 'abcde ' 10s abcde 'abcdefghi ' 10s abcdefghi 'abcdefghij' 10s abcdefghij 'abcdefghijk' 10s abcdefghijk 'abcdefghijklmnop' 10s abcdefghijklmnop 'abcde' -10s abcde 'abcdefghi' -10s abcdefghi 'abcdefghij' -10s abcdefghij 'abcdefghij' -10s abcdefghijk 'abcdefghij' -10s abcdefghijklmnop 'abcde ' 010s abcde 'abcdefghi ' 010s abcdefghi 'abcdefghij' 010s abcdefghij 'abcdefghij' 010s abcdefghijk 'abcdefghij' 010s abcdefghijklmnop '-98765432' d -98765432 '-98765' d -98765 '-9876' d -9876 '-987' d -987 '-1' d -1 '0' d 0 '1' d 1 '123' d 123 '1234' d 1234 '12345' d 12345 '2147483647' d 0x7FFFFFFF '-2147483648' d 0x80000000 '-98765432' 5d -98765432 '-98765' 5d -98765 '-9876' 5d -9876 ' -987' 5d -987 ' -1' 5d -1 ' 0' 5d 0 ' 1' 5d 1 ' 1234' 5d 1234 '12345' 5d 12345 '123456' 5d 123456 '2147483647' 5d 0x7FFFFFFF '-2147483648' 5d 0x80000000 '-98765432' 05d -98765432 '-98765' 05d -98765 '-9876' 05d -9876 '-0987' 05d -987 '-0001' 05d -1 '00000' 05d 0 '00001' 05d 1 '01234' 05d 1234 '12345' 05d 12345 '123456' 05d 123456 '2147483647' 05d 0x7FFFFFFF '-2147483648' 05d 0x80000000 '-98765432' -5d -98765432 '-98765' -5d -98765 '-9876' -5d -9876 '-987 ' -5d -987 '-1 ' -5d -1 '0 ' -5d 0 '1 ' -5d 1 '1234 ' -5d 1234 '12345' -5d 12345 '123456' -5d 123456 '2147483647' -5d 0x7FFFFFFF '-2147483648' -5d 0x80000000 '-98765432' -05d -98765432 '-98765' -05d -98765 '-9876' -05d -9876 '-0987' -05d -987 '-0001' -05d -1 '00000' -05d 0 '00001' -05d 1 '01234' -05d 1234 '12345' -05d 12345 '123456' -05d 123456 '2147483647' -05d 0x7FFFFFFF '-2147483648' -05d 0x80000000 ' 123' *d 7 123 ' -123' *d 7 -123 '123456789' *d 7 123456789 '-123456789' *d 7 -123456789 '123 ' -*d 7 123 '-123 ' -*d 7 -123 '123456789' -*d 7 123456789 '-123456789' -*d 7 -123456789 '0000123' 0*d 7 123 '-000123' 0*d 7 -123 '123456789' 0*d 7 123456789 '-123456789' 0*d 7 -123456789 '0000123' -0*d 7 123 '-000123' -0*d 7 -123 '123456789' -0*d 7 123456789 '-123456789' -0*d 7 -123456789 'abcd ' *s 5 abcd 'abcde' *s 5 abcde 'abcdef' *s 5 abcdef 'abcd' -*s 5 abcd 'abcde' -*s 5 abcde 'abcde' -*s 5 abcdef 'abcd ' 0*s 5 abcd 'abcde' 0*s 5 abcde 'abcde' 0*s 5 abcdef 'abcd ' -0*s 5 abcd 'abcde' -0*s 5 abcde 'abcde' -0*s 5 abcdef '0' x 0x0 'a' x 0xA '1234' x 0x1234 '12345' x 0x12345 '123456' x 0x123456 '12345678' x 0x12345678 'abcd4321' x 0xABCD4321 'ffffffff' x 0xFFFFFFFF ' 0' 5x 0x0 ' a' 5x 0xA ' 1234' 5x 0x1234 '12345' 5x 0x12345 '123456' 5x 0x123456 '12345678' 5x 0x12345678 'abcd4321' 5x 0xABCD4321 'ffffffff' 5x 0xFFFFFFFF '00000' 05x 0x0 '0000a' 05x 0xA '01234' 05x 0x1234 '12345' 05x 0x12345 '123456' 05x 0x123456 '12345678' 05x 0x12345678 'abcd4321' 05x 0xABCD4321 'ffffffff' 05x 0xFFFFFFFF '0 ' -5x 0x0 'a ' -5x 0xA '1234 ' -5x 0x1234 '12345' -5x 0x12345 '123456' -5x 0x123456 '12345678' -5x 0x12345678 'abcd4321' -5x 0xABCD4321 'ffffffff' -5x 0xFFFFFFFF '0' X 0x0 'A' X 0xA '1234' X 0x1234 '12345' X 0x12345 '123456' X 0x123456 '12345678' X 0x12345678 'ABCD4321' X 0xABCD4321 'FFFFFFFF' X 0xFFFFFFFF ' 0' 5X 0x0 ' A' 5X 0xA ' 1234' 5X 0x1234 '12345' 5X 0x12345 '123456' 5X 0x123456 '12345678' 5X 0x12345678 'ABCD4321' 5X 0xABCD4321 'FFFFFFFF' 5X 0xFFFFFFFF '00000' 05X 0x0 '0000A' 05X 0xA '01234' 05X 0x1234 '12345' 05X 0x12345 '123456' 05X 0x123456 '12345678' 05X 0x12345678 'ABCD4321' 05X 0xABCD4321 'FFFFFFFF' 05X 0xFFFFFFFF '0 ' -5X 0x0 'A ' -5X 0xA '1234 ' -5X 0x1234 '12345' -5X 0x12345 '123456' -5X 0x123456 '12345678' -5X 0x12345678 'ABCD4321' -5X 0xABCD4321 'FFFFFFFF' -5X 0xFFFFFFFF */