質問編集履歴

2

質問の簡略化

2022/01/29 01:36

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- 端末情報のリセットについて
1
+ C言語でのLinuxの端末状態についての理解
test CHANGED
@@ -1,98 +1,6 @@
1
1
 
2
2
 
3
3
  ### 前提・実現したいこと
4
+ Linux, Unixにおいて、端末状態をリセットするというのはどういう意味なんでしょうか。C言語で端末状態をリセットする関数が存在するのでしょうか。
4
5
 
5
- とある問題集において、「キーボードからのシグナルを受け取ったら、端末属性をリセットし、値2を返して終了するようにプログラムを変更せよ」という文言がありました。
6
6
 
7
- Linux, Unixにおいて、「端末属性をリセット」するというのはどういう意味なんでしょうか。C言語で端末属性をリセットする関数が存在するのでしょうか。
8
-
9
- ```c
10
- #include <stdio.h>
11
- #include <termios.h>
12
- #include <fcntl.h>
13
- #include <string.h>
14
-
15
- #define ASK "Do you want another transaction"
16
- #define TRIES 3
17
- #define SLEEPTIME 2
18
- #define BEEP putchar('\a')
19
-
20
- int get_response(char *, int maxtries);
21
-
22
- main()
23
- {
24
- int response;
25
-
26
- tty_mode(0);
27
- set_cr_noecho_mode();
28
- set_nodelay_mode();
29
- response = get_response(ASK, TRIES);
30
- tty_mode(1);
31
-
32
- return response;
33
- }
34
-
35
- int get_response(char *question, int maxtries)
36
- {
37
- int input;
38
- printf("%s (y/n)?", question);
39
- fflush(stdout);
40
- while (1)
41
- {
42
- sleep(SLEEPTIME);
43
- input = tolower(get_ok_char());
44
- if (input == 'y')
45
- return 0;
46
- if (input == 'n')
47
- return 1;
48
- if (maxtries-- == 0)
49
- return 2;
50
- BEEP;
51
- }
52
- }
53
-
54
- get_ok_char()
55
- {
56
- int c;
57
- while ((c = getchar()) != EOF && strchr("yYnN", c) == NULL)
58
- ;
59
- return c;
60
- }
61
-
62
- set_cr_noecho_mode()
63
- {
64
- struct termios ttystate;
65
-
66
- tcgetattr(0, &ttystate);
67
- ttystate.c_lflag &= ~ICANON;
68
- ttystate.c_lflag &= ~ECHO;
69
- ttystate.c_cc[VMIN] = 1;
70
- tcsetattr(0, TCSANOW, &ttystate);
71
- }
72
-
73
- set_nodelay_mode()
74
- {
75
- int termflags;
76
- termflags = fcntl(0, F_GETFL);
77
- termflags != O_NDELAY;
78
- fcntl(0, F_SETFL, termflags);
79
- }
80
-
81
- tty_mode(int how)
82
- {
83
- static struct termios original_mode;
84
- static int original_flags;
85
- if (how == 0)
86
- {
87
- tcgetattr(0, &original_mode);
88
- original_flags = fcntl(0, F_GETFL);
89
- }
90
- else
91
- {
92
- tcsetattr(0, TCSANOW, &original_mode);
93
- fcntl(0, F_SETFL, original_flags);
94
- }
95
- }
96
- ```
97
- 補足情報として、この内容は「Unix/Linuxプログラミング理論と実践」という書籍をから引用しています。
98
-

1

質問の改変

2022/01/27 10:38

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,97 @@
2
2
 
3
3
  ### 前提・実現したいこと
4
4
 
5
- とある問題集において、「キーボードからのシグナルを受け取ったら、端末属性をリセットし、値2を返して終了するようにプログラムを書け」という文言がありました。
5
+ とある問題集において、「キーボードからのシグナルを受け取ったら、端末属性をリセットし、値2を返して終了するようにプログラムを変更せよ」という文言がありました。
6
6
 
7
7
  Linux, Unixにおいて、「端末属性をリセット」するというのはどういう意味なんでしょうか。C言語で端末属性をリセットする関数が存在するのでしょうか。
8
8
 
9
+ ```c
10
+ #include <stdio.h>
11
+ #include <termios.h>
12
+ #include <fcntl.h>
13
+ #include <string.h>
14
+
15
+ #define ASK "Do you want another transaction"
16
+ #define TRIES 3
17
+ #define SLEEPTIME 2
18
+ #define BEEP putchar('\a')
19
+
20
+ int get_response(char *, int maxtries);
21
+
22
+ main()
23
+ {
24
+ int response;
25
+
26
+ tty_mode(0);
27
+ set_cr_noecho_mode();
28
+ set_nodelay_mode();
29
+ response = get_response(ASK, TRIES);
30
+ tty_mode(1);
31
+
32
+ return response;
33
+ }
34
+
35
+ int get_response(char *question, int maxtries)
36
+ {
37
+ int input;
38
+ printf("%s (y/n)?", question);
39
+ fflush(stdout);
40
+ while (1)
41
+ {
42
+ sleep(SLEEPTIME);
43
+ input = tolower(get_ok_char());
44
+ if (input == 'y')
45
+ return 0;
46
+ if (input == 'n')
47
+ return 1;
48
+ if (maxtries-- == 0)
49
+ return 2;
50
+ BEEP;
51
+ }
52
+ }
53
+
54
+ get_ok_char()
55
+ {
56
+ int c;
57
+ while ((c = getchar()) != EOF && strchr("yYnN", c) == NULL)
58
+ ;
59
+ return c;
60
+ }
61
+
62
+ set_cr_noecho_mode()
63
+ {
64
+ struct termios ttystate;
65
+
66
+ tcgetattr(0, &ttystate);
67
+ ttystate.c_lflag &= ~ICANON;
68
+ ttystate.c_lflag &= ~ECHO;
69
+ ttystate.c_cc[VMIN] = 1;
70
+ tcsetattr(0, TCSANOW, &ttystate);
71
+ }
72
+
73
+ set_nodelay_mode()
74
+ {
75
+ int termflags;
76
+ termflags = fcntl(0, F_GETFL);
77
+ termflags != O_NDELAY;
78
+ fcntl(0, F_SETFL, termflags);
79
+ }
80
+
81
+ tty_mode(int how)
82
+ {
83
+ static struct termios original_mode;
84
+ static int original_flags;
85
+ if (how == 0)
86
+ {
87
+ tcgetattr(0, &original_mode);
88
+ original_flags = fcntl(0, F_GETFL);
89
+ }
90
+ else
91
+ {
92
+ tcsetattr(0, TCSANOW, &original_mode);
93
+ fcntl(0, F_SETFL, original_flags);
94
+ }
95
+ }
96
+ ```
97
+ 補足情報として、この内容は「Unix/Linuxプログラミング理論と実践」という書籍をから引用しています。
98
+