1.5. An IFS "Hello World" Application

I don't know about you, but I'm falling asleep reading this book. I'm not a historian, I'm a programmer! Lets go! Let's write some code, already!

Type the following code into a source member called CH1HELLO, and we'll try to make it work:

      ** HELLO WORLD in the IFS Example:
      **  (From Chapter 1)
      **
      **  To compile:
      **     CRTBNDRPG CH1HELLO SRCFILE(xxx/QRPGLESRC) DBGVIEW(*LIST)
      **

     H DFTACTGRP(*NO) ACTGRP(*NEW)


      ** API call to open a stream file
      **
     D open            PR            10I 0 extproc('open')
     D   path                          *   value options(*string)
     D   oflag                       10I 0 value
     D   mode                        10U 0 value options(*nopass)
     D   codepage                    10U 0 value options(*nopass)

     D O_WRONLY        C                   2
     D O_CREAT         C                   8
     D O_TRUNC         C                   64

     D RW              C                   6
     D R               C                   4
     D OWNER           C                   64
     D GROUP           C                   8

      ** API call to write data to a stream file
      **
     D write           PR            10I 0 extproc('write')
     D   fildes                      10I 0 value
     D   buf                           *   value
     D   nbyte                       10U 0 value

      ** API call to close a stream file
      **
     D close           PR            10I 0 extproc('close')
     D   fildes                      10I 0 value


     D fd              S             10I 0
     D data            S             12A
     D msg             S             52A

     C* Create an empty file called 'helloworld':
     c                   eval      fd = open('/helloworld':
     c                                      O_CREAT+O_TRUNC+O_WRONLY:
     c                                      RW*OWNER + RW*GROUP + R )
     c                   if        fd < 0
     c                   eval      Msg = 'open failed!'
     c                   dsply                   msg
     c                   eval      *inlr = *on
     c                   return
     c                   endif

     C* Write 'Hello World' to the file:
     c                   eval      data = 'Hello World!'
     c                   callp     write(fd: %addr(data): %size(data))

     C* Close the file:
     c                   callp     close(fd)

     c                   eval      *inlr = *on

You typed all of that already? Wow, that was quick, good job! The instructions at the top tell you how to compile it. But, of course, you'll need to substitute the source library that you used for the "XXX" that I put in the comments.

For example, if your source library is called "TASTYSRC", you would type: CRTBNDRPG CH1HELLO SRCFILE(TASTYSRC/QRPGLESRC) DBGVIEW(*LIST)

Note: I don't recommend that name, it makes you want to stop and eat your computer.

Once you've succeeded in compiling the program, you can run it just by typing: CALL CH1HELLO

If a problem occurs when you run this program, you'll get a DSPLY message on the screen telling you that something failed. If nothing happens on your screen, rejoice! That's what we wanted to happen! In the next topic, we'll talk about what that program did, and verify that things worked.