학부 시절.. 지금으로부터 거의 10년전 유닉스 프로그래밍 수업을 들었었다.
지금 지나고 생각해보면 그걸 왜 들었는지 한심하기도 하지만..(학교에서 들은 수업을 현업에서 쓸일이 없으니..)
당시 과제중에 ls 명령어를 자기만의 방식으로 만들어오라고 했던 것이 있다.
그래서 나는 다음과 같이 너무 단순하게 만들어서 냈었다.
/*********************************************************************
* myls.
* USAGE : myls [directory]
*
*********************************************************************/
#include <dirent.h> /*to use dir entry*/
#include <stdio.h> /*to use standard IO*/
#include <sys/stat.h> /*to use stat*/
#include <string.h>
int PutStat(struct stat statinfo){
int type=0; /*0:file, 1:directory*/
char szPerm[8][4]={"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
/*check file type*/
if(S_ISLNK(statinfo.st_mode))
printf("%-18s","Symbolic Link");
else if(S_ISBLK(statinfo.st_mode))
printf("%-18s","Block Special");
else if(S_ISCHR(statinfo.st_mode))
printf("%-18s","Character Special");
else if(S_ISDIR(statinfo.st_mode)){
printf("%-18s","Directory");type=1;}/*to call recursive function*/
else if(S_ISREG(statinfo.st_mode))
printf("%-18s","Regular");
else if(S_ISSOCK(statinfo.st_mode))
printf("%-18s","Socket");
else if(S_ISFIFO(statinfo.st_mode))
printf("%-18s","FIFO");
else
printf("%-18s","Unknown type");
/*check File size*/
printf("%12ld\t",statinfo.st_size);
/*check User,Group ids*/
printf("%4d %4d\t",statinfo.st_uid,statinfo.st_gid);
/*check ownership lower 9bits are ownership information*/
printf("%s%s%s\n",szPerm[(statinfo.st_mode>>6)&0x7],szPerm[(statinfo.st_mode>>3)&0x7],szPerm[(statinfo.st_mode&0x7)]);
return type;
}
/*recursive fuction for directory*/
void list(char *prefix,char *szDirPath){
DIR *pDir;
struct dirent *pEntry;
struct stat stFile;
char temp[30];
/*Open driectory*/
pDir=opendir(szDirPath);
while((pEntry=readdir(pDir)) != NULL){
/*If we can't get file stat, print error message and exit 1*/
if(stat(pEntry->d_name,&stFile) < 0){
printf("can't get stat of %s.",pEntry->d_name);
exit(1);
}
if(prefix){/*incase of subdirectory*/
strcpy(temp,prefix);
strcat(temp,"/");
strcat(temp,pEntry->d_name);
printf("%-30s",temp);
}
else
printf("%-30s",pEntry->d_name);
/*in case of subdirectory*/
if(PutStat(stFile)&&(strcmp(pEntry->d_name,".")!=0)&&(strcmp(pEntry->d_name,"..")!=0))
list(pEntry->d_name,pEntry->d_name);/*recursive calling*/
}
}
int main(int argc, char *argv[]){
char *szDirPath;
/*If argument is not specified, argument will be replaced [current directory]*/
if(argc<2)
szDirPath=".";
else
szDirPath=argv[1];
list(NULL,szDirPath);
return 0;/*No error*/
}
그리고나서, 해당 구현하는 것에 대한 문제가 기말고사 시험에 나왔다.
아예 시험문제가 다음과 같았다.
1. ls 명령어를 구현하시오.[25점]
밑도 끝도 없는... 이런 시험이 나오게 된 이유가 뭘까 그때는 몰랐지만... 일단 그 해당 수업을 담당하셨던 분이 교수가 아니었고, 다른 학교에서 온 박사과정의 박사학생이었고...프로그래밍을 할줄 모르시던 분이었다. 그래서 수업의 대부분을 발표 수업만 했었던것 같다.
답을 저기 위에서 했던거 처럼 했더니. 0점을 주는게 아닌가??? =_=
가서 이유를 따졌더니, 이 코드는 실행되는 코드가 아닐거라고 .. =_= 그래서 그 앞에서 코드를 그대로 짜서 실행시켜서 보여줬었는데.... 위에 굵은 부분의 용도가 뭔지 몰라서 0점 처리를 했다고....
그분은 지금쯤 뭘하고 계실까???
'코딩하고 > C,C++' 카테고리의 다른 글
| OS - Page Fault Simulation(FIFO,LRU,Optimal) (0) | 2012.10.21 |
|---|---|
| 자료구조 - 계산기 만들기 (10) | 2012.10.12 |
| C언어로 만든 객체 (0) | 2012.10.11 |
| 자료구조 - 다항식 연산하기 (2) | 2012.10.10 |
| 조건문 제거하기 - Function Table사용법 (0) | 2012.08.11 |