00001 /* 00002 00003 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of version 2.1 of the GNU Lesser General Public License 00007 as published by the Free Software Foundation. 00008 00009 This program is distributed in the hope that it would be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 Further, this software is distributed without any warranty that it is 00014 free of the rightful claim of any third person regarding infringement 00015 or the like. Any license provided herein, whether implied or 00016 otherwise, applies only to this software file. Patent licenses, if 00017 any, provided herein do not apply to combinations of this program with 00018 other software, or any other product whatsoever. 00019 00020 You should have received a copy of the GNU Lesser General Public 00021 License along with this program; if not, write the Free Software 00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 00023 USA. 00024 00025 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00026 Mountain View, CA 94043, or: 00027 00028 http://www.sgi.com 00029 00030 For further information regarding this notice, see: 00031 00032 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00033 00034 */ 00035 00036 00037 #pragma ident "@(#) libf/pxf/pxfsystem.c 92.4 10/29/99 21:39:27" 00038 /* 00039 * PXFSYSTEM -- Issue a shell command 00040 * (Extension to Posix 1003.9-1992) 00041 * 00042 * Synopsis: 00043 * 00044 * SUBROUTINE PXFSYSTEM(cmd, ilen, ierror) 00045 * CHARACTER*N cmd 00046 * INTEGER ilen, ierror 00047 * 00048 * Description: 00049 * 00050 * PXFSYSTEM uses the system function to pass the character string 00051 * argument to a shell command processor as input. The 00052 * character argument is executed as if the argument is 00053 * transmitted from a terminal. 00054 * 00055 * Use an empty input string to determine if the command 00056 * processor is present. 00057 * 00058 * The arguments are: 00059 * 00060 * cmd - default character input variable containing a 00061 * string recognized by the command processor. 00062 * 00063 * ilen - default integer input variable containing the 00064 * length of cmd in characters. If ILEN is zero, 00065 * all trailing blanks at the end of cmd are removed. 00066 * 00067 * ierror - default integer output variable that contains zero 00068 * if the operation was successful or nonzero if the 00069 * operation was not successful. 00070 * 00071 * PXFSYSTEM may return one of the following error values: 00072 * 00073 * EINTR If the fork system call was interrupted by a signal. 00074 * 00075 * EINVAL If ILEN is less than zero or greater than LEN(CMD). 00076 * 00077 * ENOMEM If PXFSYSTEM is unable to obtain memory to copy cmd. 00078 * 00079 * On IRIX systems, PXFSYSTEM may also return: 00080 * 00081 * EAGAIN If the system-imposed limit on the total number of 00082 * processes under execution by a single user would 00083 * be exceeded. 00084 * 00085 * ENOMEM If the new process requires more memory than is 00086 * allowed by the system-imposed maximum MAXMEM. 00087 * 00088 */ 00089 00090 #include <fortran.h> 00091 #include <errno.h> 00092 #include <liberrno.h> 00093 #include <unistd.h> 00094 /* for malloc and memcpy */ 00095 #ifdef _LITTLE_ENDIAN 00096 #include <stdlib.h> 00097 #include <string.h> 00098 #endif 00099 00100 00101 #ifdef _UNICOS 00102 void 00103 PXFSYSTEM( 00104 #else /* _UNICOS */ 00105 void 00106 _PXFSYSTEM( 00107 #endif /* _UNICOS */ 00108 _fcd cmd, /* character variable string for shell */ 00109 _f_int *ilen, /* length in characters of cmd */ 00110 _f_int *ierror) 00111 { 00112 _f_int arglen, errsts, length; 00113 char *argstr, *pthstr; 00114 errsts = 0; 00115 argstr = _fcdtocp(cmd); 00116 arglen = _fcdlen(cmd); 00117 length = *ilen; 00118 /* 00119 * if cmd is a zero length string, then check to see if shell 00120 * command is present. This should return a nonzero, positive 00121 * value. 00122 */ 00123 if (arglen == 0) { 00124 if (errsts = system(NULL) == -1) 00125 errsts = errno; 00126 } else if (length < 0 || length > arglen) { 00127 errsts = EINVAL; 00128 } else { 00129 /* 00130 * If length is zero, strip trailing blanks. Otherwise, 00131 * malloc memory and copy the string; adding a NULL 00132 * terminator. 00133 */ 00134 if (length == 0) { 00135 pthstr = _fc_acopy(cmd); 00136 } else { 00137 pthstr = (char *) malloc(length + 1); 00138 } 00139 if (pthstr == NULL) /* If no memory allocated */ 00140 errsts = ENOMEM; 00141 else { 00142 if (length != 0) { /* Copy argument */ 00143 (void) memcpy(pthstr, argstr, length); 00144 pthstr[length] = '\0'; 00145 } 00146 /* 00147 * If cmd is null string, call system(NULL). 00148 * to see if shell command is present. Else, 00149 * call system(cmd). 00150 */ 00151 if (pthstr[0] == '\0') { 00152 if (errsts = system(NULL) == -1) 00153 errsts = errno; 00154 } else { 00155 if (errsts = system(pthstr) == -1) 00156 errsts = errno; 00157 } 00158 free(pthstr); 00159 } 00160 } 00161 *ierror = errsts; 00162 return; 00163 } 00164 00165 #ifndef _UNICOS 00166 /* 00167 * PXFSYSTEM Use system to execute command 00168 * Allow on non-UNICOS systems with f90 subroutine interface. 00169 */ 00170 void 00171 pxfsystem_( 00172 char *cmd, /* Character ptr to command */ 00173 _f_int *ilen, /* Length of cmd */ 00174 _f_int *ierror, /* Error Status */ 00175 int lenpath) 00176 { 00177 _PXFSYSTEM( _cptofcd(cmd, lenpath), ilen, ierror); 00178 return; 00179 } 00180 #endif
1.5.6