Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

260 rader
5.5 KiB

5 år sedan
  1. //gcc -m32 -fno-stack-protector -z execstack -no-pie blackmarket.c -o blackmarket
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include <time.h>
  8. #include "blackmarket.h"
  9. #define BUFSIZE 0x1ff
  10. int diamond;
  11. void not_used(){
  12. system("/bin/sh");
  13. }
  14. Node *Node_create(){
  15. Node *node = malloc(sizeof(Node));
  16. assert(node != NULL);
  17. node->data = "";
  18. node->next = NULL;
  19. }
  20. List *List_create(){
  21. List *list = malloc(sizeof(List));
  22. assert(list != NULL);
  23. Node *node = Node_create();
  24. list->first = node;
  25. return list;
  26. }
  27. void List_append(List *list, char *str){
  28. assert(list != NULL);
  29. assert(str != NULL);
  30. Node *node = list->first;
  31. while(node->next != NULL){
  32. node = node->next;
  33. }
  34. node->data = str;
  35. node->next = Node_create();
  36. }
  37. int List_find(List *list, char *str){
  38. assert(list != NULL);
  39. assert(str != NULL);
  40. int index = 0;
  41. Node *node = list->first;
  42. while(node->next != NULL){
  43. int cmp = strcmp(str, node->data);
  44. if(cmp == 0){
  45. return index;
  46. }
  47. node = node->next;
  48. index++;
  49. }
  50. return -1;
  51. }
  52. int List_length(List *list){
  53. assert(list != NULL);
  54. Node *node = list->first;
  55. int length = 0;
  56. while(node->next != NULL){
  57. length++;
  58. node = node->next;
  59. }
  60. return length;
  61. }
  62. void Node_destroy(Node *node){
  63. assert(node != NULL);
  64. free(node);
  65. }
  66. void List_remove(List *list, int index){
  67. assert(list != NULL);
  68. if(index < 0){
  69. exit(0);
  70. }else{
  71. assert(index < List_length(list));
  72. if(index == 0){
  73. Node *node = list->first;
  74. list->first = list->first->next;
  75. Node_destroy(node);
  76. }else{
  77. Node *before = list->first;
  78. for(int i=1; i<index; ++i){
  79. before = before->next;
  80. index--;
  81. }
  82. Node *node = before->next;
  83. before->next = before->next->next;
  84. Node_destroy(node);
  85. }
  86. }
  87. }
  88. void List_print(List *list){
  89. if(list != NULL){
  90. printf("[");
  91. Node *node = list->first;
  92. if(node->next == NULL){
  93. printf("NULL");
  94. }
  95. while(node->next != NULL){
  96. printf("%s", node->data);
  97. node = node->next;
  98. if (node->next != NULL){
  99. printf(", ");
  100. }
  101. }
  102. printf("]\n");
  103. }
  104. }
  105. int sell_item(char *item){
  106. int current_wallet = 0;
  107. if(strcmp(item, "potatoes") == 0){
  108. printf("You've earn 50 wallet!\n");
  109. current_wallet = 50;
  110. return current_wallet;
  111. }else if(strcmp(item, "grape") == 0){
  112. printf("You've earn 100 wallet!\n");
  113. current_wallet = 100;
  114. return current_wallet;
  115. }else if(strcmp(item, "apel") == 0){
  116. printf("You've earn 60 wallet!\n");
  117. current_wallet = 60;
  118. return current_wallet;
  119. }
  120. }
  121. void message(){
  122. printf("You don't have enough wallet!\n");
  123. }
  124. void success_msg(char *item){
  125. printf("%s added to the list!\n", item);
  126. }
  127. void menu(){
  128. printf("+-----------------------+\n");
  129. printf("MASSIVE BLACKMARKET STORE\n");
  130. printf("+-----------------------+\n");
  131. printf("1. buy flag (200 Diamond)\n");
  132. printf("2. buy potatoes (50 Wallet)\n");
  133. printf("3. buy grape (100 Wallet)\n");
  134. printf("4. buy apel (60 Wallet)\n");
  135. printf("5. show items\n");
  136. printf("6. sell items\n");
  137. printf("7. check wallet\n");
  138. printf("8. check diamond\n");
  139. printf("9. get free diamond!\n");
  140. printf("10. exit\n");
  141. }
  142. int main(){
  143. List *l = List_create();
  144. char buffer[BUFSIZE+1], ans[2];
  145. int wallet = 300, temp, try = 0;
  146. LOOP:while(1){
  147. menu();
  148. printf(">>");
  149. fflush(stdout);
  150. scanf("%s", &ans);
  151. if(strcmp(ans, "1") == 0){
  152. if(diamond < 200){
  153. printf("Not enough diamond to buy flag!\nnothing to add.\n");
  154. goto LOOP;
  155. }else if(diamond >= 200){
  156. diamond = diamond - 200;
  157. List_append(l, "flag");
  158. success_msg("flag");
  159. goto LOOP;
  160. }
  161. }else if(strcmp(ans, "2") == 0){
  162. if(wallet < 50){
  163. message();
  164. goto LOOP;
  165. }
  166. success_msg("potatoes");
  167. wallet = wallet - 50;
  168. List_append(l, "potatoes");
  169. goto LOOP;
  170. }else if(strcmp(ans, "3") == 0){
  171. if(wallet < 100){
  172. message();
  173. goto LOOP;
  174. }
  175. success_msg("grape");
  176. wallet = wallet - 100;
  177. List_append(l, "grape");
  178. goto LOOP;
  179. }else if(strcmp(ans, "4") == 0){
  180. if(wallet < 60){
  181. message();
  182. goto LOOP;
  183. }
  184. success_msg("apel");
  185. wallet = wallet - 60;
  186. List_append(l, "apel");
  187. goto LOOP;
  188. }else if(strcmp(ans, "5") == 0){
  189. printf("\nYour Items :\n");
  190. List_print(l);
  191. goto LOOP;
  192. }else if(strcmp(ans, "6") == 0){
  193. getchar();
  194. printf("What items you want to sell ?\n");
  195. fflush(stdout);
  196. fgets(buffer, sizeof(buffer), stdin);
  197. printf("You sell : ");
  198. printf(buffer);
  199. strtok(buffer, "\n");
  200. int check = List_find(l, buffer);
  201. if(check == (-1)){
  202. printf("Not Found!\n");
  203. exit(1);
  204. }else{
  205. List_remove(l, List_find(l, buffer));
  206. wallet = wallet + sell_item(buffer);
  207. }
  208. }else if(strcmp(ans, "7") == 0){
  209. printf("\nYour Wallet : %d\n", wallet);
  210. goto LOOP;
  211. }else if(strcmp(ans, "8") == 0){
  212. printf("\nYour diamond : %d\n", diamond);
  213. goto LOOP;
  214. }else if(strcmp(ans, "9") == 0){
  215. try += 1;
  216. srand(time(NULL));
  217. int r = rand() % 10;
  218. if(try <= 4){
  219. printf("You get %d diamond\n", r);
  220. diamond = diamond + r;
  221. goto LOOP;
  222. }
  223. printf("No diamond for you!\n");
  224. goto LOOP;
  225. }else if(strcmp(ans, "10") == 0){
  226. printf("Thankyou for visiting!");
  227. exit(0);
  228. }else{
  229. fprintf(stderr, "Bad Request!\n");
  230. exit(0);
  231. }
  232. }
  233. }