質問編集履歴

2

頂いた回答からコードを書き直しました

2021/06/08 03:37

投稿

takumi-33
takumi-33

スコア19

test CHANGED
File without changes
test CHANGED
@@ -16,23 +16,9 @@
16
16
 
17
17
 
18
18
 
19
- コンパイル時エラー
19
+ また、交換できているかの確認方法などはあるのでしょうか?
20
20
 
21
- ```
22
-
23
- prog5-1.c:4:5: error: conflicting types for ‘setenv’
24
-
25
- int setenv(char *name, char *value, int overwite){
21
+ シェルスクリプトの内容だけが表示されているので、環境変数が変更されているのか分かりません。
26
-
27
- ^
28
-
29
- In file included from prog5-1.c:2:0:
30
-
31
- /usr/include/stdlib.h:583:12: note: previous declaration of ‘setenv’ was here
32
-
33
- extern int setenv (const char *__name, const char *__value, int __replace)
34
-
35
- ```
36
22
 
37
23
 
38
24
 
@@ -40,7 +26,11 @@
40
26
 
41
27
  ```
42
28
 
29
+ 0
30
+
31
+ 3410
32
+
43
- Segmentation fault (core dumped)
33
+ ./testscript
44
34
 
45
35
  ```
46
36
 
@@ -60,13 +50,21 @@
60
50
 
61
51
 
62
52
 
53
+ extern char **environ;
54
+
55
+
56
+
63
- int setenv(char *name, char *value, int overwite){
57
+ int main(int argc, const char *argv[], const char *envp[]) {
64
58
 
65
59
 
66
60
 
67
61
  char *command = "./testscript";
68
62
 
69
63
  int res;
64
+
65
+
66
+
67
+ setenv(argv[1], argv[2], 1);
70
68
 
71
69
 
72
70
 

1

頂いた回答からコードを書き直しました

2021/06/08 03:37

投稿

takumi-33
takumi-33

スコア19

test CHANGED
File without changes
test CHANGED
@@ -20,19 +20,17 @@
20
20
 
21
21
  ```
22
22
 
23
- prog5-1.c: In function ‘main’:
23
+ prog5-1.c:4:5: error: conflicting types for setenv
24
24
 
25
- prog5-1.c:12:3: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [enabled by default]
25
+ int setenv(char *name, char *value, int overwite){
26
26
 
27
- strcpy(argv[1],envp);
27
+ ^
28
28
 
29
- ^
29
+ In file included from prog5-1.c:2:0:
30
30
 
31
- In file included from prog5-1.c:3:0:
31
+ /usr/include/stdlib.h:583:12: note: previous declaration of ‘setenv’ was here
32
32
 
33
- /usr/include/string.h:125:14: note: expected ‘const char * __restrict__’ but argument is of type ‘const char **’
34
-
35
- extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
33
+ extern int setenv (const char *__name, const char *__value, int __replace)
36
34
 
37
35
  ```
38
36
 
@@ -60,25 +58,15 @@
60
58
 
61
59
  #include <stdlib.h>
62
60
 
63
- #include <string.h>
64
61
 
65
62
 
66
-
67
- extern char **environ;
68
-
69
-
70
-
71
- int main(int argc, char *argv[], const char *envp[]){
63
+ int setenv(char *name, char *value, int overwite){
72
64
 
73
65
 
74
66
 
75
67
  char *command = "./testscript";
76
68
 
77
69
  int res;
78
-
79
-
80
-
81
- strcpy(argv[1],envp);
82
70
 
83
71
 
84
72