Jump to content
Excelsior Forums
qnr

How do I print results from a system call?

Recommended Posts

How do I get a result returned from a system call?

For example, if I wanted to call uptime, and then print out the result, how would I do it?

Execute and Command only return BOOLEAN, and system returns an INTEGER

Share this post


Link to post
Share on other sites

Well, I'm currently piping the results to a file, and reading that.  So I guess I could do my own routine up to do something. hmmmm

Share this post


Link to post
Share on other sites

The boolean returned should be the same size as an integer so all you need to do is to cast the M2 API boolean return value to an integer and interpret it according to the Win32 API docs.

Share this post


Link to post
Share on other sites

Thanks for the response, however, I'm not sure I understand what you're saying.  How am I going to get, for example:

12:03am  up 4 days 13:18,  17 users,  load average: 2.76, 2.92, 2.96

Out of following your procedure (which is what a call to uptime returns)?  BTW, that's just a general question since I'm using Linux, and the Windows APIs are not applicable - just curious what the results would be, following what you specified.

Share this post


Link to post
Share on other sites

Sorry, you said a system call and I though it just was a question of the API defs having a BOOLEAN rather than an INTEGER return code (and where the returned code potentially could have some further useful info and not just true/false).

Update.EXE (WIN32) is a standalone command-line program, and not an API system call, so then I guess you have to pipe the results to get to the text oputput.

Share this post


Link to post
Share on other sites

Sorry, the confusion was due to the way I phrased it.  I meant a literal "PROCEDURE system(string: ARRAY OF CHAR): INTEGER;" system call.

Share this post


Link to post
Share on other sites

The BOOLEAN result of Execute and Command only report if the call is Ok (actually by checking the GetLastError).

But what about the last VAR-parameter of Execute and Command which return the exit code? Isn't it what you need?

Share this post


Link to post
Share on other sites

Hi Vit,

The VARs are defined as CARDINALs, I'm wondering about an ARRAY OF CHAR

qnr

(Actually, I'm not looking at the definition module at the moment, I just remember them being cardinals and I could be wrong)

Share this post


Link to post
Share on other sites

Yes, the exit code is defined as CARDINAL (that is, CARD32).

I see no way to return a string value from an executed program or command line.

If you need to intercept a program's output, you better redirect its output to some stream handled by your host program. Or you can pass string data via environment variables.

If these does not solve the problem then please give more information on your particular problem.

Share this post


Link to post
Share on other sites

Thanks for all of your help.  I'm getting satisfactory results by piping to a file, as mentioned earlier.

Share this post


Link to post
Share on other sites

I just happened to be browsing the board, and came across this question I asked a long time ago.

Coincidently, yesterday, I put an example of using FIFOs up on my site that answers the question I was asking originally:

Edit: Put in the code so you don't have to visit the link if you don't want to.

http://linux.aliboom.com:8000/index.php?option=com_content&task=view&id=40&Itemid=36

1  <* GENHISTORY + *> 
2  <* LINENO + *> 
3 
4  (* Second version of uptime, this one will use FIFO rather than  
5  **     a file. 
6  *) 
7  MODULE uptime2; 
8 
9  FROM fcntl    IMPORT open, O_RDONLY; 
10  FROM stat     IMPORT mkfifo; 
11  FROM stdlib   IMPORT exit, EXIT_FAILURE; 
12  FROM string   IMPORT memset; 
13  FROM unistd   IMPORT close, read; 
14 
15  FROM SYSTEM   IMPORT TSIZE, ADDRESS; 
16 
17  FROM FileSys  IMPORT Exists; 
18  FROM STextIO  IMPORT WriteString, WriteLn; 
19  FROM ProgExec IMPORT Command; 
20 
21  CONST 
22    fifoName="/tmp/myFifo"; 
23    bufferSize = 4096; 
24    pipeBuff = bufferSize; 
25    openRead = O_RDONLY; 
26 
27  VAR 
28    return  : CARDINAL; 
29    done    : BOOLEAN; 
30    pipeFd, res : INTEGER; 
31    buffer : ARRAY [0..bufferSize+1] OF CHAR; 
32    bufAdr : ADDRESS; 
33 
34  BEGIN 
35    bufAdr := memset(buffer, 0, SIZE(buffer)); 
36    (* See if the FIFO exists, if not, create it *) 
37    IF NOT Exists(fifoName) THEN 
38      IF (mkfifo(fifoName, 777B) <> 0) THEN (* mkfifo returns 0 on success *) 
39        WriteString("Could not create FIFO."); WriteLn; 
40        exit(EXIT_FAILURE); 
41      END; 
42    END; 
43    (* By default FIFO is blocking, so put the uptime command 
44    **     the background *) 
45    done := Command("uptime > /tmp/myFifo &", return); 
46    pipeFd := open(fifoName, openRead); 
47    IF (pipeFd <> -1) THEN 
48      REPEAT 
49        res := read(pipeFd, buffer, bufferSize); 
50      UNTIL NOT (res > 0); 
51      res := close(pipeFd); 
52    ELSE; 
53      exit(EXIT_FAILURE); 
54    END; 
55    WriteString(buffer); 
56  END uptime2. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×