# HG changeset patch # User Paul Boddie # Date 1666999188 -7200 # Node ID 8929c3922e962eb61269907b1993254119125cb3 # Parent 2ebc42a6c83dfb9b71e530c1bf076f60a7240cb1 Introduced format specifier macros to avoid output formatting difficulties. diff -r 2ebc42a6c83d -r 8929c3922e96 libsystypes/include/systypes/format.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libsystypes/include/systypes/format.h Sat Oct 29 01:19:48 2022 +0200 @@ -0,0 +1,42 @@ +/* + * Format specifiers for output. + * + * Copyright (C) 2022 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include + +/* In principle, the PR macros in inttypes.h should be usable, but some strange + conflict seems to arise when building programs using them, resulting in + errors when the macros are used. */ + +#define pFMT32 "" + +#if __WORDSIZE == 64 +#define pFMT64 "l" +#else +#define pFMT64 "ll" +#endif + +/* See: systypes/base.h */ + +#define pFMToffset pFMT64 + +// vim: tabstop=2 expandtab shiftwidth=2 diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_align.cc --- a/tests/dstest_align.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_align.cc Sat Oct 29 01:19:48 2022 +0200 @@ -1,6 +1,9 @@ #include #include +#include +#define FMT32x "%" pFMT32 "x" +#define FMT64x "%" pFMT64 "x" #define OFFSET(obj, mem) ((char *) &(obj)->mem - (char *) obj) struct A @@ -26,6 +29,8 @@ int main(int argc, char *argv[]) { + (void) argc; (void) argv; + struct A a = {1, 2, 3}; struct B b = {1, 2, 3}; struct C c = {1, 2, 3}; @@ -33,21 +38,21 @@ struct A *ma = (struct A *) m; struct A *ma1 = (struct A *) (m + 1); - printf("a.a @ offset %d = %x\n", OFFSET(&a, a), a.a); - printf("a.b @ offset %d = %llx\n", OFFSET(&a, b), a.b); - printf("a.c @ offset %d = %llx\n\n", OFFSET(&a, c), a.c); - printf("b.a @ offset %d = %x\n", OFFSET(&b, a), b.a); - printf("b.b @ offset %d = %llx\n", OFFSET(&b, b), b.b); - printf("b.c @ offset %d = %llx\n\n", OFFSET(&b, c), b.c); - printf("c.a @ offset %d = %x\n", OFFSET(&c, a), c.a); - printf("c.b @ offset %d = %x\n", OFFSET(&c, b), c.b); - printf("c.c @ offset %d = %llx\n\n", OFFSET(&c, c), c.c); - printf("ma.a @ offset %d = %x\n", OFFSET(ma, a), ma->a); - printf("ma.b @ offset %d = %llx\n", OFFSET(ma, b), ma->b); - printf("ma.c @ offset %d = %llx\n\n", OFFSET(ma, c), ma->c); - printf("ma1.a @ offset %d = %x\n", OFFSET(ma1, a), ma1->a); - printf("ma1.b @ offset %d = %llx\n", OFFSET(ma1, b), ma1->b); - printf("ma1.c @ offset %d = %llx\n\n", OFFSET(ma1, c), ma1->c); + printf("a.a @ offset %d = " FMT32x "\n", OFFSET(&a, a), a.a); + printf("a.b @ offset %d = " FMT64x "\n", OFFSET(&a, b), a.b); + printf("a.c @ offset %d = " FMT64x "\n\n", OFFSET(&a, c), a.c); + printf("b.a @ offset %d = " FMT32x "\n", OFFSET(&b, a), b.a); + printf("b.b @ offset %d = " FMT64x "\n", OFFSET(&b, b), b.b); + printf("b.c @ offset %d = " FMT64x "\n\n", OFFSET(&b, c), b.c); + printf("c.a @ offset %d = " FMT32x "\n", OFFSET(&c, a), c.a); + printf("c.b @ offset %d = " FMT32x "\n", OFFSET(&c, b), c.b); + printf("c.c @ offset %d = " FMT64x "\n\n", OFFSET(&c, c), c.c); + printf("ma.a @ offset %d = " FMT32x "\n", OFFSET(ma, a), ma->a); + printf("ma.b @ offset %d = " FMT64x "\n", OFFSET(ma, b), ma->b); + printf("ma.c @ offset %d = " FMT64x "\n\n", OFFSET(ma, c), ma->c); + printf("ma1.a @ offset %d = " FMT32x "\n", OFFSET(ma1, a), ma1->a); + printf("ma1.b @ offset %d = " FMT64x "\n", OFFSET(ma1, b), ma1->b); + printf("ma1.c @ offset %d = " FMT64x "\n\n", OFFSET(ma1, c), ma1->c); return 0; } diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_block_client.cc --- a/tests/dstest_block_client.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_block_client.cc Sat Oct 29 01:19:48 2022 +0200 @@ -23,6 +23,9 @@ #include #include +#include + +#define FMToffsetd "%" pFMToffset "d" #include #include @@ -44,7 +47,7 @@ offset_t remaining = file_populated_span(file) - offset; offset_t sample_remaining = remaining < sample ? remaining : sample; - printf("%lld bytes from %p...\n", sample_remaining, (file->memory + offset)); + printf(FMToffsetd " bytes from %p...\n", sample_remaining, (file->memory + offset)); memcpy(buf, (file->memory + offset), sample_remaining); buf[sample_remaining] = '\0'; @@ -70,10 +73,10 @@ printf("----\n%s\n----\n", outbuf); - printf("[%lld zero bytes at start]\n", leading); + printf("[" FMToffsetd " zero bytes at start]\n", leading); printf("[%zd bytes shown]\n", strlen(outbuf)); - printf("[%lld bytes after string]\n", sample_remaining - leading - strlen(outbuf)); - printf("[%lld zero bytes at end]\n", trailing); + printf("[" FMToffsetd " bytes after string]\n", sample_remaining - leading - strlen(outbuf)); + printf("[" FMToffsetd " zero bytes at end]\n", trailing); } } @@ -120,7 +123,9 @@ return 1; } - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); printf("File contents:\n"); show(&file, step, sample); @@ -128,7 +133,7 @@ printf("File shown.\n"); printf("Expected...\n" "[0 zero bytes at start]\n" - "[%lld bytes shown]\n" + "[" FMToffsetd " bytes shown]\n" "[0 bytes after string]\n" "[0 zero bytes at end]\n\n", original_size); @@ -144,7 +149,9 @@ return 1; } - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); printf("File contents:\n"); show(&file, step, sample); @@ -152,9 +159,9 @@ printf("File shown.\n"); printf("Expected...\n" "[10 zero bytes at start]\n" - "[%lld bytes shown]\n" - "[%lld bytes after string]\n" - "[%lld zero bytes at end]\n\n", + "[" FMToffsetd " bytes shown]\n" + "[" FMToffsetd " bytes after string]\n" + "[" FMToffsetd " zero bytes at end]\n\n", original_size - 20, 10ULL, 10ULL); /* Resizing must occur before writing beyond the end of file. Otherwise, the @@ -166,7 +173,7 @@ offset_t new_size = old_populated_size_rounded + old_populated_size; offset_t new_region = round(new_size, page(1)); - printf("Resize to %lld...\n", new_size); + printf("Resize to " FMToffsetd "...\n", new_size); err = file_resize(&file, new_size); @@ -177,8 +184,12 @@ } printf("Resized file...\n"); - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); - printf("Expected %lld...%lld populated size %lld size %lld.\n\n", 0ULL, old_populated_size_rounded, old_populated_size_rounded, new_size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Expected " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n\n", + 0ULL, old_populated_size_rounded, old_populated_size_rounded, new_size); /* Re-map to avoid masking the region. */ @@ -191,10 +202,14 @@ } printf("Remapped file without masking...\n"); - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); - printf("Expected %lld...%lld populated size %lld size %lld.\n\n", 0ULL, new_region, old_populated_size, old_size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Expected " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n\n", + 0ULL, new_region, old_populated_size, old_size); - printf("Resize to %lld...\n", new_size); + printf("Resize to " FMToffsetd "...\n", new_size); err = file_resize(&file, new_size); @@ -205,16 +220,22 @@ } printf("Resized file...\n"); - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); - printf("Expected %lld...%lld populated size %lld size %lld.\n\n", 0ULL, round(new_size, page(1)), new_size, new_size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Expected " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n\n", + 0ULL, round(new_size, page(1)), new_size, new_size); /* Copy the file data to another file region. */ - printf("Copy data to %lld...\n", old_populated_size_rounded); + printf("Copy data to " FMToffsetd "...\n", old_populated_size_rounded); memcpy(file.memory + old_populated_size_rounded, file.memory, old_size); - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); printf("File contents:\n"); show(&file, step, sample); @@ -222,12 +243,12 @@ printf("File shown.\n"); printf("Expected...\n" "[0 zero bytes at start]\n" - "[%lld bytes shown]\n" - "[%lld bytes after string]\n" - "[%lld zero bytes at end]\n" + "[" FMToffsetd " bytes shown]\n" + "[" FMToffsetd " bytes after string]\n" + "[" FMToffsetd " zero bytes at end]\n" "...\n" "[0 zero bytes at start]\n" - "[%lld bytes shown]\n" + "[" FMToffsetd " bytes shown]\n" "[0 bytes after string]\n" "[0 zero bytes at end]\n\n", original_size, sample - original_size, sample - original_size, @@ -243,7 +264,9 @@ return 1; } - printf("Region %lld...%lld populated size %lld size %lld.\n", file.start_pos, file.end_pos, file_populated_span(&file), file.size); + printf("Region " FMToffsetd "..." FMToffsetd " populated size " FMToffsetd + " size " FMToffsetd ".\n", + file.start_pos, file.end_pos, file_populated_span(&file), file.size); printf("File contents:\n"); show(&file, step, sample); @@ -251,12 +274,12 @@ printf("File shown.\n"); printf("Expected...\n" "[10 zero bytes at start]\n" - "[%lld bytes shown]\n" - "[%lld bytes after string]\n" - "[%lld zero bytes at end]\n" + "[" FMToffsetd " bytes shown]\n" + "[" FMToffsetd " bytes after string]\n" + "[" FMToffsetd " zero bytes at end]\n" "...\n" "[0 zero bytes at start]\n" - "[%lld bytes shown]\n" + "[" FMToffsetd " bytes shown]\n" "[10 bytes after string]\n" "[10 zero bytes at end]\n", original_size - 10, sample - original_size, sample - original_size, diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_block_client_simple.cc --- a/tests/dstest_block_client_simple.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_block_client_simple.cc Sat Oct 29 01:19:48 2022 +0200 @@ -20,6 +20,9 @@ */ #include +#include + +#define FMToffsetd "%" pFMToffset "d" #include #include @@ -62,7 +65,7 @@ if (nread != size) { - printf("Could not read entire file: %ld out of %ld bytes.\n", nread, size); + printf("Could not read entire file: " FMToffsetd " out of " FMToffsetd " bytes.\n", nread, size); return 1; } @@ -70,7 +73,7 @@ fwrite(buffer, sizeof(char), nread, stdout); - printf("Copy %ld bytes to end...\n", nread); + printf("Copy " FMToffsetd " bytes to end...\n", nread); for (int times = 0; times < repetition; times++) { @@ -80,20 +83,20 @@ if (nwritten != nread) { - printf("Could not write file section: %ld instead of %ld bytes.\n", + printf("Could not write file section: " FMToffsetd " instead of " FMToffsetd " bytes.\n", nwritten, nread); return 1; } } - printf("File now has size %ld.\n", file->size); - printf("File has expected size %ld: %s\n", new_size, new_size == file->size ? "True" : "False"); + printf("File now has size " FMToffsetd ".\n", file->size); + printf("File has expected size " FMToffsetd ": %s\n", new_size, new_size == file->size ? "True" : "False"); printf("Seek to start...\n"); client_seek(file, 0, SEEK_SET); - printf("At mapped region from %ld to %ld with data at %ld to %ld.\n", + printf("At mapped region from " FMToffsetd " to " FMToffsetd " with data at " FMToffsetd " to " FMToffsetd ".\n", file->start_pos, file->end_pos, file->data_current, file->data_end); printf("File contents...\n"); @@ -104,7 +107,7 @@ { if ((nread != size) && (nread != file->size - position)) { - printf("Could not read file section: %ld instead of %ld or remaining %ld bytes.\n", + printf("Could not read file section: " FMToffsetd " instead of " FMToffsetd " or remaining " FMToffsetd " bytes.\n", nread, size, file->size - position); return 1; } diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_file_client.cc --- a/tests/dstest_file_client.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_file_client.cc Sat Oct 29 01:19:48 2022 +0200 @@ -30,6 +30,9 @@ #include #include +#include + +#define FMToffsetd "%" pFMToffset "d" @@ -44,7 +47,7 @@ offset_t nwritten = client_write(file, buffer, size); - printf("Written %lld/%lld in #%d to file with first %c, last %c...\n", nwritten, size, region, *buffer, *(buffer + nwritten - 1)); + printf("Written " FMToffsetd "/" FMToffsetd " in #%d to file with first %c, last %c...\n", nwritten, size, region, *buffer, *(buffer + nwritten - 1)); /* Flush to make the output available. */ @@ -71,7 +74,7 @@ client_seek(file, 0, SEEK_END); - printf("File %s at position %lld...\n", creator ? "creator" : "follower", client_tell(file)); + printf("File %s at position " FMToffsetd "...\n", creator ? "creator" : "follower", client_tell(file)); /* Write initial data as the file's creator. */ @@ -105,7 +108,7 @@ { total += nread; - printf("Read as %s %lld/%lld, total %lld, first %c, last %c, from file...\n", creator ? "creator" : "follower", + printf("Read as %s " FMToffsetd "/" FMToffsetd ", total " FMToffsetd ", first %c, last %c, from file...\n", creator ? "creator" : "follower", nread, size, total, *buffer, *(buffer + nread - 1)); if (!(*(buffer + nread - 1))) diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_file_remove.cc --- a/tests/dstest_file_remove.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_file_remove.cc Sat Oct 29 01:19:48 2022 +0200 @@ -28,6 +28,9 @@ #include #include +#include + +#define FMToffsetd "%" pFMToffset "d" @@ -42,7 +45,7 @@ nread = client_read(file, buf, 20); - printf("Read %lld bytes...\n", nread); + printf("Read " FMToffsetd " bytes...\n", nread); fwrite(buf, sizeof(char), nread, stdout); fputs("\n", stdout); } @@ -57,7 +60,7 @@ nwritten = client_write(file, buf, strlen(buf)); - printf("Wrote %lld bytes: ", nwritten); + printf("Wrote " FMToffsetd " bytes: ", nwritten); fwrite(buf, sizeof(char), nwritten, stdout); fputs("\n", stdout); } diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_pipe_client.cc --- a/tests/dstest_pipe_client.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_pipe_client.cc Sat Oct 29 01:19:48 2022 +0200 @@ -31,6 +31,9 @@ #include #include +#include + +#define FMToffsetd "%" pFMToffset "d" @@ -64,7 +67,7 @@ offset_t nwritten = client_write(writer, buffer, TO_TRANSFER); - printf("Written %lld/%lld in #%d of %d/%d to pipe #%d...\n", nwritten, TO_TRANSFER, region, loop, 2, number); + printf("Written " FMToffsetd "/" FMToffsetd " in #%d of %d/%d to pipe #%d...\n", nwritten, TO_TRANSFER, region, loop, 2, number); } sleep(1); @@ -130,7 +133,7 @@ { totals[p] += nread; - printf("Read %lld/%lld, total %lld, first %c, last %c, from pipe #%d...\n", nread, TO_TRANSFER, totals[p], *buffer, *(buffer + nread - 1), p + 1); + printf("Read " FMToffsetd "/" FMToffsetd ", total " FMToffsetd ", first %c, last %c, from pipe #%d...\n", nread, TO_TRANSFER, totals[p], *buffer, *(buffer + nread - 1), p + 1); nread = client_read(reader, buffer, TO_TRANSFER); } @@ -151,7 +154,7 @@ for (int p = 0; p < 2; p++) { - printf("Data received for pipe #%d: %lld\n", p, totals[p]); + printf("Data received for pipe #%d: " FMToffsetd "\n", p, totals[p]); printf("Data shown for pipe #%d: %s\n", p, totals[p] == TO_TRANSFER * 26 * NUMBER_OF_ITERATIONS ? "True" : "False"); } } diff -r 2ebc42a6c83d -r 8929c3922e96 tests/dstest_test_client.cc --- a/tests/dstest_test_client.cc Fri Oct 28 01:32:32 2022 +0200 +++ b/tests/dstest_test_client.cc Sat Oct 29 01:19:48 2022 +0200 @@ -23,6 +23,9 @@ #include #include +#include + +#define FMToffsetd "%" pFMToffset "d" #include #include @@ -112,7 +115,7 @@ } if ((fileid != _fileid) || (filepos != _filepos)) - printf("! %ld:%lld is not %ld:%lld\n", _fileid, _filepos, fileid, filepos); + printf("! %ld:" FMToffsetd " is not %ld:" FMToffsetd "\n", _fileid, _filepos, fileid, filepos); } }