/* program: killbyname  (a freeby)
** written: Stan J. Towianski
** purpose: killbyname arg, will kill the 1st process that matches the
**          name given in arg.
**    date: 1995
**    note: needs to have kill.exe program in path.
**          This can be found in c:\grpware\clients from OS/2 Warp 3.0
*/

parse upper arg options searchname num_to_kill
/*say 'entered with optoins ='options'='    'searchname ='searchname'='*/
tmp = value( 'TMP', , 'OS2ENVIRONMENT' )
listfile = SysTempFileName( tmp'\kilbntmp.???' )
'pstat /c > 'listfile

just_find_flag = 'N'
return_hex_flag = 'N'
decpid = -1
pid = -1
if ( left( options, 1 ) \= '-' ) then
    Do
    num_to_kill = searchname
    searchname = options
    End
else if ( pos( '-FX', options ) > 0 ) then
    Do
    just_find_flag = 'Y'
    return_hex_flag = 'Y'
    End
else if ( pos( '-F', options ) > 0 ) then
    just_find_flag = 'Y'

Do While( lines( listfile ) )
    word = linein( listfile )
    parse var word pid . . procname rest
    say "pid ="pid     "procname ="procname"="
    if ( pos( searchname, procname ) > 0 ) then
        Do
        decpid = x2d( pid )
        if ( just_find_flag = 'Y' ) then
            Do
            say 'found name with hex pid = 'pid   'decpid = 'decpid
            leave
            End
        "kill "decpid
        leave
        End
End
rc = stream( listfile, 'c', "close" )
"del "listfile
if return_hex_flag = 'Y' then
    return pid
else
    return decpid

