xiaoxiang781216 commented on code in PR #3875:
URL: https://github.com/apache/nuttx-apps/pull/3875#discussion_r4308465214


##########
nshlib/nsh.h:
##########
@@ -1191,6 +1191,10 @@ int cmd_switchboot(FAR struct nsh_vtbl_s *vtbl, int 
argc, FAR char **argv);
   int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT

Review Comment:
   move before line 1190



##########
nshlib/nsh_command.c:
##########
@@ -665,6 +665,11 @@ static const struct cmdmap_s g_cmdmap[] =
 #  endif
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT

Review Comment:
   move before line 660



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)
+    {
+      switch (option)
+        {
+          case 'H':
+            hard = true;
+            break;
+
+          case 'S':
+            soft = true;
+            break;
+
+          case 'a':
+            all = true;
+            break;
+
+          default:
+            for (i = 0; i < nitems(g_ulimit_resources); i++)
+              {
+                if (g_ulimit_opts[i] == option)
+                  {
+                    sel = i;
+                    break;
+                  }
+              }
+
+            if (i == nitems(g_ulimit_resources))
+              {
+                nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+                badarg = true;
+              }
+            break;
+        }
+    }
+
+  if (badarg || argc - optind > 1 || (all && argc - optind > 0))
+    {
+      if (!badarg)
+        {
+          nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
+        }
+
+      return ERROR;
+    }
+
+  /* Show the limits: the soft limit, or the hard limit with -H */
+
+  if (argc == optind)
+    {
+      first = all ? 0 : sel;
+      last  = all ? nitems(g_ulimit_resources) - 1 : sel;
+      for (i = first; i <= last; i++)
+        {
+          if (getrlimit(g_ulimit_resources[i], &rl) < 0)
+            {
+              nsh_error(vtbl, g_fmtcmdfailed, argv[0], "getrlimit",
+                        NSH_ERRNO);
+              return ERROR;
+            }
+
+          shown = hard ? rl.rlim_max : rl.rlim_cur;
+          if (all)
+            {
+              nsh_output(vtbl, "%-26s (-%c) ", g_ulimit_names[i],
+                         g_ulimit_opts[i]);
+            }
+
+          if (shown == RLIM_INFINITY)
+            {
+              nsh_output(vtbl, "unlimited\n");
+            }
+          else
+            {
+              nsh_output(vtbl, "%llu\n",
+                         (unsigned long long)(shown / g_ulimit_units[i]));

Review Comment:
   remove the cast



##########
nshlib/nsh_syscmds.c:
##########
@@ -89,6 +91,30 @@
 static const char g_unknown[] = "unknown";
 #endif
 
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+/* The resources of ulimit: option, resource, bytes per unit and name */
+
+static const char g_ulimit_opts[] = "cdflnstv";

Review Comment:
   `static const char g_ulimit_opts[] = "cdflnstvHSa";`



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)
+    {
+      switch (option)
+        {
+          case 'H':
+            hard = true;
+            break;
+
+          case 'S':
+            soft = true;
+            break;
+
+          case 'a':
+            all = true;
+            break;
+
+          default:
+            for (i = 0; i < nitems(g_ulimit_resources); i++)
+              {
+                if (g_ulimit_opts[i] == option)
+                  {
+                    sel = i;
+                    break;
+                  }
+              }
+
+            if (i == nitems(g_ulimit_resources))
+              {
+                nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+                badarg = true;
+              }
+            break;
+        }
+    }
+
+  if (badarg || argc - optind > 1 || (all && argc - optind > 0))
+    {
+      if (!badarg)
+        {
+          nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
+        }
+
+      return ERROR;
+    }
+
+  /* Show the limits: the soft limit, or the hard limit with -H */
+
+  if (argc == optind)
+    {
+      first = all ? 0 : sel;
+      last  = all ? nitems(g_ulimit_resources) - 1 : sel;
+      for (i = first; i <= last; i++)
+        {
+          if (getrlimit(g_ulimit_resources[i], &rl) < 0)
+            {
+              nsh_error(vtbl, g_fmtcmdfailed, argv[0], "getrlimit",
+                        NSH_ERRNO);
+              return ERROR;
+            }
+
+          shown = hard ? rl.rlim_max : rl.rlim_cur;
+          if (all)
+            {
+              nsh_output(vtbl, "%-26s (-%c) ", g_ulimit_names[i],
+                         g_ulimit_opts[i]);
+            }
+
+          if (shown == RLIM_INFINITY)
+            {
+              nsh_output(vtbl, "unlimited\n");
+            }
+          else
+            {
+              nsh_output(vtbl, "%llu\n",
+                         (unsigned long long)(shown / g_ulimit_units[i]));
+            }
+        }
+
+      return OK;
+    }
+
+  /* Set a limit: both the soft and the hard limit without -H or -S */
+
+  if (strcmp(argv[optind], "unlimited") == 0)
+    {
+      value = RLIM_INFINITY;
+    }
+  else
+    {
+      number = strtoull(argv[optind], &endptr, 10);
+      if (*argv[optind] == '\0' || *endptr != '\0' ||
+          number >= RLIM_INFINITY / g_ulimit_units[sel])
+        {
+          nsh_error(vtbl, g_fmtarginvalid, argv[0]);
+          return ERROR;
+        }
+
+      value = (rlim_t)number * g_ulimit_units[sel];

Review Comment:
   remove the cast too



##########
nshlib/nsh_syscmds.c:
##########
@@ -932,3 +958,151 @@ int cmd_uname(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
   return OK;
 }
 #endif
+
+/****************************************************************************
+ * Name: cmd_ulimit
+ ****************************************************************************/
+
+#ifndef CONFIG_NSH_DISABLE_ULIMIT
+int cmd_ulimit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
+{
+  unsigned long long number;
+  FAR char *endptr;
+  struct rlimit rl;
+  rlim_t value;
+  rlim_t shown;
+  bool hard = false;
+  bool soft = false;
+  bool all = false;
+  bool badarg = false;
+  int sel = 2;       /* -f, as POSIX says */
+  int option;
+  int first;
+  int last;
+  int i;
+
+  while ((option = getopt(argc, argv, ":HSacdflnstv")) != ERROR)

Review Comment:
   replace  ":HSacdflnstv" with g_ulimit_opts



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to