Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

einfo.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/mman.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <getopt.h>
  31. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  32. /** Command-line options */
  33. struct options {
  34. };
  35. /** Error usage information */
  36. struct einfo {
  37. uint32_t size;
  38. uint32_t error;
  39. uint32_t desc;
  40. uint32_t file;
  41. uint32_t line;
  42. } __attribute__ (( packed ));
  43. /**
  44. * Process einfo file
  45. *
  46. * @v infile Filename
  47. * @v opts Command-line options
  48. */
  49. static void einfo ( const char *infile,
  50. struct options *opts __attribute__ (( unused )) ) {
  51. int fd;
  52. struct stat stat;
  53. size_t len;
  54. void *start;
  55. struct einfo *einfo;
  56. /* Open einfo file */
  57. if ( ( fd = open ( infile, O_RDONLY ) ) < 0 ) {
  58. eprintf ( "Cannot open \"%s\": %s\n",
  59. infile, strerror ( errno ) );
  60. exit ( 1 );
  61. }
  62. /* Get file size */
  63. if ( fstat ( fd, &stat ) < 0 ) {
  64. eprintf ( "Cannot stat \"%s\": %s\n",
  65. infile, strerror ( errno ) );
  66. exit ( 1 );
  67. }
  68. len = stat.st_size;
  69. if ( len ) {
  70. /* Map file */
  71. if ( ( start = mmap ( NULL, len, PROT_READ, MAP_SHARED,
  72. fd, 0 ) ) == MAP_FAILED ) {
  73. eprintf ( "Cannot mmap \"%s\": %s\n",
  74. infile, strerror ( errno ) );
  75. exit ( 1 );
  76. }
  77. /* Iterate over einfo records */
  78. for ( einfo = start ; ( ( void * ) einfo ) < ( start + len ) ;
  79. einfo = ( ( ( void * ) einfo ) + einfo->size ) ) {
  80. printf ( "%08x\t%s\t%d\t%s\n", einfo->error,
  81. ( ( ( char * ) einfo ) + einfo->file ),
  82. einfo->line,
  83. ( ( ( char * ) einfo ) + einfo->desc ) );
  84. }
  85. }
  86. /* Unmap and close file */
  87. munmap ( start, len );
  88. close ( fd );
  89. }
  90. /**
  91. * Print help
  92. *
  93. * @v program_name Program name
  94. */
  95. static void print_help ( const char *program_name ) {
  96. eprintf ( "Syntax: %s file1.einfo [file2.einfo...]\n",
  97. program_name );
  98. }
  99. /**
  100. * Parse command-line options
  101. *
  102. * @v argc Argument count
  103. * @v argv Argument list
  104. * @v opts Options structure to populate
  105. */
  106. static int parse_options ( const int argc, char **argv,
  107. struct options *opts __attribute__ (( unused )) ) {
  108. int c;
  109. while (1) {
  110. int option_index = 0;
  111. static struct option long_options[] = {
  112. { "help", 0, NULL, 'h' },
  113. { 0, 0, 0, 0 }
  114. };
  115. if ( ( c = getopt_long ( argc, argv, "s:h",
  116. long_options,
  117. &option_index ) ) == -1 ) {
  118. break;
  119. }
  120. switch ( c ) {
  121. case 'h':
  122. print_help ( argv[0] );
  123. exit ( 0 );
  124. case '?':
  125. default:
  126. exit ( 2 );
  127. }
  128. }
  129. return optind;
  130. }
  131. int main ( int argc, char **argv ) {
  132. struct options opts = {
  133. };
  134. int infile_index;
  135. const char *infile;
  136. /* Parse command-line arguments */
  137. infile_index = parse_options ( argc, argv, &opts );
  138. if ( argc <= infile_index ) {
  139. print_help ( argv[0] );
  140. exit ( 2 );
  141. }
  142. /* Process each einfo file */
  143. for ( ; infile_index < argc ; infile_index++ ) {
  144. infile = argv[infile_index];
  145. einfo ( infile, &opts );
  146. }
  147. return 0;
  148. }