int main(int argc, char *argv[]) { FILE *fp; int needclose; int ttyfd; int line; if (isatty(STDIN_FILENO) == 0) { fp = stdin; needclose = 0; } else { if (argc <= 1) { printf("Usage: mymore [file]\n"); return 0; } if ((fp = fopen(argv[1], "r")) == NULL) { perror("fopen"); return 1; } needclose = 1; } // cat if (isatty(STDOUT_FILENO) == 0) { cat(fp); // more } else { if (termcap() == -1) { fprintf(stderr, "failed to load termcap."); } if ((tty = open("/dev/tty", O_RDONLY)) == -1) { perror("open"); // return; tty = STDERR_FILENO; } raw_mode(1); more(fp); raw_mode(0); if (tty != STDERR_FILENO) { close(tty); } } if (needclose) { fclose(fp); } return 0; }
void log_open( log_t* log, const char* name ) { static struct { const char* name; int fd; } special[] = { { "stdout", 1 }, { "-", 1 }, { "stderr", 2 } }; int i, fd = -1; term_t term; log->file = NULL; log->isatty = false; term.lines = 1; term.columns = 132; term.colors = 2; term.unicode = true; for( i=0; i<N_ELEM(special); i++ ) { if( strcmp(special[i].name, name) == 0 ) { fd = special[i].fd; break; } } if( fd == -1 ) { fd = open(name, O_WRONLY); } if( fd >= 0 ) { log->file = fdopen(fd, "w+"); log->isatty = isatty(fd); } if( log->isatty && termcap( &term ) == 0 ) { log->lines = term.lines; log->columns = term.columns; log->colors = term.colors; log->unicode = term.unicode; } log_fmt( log, "colors: %hi, lines: %hi, columns: %i\n", log->colors, log->lines, log->columns ); }