bug-bash
[Top][All Lists]
Advanced

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

patch for support of $DATE and $DATEFORMAT


From: fraff
Subject: patch for support of $DATE and $DATEFORMAT
Date: Mon, 23 Jan 2006 12:24:04 +0100 (CET)

Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTY
PE='i686-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/local/share/locale' 
-DPACKAGE='bash' -DS
HELL -DHAVE_CONFIG_H  -I.  -I. -I./include -I./lib   -g -O2
uname output: Linux francois 2.6.15 #1 SMP Wed Jan 11 16:27:25 CET 2006 i686 
GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 3.0
Patch Level: 16
Release Status: release

Description:
        I'd like to have an internal variable $DATE that could be configured 
with $DATEFORMAT.
    but DATE=`date +$format` in ~/.bashrc is not optimized.

Repeat-By:

Fix:
    patch attached.
    Since I'm not a C guru, patch may not be secure or optimized.
    Version 3.1 did not compile on my system, I had to work on version 3.0.16.


patch:

--- old/bash-3.0.16/variables.c 2004-09-08 17:07:44.000000000 +0200
+++ new/bash-3.0.16/variables.c 2006-01-23 11:58:30.000000000 +0100
@@ -163,6 +163,10 @@
 static SHELL_VAR *init_dynamic_array_var __P((char *, sh_var_value_func_t *, 
sh_var_assign_func_t *
, int));
 #endif

+static SHELL_VAR *get_date __P((SHELL_VAR *));
+static SHELL_VAR *get_date_format __P((SHELL_VAR *));
+static SHELL_VAR *assign_date_format __P((SHELL_VAR *, char *, arrayind_t));
+
 static SHELL_VAR *assign_seconds __P((SHELL_VAR *, char *, arrayind_t));
 static SHELL_VAR *get_seconds __P((SHELL_VAR *));
 static SHELL_VAR *init_seconds_var __P((void));
@@ -1098,7 +1102,72 @@
   INIT_DYNAMIC_VAR ("SECONDS", (v ? value_cell (v) : (char *)NULL), 
get_seconds, assign_seconds);
   return v;
 }
-
+
+/* Value of $DATEFORMAT which control $DATE */
+char *date_format;
+
+static SHELL_VAR *
+init_date_format ()
+{
+  SHELL_VAR *v;
+
+  date_format = strdup ("%Y.%m.%d");
+  return v;
+}
+
+static SHELL_VAR *
+assign_date_format (self, value, unused)
+     SHELL_VAR *self;
+     char *value;
+     arrayind_t unused;
+{
+  free (date_format);
+  date_format = strdup(value);
+
+  return (self);
+}
+
+static SHELL_VAR *
+get_date_format (var)
+        SHELL_VAR *var;
+{
+  char *p;
+
+  p = savestring (date_format);
+
+  FREE (value_cell (var));
+  var_setvalue (var, p);
+
+  return (var);
+}
+
+
+static SHELL_VAR *
+get_date (var)
+        SHELL_VAR *var;
+{
+  char *p, date[40];
+  struct tm *tmp;
+  time_t t;
+  SHELL_VAR *v;
+
+  t = time (NULL);
+  tmp = localtime (&t);
+
+  if (tmp == NULL)
+    strcpy (p, "localtime returned an error");
+
+  else
+    strftime (date, sizeof (date), date_format, tmp);
+
+  p = savestring (date);
+
+  FREE (value_cell (var));
+  var_setvalue (var, p);
+
+  return (var);
+}
+
 /* The random number seed.  You can change this by setting RANDOM. */
 static unsigned long rseed = 1;
 static int last_random_value;
@@ -1396,6 +1465,7 @@
   SHELL_VAR *v;

   v = init_seconds_var ();
+  v = init_date_format ();

   INIT_DYNAMIC_VAR ("BASH_COMMAND", (char *)NULL, get_bash_command, 
(sh_var_assign_func_t *)NULL);
   INIT_DYNAMIC_VAR ("BASH_SUBSHELL", (char *)NULL, get_subshell, 
assign_subshell);
@@ -1403,6 +1473,9 @@
   INIT_DYNAMIC_VAR ("RANDOM", (char *)NULL, get_random, assign_random);
   INIT_DYNAMIC_VAR ("LINENO", (char *)NULL, get_lineno, assign_lineno);

+  INIT_DYNAMIC_VAR ("DATE", (char *)NULL, get_date, (sh_var_assign_func_t 
*)NULL);
+  INIT_DYNAMIC_VAR ("DATEFORMAT", (char *)NULL, get_date_format, 
assign_date_format);
+
 #if defined (HISTORY)
   INIT_DYNAMIC_VAR ("HISTCMD", (char *)NULL, get_histcmd, 
(sh_var_assign_func_t *)NULL);
 #endif




reply via email to

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