Guest User

Untitled

a guest
Sep 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <time.h>
  8.  
  9. char greeting[] = "Hello there\n1. Receive wisdom\n2. Add wisdom\nSelection >";
  10. char prompt[] = "Enter some wisdom\n";
  11. char pat[] = "Achievement unlocked!\n";
  12. char secret[] = "secret key";
  13.  
  14. int infd = 0; /* stdin */
  15. int outfd = 1; /* stdout */
  16.  
  17. #define DATA_SIZE 128
  18.  
  19. typedef struct _WisdomList {
  20. struct _WisdomList *next;
  21. char data[DATA_SIZE];
  22. } WisdomList;
  23.  
  24. struct _WisdomList *head = NULL;
  25.  
  26. typedef void (*fptr)(void);
  27.  
  28. void write_secret(void) {
  29. write(outfd, secret, sizeof(secret));
  30. return;
  31. }
  32.  
  33. void pat_on_back(void) {
  34. write(outfd, pat, sizeof(pat));
  35. return;
  36. }
  37.  
  38. void get_wisdom(void) {
  39. char buf[] = "no wisdom\n";
  40. if(head == NULL) {
  41. write(outfd, buf, sizeof(buf)-sizeof(char));
  42. } else {
  43. WisdomList *l = head;
  44. while(l != NULL) {
  45. write(outfd, l->data, strlen(l->data));
  46. write(outfd, "\n", 1);
  47. l = l->next;
  48. }
  49. }
  50. return;
  51. }
  52.  
  53. void put_wisdom(void) {
  54. char wis[DATA_SIZE] = {0};
  55. int r;
  56.  
  57. r = write(outfd, prompt, sizeof(prompt)-sizeof(char));
  58. if(r < 0) {
  59. return;
  60. }
  61.  
  62. r = (int)gets(wis);
  63. if (r == 0)
  64. return;
  65.  
  66. WisdomList *l = malloc(sizeof(WisdomList));
  67.  
  68. if(l != NULL) {
  69. memset(l, 0, sizeof(WisdomList));
  70. strcpy(l->data, wis);
  71. if(head == NULL) {
  72. head = l;
  73. } else {
  74. WisdomList *v = head;
  75. while(v->next != NULL) {
  76. v = v->next;
  77. }
  78. v->next = l;
  79. }
  80. }
  81.  
  82. return;
  83. }
  84.  
  85. fptr ptrs[3] = { NULL, get_wisdom, put_wisdom };
  86.  
  87. int main(int argc, char *argv[]) {
  88.  
  89. while(1) {
  90. char buf[1024] = {0};
  91. int r;
  92. fptr p = pat_on_back;
  93. r = write(outfd, greeting, sizeof(greeting)-sizeof(char));
  94. if(r < 0) {
  95. break;
  96. }
  97. r = read(infd, buf, sizeof(buf)-sizeof(char));
  98. if(r > 0) {
  99. buf[r] = '\0';
  100. int s = atoi(buf);
  101. fptr tmp = ptrs[s];
  102. tmp();
  103. } else {
  104. break;
  105. }
  106. }
  107.  
  108. return 0;
  109. }
Add Comment
Please, Sign In to add comment