BorrowTheProgrammer

lab6_os

Dec 12th, 2021
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <setjmp.h>
  5. #include <string.h>
  6. typedef void (*sighandler)(int);
  7. void brake(int);
  8. sigjmp_buf pos;
  9. static char digit[3];
  10. static int j;
  11. static char* who[] = { "Man: ", "Man: ", "Com: ", "Com: ", "Bye.\n" };
  12. static int man_res = 0, com_res = 0;
  13. char* result_to_string(int res, int *num_size);
  14. char* rewrite_1(char* str, size_t size);
  15. char* top(int res1, int res2, int *size_res);
  16. int main(int argc, char** argv) {
  17.         char* result = NULL;
  18.         int timeout = 0, size_win_result = 0;
  19.         j = 0;
  20.         if (argc < 2) {
  21.                 write(2, "Usage: dice timeout\n", 20);
  22.                 exit(0);
  23.         }
  24.         timeout = atoi(argv[1]);
  25.         if (timeout < 1) {
  26.                 timeout = 1;
  27.         }
  28.         digit[0] = '1' + getpid() % 6;
  29.         digit[1] = '\b';
  30.         digit[2] = '\n';
  31.         j = sigsetjmp(pos, SIGINT);  //  сохраняется состояние SIGINT
  32.         signal(SIGALRM, (sighandler) brake);
  33.         signal(SIGINT, (sighandler) brake);
  34.         if (j == 2 || j == 3) {
  35.                 alarm(timeout);
  36.                 write(1, digit, 2);
  37.         }
  38.         else {
  39.                 alarm(0);}
  40.         if (j < 4) {
  41.                 write(0, who[j], 5);
  42.                 while (j < 4) {
  43.                         write(0, digit, 2);
  44.                         digit[0] = (digit[0] < '6') ? digit[0] + 1 : '1';
  45.                         usleep(50000);}
  46.         } else {
  47.                 result = top(man_res, com_res, &size_win_result);
  48.                 if (result == NULL) {
  49.                         write(0, "Memory error\n", 13);
  50.                         exit(0);}
  51.                 write(1, result, size_win_result);
  52.                 write(1, "\n", 1);
  53.                 if (man_res > com_res){
  54.                         write(1, "man won", 7);}
  55.                 if (man_res < com_res) {
  56.                         write(1, "computer won", 12);}
  57.                 write(1, "\n", 1);
  58.                 write(1, who[j], 5); }
  59.         signal(SIGINT, SIG_DFL);
  60.         exit(0);}
  61. void brake(int c) {
  62.         if (j < 2) {
  63.                 man_res += digit[0] - '0';
  64.         } else {
  65.                 com_res += digit[0] - '0';
  66.         }
  67.         write(1, digit, 3);
  68.         siglongjmp(pos, ++j);
  69.         return;
  70. }
  71. char* top(int res1, int res2, int *size_res) {
  72.         char* res = NULL;
  73.         if (res1 > res2) {
  74.                 return result_to_string(res1, size_res);
  75.         } else if (res2 > res1) {
  76.                 return result_to_string(res2, size_res);
  77.         } else {
  78.                 res = (char*)malloc(4 * sizeof(char));
  79.                 strcpy(res, "draw");
  80.                 *size_res = 4;
  81.                 return res;
  82.         }
  83. }
  84. char* rewrite_1(char* str, size_t size) {
  85.         char* new_str = (char*)malloc((size + 1) * sizeof(char));
  86.         size_t i;
  87.         for (i = size; i > 0; i--) {
  88.                 new_str[i] = str[i - 1];
  89.         }
  90.         free(str);
  91.         return new_str;
  92. }
  93. char* result_to_string(int res, int *num_size) {
  94.         char *str = (char*)malloc(sizeof(char));
  95.         size_t i = 1;
  96.         while (res / 10 != 0) {
  97.                 str[0] = (res % 10) + '0';
  98.                 str = rewrite_1(str, i);
  99.                 i++;
  100.                 res/=10;
  101.                 *num_size += 1;
  102.         }
  103.         str[0] = res + '0';
  104.         *num_size += 1;
  105.         return str;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment