Write your own shell command more

Source: Internet
Author: User

Simple implementation of the more commands under Unix:

#include <stdio.h> #define Pagelen 24#define linelen 512int do_more (file * file); int see_more (file * file); int main (i NT Argc,char * argv[]) {FILE * fp;if (argc = = 1) do_more (stdin); Else{int argv_index = 1;while (argc > 1) {if (fp = fopen (arg V[argv_index], "r")) = = NULL) exit (1);d O_more (FP); fclose (FP); argv_index++;argc--;}} return 0;} int Do_more (FILE * fp) {char Buffer[linelen];int number_of_lines = 0;int reply; FILE * Ftty;ftty = fopen ("/dev/tty", "R"), if (Ftty = = NULL) exit (1), while (Fgets (BUFFER,LINELEN,FP)) {if (Number_of_lines = = Pagelen) {fputs (buffer,stdout); reply = See_more (Ftty); if (reply = = 0) break;if (reply = = 1) number_of_lines = 0;//if (reply = = 2)//number_of_lines--;} Else{number_of_lines++;fputs (Buffer,stdout);}}} int See_more (FILE * fp) {char c;printf ("Enter your command (q space or enter):"), while ((c = getc (stdin))! = EOF) {if (c = = ' Q ') //Press Q to exit return 0;if (c = = ") return 1;//Press space to display the next page if (c = = ' n ') return 2;//press N to display the next line}return 3;}

Write your own shell command more

Related Article

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.