openenvutils/commandshell/shell/src/modules/datetime.c
changeset 0 2e3d3ce01487
child 1 0fdb7f6b0309
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // datetime.c - parameter interface to langinfo via curses
       
     2 //
       
     3 // © Portions Copyright (c) Symbian Software Ltd 2007. All rights reserved.
       
     4 //
       
     5 /*
       
     6  * This file is part of zsh, the Z shell.
       
     7  *
       
     8  * Copyright (c) 2002 Peter Stephenson, Clint Adams
       
     9  * All rights reserved.
       
    10  *
       
    11  * Permission is hereby granted, without written agreement and without
       
    12  * license or royalty fees, to use, copy, modify, and distribute this
       
    13  * software and to distribute modified versions of this software for any
       
    14  * purpose, provided that the above copyright notice and the following
       
    15  * two paragraphs appear in all copies of this software.
       
    16  *
       
    17  * In no event shall Peter Stephenson, Clint Adams or the Zsh Development Group
       
    18  * be liable to any party for direct, indirect, special, incidental, or
       
    19  * consequential damages arising out of the use of this software and its
       
    20  * documentation, even if Peter Stephenson, Clint Adams and the Zsh
       
    21  * Development Group have been advised of the possibility of such damage.
       
    22  *
       
    23  * Peter Stephenson, Clint Adams and the Zsh Development Group specifically
       
    24  * disclaim any warranties, including, but not limited to, the implied
       
    25  * warranties of merchantability and fitness for a particular purpose.
       
    26  * The software provided hereunder is on an "as is" basis, and Peter
       
    27  * Stephenson, Clint Adams and the Zsh Development Group have no obligation
       
    28  * to provide maintenance, support, updates, enhancements, or modifications.
       
    29  *
       
    30  */
       
    31 #include "datetime.mdh"
       
    32 #include "datetime.pro"
       
    33 #include <time.h>
       
    34 
       
    35 #ifdef __SYMBIAN32__
       
    36 #ifdef __WINSCW__
       
    37 #pragma warn_unusedarg off
       
    38 #endif//__WINSCW__
       
    39 #endif//__SYMBIAN32__
       
    40 
       
    41 static int
       
    42 bin_strftime(char *nam, char **argv, Options ops, UNUSED(int func))
       
    43 {
       
    44     int bufsize, x;
       
    45     char *endptr = NULL, *scalar = NULL, *buffer;
       
    46     time_t secs;
       
    47     struct tm *t;
       
    48 
       
    49     if (OPT_ISSET(ops,'s')) {
       
    50 	scalar = OPT_ARG(ops, 's');
       
    51 	if (!isident(scalar)) {
       
    52 	    zwarnnam(nam, "not an identifier: %s", scalar, 0);
       
    53 	    return 1;
       
    54 	}
       
    55     }
       
    56 
       
    57     secs = (time_t)strtoul(argv[1], &endptr, 10);
       
    58     if (secs == (time_t)ULONG_MAX) {
       
    59 	zwarnnam(nam, "%s: %e", argv[1], errno);
       
    60 	return 1;
       
    61     } else if (*endptr != '\0') {
       
    62 	zwarnnam(nam, "%s: invalid decimal number", argv[1], 0);
       
    63 	return 1;
       
    64     }
       
    65 
       
    66     t = localtime(&secs);
       
    67     bufsize = strlen(argv[0]) * 2;
       
    68     buffer = zalloc(bufsize);
       
    69 
       
    70     for (x=0; x < 4; x++) {
       
    71         if (ztrftime(buffer, bufsize, argv[0], t) >= 0)
       
    72 	    break;
       
    73 	buffer = zrealloc(buffer, bufsize *= 2);
       
    74     }
       
    75 
       
    76     if (scalar) {
       
    77 	setsparam(scalar, ztrdup(buffer));
       
    78     } else {
       
    79 	printf("%s\n", buffer);
       
    80     }
       
    81     zfree(buffer, bufsize);
       
    82 
       
    83     return 0;
       
    84 }
       
    85 
       
    86 static zlong
       
    87 getcurrentsecs(Param pm)
       
    88 {
       
    89     return (zlong) time(NULL);
       
    90 }
       
    91 
       
    92 static struct builtin bintab[] = {
       
    93     BUILTIN("strftime",    0, bin_strftime,    2,   2, 0, "s:", NULL),
       
    94 };
       
    95 
       
    96 static const struct gsu_integer epochseconds_gsu =
       
    97 { getcurrentsecs, NULL, stdunsetfn };
       
    98 
       
    99 static struct paramdef patab[] = {
       
   100     PARAMDEF("EPOCHSECONDS", PM_INTEGER|PM_SPECIAL|PM_READONLY,
       
   101 		    NULL, &epochseconds_gsu),
       
   102 };
       
   103 
       
   104 /**/
       
   105 int
       
   106 setup_(UNUSED(Module m))
       
   107 {
       
   108     return 0;
       
   109 }
       
   110 
       
   111 /**/
       
   112 int
       
   113 boot_(Module m)
       
   114 {
       
   115     return !(addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab)) |
       
   116 	     addparamdefs(m->nam, patab, sizeof(patab)/sizeof(*patab))
       
   117 	    );
       
   118 }
       
   119 
       
   120 /**/
       
   121 int
       
   122 cleanup_(Module m)
       
   123 {
       
   124     Param pm;
       
   125 
       
   126     deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
       
   127     pm = (Param) paramtab->getnode(paramtab, "EPOCHSECONDS");
       
   128     if (pm && (pm->flags & PM_SPECIAL)) {
       
   129 	pm->flags &= ~PM_READONLY;
       
   130 	unsetparam_pm(pm, 0, 1);
       
   131     }
       
   132     return 0;
       
   133 }
       
   134 
       
   135 /**/
       
   136 int
       
   137 finish_(UNUSED(Module m))
       
   138 {
       
   139     return 0;
       
   140 }