fpeek.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. //extern char *optarg;
  29. int g_start_pos = 0;
  30. int g_end_pos = -1;
  31. int g_modulus = 16;
  32. char *g_fn = NULL;
  33. int g_verbose_flag = 0;
  34. int g_cononical_flag = 0;
  35. void print_usage_and_exit(void)
  36. {
  37. printf("simple utility to look at raw data of files\n");
  38. printf("usage:\n");
  39. printf(" -f fn filename\n");
  40. printf(" [-s start] start byte (default start)\n");
  41. printf(" [-e end] end byte (default end)\n");
  42. printf(" [-m mod] newline every 'mod' entries (default 16)\n");
  43. printf(" [-C] 'cononical' view. Print printable ascii next alongsize value\n");
  44. printf(" [-h] help\n");
  45. printf(" [-v] verbose flag\n");
  46. exit(0);
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. int i, k;
  51. int fd = -1;
  52. struct stat st;
  53. int r;
  54. int read_end = -1;
  55. char buf[16] = {0};
  56. int c;
  57. while ((c = getopt(argc, argv, "hvs:e:m:f:C")) != -1)
  58. switch (c)
  59. {
  60. case 'f': g_fn = strdup(optarg); break;
  61. case 'C': g_cononical_flag = 1; break;
  62. case 'h': print_usage_and_exit(); exit(0); break;
  63. case 'v': g_verbose_flag = 1; break;
  64. case 's': g_start_pos = atoi(optarg); break;
  65. case 'e': g_end_pos = atoi(optarg); break;
  66. case 'm': g_modulus = atoi(optarg); break;
  67. default: print_usage_and_exit(); exit(0); break;
  68. }
  69. if ( !g_fn )
  70. {
  71. printf("provide filename\n");
  72. print_usage_and_exit();
  73. }
  74. if ((g_start_pos < 0) ||
  75. (g_modulus < 1) )
  76. {
  77. printf("start byte must be non-negative and modulus must be greater than 0\n");
  78. print_usage_and_exit();
  79. }
  80. r = stat( g_fn, &st );
  81. if (r < 0)
  82. {
  83. perror(g_fn);
  84. exit(-1);
  85. }
  86. fd = open( g_fn, O_RDONLY );
  87. if (fd < 0)
  88. {
  89. perror(g_fn);
  90. exit(-1);
  91. }
  92. read_end = st.st_size;
  93. if ( (g_end_pos >= 0) && (g_end_pos < st.st_size) )
  94. read_end = g_end_pos+1;
  95. if (g_start_pos>0)
  96. lseek( fd, g_start_pos, SEEK_SET );
  97. printf("start %i end %i\n", g_start_pos, read_end);
  98. printf("#output in hex\n");
  99. for (k=0, i=g_start_pos; i < read_end ; i++, k++)
  100. {
  101. if ((k % g_modulus)==0)
  102. {
  103. if (k>0)
  104. printf("\n");
  105. if (g_cononical_flag)
  106. {
  107. printf("[%4x]", i);
  108. }
  109. }
  110. r = read( fd, buf, 1 );
  111. if (r < 0)
  112. {
  113. perror("read error");
  114. exit(-1);
  115. }
  116. printf(" %02x", buf[0] );
  117. if (g_cononical_flag)
  118. {
  119. printf(" %c ", isprint(buf[0]) ? buf[0] : '.' );
  120. }
  121. }
  122. close(fd);
  123. printf("\n");
  124. return 0;
  125. }