head/tail實現,headtail實現

來源:互聯網
上載者:User

head/tail實現,headtail實現
     只實現了head/tail的準系統,預設顯示十行及-n參數。       一、使用帶緩衝的系統調用。       write/read等系統調用是不帶緩衝的,可以封裝一層,使其帶緩衝。typedef struct{ int rio_fd; int rio_cnt; char *rio_bufptr; char rio_buf[RIO_BUFFSIZE];}rio_t; void rio_readinitb(rio_t *rp, int fd){ rp->rio_fd = fd; rp->rio_cnt = 0; rp->rio_bufptr = rp->rio_buf;} ssize_t rio_read(rio_t *rp, void *usrbuf, size_t n){ int cnt = 0; while (rp->rio_cnt <= 0) { if ((rp->rio_cnt = read(rp->rio_fd, rp->rio_buf, sizeof(rp->rio_buf))) < 0) { if (errno != EINTR) { return -1; } } else if (rp->rio_cnt == 0) { return 0; } else { rp->rio_bufptr = rp->rio_buf; } } //cnt = n > rp->rio_cnt?rp->rio_cnt:n; cnt = n; if (n > rp->rio_cnt) { cnt = rp->rio_cnt; } memcpy(usrbuf, rp->rio_bufptr, cnt); rp->rio_cnt -= cnt; rp->rio_bufptr += cnt; return cnt;} ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen, int count){ int i = 0, rc = 0, num = 0; char c = 0, *buf = usrbuf; lseek(rp->rio_fd, maxlen, SEEK_END); for (i = 1; i < maxlen; i++) { if ((rc = rio_read(rp, &c, 1)) == 1) { *buf++ = c; if (c == '\n') { if (++num == count) { break; } } } else if (rc == 0) { if (i == 1) { return 0; } else { break; } } else { return -1; } } *buf = '\0'; return i;}View Code

     二、head命令實現

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include "rio.h" #define MAXSIZE 4096 int main(int argc, char **argv){ if (argc < 2) { fprintf(stderr, "usage %s [-n n] filename\n", argv[0]); return -1; } int times = 10, i = 0, in_fd = -1, n_char = 0; char filename[16] = {0}; char buf[MAXSIZE] = {0}; rio_t rio_buf = {0}; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-n")) { times = atoi(argv[++i]); } else { snprintf(filename, sizeof(filename), "%s", argv[i]); } } if ((in_fd = open(filename, O_RDONLY)) == -1) { fprintf(stderr, "open %s failed\n", filename); return -1; } rio_readinitb(&rio_buf, in_fd); if ((n_char = rio_readlineb(&rio_buf, buf, MAXSIZE, times)) > 0) { write(STDOUT_FILENO, buf, n_char); } close(in_fd); return 0;}View Code

     三、tail命令實現

#include "rio.h" #define MAXSIZE 4096 void show_info(char *buf, char **ptr, int count); int main(int argc, char **argv){ if (argc < 2) { fprintf(stderr, "usage %s [-n n] filename\n", argv[0]); return -1; } int times = 10, i = 0, in_fd = -1; char filename[16] = {0}; char buf[MAXSIZE] = {0}; rio_t rio_buf = {0}; char *ptr[MAXSIZE]; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-n")) { times = atoi(argv[++i]); } else { snprintf(filename, sizeof(filename), "%s", argv[i]); } } if ((in_fd = open(filename, O_RDONLY)) == -1) { fprintf(stderr, "open %s failed\n", filename); return -1; } rio_readinitb(&rio_buf, in_fd); rio_read(&rio_buf, buf, MAXSIZE); show_info(buf, ptr, times); return 0;} void show_info(char *buf, char **ptr, int times){ int num = 0; int flag = 0; if (num < times) { *ptr = strrchr(buf, '\n'); flag = 1; **ptr = '\0'; show_info(buf, ptr + 1, --times); } if (flag) { printf("%s\n", *ptr + 1); }}View Code

     通過遞迴show_info來實現按順序列印,其實也可以用鏈表來實現,不過遞迴寫起來簡單。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.