diff --git a/docs/src/manpage.md b/docs/src/manpage.md index a9620eefc6..3321f17987 100644 --- a/docs/src/manpage.md +++ b/docs/src/manpage.md @@ -225,7 +225,7 @@ MILLER(1) MILLER(1) percentiles pow qnorm reduce regextract regextract_or_else rightpad round roundm rstrip sec2dhms sec2gmt sec2gmtdate sec2hms sec2localdate sec2localtime select sgn sha1 sha256 sha512 sin sinh skewness sort sort_collection splita - splitax splitkv splitkvx splitnv splitnvx sqrt ssub stddev strfntime + splitax splitkv splitkvx splitnv splitnvx sqrt ssub stat stddev strfntime strfntime_local strftime strftime_local string strip strlen strmatch strmatchx strpntime strpntime_local strptime strptime_local sub substr substr0 substr1 sum sum2 sum3 sum4 sysntime system systime systimeint tan tanh tolower toupper @@ -2990,6 +2990,18 @@ MILLER(1) MILLER(1) Example: ssub("abc.def", ".", "X") gives "abcXdef" + 1mstat0m + (class=system #args=1) Returns a map containing information about the provided path: "name" with string value, "size" as decimal int value, "mode" as octal int value, "modtime" as int-valued epoch seconds, and "isdir" as boolean value. + Examples: + stat("./mlr") gives { + "name": "mlr", + "size": 38391584, + "mode": 0755, + "modtime": 1715207874, + "isdir": false + } + stat("./mlr")["size"] gives 38391584 + 1mstddev0m (class=stats #args=1) Returns the sample standard deviation of values in an array or map. Returns empty string AKA void for array/map of length less than two; returns error for non-array/non-map types. Example: @@ -3720,5 +3732,5 @@ MILLER(1) MILLER(1) - 2024-04-11 MILLER(1) + 2024-05-09 MILLER(1) diff --git a/docs/src/manpage.txt b/docs/src/manpage.txt index 65aa890cd6..5ce10b9603 100644 --- a/docs/src/manpage.txt +++ b/docs/src/manpage.txt @@ -204,7 +204,7 @@ MILLER(1) MILLER(1) percentiles pow qnorm reduce regextract regextract_or_else rightpad round roundm rstrip sec2dhms sec2gmt sec2gmtdate sec2hms sec2localdate sec2localtime select sgn sha1 sha256 sha512 sin sinh skewness sort sort_collection splita - splitax splitkv splitkvx splitnv splitnvx sqrt ssub stddev strfntime + splitax splitkv splitkvx splitnv splitnvx sqrt ssub stat stddev strfntime strfntime_local strftime strftime_local string strip strlen strmatch strmatchx strpntime strpntime_local strptime strptime_local sub substr substr0 substr1 sum sum2 sum3 sum4 sysntime system systime systimeint tan tanh tolower toupper @@ -2969,6 +2969,18 @@ MILLER(1) MILLER(1) Example: ssub("abc.def", ".", "X") gives "abcXdef" + 1mstat0m + (class=system #args=1) Returns a map containing information about the provided path: "name" with string value, "size" as decimal int value, "mode" as octal int value, "modtime" as int-valued epoch seconds, and "isdir" as boolean value. + Examples: + stat("./mlr") gives { + "name": "mlr", + "size": 38391584, + "mode": 0755, + "modtime": 1715207874, + "isdir": false + } + stat("./mlr")["size"] gives 38391584 + 1mstddev0m (class=stats #args=1) Returns the sample standard deviation of values in an array or map. Returns empty string AKA void for array/map of length less than two; returns error for non-array/non-map types. Example: @@ -3699,4 +3711,4 @@ MILLER(1) MILLER(1) - 2024-04-11 MILLER(1) + 2024-05-09 MILLER(1) diff --git a/docs/src/reference-dsl-builtin-functions.md b/docs/src/reference-dsl-builtin-functions.md index dbfafc04f8..529eddd778 100644 --- a/docs/src/reference-dsl-builtin-functions.md +++ b/docs/src/reference-dsl-builtin-functions.md @@ -76,7 +76,7 @@ is 2. Unary operators such as `!` and `~` show argument-count of 1; the ternary * [**Math functions**](#math-functions): [abs](#abs), [acos](#acos), [acosh](#acosh), [asin](#asin), [asinh](#asinh), [atan](#atan), [atan2](#atan2), [atanh](#atanh), [cbrt](#cbrt), [ceil](#ceil), [cos](#cos), [cosh](#cosh), [erf](#erf), [erfc](#erfc), [exp](#exp), [expm1](#expm1), [floor](#floor), [invqnorm](#invqnorm), [log](#log), [log10](#log10), [log1p](#log1p), [logifit](#logifit), [max](#max), [min](#min), [qnorm](#qnorm), [round](#round), [roundm](#roundm), [sgn](#sgn), [sin](#sin), [sinh](#sinh), [sqrt](#sqrt), [tan](#tan), [tanh](#tanh), [urand](#urand), [urand32](#urand32), [urandelement](#urandelement), [urandint](#urandint), [urandrange](#urandrange). * [**Stats functions**](#stats-functions): [antimode](#antimode), [count](#count), [distinct_count](#distinct_count), [kurtosis](#kurtosis), [maxlen](#maxlen), [mean](#mean), [meaneb](#meaneb), [median](#median), [minlen](#minlen), [mode](#mode), [null_count](#null_count), [percentile](#percentile), [percentiles](#percentiles), [skewness](#skewness), [sort_collection](#sort_collection), [stddev](#stddev), [sum](#sum), [sum2](#sum2), [sum3](#sum3), [sum4](#sum4), [variance](#variance). * [**String functions**](#string-functions): [capitalize](#capitalize), [clean_whitespace](#clean_whitespace), [collapse_whitespace](#collapse_whitespace), [contains](#contains), [format](#format), [gssub](#gssub), [gsub](#gsub), [index](#index), [latin1_to_utf8](#latin1_to_utf8), [leftpad](#leftpad), [lstrip](#lstrip), [regextract](#regextract), [regextract_or_else](#regextract_or_else), [rightpad](#rightpad), [rstrip](#rstrip), [ssub](#ssub), [strip](#strip), [strlen](#strlen), [strmatch](#strmatch), [strmatchx](#strmatchx), [sub](#sub), [substr](#substr), [substr0](#substr0), [substr1](#substr1), [tolower](#tolower), [toupper](#toupper), [truncate](#truncate), [unformat](#unformat), [unformatx](#unformatx), [utf8_to_latin1](#utf8_to_latin1), [\.](#dot). -* [**System functions**](#system-functions): [exec](#exec), [hostname](#hostname), [os](#os), [system](#system), [version](#version). +* [**System functions**](#system-functions): [exec](#exec), [hostname](#hostname), [os](#os), [stat](#stat), [system](#system), [version](#version). * [**Time functions**](#time-functions): [dhms2fsec](#dhms2fsec), [dhms2sec](#dhms2sec), [fsec2dhms](#fsec2dhms), [fsec2hms](#fsec2hms), [gmt2localtime](#gmt2localtime), [gmt2nsec](#gmt2nsec), [gmt2sec](#gmt2sec), [hms2fsec](#hms2fsec), [hms2sec](#hms2sec), [localtime2gmt](#localtime2gmt), [localtime2nsec](#localtime2nsec), [localtime2sec](#localtime2sec), [nsec2gmt](#nsec2gmt), [nsec2gmtdate](#nsec2gmtdate), [nsec2localdate](#nsec2localdate), [nsec2localtime](#nsec2localtime), [sec2dhms](#sec2dhms), [sec2gmt](#sec2gmt), [sec2gmtdate](#sec2gmtdate), [sec2hms](#sec2hms), [sec2localdate](#sec2localdate), [sec2localtime](#sec2localtime), [strfntime](#strfntime), [strfntime_local](#strfntime_local), [strftime](#strftime), [strftime_local](#strftime_local), [strpntime](#strpntime), [strpntime_local](#strpntime_local), [strptime](#strptime), [strptime_local](#strptime_local), [sysntime](#sysntime), [systime](#systime), [systimeint](#systimeint), [upntime](#upntime), [uptime](#uptime). * [**Typing functions**](#typing-functions): [asserting_absent](#asserting_absent), [asserting_array](#asserting_array), [asserting_bool](#asserting_bool), [asserting_boolean](#asserting_boolean), [asserting_empty](#asserting_empty), [asserting_empty_map](#asserting_empty_map), [asserting_error](#asserting_error), [asserting_float](#asserting_float), [asserting_int](#asserting_int), [asserting_map](#asserting_map), [asserting_nonempty_map](#asserting_nonempty_map), [asserting_not_array](#asserting_not_array), [asserting_not_empty](#asserting_not_empty), [asserting_not_map](#asserting_not_map), [asserting_not_null](#asserting_not_null), [asserting_null](#asserting_null), [asserting_numeric](#asserting_numeric), [asserting_present](#asserting_present), [asserting_string](#asserting_string), [is_absent](#is_absent), [is_array](#is_array), [is_bool](#is_bool), [is_boolean](#is_boolean), [is_empty](#is_empty), [is_empty_map](#is_empty_map), [is_error](#is_error), [is_float](#is_float), [is_int](#is_int), [is_map](#is_map), [is_nan](#is_nan), [is_nonempty_map](#is_nonempty_map), [is_not_array](#is_not_array), [is_not_empty](#is_not_empty), [is_not_map](#is_not_map), [is_not_null](#is_not_null), [is_null](#is_null), [is_numeric](#is_numeric), [is_present](#is_present), [is_string](#is_string), [typeof](#typeof). @@ -1502,6 +1502,21 @@ os (class=system #args=0) Returns the operating-system name as a string. +### stat +
+stat  (class=system #args=1) Returns a map containing information about the provided path: "name" with string value, "size" as decimal int value, "mode" as octal int value, "modtime" as int-valued epoch seconds, and "isdir" as boolean value.
+Examples:
+stat("./mlr") gives {
+  "name": "mlr",
+  "size": 38391584,
+  "mode": 0755,
+  "modtime": 1715207874,
+  "isdir": false
+}
+stat("./mlr")["size"] gives 38391584
+
+ + ### system
 system  (class=system #args=1) Run command string, yielding its stdout minus final carriage return.
diff --git a/man/manpage.txt b/man/manpage.txt
index 65aa890cd6..5ce10b9603 100644
--- a/man/manpage.txt
+++ b/man/manpage.txt
@@ -204,7 +204,7 @@ MILLER(1)                                                            MILLER(1)
        percentiles pow qnorm reduce regextract regextract_or_else rightpad round
        roundm rstrip sec2dhms sec2gmt sec2gmtdate sec2hms sec2localdate sec2localtime
        select sgn sha1 sha256 sha512 sin sinh skewness sort sort_collection splita
-       splitax splitkv splitkvx splitnv splitnvx sqrt ssub stddev strfntime
+       splitax splitkv splitkvx splitnv splitnvx sqrt ssub stat stddev strfntime
        strfntime_local strftime strftime_local string strip strlen strmatch strmatchx
        strpntime strpntime_local strptime strptime_local sub substr substr0 substr1
        sum sum2 sum3 sum4 sysntime system systime systimeint tan tanh tolower toupper
@@ -2969,6 +2969,18 @@ MILLER(1)                                                            MILLER(1)
        Example:
        ssub("abc.def", ".", "X") gives "abcXdef"
 
+   1mstat0m
+        (class=system #args=1) Returns a map containing information about the provided path: "name" with string value, "size" as decimal int value, "mode" as octal int value, "modtime" as int-valued epoch seconds, and "isdir" as boolean value.
+       Examples:
+       stat("./mlr") gives {
+         "name": "mlr",
+         "size": 38391584,
+         "mode": 0755,
+         "modtime": 1715207874,
+         "isdir": false
+       }
+       stat("./mlr")["size"] gives 38391584
+
    1mstddev0m
         (class=stats #args=1) Returns the sample standard deviation of values in an array or map. Returns empty string AKA void for array/map of length less than two; returns error for non-array/non-map types.
        Example:
@@ -3699,4 +3711,4 @@ MILLER(1)                                                            MILLER(1)
 
 
 
-                                  2024-04-11                         MILLER(1)
+                                  2024-05-09                         MILLER(1)
diff --git a/man/mlr.1 b/man/mlr.1
index 153ef86bda..d660b99a89 100644
--- a/man/mlr.1
+++ b/man/mlr.1
@@ -2,12 +2,12 @@
 .\"     Title: mlr
 .\"    Author: [see the "AUTHOR" section]
 .\" Generator: ./mkman.rb
-.\"      Date: 2024-04-11
+.\"      Date: 2024-05-09
 .\"    Manual: \ \&
 .\"    Source: \ \&
 .\"  Language: English
 .\"
-.TH "MILLER" "1" "2024-04-11" "\ \&" "\ \&"
+.TH "MILLER" "1" "2024-05-09" "\ \&" "\ \&"
 .\" -----------------------------------------------------------------
 .\" * Portability definitions
 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -251,7 +251,7 @@ nsec2gmt nsec2gmtdate nsec2localdate nsec2localtime null_count os percentile
 percentiles pow qnorm reduce regextract regextract_or_else rightpad round
 roundm rstrip sec2dhms sec2gmt sec2gmtdate sec2hms sec2localdate sec2localtime
 select sgn sha1 sha256 sha512 sin sinh skewness sort sort_collection splita
-splitax splitkv splitkvx splitnv splitnvx sqrt ssub stddev strfntime
+splitax splitkv splitkvx splitnv splitnvx sqrt ssub stat stddev strfntime
 strfntime_local strftime strftime_local string strip strlen strmatch strmatchx
 strpntime strpntime_local strptime strptime_local sub substr substr0 substr1
 sum sum2 sum3 sum4 sysntime system systime systimeint tan tanh tolower toupper
@@ -4602,6 +4602,24 @@ ssub("abc.def", ".", "X") gives "abcXdef"
 .fi
 .if n \{\
 .RE
+.SS "stat"
+.if n \{\
+.RS 0
+.\}
+.nf
+ (class=system #args=1) Returns a map containing information about the provided path: "name" with string value, "size" as decimal int value, "mode" as octal int value, "modtime" as int-valued epoch seconds, and "isdir" as boolean value.
+Examples:
+stat("./mlr") gives {
+  "name": "mlr",
+  "size": 38391584,
+  "mode": 0755,
+  "modtime": 1715207874,
+  "isdir": false
+}
+stat("./mlr")["size"] gives 38391584
+.fi
+.if n \{\
+.RE
 .SS "stddev"
 .if n \{\
 .RS 0
diff --git a/pkg/bifs/system.go b/pkg/bifs/system.go
index d56f0bb669..e2044b4b8c 100644
--- a/pkg/bifs/system.go
+++ b/pkg/bifs/system.go
@@ -102,3 +102,25 @@ func BIF_exec(mlrvals []*mlrval.Mlrval) *mlrval.Mlrval {
 	outputString := strings.TrimRight(string(outputBytes), "\n")
 	return mlrval.FromString(outputString)
 }
+
+func BIF_stat(input1 *mlrval.Mlrval) *mlrval.Mlrval {
+	if !input1.IsStringOrVoid() {
+		return mlrval.FromNotStringError("system", input1)
+	}
+	path := input1.AcquireStringValue()
+
+	fileInfo, err := os.Stat(path)
+
+	if err != nil {
+		return mlrval.FromError(err)
+	}
+
+	output := mlrval.NewMlrmap()
+	output.PutReference("name", mlrval.FromString(fileInfo.Name()))
+	output.PutReference("size", mlrval.FromInt(fileInfo.Size()))
+	output.PutReference("mode", mlrval.FromIntShowingOctal(int64(fileInfo.Mode())))
+	output.PutReference("modtime", mlrval.FromInt(fileInfo.ModTime().UTC().Unix()))
+	output.PutReference("isdir", mlrval.FromBool(fileInfo.IsDir()))
+
+	return mlrval.FromMap(output)
+}
diff --git a/pkg/dsl/cst/builtin_function_manager.go b/pkg/dsl/cst/builtin_function_manager.go
index 90f854fe01..ec4bfa9808 100644
--- a/pkg/dsl/cst/builtin_function_manager.go
+++ b/pkg/dsl/cst/builtin_function_manager.go
@@ -2487,6 +2487,23 @@ Run a command via executable, path, args and environment, yielding its stdout mi
 			variadicFunc: bifs.BIF_exec,
 		},
 
+		{
+			name:      "stat",
+			class:     FUNC_CLASS_SYSTEM,
+			help:      `Returns a map containing information about the provided path: "name" with string value, "size" as decimal int value, "mode" as octal int value, "modtime" as int-valued epoch seconds, and "isdir" as boolean value.`,
+			unaryFunc: bifs.BIF_stat,
+			examples: []string{
+				`stat("./mlr") gives {`,
+				`  "name": "mlr",`,
+				`  "size": 38391584,`,
+				`  "mode": 0755,`,
+				`  "modtime": 1715207874,`,
+				`  "isdir": false`,
+				`}`,
+				`stat("./mlr")["size"] gives 38391584`,
+			},
+		},
+
 		{
 			name:     "version",
 			class:    FUNC_CLASS_SYSTEM,
diff --git a/pkg/mlrval/mlrval_new.go b/pkg/mlrval/mlrval_new.go
index cb548c3d0c..c4109af262 100644
--- a/pkg/mlrval/mlrval_new.go
+++ b/pkg/mlrval/mlrval_new.go
@@ -197,6 +197,15 @@ func FromInt(input int64) *Mlrval {
 	}
 }
 
+func FromIntShowingOctal(input int64) *Mlrval {
+	return &Mlrval{
+		mvtype:        MT_INT,
+		printrepValid: true,
+		printrep:      fmt.Sprintf("0%o", input),
+		intf:          input,
+	}
+}
+
 // TryFromIntString is used by the mlrval Formatter (fmtnum DSL function,
 // format-values verb, etc).  Each mlrval has printrep and a printrepValid for
 // its original string, then a type-code like MT_INT or MT_FLOAT, and
diff --git a/test/cases/dsl-stat/0001/cmd b/test/cases/dsl-stat/0001/cmd
new file mode 100644
index 0000000000..94b141d0d5
--- /dev/null
+++ b/test/cases/dsl-stat/0001/cmd
@@ -0,0 +1 @@
+mlr --icsv --ojson put -f ${CASEDIR}/mlr ${CASEDIR}/input.csv
diff --git a/test/cases/dsl-stat/0001/experr b/test/cases/dsl-stat/0001/experr
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/cases/dsl-stat/0001/expout b/test/cases/dsl-stat/0001/expout
new file mode 100644
index 0000000000..dcb25f94ba
--- /dev/null
+++ b/test/cases/dsl-stat/0001/expout
@@ -0,0 +1,12 @@
+[
+{
+  "path": "test/cases/dsl-stat/0001/input.csv",
+  "name": "input.csv",
+  "isdir": false
+},
+{
+  "path": "test/cases/dsl-stat/0001/",
+  "name": "0001",
+  "isdir": true
+}
+]
diff --git a/test/cases/dsl-stat/0001/input.csv b/test/cases/dsl-stat/0001/input.csv
new file mode 100644
index 0000000000..d2731b359a
--- /dev/null
+++ b/test/cases/dsl-stat/0001/input.csv
@@ -0,0 +1,3 @@
+path
+test/cases/dsl-stat/0001/input.csv
+test/cases/dsl-stat/0001/
diff --git a/test/cases/dsl-stat/0001/mlr b/test/cases/dsl-stat/0001/mlr
new file mode 100644
index 0000000000..195f1c6a76
--- /dev/null
+++ b/test/cases/dsl-stat/0001/mlr
@@ -0,0 +1,3 @@
+s = stat($path);
+$name  = s["name"];
+$isdir = s["isdir"];