¡¡¡¡½ø³Ì¹ÜÀíÏà¹ØµÄϵͳµ÷ÓÃÖ®Èý
À×Õò (
[email protected])
ÕâÊDZ¾×¨À¸Öнø³ÌÏà¹ØµÄϵͳµ÷ÓõÄ×îºóһƪ£¬ÓÃ2¸öʵÀýÑÝʾÁËÒÔÍùѧϰµÄÄÚÈÝ¡£ÆäÒ»ÊÇMini Shell£¬·Â³£ÓõÄBash¶ø×ö£¬µ«¶ÔÆä×÷ÁË´ó´ó¼ò»¯£»Æä¶þÊÇÒ»¸öDaemon³ÌÐò£¬¿ÉÒÔʹ¶ÁÕßÒ»¿ú·þÎñÆ÷±à³ÌµÄ¶ËÄß¡£
1.13 Shell
¶ÔLinux²»ÊÇÌ«Ä°ÉúµÄ¶ÁÕ߶¼Ó¦¸Ã¶ÔShellÓÐÒ»¶¨µÄÁ˽⣬¾ÍÊÇÕâ¸ö³ÌÐòÔÚÎÒÃǵǽºó×Ô¶¯Ö´ÐУ¬´òÓ¡³öÒ»¸ö$·ûºÅ£¬È»ºóµÈ´ýÎÒÃÇÊäÈëÃüÁî¡£LinuxÏÂ×î³£ÓõÄShellÓ¦ÓóÌÐòÊÇBash£¬¾ø´ó²¿·ÖLinux·¢ÐаæĬÈÏ°²×°µÄ¶¼ÊÇËü¡£ÏÂÃæÎÒÃÇÒ²À´Ç×ÊÖ±àдһ¸öShell³ÌÐò£¬Õâ¸öShellÔ¶Ô¶²»ÈçBash¸´ÔÓ£¬µ«Ò²ÄÜÂú×ãÎÒÃÇÒ»°ãµÄʹÓã¬ÏÂÃ棬ÎÒÃǾͿªÊ¼¡£
Ê×ÏÈ£¬¸øÕâ¸öShellÈ¡Ò»¸öÃû×Ö£¬²»·Á¾Í½Ð×öMini Shell¡£
LinuxϵͳµÄÃüÁî·ÖΪÄÚ²¿ÃüÁîºÍÍⲿÃüÁîÁ½ÖÖ£¬ÄÚ²¿ÃüÁîÓÉShell³ÌÐòʵÏÖ£¬Èçcd¡¢echoµÈ£¬LinuxµÄÄÚ²¿ÃüÁîÊýÁ¿ÓÐÏÞ£¬¶øÇÒ¾ø´ó²¿·Ö¶¼ºÜÉÙÓõ½¡£¶øÿһ¸öLinuxÍⲿÃüÁÊÇÒ»¸öµ¥¶ÀµÄÓ¦ÓóÌÐò£¬ÎÒÃǷdz£ÊìϤµÄls¡¢cpµÈ¾ø´ó¶àÊýÃüÁÊÇÍⲿÃüÁÕâЩÃüÁÒÔ¿ÉÖ´ÐÐÎļþµÄÐÎʽ´æÔÚ£¬¾ø´ó²¿·Ö·ÅÔÚĿ¼/binºÍ/sbinÖС£ÕâÑùÒ»À´£¬ÎÒÃDZà³ÌµÄÄѶȾͿÉÒÔ´ó´óϽµÁË£¬ÎÒÃÇÖ»ÐèҪʵÏÖºÜÓÐÏÞµÄÄÚ²¿ÃüÁ¶ÔÓÚÆäËüµÄÊäÈ룬ͳͳµ±×÷Ó¦ÓóÌÐòÀ´Ö´Ðм´¿É¡£
ΪÁ˼òµ¥Ã÷ÁËÆð¼û£¬Mini ShellֻʵÏÖÁË2¸öÄÚ²¿ÃüÁ
1¡¢cd ÓÃÓÚÇл»Ä¿Â¼£¬ºÍÎÒÃÇÊìϤµÄÃüÁîcdÀàËÆ£¬³ýÁËûÓÐÄÇô¶àµÄ¸½¼Ó¹¦ÄÜ¡£
2¡¢quit ÓÃÓÚÍ˳öMini Shell¡£
ÏÂÃæÊdzÌÐòÇåµ¥£º
1: /* mshell.c */
2: #include
1: #include
3: #include
4: #include
5: #include
6: #include
7:
9: void do_cd(char *argv[]);
10: void execute_new(char *argv[]);
11:
12: main()
13: {
14: char *cmd=(void *)malloc(256*sizeof(char));
15: char *cmd_arg[10];
16: int cmdlen,i,j,tag;
17:
18: do{
19: /* ³õʼ»¯cmd */
20: for(i=0;i<255;i++) cmd[i]='\0';
21:
22: printf("-=Mini Shell=-* ");
23: fgets(cmd,256,stdin);
24:
25: cmdlen=strlen(cmd);
26: cmdlen--;
27: cmd[cmdlen]='\0';
28:
29£º /* °ÑÃüÁîÐзֽâΪָÕëÊý×écmd_arg */
30£º for(i=0;i<10;i++) cmd_arg[i]=NULL;
31£º i=0; j=0; tag=0;
32£º while(i
64£º
65£º /* ʵÏÖcdµÄ¹¦ÄÜ */
66£º void do_cd(char *argv[])
67£º {
68£º if(argv[1]!=NULL){
69£º if(chdir(argv[1])<0)
70£º switch(errno){
71£º case ENOENT:
72£º printf("DirectorY NOT FOUND\n");
73£º break;
74£º case ENOTDIR:
75£º printf("NOT A DIRECTORY NAME\n");
76£º break;
77£º case EACCES:
78£º printf("YOU DO NOT HAVE RIGHT TO Access\n");
79£º break;
80£º default:
81£º printf("SOME ERROR HAPPENED IN CHDIR\n");
82£º }
83£º }
84£º
85£º }
86£º
87£º /* Ö´ÐÐÍⲿÃüÁî»òÓ¦ÓóÌÐò */
88£º void execute_new(char *argv[])
89£º {
90£º pid_t pid;
91£º
92£º pid=fork();
93£º if(pid<0){
94£º printf("SOME ERROR HAPPENED IN FORK\n");
95£º exit(2);
96£º }else if(pid==0){
97£º if(execvp(argv[0],argv)<0)
98£º switch(errno){
99£º case ENOENT:
100£º printf("COMMAND OR FILENAME NOT FOUND\n");
101£º break;
102£º case EACCES:
103£º printf("YOU DO NOT HAVE RIGHT TO ACCESS\n");
104£º break;
105£º default:
106£º printf("SOME ERROR HAPPENED IN EXEC\n");
107£º }
108£º exit(3);
109£º }else
110£º wait(NULL);
111£º }
Õâ¸ö³ÌÐòÉÔÉÔÓе㳤£¬ÎÒÃÇÀ´¶ÔËü×÷Ò»ÏÂÏêϸµÄ½âÊÍ£º
º¯Êýmain£º
14ÐУº¶¨Òå×Ö·û´®cmd£¬ÓÃÓÚ½ÓÊÕÓû§ÊäÈëµÄÃüÁîÐС£
15ÐУº¶¨ÒåÖ¸ÕëÊý×écmd_arg£¬ËüµÄÐÎʽºÍ×÷Óö¼ÓëÎÒÃÇÊìϤµÄchar *argv[]Ò»Ñù¡£
´ÓÒÔÉÏ2¸ö¶¨Òå¿ÉÒÔ¿´³öMini Shell¶ÔÃüÁîÊäÈëµÄ2¸öÏÞÖÆ£ºÊ×ÏÈ£¬Óû§ÊäÈëµÄÃüÁîÐбØÐëÔÚ255¸ö×Ö·ûÖ®ÄÚ£¨³ýÈ¥×Ö·û´®½áÊø±êÖ¾'\0'£©£»Æä´Î£¬ÃüÁîÐеIJÎÊý¸öÊý²»µÃ³¬¹ý10¸ö£¨°üÀ¨ÃüÁî±¾ÉíÔÚÄÚ£©¡£
18ÐУº½øÈëÒ»¸ödo-whileÑ»·£¬Õâ¸öÑ»·ÊDZ¾³ÌÐòµÄÖ÷Ì岿·Ö£¬»ù±¾Ë¼ÏëÊÇ"µÈ´ýÊäÈëÃüÁî--´¦ÀíÒÑÊäÈëÃüÁî--µÈ´ýÊäÈëÃüÁî"¡£
22ÐУº´òÓ¡ÊäÈëÌáʾÐÅÏ¢¡£ÔÚMini ShellÖУ¬Äã¿ÉÒÔËæÒⶨ×Ô¼ºÏ²»¶µÄÃüÁîÊäÈëÌáʾÐÅÏ¢£¬±¾³ÌÐòÖÐʹÓÃÁË"-=Mini Shell=-* "£¬ÊDz»ÊÇÓеãÏñÒ»¸öCS¸ßÊÖ£¿Èç¹û²»Ï²»¶£¬Äã¿ÉÒÔÓÃÈÎÒâµÄ×Ö·ûÌæ»»Ëü¡£
23ÐУº½ÓÊÕÓû§ÊäÈë¡£
25-27ÐУºfgets½ÓÊÜÊäÈëʱ£¬»á½«ÊäÈë×Ö·û´®Ê±Ä©Î²µÄ»»Ðзû£¨"\n"£©Ò»Æð½ÓÊÜ£¬ÕâÊÇÎÒÃDz»ÐèÒªµÄ£¬ËùÒÔÒª°ÑËüÈ¥µô¡£±¾³ÌÐòÖмòµ¥µÄÓÃ×Ö·û´®½áÊø±êÖ¾'\0'¸²¸ÇÁË×Ö·û´®cmdµÄ×îºóÒ»¸ö×Ö·ûÀ´ÊµÏÖÕâ¸öÄ¿µÄ¡£
30ÐУº³õʼ»¯Ö¸ÕëÊý×écmd_arg¡£
32-42ÐУº¶ÔÊäÈë½øÐзÖÎö£¬½«cmdÖвÎÊý¼äµÄ¿Õ¸ñÓÃ'\0'Ìî³ä£¬²¢°Ñ¸÷²ÎÊýµÄÆðʼµØÖ··Ö±ð¸³Óëcmd_argÊý×é¡£ÕâÑù¾Í°Ñcmd·Ö½â³ÉÁËcmd_arg£¬µ«·Ö½âºóµÄ¸÷ÃüÁî²ÎÊýÈÔȻʹÓÃ×ÅcmdµÄÄÚ´æ¿Õ¼ä£¬ËùÒÔÔÚÃüÁîÖ´ÐнáÊøÇ°²»Ò˶ÔcmdÁíÍ⸳ֵ¡£
45ÐУºÈç¹û»¹Î´·ÖÎöµ½ÊäÈë×Ö·û´®µÄĩ⣨i =10£©£¬¾ÍÈÏΪÊäÈëµÄÃüÁîÐг¬³öÁË10¸ö²ÎÊýµÄÏÞÖÆ£¬´òÓ¡´íÎó²¢ÖØнÓÊÕÃüÁî¡£
51-52ÐУºÄÚ²¿ÃüÁîquit£º×Ö·û´®cmd_arg[0]¾ÍÊÇÃüÁî±¾Éí£¬Èç¹ûÃüÁîÊÇquit£¬ÔòÍ˳öÑ»·£¬Ò²¾ÍµÈÓÚÍ˳ö¸Ã³ÌÐò¡£
55-58ÐУºÄÚ²¿ÃüÁîcd£ºµ÷Óú¯Êýdo_cd()Íê³ÉcdÃüÁîµÄ¶¯×÷¡£
61ÐУº¶ÔÓÚÆäËüµÄÍⲿÃüÁîºÍÓ¦ÓóÌÐò£¬µ÷Óú¯Êýexecute_new()Ö´ÐС£
º¯Êýdo_cd£º
68ÐУº½ö½ö¿¼Âǽô¸úÔÚÃüÁîºóÃæµÄ²ÎÊýargv[1]£¬¶ø²»ÔÙ¿¼ÂÇÆäËüµÄ²ÎÊý¡£Èç¹ûÕâ¸ö²ÎÊý´æÔÚ£¬¾Í°ÑËü×÷ΪҪת»»µÄĿ¼¡£
69ÐУºµ÷ÓÃϵͳµ÷ÓÃchdirÇл»µ±Ç°Ä¿Â¼£¬²Î¼û¸½Â¼1¡£
70-82ÐУº¶Ôchdir¿ÉÄܳöÏֵĴíÎó½øÐд¦Àí¡£
º¯Êýexecute_new£º
ÉÏÒ»Ò³ [1] [2] [3] ÏÂÒ»Ò³
92ÐУºµ÷ÓÃϵͳµ÷ÓÃfork²úÉúеÄ×Ó½ø³Ì¡£
93ÐУºÈç¹û·µ»Ø¸ºÖµ£¬ËµÃ÷forkµ÷ÓóöÏÖ´íÎó¡£
96ÐУºÈç¹û·µ»Ø0£¬ËµÃ÷µ±Ç°½ø³ÌÊÇ×Ó½ø³Ì¡£
97ÐУºµ÷ÓÃexecvpÖ´ÐÐеÄÓ¦ÓóÌÐò£¬²¢¼ì²âµ÷ÓÃÊÇ·ñ³ö´í£¨·µ»Ø¸ºÖµ£©¡£ÕâÀïʹÓÃexecvpµÄÔÒòÊÇËü¿ÉÒÔ×Ô¶¯ÔÚ¸÷ĬÈÏĿ¼ÀïÑ°ÕÒÄ¿±êÓ¦ÓóÌÐòµÄλÖ㬶ø²»±ØÎÒÃÇ×Ô¼º±à³ÌʵÏÖ¡£
98-107ÐУº¶Ôexecvp¿ÉÄܳöÏֵĴíÎó½ø³Ì´¦Àí¡£
108ÐУºÈç¹ûexecvpµÄÖ´ÐгöÏÖ´íÎó£¬×Ó½ø³ÌÔÚÕâÀïÖÕÖ¹¡£±íÃæÉÏ¿´ÆðÀ´£¬Õâ¸öexitÊǽÓ×Å97ÐеĴíÎóÅжϵÄÏÂÒ»ÐÐÓï¾ä£¬¶ø·ÇifÓï¾äµÄÒ»²¿·Ö£¬ËƺõÎÞÂÛµ÷ÓÃexecvp³É¹¦Óë·ñ¶¼»á½Ó×ÅÖ´ÐÐexit¡£µ«ÊÂʵÉÏ£¬Èç¹ûexecvpµ÷Óóɹ¦µÄ»°£¬Õâ¸ö½ø³Ì½«»á±»ÐµijÌÐò´úÂëÌî³ä£¬Òò¶ø¸ù±¾²»¿ÉÄÜÖ´Ðе½ÕâÒ»ÐС£·´Ö®£¬Èç¹ûÖ´Ðе½ÁËÕâÒ»ÐУ¬ËµÃ÷Ç°ÃæµÄexecvpµ÷ÓÃÒ»¶¨³öÏÖÁË´íÎó¡£ÕâÑùµÄЧ¹ûºÍexit±»°üº¬ÔÚifÓï¾äÖеÄЧ¹ûÊÇÍêÈ«Ò»ÑùµÄ¡£
109ÐУºÈç¹ûfork·µ»ØÆäËüÖµ£¬ËµÃ÷µ±Ç°½ø³ÌÊǸ¸½ø³Ì¡£
110ÐУºµ÷ÓÃϵͳµ÷ÓÃwait¡£waitÔÚÕâÀïÓÐÁ½¸ö×÷Óãº
ʹ¸¸½ø³ÌÔÚ´ËÔÝÍ££¬µÈ´ý×Ó½ø³ÌÖ´ÐÐÍê±Ï¡£ÕâÑù£¬¾Í¿ÉÒÔµÈ×Ó½ø³ÌµÄËùÓÐÐÅϢȫ²¿Êä³öÍê±Ïºó²Å´òÓ¡ÃüÁîÌáʾ·û£¬µÈ´ýÏÂÒ»ÌõÃüÁîµÄÊäÈ룬´Ó¶ø±ÜÃâÁËÃüÁîÌáʾ·ûºÍÓ¦ÓóÌÐòÊä³ö»ìÔÓÔÚÒ»ÆðµÄÏÖÏó¡£
ÊÕ¼¯×Ó½ø³ÌÍ˳öºóÁôϵĽ©Ê¬½ø³Ì¡£¿ÉÄÜÓжÁÕßÒ»Ö±¶ÔÕâ¸öÎÊÌâ´æÓÐÒÉÎÊ--"ÎÒÃDZà³ÌÉú³ÉµÄ×Ó½ø³ÌÓÉÎÒÃÇ×Ô¼ºÉè¼ÆµÄ¸¸½ø³Ì¸ºÔðÊÕ¼¯£¬µ«ÎÒÃÇÊÖ¶¯Ö´ÐеÄÕâ¸ö¸¸½ø³ÌÓÉËÊÕ¼¯ÄØ£¿"ÏÖÔÚ´ó¼ÒÓ¦¸ÃÃ÷°×ÁË£¬ÎÒÃÇ´ÓÃüÁîÐÐÖ´ÐеÄËùÓнø³Ì×îºó¶¼ÊÇÓÉshellÊÕ¼¯µÄ¡£
¹ØÓÚMini ShellµÄ±àÒëºÍÔËÐУ¬ÕâÀï¾Í²»ÔÙ·óÊöÁË£¬ÓÐÐËȤµÄ¶ÁÕß¿ÉÒÔ×ÔÐж¯ÊÖʵÑ飬»òÕ߶ÔÕâ¸ö³ÌÐò½øÐиĽø£¬Ê¹Ö®¸ü½Ó½üÉõÖÁ³¬¹ýÎÒÃÇÕýʹÓõÄBash¡£
1.14 daemon½ø
£¨³ö´¦£ºhttp://www.sheup.com£©
ÉÏÒ»Ò³ [1] [2] [3]
98-107ÐУº¶Ôexecvp¿ÉÄܳöÏֵĴíÎó½ø³Ì´¦Àí¡£
108ÐУºÈç¹ûexecvpµÄÖ´ÐгöÏÖ´íÎó£¬×Ó½ø³ÌÔÚÕâÀïÖÕÖ¹¡£±íÃæÉÏ¿´ÆðÀ´£¬Õâ¸öexitÊǽÓ×Å97ÐеĴíÎóÅжϵÄÏÂÒ»ÐÐÓï¾ä£¬¶ø·ÇifÓï¾äµÄÒ»²¿·Ö£¬ËƺõÎÞÂÛµ÷ÓÃexecvp³É¹¦Óë·ñ¶¼»á½Ó×ÅÖ´ÐÐexit¡£µ«ÊÂʵÉÏ£¬Èç¹ûexecvpµ÷Óóɹ¦µÄ»°£¬Õâ¸ö½ø³Ì½«»á±»ÐµijÌÐò´úÂëÌî³ä£¬Òò¶ø¸ù±¾²»¿ÉÄÜÖ´Ðе½ÕâÒ»ÐС£·´Ö®£¬Èç¹ûÖ´Ðе½ÁËÕâÒ»ÐУ¬ËµÃ÷Ç°ÃæµÄexecvpµ÷ÓÃÒ»¶¨³öÏÖÁË´íÎó¡£ÕâÑùµÄЧ¹ûºÍexit±»°üº¬ÔÚifÓï¾äÖеÄЧ¹ûÊÇÍêÈ«Ò»ÑùµÄ¡£
109ÐУºÈç¹ûfork·µ»ØÆäËüÖµ£¬ËµÃ÷µ±Ç°½ø³ÌÊǸ¸½ø³Ì¡£
110ÐУºµ÷ÓÃϵͳµ÷ÓÃwait¡£waitÔÚÕâÀïÓÐÁ½¸ö×÷Óãº
ʹ¸¸½ø³ÌÔÚ´ËÔÝÍ££¬µÈ´ý×Ó½ø³ÌÖ´ÐÐÍê±Ï¡£ÕâÑù£¬¾Í¿ÉÒÔµÈ×Ó½ø³ÌµÄËùÓÐÐÅϢȫ²¿Êä³öÍê±Ïºó²Å´òÓ¡ÃüÁîÌáʾ·û£¬µÈ´ýÏÂÒ»ÌõÃüÁîµÄÊäÈ룬´Ó¶ø±ÜÃâÁËÃüÁîÌáʾ·ûºÍÓ¦ÓóÌÐòÊä³ö»ìÔÓÔÚÒ»ÆðµÄÏÖÏó¡£
ÊÕ¼¯×Ó½ø³ÌÍ˳öºóÁôϵĽ©Ê¬½ø³Ì¡£¿ÉÄÜÓжÁÕßÒ»Ö±¶ÔÕâ¸öÎÊÌâ´æÓÐÒÉÎÊ--"ÎÒÃDZà³ÌÉú³ÉµÄ×Ó½ø³ÌÓÉÎÒÃÇ×Ô¼ºÉè¼ÆµÄ¸¸½ø³Ì¸ºÔðÊÕ¼¯£¬µ«ÎÒÃÇÊÖ¶¯Ö´ÐеÄÕâ¸ö¸¸½ø³ÌÓÉËÊÕ¼¯ÄØ£¿"ÏÖÔÚ´ó¼ÒÓ¦¸ÃÃ÷°×ÁË£¬ÎÒÃÇ´ÓÃüÁîÐÐÖ´ÐеÄËùÓнø³Ì×îºó¶¼ÊÇÓÉshellÊÕ¼¯µÄ¡£
¹ØÓÚMini ShellµÄ±àÒëºÍÔËÐУ¬ÕâÀï¾Í²»ÔÙ·óÊöÁË£¬ÓÐÐËȤµÄ¶ÁÕß¿ÉÒÔ×ÔÐж¯ÊÖʵÑ飬»òÕ߶ÔÕâ¸ö³ÌÐò½øÐиĽø£¬Ê¹Ö®¸ü½Ó½üÉõÖÁ³¬¹ýÎÒÃÇÕýʹÓõÄBash¡£
1.14 daemon½ø
£¨³ö´¦£ºhttp://www.sheup.com/£©
ÉÏÒ»Ò³ [1] [2] [3] [4]