Otherwise you can simply call the system() function. The declaration is:
function System (CmdLine: CString): Integer; C;
(CString is the same as PChar in BP, and newer versions of GPC understand both CString and PChar.)
Frank,
Thanks for the info. If it's not too much to ask, could you send me a sample program that uses the system() function. I have not yet explored the depths of CStrings, and need to get some programs to function properly in a short period of time.
Try the program below.
(* Note: The function Str2CStrRO converts a Pascal string into a CString. It's about the same as BP's StrPCopy. I called it Str2CStr rather than StrPCopy because this seems clearer to me, and I appended "RO" (read-only) to make it clear that neither the Pascal string nor the CString should be modified while the CString is used. Future versions of GPC should have something like this built-in, but for now, one has to do it manually. *)
The rest of the program should be straightforward. Unlike BP's Exec, you don't have to specify the program and paramters in two separate strings, but rather combined into one string (with a space between them). Also, you don't have to give the full path to the program when the program is located in the system path.
So, you could test it with a command like "command /s dir" for Dos or "ls -la" for Unix.
program Exec_Demo;
function System (cmd: CString): Integer; C;
function Str2CStrRO (var s: String): CString; begin s[Length(s)+1] := #0; Str2CStrRo := @s[1] end;
var s: String (1000); i: Integer;
begin Write ('Enter a system command: '); Readln (s); i := System (Str2CStrRO (s)); Writeln ('The returned status of the system was ', i, '.') end.
The extend() procedure seems to tack on a newline & carriage return onto the end of the text file before any writing can be done to the file.
(* Only if there isn't one already. *)
Is this a bug/feature?
It's a feature! :-)
The Extended Pascal standard demands this, and actually it took a bit of extra code within the RTS to implement this behaviour.
Is there a fix/way to get around this?
AFAIK, there's no way to prevent this (except using a file of char and seeking to the end, but then the formatting of Write[ln] isn't available -- only via WriteStr)-:
Perhaps it would be useful to have a GPC switch to turn this behaviour off, perhaps coupled to the --borland-pascal switch? Any comments, also from the mailing list (to which I'm sending a copy)?