|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <string.h> |
| 5 | +#include <sys/unistd.h> |
| 6 | + |
| 7 | +typedef struct |
| 8 | +{ |
| 9 | + long size; |
| 10 | + char *buf; |
| 11 | +} node_t; |
| 12 | + |
| 13 | +node_t *node_list[0x10] = {0}; |
| 14 | + |
| 15 | +void show_menu() |
| 16 | +{ |
| 17 | + puts("1. add"); |
| 18 | + puts("2. delete"); |
| 19 | + puts("3. edit"); |
| 20 | + puts("4. show"); |
| 21 | + puts("5. exit"); |
| 22 | + printf("Your choice: "); |
| 23 | +} |
| 24 | + |
| 25 | +long get_long() |
| 26 | +{ |
| 27 | + char tmp[8]; |
| 28 | + memset(tmp, 0, sizeof(tmp)); |
| 29 | + read(0, tmp, sizeof(tmp)); |
| 30 | + return atol(tmp); |
| 31 | +} |
| 32 | + |
| 33 | +void do_add() |
| 34 | +{ |
| 35 | + static int node_count = 0; |
| 36 | + if (node_count >= sizeof(node_list) / sizeof(node_list[0])) |
| 37 | + { |
| 38 | + puts("Enough!"); |
| 39 | + return; |
| 40 | + } |
| 41 | + node_list[node_count] = (node_t *)malloc(sizeof(node_t)); |
| 42 | + node_list[node_count]->size = 0x1000; |
| 43 | + node_list[node_count]->buf = malloc(0x1000); |
| 44 | + if (!node_list[node_count]) { |
| 45 | + perror("malloc"); |
| 46 | + exit(-1); |
| 47 | + } |
| 48 | + printf("Current node: %d\n", node_count); |
| 49 | + ++node_count; |
| 50 | + return; |
| 51 | + |
| 52 | + // long idx = get_long(); |
| 53 | + // if (idx >= 0 && idx < sizeof(node_list) / sizeof(node_list[0])) |
| 54 | + // { |
| 55 | + // node_list[idx] = (node_t *)malloc(sizeof(node_t)); |
| 56 | + // node_list[idx]->size = 0x1000; |
| 57 | + // node_list[idx]->buf = malloc(0x1000); |
| 58 | + // if (!node_list[idx]->buf) |
| 59 | + // { |
| 60 | + // perror("malloc"); |
| 61 | + // exit(-1); |
| 62 | + // } |
| 63 | + // } |
| 64 | + // else |
| 65 | + // { |
| 66 | + // puts("Invalid!"); |
| 67 | + // } |
| 68 | +} |
| 69 | + |
| 70 | +void do_edit() |
| 71 | +{ |
| 72 | + static int edit_count = 0; |
| 73 | + if (edit_count > 16) |
| 74 | + { |
| 75 | + puts("No chance!"); |
| 76 | + return; |
| 77 | + } |
| 78 | + printf("Index: "); |
| 79 | + long idx = get_long(); |
| 80 | + if (!node_list[idx]) { |
| 81 | + puts("Invalid index!"); |
| 82 | + return; |
| 83 | + } |
| 84 | + printf("Offset: "); |
| 85 | + long offset = get_long(); |
| 86 | + if (offset + 8 > node_list[idx]->size) { |
| 87 | + puts("Invalid offset!"); |
| 88 | + return; |
| 89 | + } |
| 90 | + printf("Content: "); |
| 91 | + read(0, node_list[idx]->buf+offset, 8); |
| 92 | + ++edit_count; |
| 93 | + return; |
| 94 | +} |
| 95 | + |
| 96 | +int main() |
| 97 | +{ |
| 98 | + for (;;) |
| 99 | + { |
| 100 | + show_menu(); |
| 101 | + long choice = get_long(); |
| 102 | + switch (choice) |
| 103 | + { |
| 104 | + case 1: |
| 105 | + do_add(); |
| 106 | + break; |
| 107 | + case 2: |
| 108 | + puts("Not implemented!"); |
| 109 | + break; |
| 110 | + case 3: |
| 111 | + do_edit(); |
| 112 | + break; |
| 113 | + case 4: |
| 114 | + puts("Not implemented!"); |
| 115 | + break; |
| 116 | + case 5: |
| 117 | + return 0; |
| 118 | + default: |
| 119 | + goto fail; |
| 120 | + } |
| 121 | + } |
| 122 | +fail: |
| 123 | + puts("invalid choice"); |
| 124 | + return -1; |
| 125 | +} |
| 126 | + |
| 127 | +__attribute__((constructor)) void init() |
| 128 | +{ |
| 129 | + setbuf(stdout, NULL); |
| 130 | + setbuf(stdin, NULL); |
| 131 | + setbuf(stderr, NULL); |
| 132 | +} |
0 commit comments