Simple implementation of Linux programming more commands

Source: Internet
Author: User

The more command has three uses:

1.more filename

2.command | More

3.more < filename

This example is used when using./more_1 execution can also copy more_1 to/bin file, equivalent to adding a new command

more_1.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #define LINE_LENGTH 512#define page_length    24int See_more (file* fp_cmd) {if (Fp_cmd = = NULL) printf ("Error stop\n");    Char ch; printf ("\033[7m more?        \033[m ");//vt100 programming, detailed reference on-line while ((ch = getc (fp_cmd)) = NULL)//Read {if (ch = = ' Q ') return 0 from the terminal;        if (ch = = ") return page_length;;    if (ch = = ' \ n ') return 1; } return 0;}    void Do_more (file* fp) {int line_num = 0;    Char Line_buf[line_length];    int reply;    File* Fp_tty;    if (Fp_tty = fopen ("/dev/tty", "r") = = NULL)//parentheses are used, no amplification will be wrong perror ("open TTY error!\n"); while (Fgets (LINE_BUF,LINE_LENGTH,FP)) {if (Line_num = = page_length) {reply =                See_more (Fp_tty);                if (reply = = 0) break;                if (reply = = page_length) line_num = 0; if (reply = = 1) liNe_num--;            } if (fputs (line_buf,stdout) = = EOF) exit (-1);        line_num++; } fclose (Fp_tty);}    int main (int argc,char* argv[]) {file* fp; if (argc = = 1) {printf ("Usage:more [-DFLPCSU] [+linenum |        +/pattern] name1 name2 ... \ n ");    Exit (0);                } else {while (--ARGC) {fp = fopen (* ++argv, "R");                    if (fp! = NULL) {do_more (FP);                Fclose (FP);            } else Perror ("Open eror!\n"); }        }}



Legacy issue: Anti-white display with case input bounce display

Percent display

To press ENTER to execute

We'll do it later.

Simple implementation of Linux programming more commands

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.