🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.7 KiB

  1. /*
  2. * Output contents of a file.
  3. *
  4. * Compile this file with Visual Studio and run the produced command in
  5. * console with a file name argument. For example, command
  6. *
  7. * cat include\dirent.h
  8. *
  9. * will output the dirent.h to screen.
  10. *
  11. * Copyright (C) 1998-2019 Toni Ronkko
  12. * This file is part of dirent. Dirent may be freely distributed
  13. * under the MIT license. For all details and documentation, see
  14. * https://github.com/tronkko/dirent
  15. */
  16. #define _CRT_SECURE_NO_WARNINGS
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <dirent.h>
  21. #include <errno.h>
  22. #include <locale.h>
  23. static void output_file (const char *fn);
  24. int
  25. main(
  26. int argc, char *argv[])
  27. {
  28. int i;
  29. /* Select default locale */
  30. setlocale (LC_ALL, "");
  31. /* Require at least one file */
  32. if (argc == 1) {
  33. fprintf (stderr, "Usage: cat filename\n");
  34. return EXIT_FAILURE;
  35. }
  36. /* For each file name argument in command line */
  37. i = 1;
  38. while (i < argc) {
  39. output_file (argv[i]);
  40. i++;
  41. }
  42. return EXIT_SUCCESS;
  43. }
  44. /*
  45. * Output file to screen
  46. */
  47. static void
  48. output_file(
  49. const char *fn)
  50. {
  51. FILE *fp;
  52. /* Open file */
  53. fp = fopen (fn, "r");
  54. if (fp != NULL) {
  55. size_t n;
  56. char buffer[4096];
  57. /* Output file to screen */
  58. do {
  59. /* Read some bytes from file */
  60. n = fread (buffer, 1, 4096, fp);
  61. /* Output bytes to screen */
  62. fwrite (buffer, 1, n, stdout);
  63. } while (n != 0);
  64. /* Close file */
  65. fclose (fp);
  66. } else {
  67. /* Could not open directory */
  68. fprintf (stderr, "Cannot open %s (%s)\n", fn, strerror (errno));
  69. exit (EXIT_FAILURE);
  70. }
  71. }