function detect_archive(url, retval, str) { str = tolower(url) if (str ~ /\.iso$/ || str ~ /\.zip$/) { retval = 1 } else { retval = 0 } return retval } function detect_html(url, retval, str) { str = tolower(url) if (str ~ /\.html?$/ || str ~ /\.php$/) { retval = 1 } else { retval = 0 } return retval } function detect_image(url, retval, str) { str = tolower(url) if (str ~ /\.bmp$/ || str ~ /\.gif$/ || str ~ /\.jpg$/ || str ~ /\.jpeg$/ || str ~ /\.png$/ || str ~ /\.webp$/) { retval = 1 } else { retval = 0 } return retval } function gettemp( result, retval) { while ((cmd_mktemp | getline) > 0) { retval = $0 } result = close(cmd_mktemp) if (result != 0) { info("", "Error: mktemp failed exit status: " result) exit } if (length(retval) == 0) { info("", "Error: mktemp failed, no tmpfile") exit } return retval } # heapsort # # Unstable, and unlike merge and quicksort it relies on random-access # so has poorer cache performance. # # Advantage over quicksort is that its worst-case same as avg: O(n log n) # # This presentation based on http://dada.perl.it/shootout/heapsort.lua5.html # # From: # # Requires 1-based numerically indexed arrays. function hsort(A, n, c, p, t, i) { if (!n) { n = 1 while (n in A) n++ n-- } i = int(n/2) + 1 while (1) { if (i > 1) { i-- t = A[i] } else { t = A[n] A[n] = A[1] n-- if (n == 1) { A[1] = t return } } for (p = i; (c = 2*p) <= n; p = c) { if ((c < n) && (A[c] < A[c + 1])) c++ if (t < A[c]) A[p] = A[c] else break } A[p] = t } } function human_size(bytes, retval) { if (bytes > size_gb) { retval = sprintf("%.1fG", bytes / size_gb) } else if (bytes > size_mb) { retval = sprintf("%.1fM", bytes / size_mb) } else if (bytes > size_kb) { retval = sprintf("%.1fK", bytes / size_kb) } else { retval = sprintf("%dB", bytes) } return retval } function info(out, str) { if (length(out) == 0) { printf "i%s\t\t\t\r\n", str } else { printf "i%s\t\t\t\r\n", str >>out } return } function item(out, type, label, sel, host, port, line) { line = item_str(type, label, sel, host, port) if (length(out) == 0) { printf "%s\r\n", line } else { printf "%s\r\n", line >>out } return } function item_str(type, label, sel, host, port) { retval = sprintf("%s%s\t%s\t%s\t%s", type, label, sel, host, port) return retval } function print_not_found(out, url) { info(out, "Item cannot be found") info(out, "") info(out, "Items may be taken down for various reasons,") info(out, "including by decision of the uploader or") info(out, "due to a violation of the Terms of Use.") info(out, "") item(out, "h", "Metadata", "URL:" url, server, port) info(out, "") url = api_ssl_endpoint "/about/terms.php" item(out, "0", "Terms of Use", cgipath "/text?" url, server, port) info(out, "") item(out, "1", "PHAROS", docpath, server, port) return } function read_file(name, retval) { while ((getline 0) { retval = retval $0 "\r\n" } close(name) return retval } function shorten_left(str, len, retval) { if (length(str) > len) { retval = "..." substr(str, 3 + length(str) - len) } else { retval = str } return retval } function shorten(str, len, retval) { if (length(str) > len) { retval = substr(str, 0, len - 3) "..." } else { retval = str } return retval } function unlink(name) { system(cmd_rm " " name) return } function util_init() { licenseurl[""] = "None" licenseurl["https://creativecommons.org/publicdomain/zero/1.0/"] = \ "Public Domain" licenseurl["https://creativecommons.org/licenses/by/4.0/"] = \ "Attribution 4.0 International" licenseurl["https://creativecommons.org/licenses/by-sa/4.0/"] = \ "Attribution-ShareAlike 4.0 International" licenseurl["https://creativecommons.org/licenses/by-nd/4.0/"] = \ "Attribution-NoDerivs 4.0 International" licenseurl["https://creativecommons.org/licenses/by-nc/4.0/"] = \ "Attribution-NonCommercial 4.0 International" licenseurl["https://creativecommons.org/licenses/by-nc-sa/4.0/"] = \ "Attribution-NonCommercial-ShareAlike 4.0 International" licenseurl["https://creativecommons.org/licenses/by-nc-nd/4.0/"] = \ "Attribution-NonCommercial-NoDerivs 4.0 International" mediatype["audio"] = "aud" mediatype["collection"] = "col" mediatype["data"] = "dat" mediatype["etree"] = "aud" mediatype["image"] = "img" mediatype["movies"] = "mov" mediatype["software"] = "bin" mediatype["texts"] = "txt" mediatype["web"] = "web" size_kb = 1024 size_mb = 1024 * 1024 size_gb = 1024 * 1024 * 1024 return }