home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / fortran / forexec.inc < prev    next >
Encoding:
Text File  |  1988-08-11  |  5.2 KB  |  114 lines

  1. $LIST
  2. c       FOREXEC.INC - interface file for C library routines
  3.  
  4. c       This file has been included
  5. c       with your FORTRAN 4.0 to show you how easy it is to call routines
  6. c       written in Microsoft C (version 3.0 or higher).
  7. c
  8. c       The Microsoft FORTRAN 4.0, and C 4.0 releases
  9. c       have been designed so that libraries or subprograms can be written
  10. c       and used in either one of these languages.
  11. c
  12. c       Try compiling and running the demonstration program DEMOEXEC.FOR
  13. c       to see some actual examples.
  14.  
  15. c       C function
  16. c
  17. c               int system(string)
  18. c                       char *string;
  19. c
  20. c       The system() function passes the given C string (which ends with a
  21. c    CHAR(0)) to the DOS command interpreter (COMMAND.COM), which interprets
  22. c    and executes the string as an MS-DOS command.  This allows MS-DOS 
  23. c    commands (i.e., DIR or DEL), batch files, and programs to be executed.
  24. c
  25. c       Example usage in FORTRAN
  26. c
  27. c       integer*2 system                (the return type must be declared)
  28. c       ...
  29. c       i = system('dir *.for'c)        (notice the C literal string '...'c)
  30. c             OR
  31. c    i = system('dir *.for'//char(0))
  32. c
  33. c       The interface to system is given below.  The [c] attribute is given
  34. c       after the function name.  The argument string has the attribute
  35. c       [reference] to indicate that the argument is passed by reference.
  36. c       Normally, arguments are passed to C procedures by value.
  37.  
  38.         INTERFACE TO FUNCTION SYSTEM[C] 
  39.      +          (STRING)
  40.         INTEGER*2 SYSTEM
  41.         CHARACTER*1 STRING[REFERENCE]
  42.         END
  43.  
  44.  
  45. c       C function
  46. c
  47. c       int spawnlp(mode,path,arg0,arg1,...,argn)
  48. c               int mode;               /* spawn mode */
  49. c               char *path;             /* pathname of program to execute */
  50. c               char *arg0;             /* should be the same as path */
  51. c               char *arg1,...,*argn;   /* command line arguments */
  52. c                                       /* argn must be NULL */
  53. c
  54. c       The spawnlp (to be referenced in FORTRAN as spawn) creates and
  55. c       executes a new child process.  There must be enough memory to load
  56. c       and execute the child process.  The mode argument determines which
  57. c       form of spawn is executed. When calling from FORTRAN, the mode 
  58. c    argument must be set to zero.
  59. c
  60. c           Value       Action
  61. c
  62. c             0         Suspend parent program and execute the child program.
  63. c                       When the child program terminates, the parent program
  64. c                       resumes execution.  The return value from spawn is -1
  65. c                       if an error has occurred or if the child process has
  66. c                       run, the return value is the child process'return
  67. c                       code.
  68. c
  69. c       The path argument specifies the file to be executed as the child
  70. c       process.  The path can specify a full path name (from the root
  71. c       directory \), a partial path name (from the current working directory),
  72. c       or just a file name.  If the path argument does not have a filename
  73. c       extension or end with a period (.), the spawn call first appends
  74. c       the extension ".COM" and searches for the file; if unsuccessful, the
  75. c       extension ".EXE" is tried.  The spawn routine will also search for
  76. c       the file in any of the directories specified in the PATH environment
  77. c       variable (using the same procedure as above).
  78. c
  79. c       Example usage in FORTRAN
  80. c
  81. c       integer*2 spawn                 (the return type must be declared)
  82. c       ...
  83. c       i = spawn(0, loc('exemod'c), loc('exemod'c),
  84. c    +          loc('demoexec.exe'c), int4(0))          (execute as a child)
  85. c
  86. c       The interface to _spawnlp is given below.  The [c] attribute is given
  87. c       after the function name.  The [varying] attribute indicates that a
  88. c       variable number of arguments may be given to the function.  The
  89. c       [alias] attribute has to be used because the C name for the function
  90. c       _spawnlp has 8 characters.  By default, names in FORTRAN are only 
  91. c    significant to 6 characters, so we 'alias' the FORTRAN name spawn
  92. c    to the actual C name _spawnlp. 
  93. c    When using the [alias] attribute, remember that the name is passed 
  94. c    EXACTLY as you type it. This means that if you call a C routine using
  95. c    the [alias] attribute, you MUST add an underscore character before
  96. c    the name of the function.
  97.  
  98. c    Notice in the example above that the C 
  99. c    strings are passed differently than those in the system function.  This 
  100. c    is because the string arguments to spawn are undeclared in the 
  101. c    interface below and assumed to be passed by value.  The C spawnlp 
  102. c    function is expecting the addresses of the strings (not the actual 
  103. c    characters), so we use the LOC() function to pass the address 
  104. c    (remember that functions with the [c] attribute pass arguments by 
  105. c    value).  The last parameter to the spawn routine must be a C NULL 
  106. c    pointer which is a 32-bit integer 0, so we use the INT4(0) function 
  107. c    to pass this number by value as the last parameter.
  108.  
  109.         interface to function spawn
  110.      +          [c,varying,alias:'_spawnlp']
  111.      +          (mode)
  112.         integer*2 mode,spawn
  113.         end
  114.