1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include <stdio.h> #include <stdlib.h> #include <unistd.h>
void read_input(char *buf, size_t size) { int ret; ret = read(0, buf, size); if (ret <= 0) { puts("Error"); _exit(-1); } }
char *heaparray[10]; unsigned long int magic = 0;
void menu() { puts("--------------------------------"); puts(" Magic Heap Creator "); puts("--------------------------------"); puts(" 1. Create a Heap "); puts(" 2. Edit a Heap "); puts(" 3. Delete a Heap "); puts(" 4. Exit "); puts("--------------------------------"); printf("Your choice :"); }
void create_heap() { int i; char buf[8]; size_t size = 0; for (i = 0; i < 10; i++) { if (!heaparray[i]) { printf("Size of Heap : "); read(0, buf, 8); size = atoi(buf); heaparray[i] = (char *)malloc(size); if (!heaparray[i]) { puts("Allocate Error"); exit(2); } printf("Content of heap:"); read_input(heaparray[i], size); puts("SuccessFul"); break; } } }
void edit_heap() { int idx; char buf[4]; size_t size; printf("Index :"); read(0, buf, 4); idx = atoi(buf); if (idx < 0 || idx >= 10) { puts("Out of bound!"); _exit(0); } if (heaparray[idx]) { printf("Size of Heap : "); read(0, buf, 8); size = atoi(buf); printf("Content of heap : "); read_input(heaparray[idx], size); puts("Done !"); } else { puts("No such heap !"); } }
void delete_heap() { int idx; char buf[4]; printf("Index :"); read(0, buf, 4); idx = atoi(buf); if (idx < 0 || idx >= 10) { puts("Out of bound!"); _exit(0); } if (heaparray[idx]) { free(heaparray[idx]); heaparray[idx] = NULL; puts("Done !"); } else { puts("No such heap !"); } }
void l33t() { system("cat ./flag"); }
int main() { char buf[8]; setvbuf(stdout, 0, 2, 0); setvbuf(stdin, 0, 2, 0); while (1) { menu(); read(0, buf, 8); switch (atoi(buf)) { case 1: create_heap(); break; case 2: edit_heap(); break; case 3: delete_heap(); break; case 4: exit(0); break; case 4869: if (magic > 4869) { puts("Congrt !"); l33t(); } else puts("So sad !"); break; default: puts("Invalid Choice"); break; } } return 0; }
|