Cosmetic: use always g_getenv instead of getenv
[claws.git] / src / jpilot.c
index 7a1902b641491a8b9629588a50c4b870901f1713..cedfca999426f5a7b5ffa3ea52ffadda6e449a49 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001-2011 Match Grun and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 /*
@@ -25,6 +24,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_JPILOT
@@ -163,7 +163,7 @@ static const gchar *jpilot_get_charset(void)
        static const gchar *charset = NULL;
 
        if (charset == NULL)
-               charset = getenv("PILOT_CHARSET");
+               charset = g_getenv("PILOT_CHARSET");
 
        if (charset == NULL)
                charset = CS_CP1252;
@@ -352,7 +352,7 @@ static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
  */
 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
        gboolean retVal = FALSE;
-       struct stat filestat;
+       GStatBuf filestat;
        gchar *pcFile;
 
        /* Mark PDB file cache */
@@ -380,7 +380,7 @@ static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
  */
 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
        gboolean retVal = TRUE;
-       struct stat filestat;
+       GStatBuf filestat;
        gchar *pcFile;
 
        /* Check main file */
@@ -539,6 +539,8 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
 
        fseek(in, 0, SEEK_SET);
        r = fread(&rdbh, sizeof(RawDBHeader), 1, in);
+       if (r < 1)
+               return MGU_ERROR_READ;
        if (feof(in)) {
                return MGU_EOF;
        }
@@ -559,6 +561,9 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
        }
 
        r = fread(&rh, sizeof(record_header), 1, in);
+       if (r < 1)
+               return MGU_ERROR_READ;
+
        offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
        *size=offset - dbh.app_info_offset;
 
@@ -614,7 +619,10 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
                return MGU_ERROR_READ;
        }
 
-       fseek(in, dbh.app_info_offset, SEEK_SET);
+       if (fseek(in, dbh.app_info_offset, SEEK_SET) < 0) {
+               fclose(in);
+               return MGU_ERROR_READ;
+       }
        *buf = ( char * ) malloc(rec_size);
        if (!(*buf)) {
                fclose(in);
@@ -675,6 +683,8 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        unsigned char packed_header[256];
        int num;
 
+       memset(header, 0, sizeof(PC3RecordHeader));
+
        num = fread(&l, sizeof(l), 1, pc_in);
        if (feof(pc_in)) {
                return -1;
@@ -684,7 +694,7 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        }
        memcpy(packed_header, &l, sizeof(l));
        len=ntohl(l);
-       if (len > 255) {
+       if (len > 255 || len < sizeof(l)) {
                return -1;
        }
        num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
@@ -717,12 +727,12 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
        }
        num = read_header( in, &header );
        if( num < 1 ) {
-               if( ferror( in ) ) {
+               if( ferror( in ) )
                        return MGU_ERROR_READ;
-               }
-               if( feof( in ) ) {
+               else if( feof( in ) )
                        return MGU_EOF;
-               }
+               else
+                       return -1;
        }
        rec_len = header.rec_len;
        record = malloc( rec_len );
@@ -811,6 +821,8 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        }
                        if( feof( in ) ) {
                                fclose( in );
+                               if (mem_rh)
+                                       free_mem_rec_header( &mem_rh );
                                return MGU_EOF;
                        }
                }
@@ -858,7 +870,11 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                                unique_id = mem_rh->unique_id;
                        }
                }
-               fseek( in, next_offset, SEEK_SET );
+               if (fseek( in, next_offset, SEEK_SET ) < 0) {
+                       free_mem_rec_header( &mem_rh );
+                       fclose(in);
+                       return MGU_ERROR_READ;
+               }
                while( ! feof( in ) ) {
                        fpos = ftell( in );
                        if( out_of_order ) {
@@ -1040,7 +1056,7 @@ static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPe
 
        if( labelEntry ) {
                *buffer = '\0';
-               strcpy( buffer, labelEntry );
+               g_strlcpy( buffer, labelEntry, sizeof(buffer) );
                node = list = jpilot_parse_email( buffer );
                while( node ) {
                        email = addritem_create_item_email();
@@ -1088,7 +1104,6 @@ static void jpilot_load_address(
        gchar *labelEntry;
        GList *node;
        gchar* extID;
-       struct AddressAppInfo *ai;
        gchar **firstName = NULL;
        gchar **lastName = NULL;
 #if (PILOT_LINK_MAJOR > 11)
@@ -1166,19 +1181,9 @@ static void jpilot_load_address(
        g_free( extID );
        extID = NULL;
 
-       /* Pointer to address metadata. */
-       ai = & pilotFile->addrInfo;
-
        /* Add entry for each email address listed under phone labels. */
        indPhoneLbl = addr.phoneLabel;
        for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
-               gint ind;
-
-               ind = indPhoneLbl[k];
-               /*
-               * g_print( "%d : %d : %20s : %s\n", k, ind,
-               * ai->phoneLabels[ind], addrEnt[3+k] );
-               */
                if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
                        labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
                        jpilot_parse_label( pilotFile, labelEntry, person );
@@ -1204,17 +1209,10 @@ static void jpilot_load_address(
        }
 
        if( person->listEMail ) {
-               if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
-                       /* Add to specified category */
-                       addrcache_folder_add_person(
-                               pilotFile->addressCache,
-                               folderInd[cat_id], person );
-               }
-               else {
-                       /* Add to root folder */
-                       addrcache_add_person(
-                               pilotFile->addressCache, person );
-               }
+               /* Add to specified category */
+               addrcache_folder_add_person(
+                       pilotFile->addressCache,
+                       folderInd[cat_id], person );
        }
        else {
                addritem_free_item_person( person );
@@ -1354,8 +1352,7 @@ GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
                for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
                        gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
                        if( labelName ) {
-                               g_strchomp( labelName );
-                               g_strchug( labelName );
+                               g_strstrip( labelName );
                                if( *labelName != '\0' ) {
                                        if( convert_charcode ) {
                                                gchar *convertBuff = NULL;
@@ -1523,7 +1520,11 @@ gint jpilot_read_data( JPilotFile *pilotFile ) {
 
        cur_locale = conv_get_current_locale();
 
-       if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
+       if( g_ascii_strncasecmp( cur_locale, "hu", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "ko", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "vi", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "zh", 2 ) == 0 ) {
                name_order = FAMILY_FIRST;
        }
 
@@ -1602,7 +1603,7 @@ gchar *jpilot_find_pilotdb( void ) {
        homedir = get_home_dir();
        if( ! homedir ) return g_strdup( "" );
 
-       strcpy( str, homedir );
+       g_strlcpy( str, homedir , sizeof(str));
        len = strlen( str );
        if( len > 0 ) {
                if( str[ len-1 ] != G_DIR_SEPARATOR ) {