C programming question

John Wildberger wildberger-iRg7kjdsKiH3fQ9qLvQP4Q at public.gmane.org
Tue Dec 23 23:29:39 UTC 2003


On December 22, 2003 10:19 pm, Greg Franks wrote:

> With all due respect, are you really sure that you know what you are
> doing and or what you want?

Well, most of the time I don't know what I am doing. However, for guys that 
are even more challenged I have converted your program to c++ and made a few 
changes to the effect that I can specify on the command line the memory 
location I want to look at.
For instance to look at memory location 68 you have to type:
./mem 68
The result is:
 2 bytes at offset 68 are: f84d

Here is my modified version:

/* mem.cpp
 * Original program 'mem.c' by  Greg Franks  Dec 22 2003
 * modified, converted to C++ and commented  by J.Wildberger
 * execute with: ./mem + arg (memloc in decimal number)
 * compile with: c++ -o mem mem.cpp
 */


#include <iostream>
#include <fcntl.h>       //required for O_RDONLY
#include <unistd.h>      //required for lseek
#include <iomanip>       //required for setbase

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
        {
        cout <<argc<<endl;
        cout <<"Specify memory offset from zero"<<endl;
        exit (1);
        }
    int n=atoi(argv[1]);
    int fd = open( "/dev/mem", O_RDONLY ); //open for read only
    if ( fd < 0 )
        {
	perror( "Cannot open /dev/mem: " );
	exit( 1 );
        }
    else
        {
	off_t offset = lseek( fd,  n, SEEK_SET );
	if ( offset == (off_t)-1 )
	    {
	    perror( "Seek: "  );
	    exit( 1 );
	    }
	else
	    {
	    short x;
	    ssize_t size = read( fd, &x, 4 );
	    if ( size == -1 )
	        {
		perror( "Read: " );
		exit( 1 );
	        }
	    cout << "2 bytes at offset " << n << " are:  "<< setbase(16)<< x << endl;
	    }
        }
    exit ( 0 );
}



--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list