teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

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

2021/06/08 03:37

投稿

takumi-33
takumi-33

スコア19

title CHANGED
File without changes
body CHANGED
@@ -7,19 +7,14 @@
7
7
  「コマンドライン引数を2 つ持ち,第1 引数で指定した環境変数の値を第2 引数で指定した値に設定」をどのようにすれば良いのか分かりません。
8
8
  何か分かることがありましたら、コメント頂けると幸いです。
9
9
 
10
- コンパイル時エラー
11
- ```
12
- prog5-1.c:4:5: error: conflicting types for ‘setenv’
10
+ また、交換できているかの確認方法などはあるのでしょうか?
13
- int setenv(char *name, char *value, int overwite){
11
+ シェルスクリプトの内容だけが表示されているので、環境変数が変更されているのか分かりません。
14
- ^
15
- In file included from prog5-1.c:2:0:
16
- /usr/include/stdlib.h:583:12: note: previous declaration of ‘setenv’ was here
17
- extern int setenv (const char *__name, const char *__value, int __replace)
18
- ```
19
12
 
20
13
  実行結果
21
14
  ```
15
+ 0
16
+ 3410
22
- Segmentation fault (core dumped)
17
+ ./testscript
23
18
  ```
24
19
 
25
20
  ### 該当のソースコード
@@ -29,11 +24,15 @@
29
24
  #include <stdio.h>
30
25
  #include <stdlib.h>
31
26
 
32
- int setenv(char *name, char *value, int overwite){
27
+ extern char **environ;
33
28
 
29
+ int main(int argc, const char *argv[], const char *envp[]) {
30
+
34
31
  char *command = "./testscript";
35
32
  int res;
36
33
 
34
+ setenv(argv[1], argv[2], 1);
35
+
37
36
  res = system(command);
38
37
 
39
38
  return 0;

1

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

2021/06/08 03:37

投稿

takumi-33
takumi-33

スコア19

title CHANGED
File without changes
body CHANGED
@@ -9,13 +9,12 @@
9
9
 
10
10
  コンパイル時エラー
11
11
  ```
12
- prog5-1.c: In functionmain:
12
+ prog5-1.c:4:5: error: conflicting types for setenv
13
- prog5-1.c:12:3: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [enabled by default]
13
+ int setenv(char *name, char *value, int overwite){
14
- strcpy(argv[1],envp);
15
- ^
14
+ ^
16
- In file included from prog5-1.c:3:0:
15
+ In file included from prog5-1.c:2:0:
17
- /usr/include/string.h:125:14: note: expected ‘const char * __restrict__’ but argument is of type const char **’
16
+ /usr/include/stdlib.h:583:12: note: previous declaration of ‘setenv’ was here
18
- extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
17
+ extern int setenv (const char *__name, const char *__value, int __replace)
19
18
  ```
20
19
 
21
20
  実行結果
@@ -29,17 +28,12 @@
29
28
  ```c言語
30
29
  #include <stdio.h>
31
30
  #include <stdlib.h>
32
- #include <string.h>
33
31
 
34
- extern char **environ;
32
+ int setenv(char *name, char *value, int overwite){
35
33
 
36
- int main(int argc, char *argv[], const char *envp[]){
37
-
38
34
  char *command = "./testscript";
39
35
  int res;
40
36
 
41
- strcpy(argv[1],envp);
42
-
43
37
  res = system(command);
44
38
 
45
39
  return 0;