Apue Fig 1.10 Example code refinement--optimization of the prompt and input enter

Source: Internet
Author: User

Apue the shell2.c Example program on page 15th of version 3rd, the effect is as follows:

1 GCCOL.SHELL.C-o Origin_shell2 3./Origin_shell4%Date5 December 13, 2015 Sunday 22:04 48 SEC CST6% ^Cinterrupt7 ls8 Makefile getcputc.c mycat.c myls_info.c note.md origin_shell shell2.c uidgid.c9 a.out hello.c myls.c myls_sort.c ol.shell.c shell1.c testerror.cTen% % OneCouldn'T execute:: No such file or directory A%ls - Makefile getcputc.c mycat.c myls_info.c note.md origin_shell shell2.c uidgid.c - a.out hello.c myls.c myls_sort.c ol.shell.c shell1.c testerror.c the%

As you can see from the example above, there are 2 minor problems:

1. When the interrupt signal is entered via CTRL + C, the "%" of the output statement of the interrupt capture program is not immediately output and is not output until the next carriage return, causing the 10th line to have two percent of the number.

2. When the% prompt appears, the direct carriage return prompts for the error as shown in line 11. Because the BUF content passed to the EXECLP function at this time is empty. where issue 1 is the following sentence that appears in the program:

 while  (fgets (buf, MAXLINE, stdin)!= //  that is, the interrupt signal is the fgets function, At this point the system is requesting input, and the output of the interrupt handler:  printf (  "  interrupt\n%%   " ); //  
printf ( " interrupt\n %%\n ); //
1 % ^cinterrupt2 3ls4Makefile    mycat.c        note.md        shell2.c 5 a.out        myls.c        ol.shell.c    testerror.c6getcputc.c    myls_info.c    Origin_shell    uidgid.c7 hello.c        myls_sort.c    shell1.c

As can be seen above, add a ' \ n ', although immediately output%, but as seen in line 3rd, the cursor moved to the next line.

One solution is to use the Fflush () function. Complete code, added as 21-24 lines, and 46 lines:

1#include".. /apue.h"2#include <sys/wait.h>3 4 Static voidSig_int (int);/*Our signal-catching function*/5 6 int7Mainvoid)8 {9     CharBuf[maxline];/*From apue.h*/Ten pid_t pid; One     intstatus; A  -     if(Signal (SIGINT, sig_int) = =Sig_err) -Err_sys ("Signal Error"); the  -printf"%% ");/*Print prompt (printf requires% to print%)*/ -      while(Fgets (buf, MAXLINE, stdin)! =NULL) { -         if(Buf[strlen (BUF)-1] =='\ n') +Buf[strlen (BUF)-1] =0;/*replace newline with null*/ -  +         if(strlen (buf) = =0) {//this if statement is used to handle the direct carriage return Aprintf"%% ");  at             Continue; -         } -  -         if(PID = fork ()) <0) { -Err_sys ("Fork Error"); -}Else if(PID = =0) {/* Child*/ inEXECLP (buf, buf, (Char*)0); -Err_ret ("couldn ' t execute:%s", buf); toExit127); +         } -  the         /*Parent*/ *         if(PID = Waitpid (PID, &status,0)) <0) $Err_sys ("waitpid Error");Panax Notoginsengprintf"%% "); -     } theExit0); + } A  the void +Sig_int (intSigno) - { $printf"interrupt!\n%%"); $Fflush (stdout);//force output of character streams in all stdout -}

Perfect after the effect is as follows:

1 Date 2 December 13, 2015 Sunday 22:31 53 SEC CST 3 % ^c interrupt! 4 Date 5 December 13, 2015 Sunday 22:32 15 SEC CST 6 %7

Apue Fig 1.10 Example code refinement--optimization of the prompt and input enter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.