Changes in uspace/app/sysinfo/sysinfo.c [0499235:9a426d1f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sysinfo/sysinfo.c
r0499235 r9a426d1f 37 37 #include <stdio.h> 38 38 #include <sysinfo.h> 39 #include <malloc.h>40 39 #include <sys/types.h> 40 41 static int print_item_val(char *ipath); 42 static int print_item_data(char *ipath); 43 44 static void dump_bytes_hex(char *data, size_t size); 45 static void dump_bytes_text(char *data, size_t size); 46 47 static void print_syntax(void); 48 49 int main(int argc, char *argv[]) 50 { 51 int rc; 52 char *ipath; 53 sysinfo_item_val_type_t tag; 54 55 if (argc != 2) { 56 print_syntax(); 57 return 1; 58 } 59 60 ipath = argv[1]; 61 62 tag = sysinfo_get_val_type(ipath); 63 64 /* Silence warning */ 65 rc = EOK; 66 67 switch (tag) { 68 case SYSINFO_VAL_UNDEFINED: 69 printf("Error: Sysinfo item '%s' not defined.\n", ipath); 70 rc = 2; 71 break; 72 case SYSINFO_VAL_VAL: 73 rc = print_item_val(ipath); 74 break; 75 case SYSINFO_VAL_DATA: 76 rc = print_item_data(ipath); 77 break; 78 default: 79 printf("Error: Sysinfo item '%s' with unknown value type.\n", 80 ipath); 81 rc = 2; 82 break; 83 } 84 85 return rc; 86 } 87 88 static int print_item_val(char *ipath) 89 { 90 sysarg_t value; 91 int rc; 92 93 rc = sysinfo_get_value(ipath, &value); 94 if (rc != EOK) { 95 printf("Error reading item '%s'.\n", ipath); 96 return rc; 97 } 98 99 printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath, 100 (uint64_t) value, (uint64_t) value); 101 102 return EOK; 103 } 104 105 static int print_item_data(char *ipath) 106 { 107 void *data; 108 size_t size; 109 110 data = sysinfo_get_data(ipath, &size); 111 if (data == NULL) { 112 printf("Error reading item '%s'.\n", ipath); 113 return -1; 114 } 115 116 printf("%s -> ", ipath); 117 dump_bytes_hex(data, size); 118 fputs(" ('", stdout); 119 dump_bytes_text(data, size); 120 fputs("')\n", stdout); 121 122 return EOK; 123 } 41 124 42 125 static void dump_bytes_hex(char *data, size_t size) 43 126 { 44 for (size_t i = 0; i < size; i++) { 45 if (i > 0) 46 putchar(' '); 127 size_t i; 128 129 for (i = 0; i < size; ++i) { 130 if (i > 0) putchar(' '); 47 131 printf("0x%02x", (uint8_t) data[i]); 48 132 } … … 51 135 static void dump_bytes_text(char *data, size_t size) 52 136 { 53 size_t offset = 0; 54 137 wchar_t c; 138 size_t offset; 139 140 offset = 0; 141 55 142 while (offset < size) { 56 wchar_tc = str_decode(data, &offset, size);143 c = str_decode(data, &offset, size); 57 144 printf("%lc", (wint_t) c); 58 145 } 59 146 } 60 147 61 static int print_item_val(char *ipath) 148 149 static void print_syntax(void) 62 150 { 63 sysarg_t value; 64 int rc = sysinfo_get_value(ipath, &value); 65 if (rc != EOK) { 66 printf("Error reading item '%s'.\n", ipath); 67 return rc; 68 } 69 70 printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath, 71 (uint64_t) value, (uint64_t) value); 72 73 return EOK; 74 } 75 76 static int print_item_data(char *ipath) 77 { 78 size_t size; 79 void *data = sysinfo_get_data(ipath, &size); 80 if (data == NULL) { 81 printf("Error reading item '%s'.\n", ipath); 82 return -1; 83 } 84 85 printf("%s -> ", ipath); 86 dump_bytes_hex(data, size); 87 fputs(" ('", stdout); 88 dump_bytes_text(data, size); 89 fputs("')\n", stdout); 90 91 return EOK; 92 } 93 94 static int print_item_property(char *ipath, char *iprop) 95 { 96 size_t size; 97 void *data = sysinfo_get_property(ipath, iprop, &size); 98 if (data == NULL) { 99 printf("Error reading property '%s' of item '%s'.\n", iprop, 100 ipath); 101 return -1; 102 } 103 104 printf("%s property %s -> ", ipath, iprop); 105 dump_bytes_hex(data, size); 106 fputs(" ('", stdout); 107 dump_bytes_text(data, size); 108 fputs("')\n", stdout); 109 110 return EOK; 111 } 112 113 static void print_spaces(size_t spaces) 114 { 115 for (size_t i = 0; i < spaces; i++) 116 printf(" "); 117 } 118 119 static void print_keys(const char *path, size_t spaces) 120 { 121 size_t size; 122 char *keys = sysinfo_get_keys(path, &size); 123 if ((keys == NULL) || (size == 0)) 124 return; 125 126 size_t pos = 0; 127 while (pos < size) { 128 /* Process each key with sanity checks */ 129 size_t cur_size = str_nsize(keys + pos, size - pos); 130 if (keys[pos + cur_size] != 0) 131 break; 132 133 size_t path_size = str_size(path) + cur_size + 2; 134 char *cur_path = (char *) malloc(path_size); 135 if (cur_path == NULL) 136 break; 137 138 size_t length; 139 140 if (path[0] != 0) { 141 print_spaces(spaces); 142 printf(".%s\n", keys + pos); 143 length = str_length(keys + pos) + 1; 144 145 snprintf(cur_path, path_size, "%s.%s", path, keys + pos); 146 } else { 147 printf("%s\n", keys + pos); 148 length = str_length(keys + pos); 149 150 snprintf(cur_path, path_size, "%s", keys + pos); 151 } 152 153 print_keys(cur_path, spaces + length); 154 155 free(cur_path); 156 pos += cur_size + 1; 157 } 158 159 free(keys); 160 } 161 162 int main(int argc, char *argv[]) 163 { 164 int rc = 0; 165 166 if (argc < 2) { 167 /* Print keys */ 168 print_keys("", 0); 169 return rc; 170 } 171 172 char *ipath = argv[1]; 173 174 if (argc < 3) { 175 sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath); 176 177 switch (tag) { 178 case SYSINFO_VAL_UNDEFINED: 179 printf("Error: Sysinfo item '%s' not defined.\n", ipath); 180 rc = 2; 181 break; 182 case SYSINFO_VAL_VAL: 183 rc = print_item_val(ipath); 184 break; 185 case SYSINFO_VAL_DATA: 186 rc = print_item_data(ipath); 187 break; 188 default: 189 printf("Error: Sysinfo item '%s' with unknown value type.\n", 190 ipath); 191 rc = 2; 192 break; 193 } 194 195 return rc; 196 } 197 198 char *iprop = argv[2]; 199 rc = print_item_property(ipath, iprop); 200 return rc; 151 printf("Syntax: sysinfo <item_path>\n"); 201 152 } 202 153
Note:
See TracChangeset
for help on using the changeset viewer.