Advertisement
Guest User

Untitled

a guest
May 29th, 2017
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. // fvkernel.c
  2. //
  3. // 2009 Wil van Lierop <support@sa-maastricht.net>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. // MA 02110-1301, USA.
  19.  
  20. // fvkernel extracts kernel version string from the vmlinuz kernel file.
  21.  
  22. // At the moment it assumes that this string can be found directly
  23. // after the video mode magic 0x25780A00, i have no debian nor ubuntu
  24. // kernels found or selfcompiled kernels on 2.6 systems that have otherwise.
  25. // Please report if you do find those exceptions.
  26.  
  27. #include <stdio.h>
  28.  
  29. #define MAGIC 0x25780A00
  30.  
  31. int extract_ks(int file_handle, FILE * ptr )
  32. {
  33. // this function extract kernel version string
  34. // even if there are preceding or trailing 0x00.
  35. int c= file_handle;
  36. int s_stat= 0;
  37. int d_stat= 0;
  38.  
  39. while (s_stat == 0)
  40. {
  41. c = fgetc(ptr);
  42. if (c != 0x00)
  43. {
  44. printf("%c",c);
  45. d_stat= 1;
  46. }
  47. else
  48. {
  49. if (d_stat == 1)
  50. {
  51. s_stat = 1;
  52. }
  53. }
  54. }
  55. return 0;
  56. }
  57.  
  58. int get_kernelstring(char * prog_name, char * filename)
  59. {
  60. int c=' ';
  61. FILE * ptr;
  62.  
  63. // lets make a file handle to our kernel we have choosen
  64. // but only readonly, and gracefully exit when not found
  65. ptr = fopen (filename, "r");
  66. if(ptr==NULL)
  67. {
  68. printf("\n\t%s: Unable to open %s\n\n", prog_name, filename);
  69. return 0;
  70. }
  71. // lets find our magic number in the fastest way.
  72. // This code could be improved; i found this
  73. // the fastest way to do it. It also gracefully
  74. // will be quiet, if not run on a kernel file.
  75. // It will then return nothing to the standaard
  76. // output.
  77. while (c != EOF)
  78. {
  79. c = fgetc(ptr);
  80. if( c==0x25 )
  81. {
  82. c = fgetc(ptr);
  83. if( c==0x78 )
  84. {
  85. c = fgetc(ptr);
  86. if( c==0x0A )
  87. {
  88. c = fgetc(ptr);
  89. if( c==0x00 )
  90. {
  91. extract_ks(c, ptr);
  92. // we are done, but lets nice it up with just an end of line
  93. // and close our file handle before we return
  94. // because we dont want to walk the rest of the file
  95. // anymore, we are done.
  96. printf("\n");
  97. fclose(ptr);
  98. return 0;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. fclose(ptr);
  105. return 0;
  106. }
  107.  
  108.  
  109. int main(int argc, char** argv)
  110. {
  111. // inform user if no /path/kernel are given,
  112. // and blurb a syntax message
  113. if (argc != 2)
  114. {
  115. printf("\n\t%s syntax:", argv[0]);
  116. printf("\t%s [/path/kernel]\n\n", argv[0]);
  117. return 0;
  118. }
  119. // invoke get_kernelstring but at least warn
  120. // user when something unforseen has happened
  121. // like not being a kernel file, but with a magic number.
  122. if (get_kernelstring( argv[0], argv[1]) != 0)
  123. {
  124. printf("unkown error happened, are you sure you pointed me to a kernel file?\n");
  125. }
  126.  
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement