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