[fpc-pascal] Linux Questions

Jonas Maebe jonas.maebe at elis.ugent.be
Tue Sep 19 15:11:23 CEST 2006


On 19 sep 2006, at 15:11, Rainer Stratmann wrote:

> Second question is how to mount and umount for example "/dev/sda1"  
> in a fpc
> program.

Generic solution to this sort of problem:
a) look up how it has to be done in C
b) if it's a "standard" C function, just translate the prototype to  
Pascal and call it.

In this case, "man 2 mount" gives:

#include <sys/mount.h>

int mount(const char *source, const char *target, const char  
*filesystemtype, unsigned long mountflags, const void *data);

So this becomes

uses ctypes;

{$linklib c}

function mount(source, target, filesystemtype: pchar; mountflags:  
culong; data: pointer); cdecl; external;

For the flags values, look in /usr/include/sys/mount. It contains

enum
{
   MS_RDONLY = 1,                /* Mount read-only.  */
#define MS_RDONLY       MS_RDONLY
   MS_NOSUID = 2,                /* Ignore suid and sgid bits.  */
#define MS_NOSUID       MS_NOSUID
etc.

So that becomes

const
   MS_RDONLY = 1;
   MS_NOSUID = 2;
etc.

You can also try running h2pas on it, but I don't know if it can cope  
with an "enum" definition like that (and if it translates it into a  
Pascal enumeration, you won't be able to "or" the values together).

If you are allergic to linking to libc, you can also try to translate  
it to a syscall.

The easiest solution is probably however simply calling the command  
line utility "mount" from within your program. That's the least  
likely to break, does not require libc linking either, the simplest  
to implement and does not require any C knowledge whatsoever.


Jonas



More information about the fpc-pascal mailing list