akaros/tests/printf-ext.c
<<
>>
Prefs
   1/* Basic tests for custom printf strings, such as %i (ipaddr) and %r */
   2
   3#include <stdlib.h>
   4#include <stdio.h>
   5#include <parlib/parlib.h>
   6#include <unistd.h>
   7#include <signal.h>
   8#include <parlib/printf-ext.h>
   9#include <fcntl.h>
  10#include <sys/types.h>
  11#include <sys/stat.h>
  12
  13uint8_t v4addr[] = {
  14        0, 0, 0, 0,
  15        0, 0, 0, 0,
  16        0, 0, 0xff, 0xff,
  17        192, 168, 0, 2
  18};
  19
  20uint8_t v6addr[] = {
  21        0xfe, 0x80, 0, 0,
  22        0, 0, 0, 0,
  23        0x20, 0x0, 0x0a, 0xff,
  24        0xfe, 0xa7, 0x0f, 0x7c
  25};
  26
  27uint8_t v6mask[] = {
  28        0xff, 0xff, 0xff, 0xff,
  29        0xff, 0xff, 0xff, 0xff,
  30        0xff, 0xff, 0xff, 0xff,
  31        255, 255, 192, 0
  32};
  33
  34uint8_t ethaddr[] = {
  35        0x90, 0xe6, 0xba, 0x12, 0x48, 0x64
  36};
  37
  38int main(int argc, char **argv)
  39{
  40        int ret;
  41
  42        if (register_printf_specifier('i', printf_ipaddr, printf_ipaddr_info))
  43                printf("Failed to register 'i'\n");
  44        if (register_printf_specifier('M', printf_ipmask, printf_ipmask_info))
  45                printf("Failed to register 'M'\n");
  46        if (register_printf_specifier('E', printf_ethaddr, printf_ethaddr_info))
  47                printf("Failed to register 'E'\n");
  48        if (register_printf_specifier('H', printf_hexdump, printf_hexdump_info))
  49                printf("Failed to register 'H'\n");
  50
  51        printf("IPv4 addr %i\n", v4addr);
  52        printf("IPv6 addr %i\n", v6addr);
  53        printf("IPv6 mask %M\n", v6mask);
  54        printf("IPv4 addr as mask %M\n", v4addr);
  55        printf("IPv6 addr as mask %M\n", v6addr);
  56        printf("ethaddr %E\n", ethaddr);
  57        printf("ethaddr null %E\n", 0);
  58        printf("IPv6 addr as hex (4 bytes only): %.4H\n", v6addr);
  59        printf("IPv6 addr as hex: %.*H\n", COUNT_OF(v6addr), v6addr);
  60
  61        ret = open("/9/proc/no/such/file", 0, 0);
  62        printf("Open ret %d, errstr: %r\n", ret);
  63        printf("%s %i %M %d %s %r %E\n", "testing a few,", v6addr, v6mask, 1337,
  64               "more cowbell", ethaddr);
  65
  66        printf("Done\n");
  67        return 0;
  68}
  69