more.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <termios.h>
  11. #include <sys/ioctl.h>
  12. #define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
  13. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  14. #define DESCRIPTION "Display files in a page-by-page basis."
  15. #define OPERANDS "file ..."
  16. #include "../common/common.h"
  17. int main(int argc, char *const argv[]) {
  18. int i = 0, argument, success, read_file;
  19. long int /* columns, */ lines;
  20. char buffer[4096], cmd, *argv0 = strdup(argv[0]);
  21. struct winsize w;
  22. FILE *file;
  23. struct termios oldattr, newattr;
  24. ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  25. if (getenv("LINES")) lines = strtol(getenv("LINES"), NULL, 10) - 1;
  26. else lines = w.ws_row - 1;
  27. //if (getenv("COLUMNS")) columns = strtol(getenv("COLUMNS"), NULL, 10);
  28. //else columns = w.ws_col - 1;
  29. while ((argument = getopt(argc, argv, "")) != -1) {
  30. if (argument == '?') {
  31. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  32. return 1;
  33. }
  34. } argc -= optind; argv += optind;
  35. if (argc != 1) {
  36. print_usage(argv0, DESCRIPTION, OPERANDS, VERSION); return 1;
  37. }
  38. file = fopen(argv[0], "r");
  39. if (errno) return errprint(argv0, argv[0], errno);
  40. success = 0;
  41. while (!success) {
  42. if (!read_file) read_file = 1; /* 1 => read on a page-by-page basis
  43. * 0 => don't read at all
  44. * 2 => read on a line-by-line basis
  45. */
  46. for (i = 0; i < lines && read_file != 0; i++) {
  47. if (fgets(buffer, 4096, file) != NULL) {
  48. printf("%s", buffer);
  49. }
  50. else if (errno) return errprint(argv0, "fgets()", errno);
  51. else
  52. putchar((int)'\n');
  53. if (read_file == 2) {
  54. break;
  55. }
  56. }
  57. read_file = 0;
  58. /* Get a character */
  59. tcgetattr(0, &oldattr); /* Get previous terminal parameters */
  60. newattr = oldattr;
  61. newattr.c_lflag &= ~( ICANON | ECHO ); /* Disable ECHO and ICANON */
  62. tcsetattr(0, TCSANOW, &newattr); /* Set net parameters */
  63. cmd = getchar();
  64. tcsetattr(0, TCSANOW, &oldattr); /* Restore old parameters */
  65. if (errno) return errprint(argv0, NULL, errno);
  66. switch (cmd) {
  67. case 'q':
  68. return 0;
  69. case 'h':
  70. print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
  71. read_file = 0;
  72. break;
  73. case ' ':
  74. read_file = 1; /* page-by-page */
  75. break;
  76. default:
  77. read_file = 2; /* line-by-line */
  78. break;
  79. }
  80. }
  81. return errprint(argv0, NULL, errno);
  82. }