home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / GDL.ICN < prev    next >
Text File  |  1991-09-05  |  4KB  |  133 lines

  1. ############################################################################
  2. #
  3. #    Name:     gdl.icn
  4. #
  5. #    Title:    Get directory list 
  6. #
  7. #    Author:    Richard L. Goerwitz
  8. #
  9. #    Version: 1.2
  10. #
  11. #    Date:     June 1, 1991
  12. #
  13. ############################################################################
  14. #
  15. #  gdl returns a list containing everything in a directory (whose name
  16. #  must be passed as an argument to gdl).  Nothing fancy.  I use this file
  17. #  as a template, modifying the procedures according to the needs of the
  18. #  program in which they are used.
  19. #
  20. ############################################################################
  21. #
  22. #  Requires:  UNIX or MS-DOS
  23. #
  24. ############################################################################
  25.  
  26. procedure gdl(dir)
  27.  
  28.     getdir := set_getdir_by_os()
  29.     return getdir(dir)
  30.  
  31. end
  32.  
  33.  
  34. procedure set_getdir_by_os()
  35.  
  36.     # Decide how to get a directory, based on whether we are running
  37.     # under Unix or MS-DOS.
  38.  
  39.     if find("UNIX", &features)
  40.     then return unix_get_dir
  41.     else if find("MS-DOS", &features)
  42.     then return msdos_get_dir
  43.     else stop("Your operating system is not (yet) supported.")
  44.  
  45. end
  46.  
  47.  
  48.  
  49. procedure msdos_get_dir(dir)
  50.  
  51.     # Returns a sorted list of all filenames (full paths included) in
  52.     # directory "dir."  The list is sorted.  Fails on invalid or empty
  53.     # directory.  Aborts if temp file cannot be opened.
  54.     #
  55.     # Temp files can be directed to one or another directory either by
  56.     # manually setting the variable temp_dir below, or by setting the
  57.     # value of the environment variable TEMPDIR to an appropriate
  58.     # directory name.
  59.  
  60.     local in_dir, filename_list, line
  61.     static temp_dir
  62.     initial {
  63.         temp_dir := 
  64.             (trim(map(getenv("TEMPDIR"), "/", "\\"), '\\') || "\\") |
  65.                 ".\\"
  66.     }
  67.  
  68.     # Get name of tempfile to be used.
  69.     temp_name := get_dos_tempname(temp_dir) |
  70.     stop("No more available tempfile names!")
  71.  
  72.     # Make sure we have an unambiguous directory name, with backslashes
  73.     # instead of Unix-like forward slashes.
  74.     dir := trim(map(dir, "/", "\\"), '\\') || "\\"
  75.  
  76.     # Put dir listing into a temp file.
  77.     system("dir "||dir||" > "||temp_name)
  78.  
  79.     # Put tempfile entries into a list, removing blank- and
  80.     # space-initial lines.  Exclude directories (i.e. return file
  81.     # names only).
  82.     in_dir := open(temp_name,"r") |
  83.     stop("Can't open temp file in directory ",temp_dir,".")
  84.     filename_list := list()
  85.     every filename := ("" ~== !in_dir) do {
  86.         match(" ",filename) | find(" <DIR>", filename) & next
  87.     # Exclude our own tempfiles (may not always be appropriate).
  88.     filename ?:= trim(trim(tab(10)) || "." || tab(13), '. ')
  89.     put(filename_list, map(dir || filename))
  90.     }
  91.  
  92.     # Clean up.
  93.     close(in_dir) & remove(temp_name)
  94.  
  95.     # Check to be sure we actually managed to read some files.
  96.     if *filename_list = 0 then fail
  97.     else return sort(filename_list)
  98.  
  99. end
  100.  
  101.  
  102.  
  103. procedure get_dos_tempname(dir)
  104.  
  105.     # Don't clobber existing files.  Get a unique temp file name for
  106.     # use as a temporary storage site.
  107.  
  108.     every temp_name := dir || "icondir." || right(string(1 to 999),3,"0") do {
  109.     temp_file := open(temp_name,"r") | break
  110.         close(temp_file)
  111.     }
  112.     return \temp_name
  113.  
  114. end
  115.  
  116.  
  117.  
  118. procedure unix_get_dir(dir)
  119.  
  120.     dir := trim(dir, '/') || "/"
  121.     filename_list := list()
  122.     in_dir := open("/bin/ls -F "||dir, "pr")
  123.     every filename := ("" ~== !in_dir) do {
  124.     match("/",filename,*filename) & next
  125.     put(filename_list, trim(dir || filename, '*'))
  126.     }
  127.     close(in_dir)
  128.  
  129.     if *filename_list = 0 then fail
  130.     else return filename_list
  131.  
  132. end
  133.