help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Check if file size greater than a small number?


From: Jesse Hathaway
Subject: Re: Check if file size greater than a small number?
Date: Thu, 10 Mar 2022 10:30:54 -0600

On Thu, Mar 10, 2022 at 9:50 AM Chet Ramey <chet.ramey@case.edu> wrote:
>
> On 3/9/22 10:48 PM, Peng Yu wrote:
>
> > This seems to be buggy. When I put stat in a function, and declare a
> > variable that is used as the -A option of stat. Nothing will be saved
> > in the variable.
>
> Thanks for the report.

Below is my attempt at a fix, any feedback would be appreciated:

--- stat.orig.c 2022-03-10 10:27:48.529197073 -0600
+++ stat.c 2022-03-10 10:28:53.120944839 -0600
@@ -35,6 +35,7 @@
 #include <grp.h>
 #include <errno.h>
 #include "posixtime.h"
+#include <stdbool.h>

 #include "bashansi.h"
 #include "shell.h"
@@ -303,12 +304,11 @@
 {
   int i;
   char *key, *value;
-  SHELL_VAR *v;

   for (i = 0; arraysubs[i]; i++) {
     key = savestring(arraysubs[i]);
     value = statval(i, fname, flags, sp);
-    v = bind_assoc_variable(var, vname, key, value, ASS_FORCE);
+    bind_assoc_variable(var, vname, key, value, ASS_FORCE);
   }
   return 0;
 }
@@ -318,17 +318,21 @@
   int opt, flags;
   char *aname, *fname;
   struct stat st;
+  bool global_vars = false;
   SHELL_VAR *v;

   aname = "STAT";
   flags = 0;

   reset_internal_getopt();
-  while ((opt = internal_getopt(list, "A:Ll")) != -1) {
+  while ((opt = internal_getopt(list, "A:gLl")) != -1) {
     switch (opt) {
     case 'A':
       aname = list_optarg;
       break;
+    case 'g':
+      global_vars = true;
+      break;
     case 'L':
       flags |= 1; /* operate on links rather than resolving them */
       break;
@@ -356,7 +360,11 @@
   }

   unbind_variable(aname);
-  v = make_new_assoc_variable(aname);
+  if (variable_context && !global_vars) {
+    v = make_local_assoc_variable(aname, 0);
+  } else {
+    v = make_new_assoc_variable(aname);
+  }
   if (v == 0) {
     builtin_error("%s: cannot create variable", aname);
     return (EXECUTION_FAILURE);
@@ -378,21 +386,23 @@
     "",
     "Take a filename and load the status information returned by a",
     "stat(2) call on that file into the associative array specified",
-    "by the -A option.  The default array name is STAT.  If the -L",
-    "option is supplied, stat does not resolve symbolic links and",
-    "reports information about the link itself.  The -l option results",
-    "in longer-form listings for some of the fields. The exit status is 0",
-    "unless the stat fails or assigning the array is unsuccessful.",
+    "by the -A option.  The default array name is STAT.  Variables",
+    "are created with local scope inside a function unless the -g option",
+    "is specified.  If the -L option is supplied, stat does not resolve",
+    "symbolic links and reports information about the link itself.  The",
+    "-l option results in longer-form listings for some of the fields.",
+    "The exit status is 0 unless the stat fails or assigning the array",
+    "is unsuccessful.",
     (char *)NULL};

 /* The standard structure describing a builtin command.  bash keeps an array
    of these structures.  The flags must include BUILTIN_ENABLED so the
    builtin can be used. */
 struct builtin stat_struct = {
-    "stat",                       /* builtin name */
-    stat_builtin,                 /* function implementing the builtin */
-    BUILTIN_ENABLED,              /* initial flags for builtin */
-    stat_doc,                     /* array of long documentation strings. */
-    "stat [-lL] [-A aname] file", /* usage synopsis; becomes short_doc */
-    0                             /* reserved for internal use */
+    "stat",                        /* builtin name */
+    stat_builtin,                  /* function implementing the builtin */
+    BUILTIN_ENABLED,               /* initial flags for builtin */
+    stat_doc,                      /* array of long documentation strings. */
+    "stat [-glL] [-A aname] file", /* usage synopsis; becomes short_doc */
+    0                              /* reserved for internal use */
 };



reply via email to

[Prev in Thread] Current Thread [Next in Thread]