4-1 과제-2
ch_mod 권한 변경
- 에러 예외처리
#include <stdio.h>
#include <sys/types.h> //chmod()
#include <sys/stat.h> //chmod() stat()
#include <string.h> //strcpy()
#include <errno.h>
int main(int argc, char** argv)
{
/* if (argc != 3) {
printf("유효하지 않은 인자입니다.\n");
return 0;
}*/
struct stat buf;
if (stat(argv[1], &buf) == -1) {
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
if (errno == ENOENT) {
printf("해당하는 파일이 없습니다.\n");
return 0;
}
else if (errno == EFAULT) {
printf("빈 문자열 입력.\n");
return 0;
}
else {
return 0;
}
}
mode_t t = 0;
char mode[9];
strcpy(mode, argv[2]);
if (mode[0] == 'r')
t += S_IRUSR;
if (mode[1] == 'w')
t += S_IWUSR;
if (mode[2] == 'x')
t += S_IXUSR;
if (mode[3] == 'r')
t += S_IRGRP;
if (mode[4] == 'w')
t += S_IWGRP;
if (mode[5] == 'x')
t += S_IXGRP;
if (mode[6] == 'r')
t += S_IROTH;
if (mode[7] == 'w')
t += S_IWOTH;
if (mode[8] == 'x')
t += S_IXOTH;
for (int i = 0; i < 9; i++) {
if ((mode[i] != 'r') && (mode[i] != 'w') &&
(mode[i] != 'x') && (mode[i] != '-')) {
printf("권한을 잘 못 입력하였습니다.\n");
return 0;
}
}
if (chmod(argv[1], t) == -1) {
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
if (errno == EPERM) {
printf("변경 권한이 없습니다.\n");
return 0;
}
else {
return 0;
}
}
else {
printf("mode change %s\n", argv[2]);
}
return 0;
}
4-2 과제-1
dirinfo 디렉토리 정보 출력
- 반복된 반복문 수정
- file[1000][256] 말고 이름 바로 받아서 출력
- scanf, fgets 중 하나만 사용
- 에러 예외처리
#include <stdio.h>
#include <string.h> //strlen()
#include <stdio_ext.h> //__fpurge()
#include <stdlib.h> //malloc() free()
#include <unistd.h> //chdir()
#include <dirent.h> //opendir()
#include <sys/stat.h> //stat()
#include <errno.h>
enum { MAXLENGTH = 256 };
int Range(int n);
int Change(char* path);
int Read(char* path);
int Range(int n)
{
if (n != 1 && n != 2 && n != 3) {
printf("해당하는 숫자 범위를 넘어갔습니다.\n");
return -1;
}
return 0;
}
int Change(char* path)
{
printf("input path: ");
scanf("%s", path);
if (strlen(path) > MAXLENGTH) {
printf("디렉토리명이 너무 깁니다. \n");
return 0;
}
if (chdir(path) == -1) {
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
if (errno == ENOENT) {
printf("해당하는 디렉토리가 없습니다.\n");
return -1;
}
else if (errno == ENOTDIR) {
printf("디렉토리가 아닌 파일을 입력하였습니다.\n");
return 0;
}
else {
return -1;
}
}
return 0;
}
int Read(char* path)
{
DIR* d = NULL;
struct dirent* s = NULL;
if ((d = opendir(path)) == NULL) {
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
if (errno == ENOENT) {
printf("디렉토리가 존재하지 않습니다.\n");
return -1;
}
else {
return -1;
}
}
int count = 0;
while ((s = readdir(d)) != NULL) {
struct stat buf;
if ((stat(s->d_name, &buf)) != -1) {
if (S_ISREG(buf.st_mode) != 0) {
strcat(s->d_name, "*");
printf("%-40s", s->d_name);
}
else if (S_ISDIR(buf.st_mode) != 0) {
strcat(s->d_name, "/");
printf("%-40s", s->d_name);
}
if (count % 5 == 0) {
printf("\n");
}
count++;
}
else {
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
if (errno == ENOENT) {
printf("해당하는 파일이 없습니다.\n");
return -1;
}
else {
return -1;
}
}
}
printf("\n");
if (closedir(d) == -1) {
fprintf(stderr, "closedir errno[%d] : %s\n", errno, strerror(errno));
if (errno == EBADF) {
return -1;
}
}
return 0;
}
int main()
{
int num = 0;
char* path;
if ((path = (char*)malloc(MAXLENGTH)) == NULL) {
fprintf(stderr, "errno[%d] : %s\n", errno, strerror(errno));
if (errno == ENOMEM) {
printf("Out of memory.\n");
return 0;
}
}
do {
printf("============================\n");
printf("1) 현재 작업 디렉토리 변경\n");
printf("2) 디렉토리 정보 출력\n");
printf("3) 종료\n");
printf("============================\n");
printf("select num: ");
scanf("%d", &num);
if (Range(num) != 0) {
printf("숫자를 다시 입력해주세요.\n");
}
switch(num) {
case 1:
{
if (Change(path) != 0) {
return 0;
}
}
break;
case 2:
{
if (Read(path) != 0) {
return 0;
}
}
break;
case 3:
{
printf("프로그램을 종료합니다.\n");
free(path);
return 0;
}
break;
}
} while(num != 3);
return 0;
}