Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for which.rex

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

  1. /* WHICH, Version 1.00 for Windows    */
  2. /* Rexx "port" of UNIX' WHICH command */
  3. /* that also looks for DLL's          */
  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. /* Display empty line */
  13. Say
  14.  
  15. /* Parse command line parameters */
  16. Parse Upper Arg params
  17. invalid.0 = 7
  18. invalid.1 = "*"
  19. invalid.2 = "?"
  20. invalid.3 = "/"
  21. invalid.4 = "\"
  22. invalid.5 = ","
  23. invalid.6 = ";"
  24. invalid.7 = ":"
  25. Do i = 1 To invalid.0
  26. 	If Pos( invalid.i, params ) > 0 Then Call Syntax
  27. End
  28. Parse Value params With dummy1'"'longfilename'"'dummy2
  29. If longfilename <> "" Then Do
  30. 	If dummy1||dummy2 <> "" Then Call Syntax
  31. 	filename = longfilename
  32. 	longname = 1
  33. End
  34. Else Do
  35. 	Parse Value params With filename dummy
  36. 	If filename <> "" Then Do
  37. 		If dummy <> "" Then Call Syntax
  38. 		longname = 0
  39. 	End
  40. 	Else Do
  41. 		Call Syntax
  42. 	End
  43. End
  44.  
  45. /* Check if extension was given */
  46. pathext = Translate( Value( "PATHEXT", , "ENVIRONMENT" ) )
  47. Do i = 1 By 1 Until pathext = ""
  48. 	Parse Value pathext With commandext.i";"pathext
  49. 	If commandext.i <> "" Then commandext.0 = i
  50. End
  51. i = commandext.0 + 1
  52. commandext.i = ".DLL"
  53. i = commandext.0 + 2
  54. commandext.i = ".SYS"
  55. i = commandext.0 + 3
  56. commandext.i = ".OCX"
  57. commandext.0 = i
  58. addext       = 1
  59. chk4ext      = Right( filename, 4 )
  60. Do i = 1 To commandext.0
  61. 	If chk4ext = commandext.i Then addext = 0
  62. End
  63.  
  64. /* Read PATH from environment */
  65. path = Translate( Value( "PATH", , "ENVIRONMENT" ) )
  66. path.  = ""
  67. path.0 = 1
  68. /* Add current directory as first PATH entry */
  69. path.1 = Strip( Directory( ), "T", "\" )||"\"
  70. Do i = 2 By 1 Until path = ""
  71. 	Parse Value path With path.i";"path
  72. 	If Right( path.i, 1 ) <> "\" Then path.i = path.i||"\"
  73. 	path.0 = i
  74. End
  75.  
  76. /* Check if file can be found in PATH */
  77. Do i = 1 To path.0 Until found.0 = 1
  78. 	If addext = 0 Then Do
  79. 		file = path.i||filename
  80. 		If longname = 1 Then Do
  81. 			file = Translate( file, "?", " " )
  82. 		End
  83. 		Call SysFileTree file, "found.", "FO"
  84. 		If found.0 = 1 Then Do
  85. 			If longname = 0 Then Do
  86. 				Call FileFound file
  87. 			End
  88. 			Else Do
  89. 				file = Translate( Translate( file, " ", "?" ) )
  90. 				If file = Translate( found.1 ) Then Do
  91. 					Call FileFound file
  92. 				End
  93. 			End
  94. 		End
  95. 	End
  96. 	Else Do j = 1 To commandext.0 - 3
  97. 		file = path.i||filename||commandext.j
  98. 		If longname = 1 Then Do
  99. 			file = Translate( file, "?", " " )
  100. 		End
  101. 		Call SysFileTree file, "found.", "FO"
  102. 		If found.0 = 1 Then Do
  103. 			If longname = 0 Then Do
  104. 				Call FileFound file
  105. 			End
  106. 			Else Do
  107. 				file = Translate( Translate( file, " ", "?" ) )
  108. 				If file = Translate( found.1 ) Then Do
  109. 					Call FileFound file
  110. 				End
  111. 			End
  112. 		End
  113. 	End
  114. End
  115. Say "Not found: "||filename
  116. Exit 1
  117.  
  118.  
  119. FileFound:
  120. 	If longname = 1 Then Do
  121. 		Say 'Found: "'||arg( 1 )||'"'
  122. 	End
  123. 	Else Do
  124. 		Say "Found: "||arg( 1 )
  125. 	End
  126. 	Exit 0
  127. Return
  128.  
  129. Syntax: Procedure
  130. 	Say
  131. 	Say "Which.rex, Version 1.00 for Regina Rexx for Windows"
  132. 	Say "UNIX-like WHICH utility for Windows that can search for"
  133. 	Say ".DLL, .OCX and .SYS files as well"
  134. 	Say
  135. 	Say "Usage:    WHICH  program_name"
  136. 	Say
  137. 	Say 'Where:    "program_name"  is any program name with or without extension,'
  138. 	Say "                          but always without drive and/or path."
  139. 	Say
  140. 	Say "Returns:  The first file with a valid or specified extension found in the PATH."
  141. 	Say
  142. 	Say "If program_name is specified without extension, all extensions from the PATHEXT"
  143. 	Say "environment variable will be searched for."
  144. 	Say "Files with .DLL, .OCX or .SYS extension will be ignored unless the extension is"
  145. 	Say "specified."
  146. 	Say "Spaces in long file names are allowed, wildcards aren't."
  147. 	Say
  148. 	Say "Written by Rob van der Woude"
  149. 	Say "http://www.robvanderwoude.com"
  150. 	Exit 9
  151. Return
  152.  

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