This email list is read-only.  Emails sent to this list will be discarded
----------------------------------
 ChangeLog                                |    7 +
 moblin-system-daemon/datetime/datetime.c |  190 +++++++++++++++++++++---------
 moblin-system-daemon/datetime/datetime.h |    2 +-
 3 files changed, 142 insertions(+), 57 deletions(-)

New commits:
commit f1aab736fa55aa622853277b406add7a379230cd
Author: Lilli Szafranski <[EMAIL PROTECTED]>
Date:   Fri Aug 15 16:12:34 2008 -0700

    Added comments and minor fixes


Diff in this email is a maximum of 400 lines.
diff --git a/ChangeLog b/ChangeLog
index ee2e8c4..b6f0b07 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+moblin-settings (2.17)
+
+  * Added comments and fixed get_all_timezones to not accept options values 
larger
+    than 16 
+
+ -- Lilli Szafranski <[EMAIL PROTECTED]>  Fri Aug 15 16:10:56 PDT 2008
+
 moblin-settings (2.16)
 
   * Changed set_timezone to remember the current time so that when the
diff --git a/moblin-system-daemon/datetime/datetime.c 
b/moblin-system-daemon/datetime/datetime.c
index 7e14830..8b60bc8 100644
--- a/moblin-system-daemon/datetime/datetime.c
+++ b/moblin-system-daemon/datetime/datetime.c
@@ -4,15 +4,17 @@
 #include "moblin-system-dbus.h"
 #include <glib/gi18n.h>
 
-// lilli
 #include <sys/time.h>
 #include <errno.h>
 #include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
-// Bitmask values for get_all_timezones options.
-#define TZ_ALL         16      // 0b00010000
+// Bitmap values for the get_all_timezones interger function argument: options.
+// The function can return any combination of the timezone name, latitude and
+// longitude, comment, and 2-letter alphabetical code, depending on how the
+// caller passes the argument as a bit-wise-or with the following bitmaps
+#define TZ_ALL         16      // 0b00001111
 #define TZ_CODE                8       // 0b00001000
 #define TZ_LATLON      4       // 0b00000100
 #define TZ_NAME                2       // 0b00000010
@@ -20,24 +22,25 @@
 
 #define AVG_LINE_LEN 40        // Using a Perl script, I found the average 
length
                                                // per line in the 
/usr/share/zoneinfo/zone.tab file
-                                               // was about 43 characters.  
The best optimize memory
-                                               // allocation for the 
get_all_timezones array of char*s
-                                               // I will divide the length of 
the file in characters 
-                                               // by the line length to get an 
approx of the number of 
-                                               // timezones so not to allocate 
way too many or too few
-                                               // char**s
+                                               // was about 43 characters.  To 
best optimize memory
+                                               // allocation for the 
get_all_timezones array of 
+                                               // char*s, I will divide the 
length of the file (in 
+                                               // characters) by the average 
line length (43).
+                                               // This will return the approx 
of the number of lines 
+                                               // in the zone.tab file (with 1 
timezones per line)
+                                               // so not to allocate way too 
many or too few char**s
+                                               // for different (and unknown) 
versions of the file
 
 // These are valid on most machines except for older
 // versions of RedHat, I believe...
-// TODO: add preproc directives to check for this
+// TODO: add preproc directives or configure flags to check for this
 #define LOCAL_INFO_DIR "/usr/share/zoneinfo/"                                  
        
 #define ETC_LOCAL_DIR "/etc/localtime"
 #define FILE_ZONE_TAB "/usr/share/zoneinfo/zone.tab"
                        
-// end lilli
-
+                       
 extern GQuark error_quark;
-enum
+enum // most error enumerations listed as <HEADERFILE>_<FUNCTION>_ERROR
 {
        INVALID_INPUT,
        STDLIB_MALLOC_ERROR,
@@ -56,21 +59,29 @@ enum
        UNISTD_READLINK_ERROR
 };
 
-gboolean __verify_time_input(const gint year, const gint month, const gint 
day, 
+gboolean __validate_time_input(const gint year, const gint month, const gint 
day, 
                                                         const gint hour, const 
gint minute, const gint second, 
                                                         char** errorMsg)
 {
-
-       if( INT_MAX <= 2147483647 )
+       
+       // First this function checks that the caller isn't trying to set the 
time
+       // past the year 2035 on a 32-bit machine.  Because the epoch is stored 
as
+       // 32-bit int, the number overflows in 2038.  This may be patched on 
many 
+       // machines but it kept failing on mine.
+       if( INT_MAX <= 2147483647 ) // this is the size of an integer on a 
32-bit machine
        {
                PRINTF("test\n");
                
+               // If year is between 0 and 100 (YY format instead of YYYY), I 
add 2000
+               // to it, so I have to check to make sure it isn't between 38 & 
100 too.
                if( ( (year < 100) && (year >= 38) ) || (year >= 2038) )
                {
                        *errorMsg = "32-bit Linux machines do not support 
time/dates past\n\t03:14:07 GMT Tuesday, January 19, 2038.";
                        return FALSE;
                }
                
+               // Even if you aren't past 2038, applications may use time 
values in the future
+               // so for now I am not even letting you set the time within 3 
years of 2038.
                if( ( (year < 100) && (year >= 35) ) || (year >= 2035) )
                {
                        *errorMsg = "You are within three years of 03:14:07 GMT 
Tuesday, January 19, 2038.\n\tGet a 64-bit device.";
@@ -78,15 +89,18 @@ gboolean __verify_time_input(const gint year, const gint 
month, const gint day,
                }       
        }
        
+       // If the caller messes up two inputs, I still only return the first as 
an
+       // error.  It was easier than mallocing error and concatenating several 
+       // error messages.
        if( (month > 12)  || (month < 1) )
        {
                *errorMsg = "Month must be between 1 and 12.";
                return FALSE;
        }
        
-       if( (day > 31) && (day < 1) )
+       if( (day > 31) || (day < 1) )
        {
-               *errorMsg = "Day must be between 1 and 29 if it's February.";
+               *errorMsg = "Day must be between 1 and 31.";
                return FALSE;
        }
        
@@ -120,8 +134,14 @@ system_daemon_set_time(MoblinSystemServer * server,
        PRINTF("[system_daemon_set_time]:\n\tyear=%d, month=%d, 
day=%d\n\thour=%d, minute=%d, second=%d\n",
        year, month, day, hour, minute, second);
        
+       int retVal = 0;
+       
+       // Validating the input
+       if(!*error)
+               *error = NULL;
+       
        char* errorMsg = NULL;
-       if(!__verify_time_input(year, month, day, hour, minute, second, 
&errorMsg))
+       if( !__validate_time_input(year, month, day, hour, minute, second, 
&errorMsg) )
        {
                g_set_error (error, error_quark, INVALID_INPUT,
                        errorMsg);
@@ -130,10 +150,9 @@ system_daemon_set_time(MoblinSystemServer * server,
                return FALSE;
        }
        
-       int retVal = 0;
-       
+       // timeval struct used to get the epoc in seconds from the time struct
        struct timeval* timev = (struct timeval*)malloc(sizeof(struct timeval));
-       if(!timev)
+       if( !timev )
        {
                g_set_error (error, error_quark, STDLIB_MALLOC_ERROR,
                        "malloc failed");
@@ -144,8 +163,14 @@ system_daemon_set_time(MoblinSystemServer * server,
        }
        memset(timev, 0, sizeof(struct timeval));
        
+       // Timezones and Daylight Savings Time cannot be determined by using 
one algorithm
+       // for an entire country or continent because of unpredictable 
political decisions in the past
+       // and future which may change the zones and DST periods.  Therefore, 
the old method 
+       // of setting timezones has been replaced by setting a symbolic link to 
a timezones 
+       // rule file. Calls to settimeofday() should set the tz_dsttime field 
to NULL.
     struct timezone* timez = NULL;
     
+       // time struct used to hold the time/date values passed by the caller
        struct tm* time = (struct tm*)malloc(sizeof(struct tm));
        if(!time)
        {
@@ -161,6 +186,10 @@ system_daemon_set_time(MoblinSystemServer * server,
        }
        memset(time, 0, sizeof(struct tm));
        
+       // setting time struct members with values passed by the caller
+       // with tm_isdst set to -1 (no daylight savings time set)
+       // because this will be configured with the rules in the 
+       // timezone file set in set_timezone
        time->tm_isdst  = -1;
        time->tm_sec    = second;
        time->tm_min    = minute;
@@ -168,12 +197,14 @@ system_daemon_set_time(MoblinSystemServer * server,
        time->tm_mday   = day;
        time->tm_mon    = month - 1;
                
+       // if year is in format YY instead of YYYY, I am adding 
+       // 2000 to make it a YYYY value
        if( (year < 100) && (year > -1) )
-               time->tm_year   = year + 2000 - 1900;
+               time->tm_year   = year + 2000 - 1900; // mktime expects the 
year - 1900
        else
                time->tm_year   = year - 1900;
                
-       // Getting the seconds since the Epoch from the time structure
+       // mktime returns the seconds since the Epoch given the time struct
        timev->tv_sec = mktime(time);
        if(timev->tv_sec == (time_t)(-1)) 
        {
@@ -192,9 +223,11 @@ system_daemon_set_time(MoblinSystemServer * server,
        }
     PRINTF("[system_daemon_set_time] mktime successful:\n\tepoch = %d\n", 
((int)timev->tv_sec));
        
-       // This function does not support milliseconds
+       // The system_daemon_set_time function does not support milliseconds
        timev->tv_usec = 0;
 
+       // settimeofday will set the system time to UTC given the number of 
+       // seconds after the epoch
     retVal = settimeofday(timev, timez);
        if(retVal) 
        {
@@ -236,8 +269,25 @@ system_daemon_get_time(MoblinSystemServer * server,
     struct timeval timev;
     struct timezone timez;
        
-       struct tm* getTime;
-
+       struct tm* getTime = NULL;
+
+       if(!*year)
+               *year = 0;
+       if(!*month)
+               *month = 0;
+       if(!*day)
+               *day = 0;
+       if(!*hour)
+               *hour = 0;
+       if(!*minute)
+               *minute = 0;
+       if(!*second)
+               *second = 0;
+       if(!*error)
+               *error = NULL;
+       
+       // gettimeofday will return the current system time in seconds
+       // from the epoc into the timev struct
        retVal = gettimeofday(&timev, &timez);
        if(retVal) 
        {
@@ -250,6 +300,8 @@ system_daemon_get_time(MoblinSystemServer * server,
     }
        PRINTF("[system_daemon_get_time] gettimeofday successful:\n\tepoch = 
%d\n", ((int)timev.tv_sec));
        
+       // localtime will convert the seconds from epoch to a time struct
+       // so that we can extract the hours, minutes, secones, etc.
        getTime = localtime(&timev.tv_sec);
        if(getTime == NULL)
        {
@@ -260,9 +312,9 @@ system_daemon_get_time(MoblinSystemServer * server,
 
                return FALSE;
        }
-       
        PRINTF ( "[system_daemon_get_time] localtime successful:\n\ttime = 
%s\n", asctime (getTime) );
   
+       // set the values to be returned to the caller
        *year   = (gint)(getTime->tm_year + 1900);
        *month  = (gint)(getTime->tm_mon + 1);
        *day    = (gint)(getTime->tm_mday);
@@ -299,11 +351,13 @@ system_daemon_get_all_timezones(MoblinSystemServer * 
server,
        int arrLen = 0, approxLines = 0;
        int arrIter = 0;
                
-       if(!options) //  if options == NULL, set options to be the default 
which is b00000010
-       {
+       if(!options) // if options == NULL, set options to be the default which 
is b00000010
                options = TZ_NAME;      
-       }
+       if(options > 16)
+               options = TZ_ALL;
                
+       // The file /usr/share/localinfo/zone.tab is a machine parsable file 
that
+       // lists all of the available system timezones in tab deliminated 
columns
        pFile = fopen(FILE_ZONE_TAB, "r");
        if(pFile == NULL)
        {
@@ -319,12 +373,11 @@ system_daemon_get_all_timezones(MoblinSystemServer * 
server,
        }
        PRINTF("[system_daemon_get_all_timezones] fopen successful:\n\t%s\n", 
strerror(errno));
        
+       // getting the length of the file zone.tab
        fseek(pFile, 0, SEEK_END);
        fileLen = ftell(pFile);
        rewind(pFile);
        
-       //PRINTF("length of file: %d\n", fileLen);
-
        if(fileLen == 0)
        {
                g_set_error (error, error_quark, STDIO_FSEEK_FTELL_ERROR,
@@ -339,7 +392,8 @@ system_daemon_get_all_timezones(MoblinSystemServer * server,
        }
        PRINTF("[system_daemon_get_all_timezones] fseek/ftell 
successful:\n\tfile length = %d\n", ((int)fileLen));
        
-       //PRINTF("allocating buffer and zones\n");
+       // allocating a buffer to the size of the file so that we can write 
+       // the file to it
        buffer = (char*)malloc(sizeof(char) * (fileLen + 1));
        if(!buffer)
        {
@@ -355,9 +409,15 @@ system_daemon_get_all_timezones(MoblinSystemServer * 
server,
        }
        memset(buffer, '\0', fileLen+1);
                
+       // The average length per line in the zone.tab file was about 43 
characters.  
+       // To best optimize memory allocation for the get_all_timezones array 
of 
+       // char*s, I will divide the length of the file (in characters) by the 
average line length (43),
+       // so not to allocate way too many or too few char**s for different 
(and unknown) versions of the file
        arrLen = approxLines = fileLen/AVG_LINE_LEN;
-       printf("arrLen = %d\n", arrLen);
                                        
+       // Allocating the number of char*s in my list of timezones
+       // to be about the number of lines in the zone.tab file where
+       // each line is a timezone
        (*tz_list) = (char**)malloc(sizeof(char*) * approxLines);
        if(!(*tz_list))
        {
@@ -378,6 +438,7 @@ system_daemon_get_all_timezones(MoblinSystemServer * server,
 
        PRINTF("[system_daemon_get_all_timezones] memory allocations 
successful.\n");
        
+       // reading the file zone.tab into the buffer
        if (fread(buffer, sizeof(char), fileLen, pFile) != (sizeof(char) * 
fileLen)) 
        {
                g_set_error (error, error_quark, STDIO_FREAD_ERROR,
@@ -399,16 +460,23 @@ system_daemon_get_all_timezones(MoblinSystemServer * 
server,
        buffer[fileLen] = '\0';
        PRINTF("[system_daemon_get_all_timezones] fread successful:\n\t%ld 
bytes read.\n", fileLen);
        
+       // tokenizing the buffer by line
        tok = strtok(buffer, "\n");
        while(tok != NULL)
        {
                optionsBitMask = 8; // 0b00001000
                
-               if(tok[0] != '#')
+               if(tok[0] != '#') // if the line starts with a "#", it's a 
comment
                {
-                       orig = origIter = tok;
-                                               
-                       copyIter = copy = (char*)malloc(sizeof(char) * 
(strlen(tok)+2));                        
+                       orig = origIter = tok;  // keeping track of the mem 
location of beginning of the
+                                                                       // char 
array and starting our iterator at that location
+                       
+                       // allocating a new buffer to the length of the row in 
the 
+                       // timezone table (a tab-deliminated line in zone.tab), 
+                       // even if we keep less than the whole line.
+                       // This buffer will be added to the array and passed 
back
+                       // to the caller
+                       copyIter = copy = (char*)malloc(sizeof(char) * 
(strlen(tok)+2));
                        if(!copyIter)
                        {
                                g_set_error (error, error_quark, 
STDLIB_MALLOC_ERROR,
@@ -423,41 +491,50 @@ system_daemon_get_all_timezones(MoblinSystemServer * 
server,
                        }
                        memset(copyIter, '\0', strlen(tok) + 2);
                                                
-                       if(!(options & TZ_ALL))
-                       { 
-                               while(optionsBitMask > 0)
+                       if(!(options & TZ_ALL)) // if we DO want the whole row 
in the table, skip to the
+                       {                                               // else 
and just copy it into the array
+                               while(optionsBitMask > 0) // The bitmask goes 8 
-> 4 -> 2 -> 1 -> 0, breaking the while loop
                                {
+                                       // find the position of the next tab 
character in the 
+                                       // remaining columns of the original 
char array
+                                       // (walked by origIter)
                                        tabIter = strpbrk(origIter, "\t");
+                                       
+                                       // If there are no more tabs, then this 
row
+                                       // may only have 3 columns
                                        if(!tabIter)
                                                tabIter = strrchr(origIter, 
'\0');
                        
+                                       // The length of the substring, or 
entry in the table, 
+                                       // is the memory location pointed to by 
tabIter minus 
+                                       // the memory location pointed to by 
origIter
                                        substrlen = tabIter - origIter + 1;
                                        
-                                       if( (optionsBitMask & options) && 
tabIter)
-                                       {
-                                               strncpy(copyIter, origIter, 
substrlen);
+                                       if( (optionsBitMask & options) && 
tabIter)      // if this is a column that the caller
+                                       {                                       
                                                // wants returned, copy from 
origIter
+                                               strncpy(copyIter, origIter, 
substrlen); // to copyIter the substring
                                                
                                                //PRINTF("origIter  = 
\t\"%s\"\n", origIter);
                                                //PRINTF("copyIter = 
\t\"%s\"\n", copyIter);
                                                
-                                               copyIter += substrlen;
-                                               copyIter[0] = '\0';
+                                               copyIter += substrlen; // walk 
the copyIter char pointer forward the
+                                               copyIter[0] = '\0';    // 
length of the substring
                                        }
-                                       if(*tabIter == '\0')
-                                               break;
_______________________________________________
Commits mailing list
[email protected]
https://www.moblin.org/mailman/listinfo/commits

Reply via email to