#include
#include
#include
#include
#include
#define MAX_PATH 5
main(int argc, char **argv)
{
char line[256], *pathPtr[MAX_PATH];
char *tempLine[MAX_PATH];
char tempLine2[30];
// read ".myshell" file, and adjust pathPtr.;
int fd; // ".myshell" file descriptor
char buf[1];
char tempPtr[20];
int i;
int rd_cnt;
int j=0;
int k=0;
int path=0;
if((fd=open(".myshell",O_RDONLY))<0)<br />
{
perror(".myshell error\n");
exit(1);
}
lseek(fd,5,SEEK_SET);
while(read(fd,buf,1)>0)
{
if(buf[0]==':')
{
pathPtr[path]=strdup(tempPtr);
path++;
memset(tempPtr,0,sizeof(tempPtr));
//strcpy(tempPtr,"");
}
if(buf[0]!=':')
{
//printf("%c",buf[0]);
strncat(tempPtr, buf,1);
}
}
printf("file read end\n");
/*
printf("%s\n",pathPtr[0]);
printf("%s\n",pathPtr[1]);
*/
while(1)
{
for(k=0; k
{
tempLine[k]=strdup(pathPtr[k]);
}
/*
for(k=0; k
{
printf("%s\n",tempLine[k]);
}
*/
printf("COMMAND : ");
fgets(line, 255, stdin);
//extract command in "Line";
for(k=0; k
{
strcpy(tempLine2, tempLine[k]);
strcat(tempLine2,"/");
strcat(tempLine2, line);
tempLine[k] = strdup(tempLine2);
//printf("%s\n",tempLine[k]);
}
//test//
/*
if(access("/bin/rm", F_OK)!=0)
{
printf("X\n");
}
else
{
printf("O\n");
}
exit(1);
*/
for(k=0; k
{
if(access(tempLine[k], F_OK)!= 0)
{
printf("%s [X] \n",tempLine[k]);
}
else
{
printf("%s [O]\n",tempLine[k]);
}
}
exit(0);
///////////////////////////////////////////////////////////////////
À̺κÐÀÔ´Ï´Ù. test·Î ÁÖ¼®ÃijõÀº °÷¿¡
access·Î Á÷Á¢ "/bin/rm" ÀÌ·¸°Ô °Ë»ç¸¦ Çغ¸¸é...
O°¡ Ãâ·ÂÀÌ µË´Ï´Ù.. ÆÄÀÏÀÌ Á¸ÀçÇÑ´Ù´Â ¶æÀÌÁö¿ä...
ÇÏÁö¸¸ Á¦°¡ .myshell¿¡ ÀÖ´Â ³»¿ëÀ» ¹Þ¾Æ¼ Ãâ·ÂÀ» ÇÏ°Ô µÇ¸é
%s·Î Âï¾îº¸¸é /bin/rm ÀÌ·¸°Ô ³ª¿À´Âµ¥
Á¤ÀÛ.. access°¡ Á¤»óµ¿ÀÛÀ» ÇÏÁö ¾Ê½À´Ï´Ù...
¾îµð¼ À߸øµÈ°ÇÁö... ³Ê¹« ÇظŰíÀÖ½À´Ï´Ù..
Á¶¾ðÁ» ¹ÞÀ» ¼ö ÀÖÀ»±î¿ä?..
Ȥ½Ã¸ô¶ó¼ ÆÄÀÏ÷ºÎ Çص帳´Ï´Ù.
.myshell ÀÇ ³»¿ëÀº PATH=/bin:/usr/bin: ÀÔ´Ï´Ù...
lseek·Î PATH= ºÎºÐ Åë°úÇÏ°í /binºÎÅÍ ¹Þ¾Æ¿É´Ï´Ù..
//////////////////////////////////////////////////////
// find command in pathPtr path;
if(1)
{
if(fork()==0)
{
//execute command with option;
}
//wait until child terminates;
else
{
printf("Command not found\n");
}
}
}
} | |
|
¹®ÀÚ¿¿¡¼ ¹º°¡ ÀÌ»óÇÑ°Å°°Àºµ¥..
memsetÀ¸·Î ´Ù 0À¸·Î ÃʱâÈÇØ¢Z´Âµ¥...
ÀÌ°ªÀ̶û char¼±¾ðÇØÁÙ¶§ ÃʱâÈÇØÁÖ´Â °ªÀ̶û ´Ù¸¥°¡ºÁ¿ä