*! version 1.0.0  30nov2005
*! written for "John P. Haisken-DeNew / RWI-Essen" <jhaiskendenew@rwi-essen.de>
program fileinfo, rclass
	version 9.1
	syntax anything(name=files id="file") [ , all ]

	if "`c(os)'" == "Windows" {
		parseWindows `"`files'"', `all'
	}
	else if "`c(os)'" == "Unix" | index("`c(os)'", "Mac") == 1 {
		parseUnix `"`files'"', `all'
	}
	*else if index("`c(os)'","Mac") == 1 {
	*	parseMac `"`files'"', `all'
	*}
	return add
end

program parseWindows, rclass
	syntax anything(name=files id="file") [ , all ]

	tempfile fileinfo
	qui log using `"`fileinfo'"', text
	ls `files'
	qui log close

	tempname fh
	file open `fh' using `"`fileinfo'"', read
	file read `fh' line

	if "`all'" != "" {
		local i 0
	}
	local lines 0
	while r(eof) == 0 {
		if index(`"`line'"', "No such file") {
			continue, break
		}

		if `"`line'"' == "" | ///
		   index(`"`line'"', `"- ls `files'"') | ///
		   index(`"`line'"', `"= ls `files'"') | ///
		   index(`"`line'"', "log close") {
			file read `fh' line
			continue
		}

		local size   : word 1 of `line'
		local date   : word 2 of `line'
		local time   : word 3 of `line'
		local filename = ///
			substr(`"`line'"', index(`"`line'"', "`time'") + length("`time'") + 1, .)

		if "`all'" != "" {
			local `i++'
		}

		return local filename`i'   = trim("`filename'")
		return local date`i'       = "`date' `time'"
		return local size`i'      = "`size'"

		local `lines++'

		file read `fh' line
	}
	return scalar k = `lines'
	file close `fh'
end

program parseUnix, rclass
	syntax anything(name=files id="file") [ , all ]

	tempfile fileinfo
	qui log using `"`fileinfo'"', text
	ls `files'
	qui log close

	tempname fh
	file open `fh' using `"`fileinfo'"', read
	file read `fh' line

	if "`all'" != "" {
		local i 0
	}
	local lines 0
	while r(eof) == 0 {
		if index(`"`line'"', "No such file") {
			continue, break
		}

		if `"`line'"' == "" | ///
		   index(`"`line'"', `"- ls `files'"') | ///
		   index(`"`line'"', `"= ls `files'"') | ///
		   index(`"`line'"', "log close") {
			file read `fh' line
			continue
		}

		local perms  : word 1 of `line'
		local linkct : word 2 of `line'
		local owner  : word 3 of `line'
		local group  : word 4 of `line'
		local size   : word 5 of `line'
		local month  : word 6 of `line'
		local date   : word 7 of `line'
		local time   : word 8 of `line'
		local filename = ///
			substr(`"`line'"', index(`"`line'"', "`time'") + 6, .)

		if "`all'" != "" {
			local `i++'
		}

		return local filename`i'   = "`filename'"
		return local date`i'       = "`month' `date' `time'"
		return scalar size`i'      = `size'
		return local group`i'      = "`group'"
		return local owner`i'      = "`owner'"
		return scalar linkcount`i' = `linkct'
		return local perms`i'      = "`perms'"

		local `lines++'

		file read `fh' line
	}
	return scalar k = `lines'
	file close `fh'
end