Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fvkernel.c
- //
- // 2009 Wil van Lierop <support@sa-maastricht.net>
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- // MA 02110-1301, USA.
- // fvkernel extracts kernel version string from the vmlinuz kernel file.
- // At the moment it assumes that this string can be found directly
- // after the video mode magic 0x25780A00, i have no debian nor ubuntu
- // kernels found or selfcompiled kernels on 2.6 systems that have otherwise.
- // Please report if you do find those exceptions.
- #include <stdio.h>
- #define MAGIC 0x25780A00
- int extract_ks(int file_handle, FILE * ptr )
- {
- // this function extract kernel version string
- // even if there are preceding or trailing 0x00.
- int c= file_handle;
- int s_stat= 0;
- int d_stat= 0;
- while (s_stat == 0)
- {
- c = fgetc(ptr);
- if (c != 0x00)
- {
- printf("%c",c);
- d_stat= 1;
- }
- else
- {
- if (d_stat == 1)
- {
- s_stat = 1;
- }
- }
- }
- return 0;
- }
- int get_kernelstring(char * prog_name, char * filename)
- {
- int c=' ';
- FILE * ptr;
- // lets make a file handle to our kernel we have choosen
- // but only readonly, and gracefully exit when not found
- ptr = fopen (filename, "r");
- if(ptr==NULL)
- {
- printf("\n\t%s: Unable to open %s\n\n", prog_name, filename);
- return 0;
- }
- // lets find our magic number in the fastest way.
- // This code could be improved; i found this
- // the fastest way to do it. It also gracefully
- // will be quiet, if not run on a kernel file.
- // It will then return nothing to the standaard
- // output.
- while (c != EOF)
- {
- c = fgetc(ptr);
- if( c==0x25 )
- {
- c = fgetc(ptr);
- if( c==0x78 )
- {
- c = fgetc(ptr);
- if( c==0x0A )
- {
- c = fgetc(ptr);
- if( c==0x00 )
- {
- extract_ks(c, ptr);
- // we are done, but lets nice it up with just an end of line
- // and close our file handle before we return
- // because we dont want to walk the rest of the file
- // anymore, we are done.
- printf("\n");
- fclose(ptr);
- return 0;
- }
- }
- }
- }
- }
- fclose(ptr);
- return 0;
- }
- int main(int argc, char** argv)
- {
- // inform user if no /path/kernel are given,
- // and blurb a syntax message
- if (argc != 2)
- {
- printf("\n\t%s syntax:", argv[0]);
- printf("\t%s [/path/kernel]\n\n", argv[0]);
- return 0;
- }
- // invoke get_kernelstring but at least warn
- // user when something unforseen has happened
- // like not being a kernel file, but with a magic number.
- if (get_kernelstring( argv[0], argv[1]) != 0)
- {
- printf("unkown error happened, are you sure you pointed me to a kernel file?\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement