Changes in uspace/app/sysinfo/sysinfo.c [9a426d1f:0499235] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sysinfo/sysinfo.c
r9a426d1f r0499235 37 37 #include <stdio.h> 38 38 #include <sysinfo.h> 39 #include <malloc.h> 39 40 #include <sys/types.h> 40 41 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; 42 static void dump_bytes_hex(char *data, size_t size) 43 { 44 for (size_t i = 0; i < size; i++) { 45 if (i > 0) 46 putchar(' '); 47 printf("0x%02x", (uint8_t) data[i]); 48 } 49 } 50 51 static void dump_bytes_text(char *data, size_t size) 52 { 53 size_t offset = 0; 54 55 while (offset < size) { 56 wchar_t c = str_decode(data, &offset, size); 57 printf("%lc", (wint_t) c); 58 } 86 59 } 87 60 … … 89 62 { 90 63 sysarg_t value; 91 int rc; 92 93 rc = sysinfo_get_value(ipath, &value); 64 int rc = sysinfo_get_value(ipath, &value); 94 65 if (rc != EOK) { 95 66 printf("Error reading item '%s'.\n", ipath); 96 67 return rc; 97 68 } 98 69 99 70 printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath, 100 71 (uint64_t) value, (uint64_t) value); 101 72 102 73 return EOK; 103 74 } … … 105 76 static int print_item_data(char *ipath) 106 77 { 107 void *data;108 78 size_t size; 109 110 data = sysinfo_get_data(ipath, &size); 79 void *data = sysinfo_get_data(ipath, &size); 111 80 if (data == NULL) { 112 81 printf("Error reading item '%s'.\n", ipath); 113 82 return -1; 114 83 } 115 84 116 85 printf("%s -> ", ipath); 117 86 dump_bytes_hex(data, size); … … 119 88 dump_bytes_text(data, size); 120 89 fputs("')\n", stdout); 121 90 122 91 return EOK; 123 92 } 124 93 125 static void dump_bytes_hex(char *data, size_t size) 126 { 127 size_t i; 128 129 for (i = 0; i < size; ++i) { 130 if (i > 0) putchar(' '); 131 printf("0x%02x", (uint8_t) data[i]); 132 } 133 } 134 135 static void dump_bytes_text(char *data, size_t size) 136 { 137 wchar_t c; 138 size_t offset; 139 140 offset = 0; 141 142 while (offset < size) { 143 c = str_decode(data, &offset, size); 144 printf("%lc", (wint_t) c); 145 } 146 } 147 148 149 static void print_syntax(void) 150 { 151 printf("Syntax: sysinfo <item_path>\n"); 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; 152 201 } 153 202
Note:
See TracChangeset
for help on using the changeset viewer.