OBR_EXO
OBR_EXO

Reputation: 600

Dealing with long type from a 32-bit codebase on a 64-bit system (Linux)

I have a program written originally WAY back in 1995, maintained to 2012.

It's obviously written for a 32-bit architecture, I've managed to get the damn thing running, but I'm getting stumped on how it's saving data...

My issue is with the sizeof(long) under 64-bit (a common problem I know), I've tried doing a sed across the code and replacing long with int_32t, but then I get errors where it's trying to define a variable like:

unsigned long int count;

I've also tried -m32 on the gcc options, but then it fails to link due to 64-bit libraries being required.

My main issue is where it tries to save player data (it's a MUD), at the following code lines:

if ((sizeof(char) != 1) || (int_size != long_size))
   {
     logit(LOG_DEBUG,
           "sizeof(char) must be 1 and int_size must == long_size for player saves!\n");
     return 0;
   }

Commenting this out allows the file to save, but because it's reading bytes from a buffer as it reloads the characters, the saved file is no longer readable by the load function.

Can anyone offer advice, maybe using a typedef?

I'm trying to avoid having to completely rewrite the save/load routines - this is my very last resort!.

Thanks in advance for answers!

Upvotes: 0

Views: 109

Answers (1)

Lindydancer
Lindydancer

Reputation: 26094

Instead of using types like int and long you can use int32_t and int64_t, which are typedef:s to types that have the correct size in your environment. They exists in signed and unsigned variants as in int32_t and uint32_t.

In order to use them you need to include stdint.h. If you include inttypes.h you will also get macros useful when printing using printf, e.g. PRIu64.

Upvotes: 1

Related Questions