Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for which.cmd

(view source code of which.cmd as plain text)

  1. /* WHICH, Version 3.00                  */
  2. /* Rexx "port" of UNIX' WHICH command   */
  3. /* that also looks for DLL's in LIBPATH */
  4. /* Written by Rob van der Woude         */
  5.  
  6. /* Initialize RexxUtil */
  7. if RxFuncQuery( "SysLoadFuncs" ) <> 0 then do
  8. 	call RxFuncAdd "SysLoadFuncs", "RexxUtil", "SysLoadFuncs"
  9. 	call SysLoadFuncs
  10. end
  11.  
  12. /* Parse command line parameters */
  13. parse upper arg params
  14. invalid.0 = 7
  15. invalid.1 = "*"
  16. invalid.2 = "?"
  17. invalid.3 = "/"
  18. invalid.4 = "\"
  19. invalid.5 = ","
  20. invalid.6 = ";"
  21. invalid.7 = ":"
  22. do i = 1 to invalid.0
  23. 	if pos( invalid.i, params ) > 0 then call Syntax
  24. end
  25. parse value params with dummy1'"'longfilename'"'dummy2
  26. if longfilename <> "" then do
  27. 	if dummy1||dummy2 <> "" then call Syntax
  28. 	filename = longfilename
  29. 	longname = 1
  30. end
  31. else do
  32. 	parse value params with filename dummy
  33. 	if filename <> "" then do
  34. 		if dummy <> "" then call Syntax
  35. 		longname = 0
  36. 	end
  37. 	else do
  38. 		call Syntax
  39. 	end
  40. end
  41.  
  42. /* Check if extension was given */
  43. commandext.0 = 5
  44. commandext.1 = ".BAT"
  45. commandext.2 = ".CMD"
  46. commandext.3 = ".COM"
  47. commandext.4 = ".EXE"
  48. commandext.5 = ".DLL"
  49. addext       = 1
  50. chk4ext      = right( filename, 4 )
  51. do i = 1 to commandext.0
  52. 	if chk4ext = commandext.i then addext = 0
  53. end
  54.  
  55. /* Read PATH from environment */
  56. path = translate( value( "PATH", , "OS2ENVIRONMENT" ) )
  57. if chk4ext = ".DLL" then do
  58. 	parse value path with ":\OS2\SYSTEM" -1 bootdrive +2
  59. 	configsys = bootdrive||"\CONFIG.SYS"
  60. 	call linein configsys, 1, 0
  61. 	do until lines( configsys ) = 0
  62. 		line = strip( translate( linein( configsys ) ) )
  63. 		if left( line, 8 ) = "LIBPATH=" then do
  64. 			parse value line with "LIBPATH="path
  65. 			leave
  66. 		end
  67. 	end
  68. end
  69. path.  = ""
  70. path.0 = 1
  71. path.1 = strip( directory( ), "T", "\" )||"\"
  72. do i = 2 by 1 until path = ""
  73. 	parse value path with path.i";"path
  74. 	if right( path.i, 1 ) <> "\" then path.i = path.i||"\"
  75. 	path.0 = i
  76. end
  77.  
  78. /* Check if file can be found in PATH */
  79. do i = 1 to path.0 until found.0 = 1
  80. 	if addext = 0 then do
  81. 		file = path.i||filename
  82. 		if longname = 1 then do
  83. 			file = translate( file, "?", " " )
  84. 		end
  85. 		call SysFileTree file, "found.", "FO"
  86. 		if found.0 = 1 then do
  87. 			if longname = 0 then do
  88. 				call FileFound file
  89. 			end
  90. 			else do
  91. 				file = translate( translate( file, " ", "?" ) )
  92. 				if file = translate( found.1 ) then do
  93. 					call FileFound file
  94. 				end
  95. 			end
  96. 		end
  97. 	end
  98. 	else do j = 1 to commandext.0
  99. 		file = path.i||filename||commandext.j
  100. 		if longname = 1 then do
  101. 			file = translate( file, "?", " " )
  102. 		end
  103. 		call SysFileTree file, "found.", "FO"
  104. 		if found.0 = 1 then do
  105. 			if longname = 0 then do
  106. 				call FileFound file
  107. 			end
  108. 			else do
  109. 				file = translate( translate( file, " ", "?" ) )
  110. 				if file = translate( found.1 ) then do
  111. 					call FileFound file
  112. 				end
  113. 			end
  114. 		end
  115. 	end
  116. end
  117. say "Not found: "||filename
  118. EXIT 1
  119.  
  120.  
  121. FileFound:
  122. 	if longname = 1 then do
  123. 		say 'Found: "'||arg( 1 )||'"'
  124. 	end
  125. 	else do
  126. 		say "Found: "||arg( 1 )
  127. 	end
  128. 	EXIT 0
  129. return
  130.  
  131. Syntax: procedure
  132. 	say
  133. 	say "WHICH, Version 3.00"
  134. 	say "UNIX-like WHICH utility for OS/2 that"
  135. 	say "can search for DLL's in LIBPATH as well"
  136. 	say "Written by Rob van der Woude"
  137. 	say
  138. 	say "Usage:  WHICH  program_name"
  139. 	say
  140. 	say "   or:  WHICH  dll_name.DLL"
  141. 	say
  142. 	say "You may specify program_name with or without"
  143. 	say "extension. program_name as well as dll_name.DLL"
  144. 	say "should be specified without a drive or path."
  145. 	say
  146. 	say "Spaces in long file names are allowed, wildcards"
  147. 	say "aren't."
  148. 	EXIT 9
  149. return
  150.  

page last modified: 2024-04-16; loaded in 0.0207 seconds