⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rev.c

📁 操作系统源代码
💻 C
字号:
/* rev - reverse an ASCII line	  Authors: Paul Polderman & Michiel Huisjes */#include <sys/types.h>#include <fcntl.h>#include <stdlib.h>#include <unistd.h>#include <minix/minlib.h>#define CHUNK_SIZE	4096#ifndef EOF#define	EOF	((char) -1)#endif_PROTOTYPE(int main, (int argc, char **argv));_PROTOTYPE(void rev, (void));_PROTOTYPE(int nextchar, (void));int fd;				/* File descriptor from file being read */int main(argc, argv)int argc;char *argv[];{  register unsigned short i;  if (argc == 1) {		/* If no arguments given, use stdin as input */	fd = 0;	rev();	exit(0);  }  for (i = 1; i < argc; i++) {	/* Reverse each line in arguments */	if ((fd = open(argv[i], O_RDONLY)) < 0) {		std_err("Cannot open ");		std_err(argv[i]);		std_err("\n");		continue;	}	rev();	close(fd);  }  return(0);}void rev(){  char output[CHUNK_SIZE];	/* Contains a reversed line */  register unsigned short i;	/* Index in output array */  do {	i = CHUNK_SIZE - 1;	while ((output[i] = nextchar()) != '\n' && output[i] != EOF) i--;	write(1, &output[i + 1], CHUNK_SIZE - 1 - i); /* write reversed line */	if (output[i] == '\n')	/* and write a '\n' */		write(1, "\n", 1);  } while (output[i] != EOF);}char buf[CHUNK_SIZE];int nextchar(){				/* Does a sort of buffered I/O */  static int n = 0;		/* Read count */  static int i;			/* Index in input buffer to next character */  if (--n <= 0) {		/* We've had this block. Read in next block */	n = read(fd, buf, CHUNK_SIZE);	i = 0;			/* Reset index in array */  }  return((n <= 0) ? EOF : buf[i++]);	/* Return -1 on EOF */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -