0

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!

OBR_EXO
  • 600
  • 4
  • 19
  • `then I get errors where it's trying to define a variable like:` So fix your sed `s/long\([[:space:]]\+int\)\?/uint32_t/`? – KamilCuk Aug 28 '22 at 06:38
  • `sizeof(char) != 1` this is silly. **`sizeof(char)` is always 1** in C, only `CHAR_BIT` can change in systems where a `char` isn't 8-bit. `I've also tried -m32 on the gcc options, but then it fails to link due to 64-bit libraries being required` then you just need to [install 32-bit libs](https://stackoverflow.com/q/22355436/995714) and sometimes also the 32-bit subsystem. But really you should fix the code, it'll make forward maintenance much easier – phuclv Aug 28 '22 at 09:13
  • 2
    It seems like your problem is to change all the `long` types, whether they are declared as `long`, `signed long`, `long int`, `signed long int`, `unsigned long`, or `unsigned long int`, to the corresponding `int` types. There is no magic option for that; you should simply change the source code. Since a simple bulk replace of `long` with `int32_t` does not work, why do you not do a “smart” edit of changing `unsigned long int` to `unsigned int`, `long int` to `int`, `long` to `int`, and so on? How large is the source code? How many places need to be changed? – Eric Postpischil Aug 28 '22 at 10:20
  • You could search the source code for all the places `long` appears, isolate the context of each, reduce them to the unique combinations, and then write some sed replacements that cover those. This seems like an editing problem, not a real programming problem. – Eric Postpischil Aug 28 '22 at 10:24
  • @EricPostpischil - How large is the source code? How many places need to be changed? It's a pretty big code-base - A LOT of different files and roughly 420,000 lines in C (plus some SQL)... Manually editing would be VERY tedious, I'm hoping to use sed. – OBR_EXO Aug 28 '22 at 20:38
  • @OBR_EXO: Find all the lines where `long` appears as a word (e.g., not `longboat`), reduce each line to just its C keywords and the first punctuation character that follows `long` (that will remove identifiers such as the `x` in `unsigned long x`, reduce all white space sequences to a single space, sort the lines, and find the unique lines. You will likely end up with a small list that you could write a sed script to update. – Eric Postpischil Aug 29 '22 at 00:51
  • Thanks for everyone's feedback, I decided not to port the code any further (have already edited it to get it to compile under the newer compiler), but changed the environment to get the code running, what I did is [explained here.](https://serverfault.com/questions/1109323/installing-mysql-32-bit-on-ubuntu-22) – OBR_EXO Aug 29 '22 at 00:59

1 Answers1

1

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.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68