Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 시스템 프로그래밍
- 5622
- File 조작
- Baekjoon
- IT
- Zero That Out
- BAKA
- c++
- 브런치
- 바샤
- 해바
- The Balance of the World
- For Beginners
- 1874
- 백준
- file IO
- c
- 4949
- system programming
- 균형잡힌 세상
- 10773
- 입력 버퍼
- Parenthesis
- 시프
- Process Communication
- process control
- 전자책
- QA
- LJESNJAK
- 2941
Archives
- Today
- Total
해바
Lab #7 본문
시스템 호출 getenv, setenv 등을 사용하여 환경 변수 PATH의 현재 값을 지우고 Lab #7이 저장된 directory 값으로 변경하는 프로그램을 작성하라.
// env.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char* argv[]) {
char* path, *cwd;
if(argc != 2) {
printf("usage: a.out <pathname>\n");
exit(1);
}
if((path = getenv(argv[1])) == NULL) {
printf("File does not exist.\n");
exit(1);
}
printf("BEFORE ENV : %s\n", path);
cwd = getcwd(NULL, BUFSIZ); // NULL일 경우 malloc처럼 동적할당해서 가져옴
if(!setenv(argv[1], cwd, 1)) {
path = getenv(argv[1]);
printf("\nSUCCESS\n\n");
}
else {
printf("\nFAIL\n\n");
exit(1);
}
printf("AFTER ENV : %s\n", path);
exit(0);
}
컴파일 :
%gcc env.c
실행 :
%./a.out PATH
Comments