summaryrefslogtreecommitdiff
path: root/src/rcl-zone-utils.c
blob: 4ffe8cd8d297a5b942719ad982600f8ecc0ae25a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2023 Andrey V.Kosteltsev <kx@radix.pro>
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "rcl-zone-utils.h"

static gsize strv_lenght( const gchar *const *list )
{
  gsize   len = 0;
  gchar **p   = (gchar **)list;

  if( !list || *list == NULL )
    return 0;

  while( *p )
  {
    ++len;
    ++p;
  }

  return len;
}

static gboolean strv_append( const gchar *const **list, const gchar *value )
{
  gchar **c;
  gchar  *v;
  gsize   len;

  if( !list || !value )
    return FALSE;

  len = strv_lenght( *list );

  v = g_strdup( value );
  if( !v )
    return FALSE;

  c = g_realloc( (gpointer)*list, len * sizeof(gchar *) + sizeof(gchar *) * 2 );
  if( !c )
  {
    g_free( (gpointer)v );
    return FALSE;
  }

  c[len]   = v;
  c[len+1] = NULL;

  *list = (const gchar *const *)c;

  return TRUE;
}

static gint comparator( gconstpointer item1, gconstpointer item2 )
{
  return g_strcmp0( item1, item2 );
}

static GSList *g_slist_insert_sorted_unique( GSList *list, gpointer data, GCompareFunc func )
{
  if( !data || !func )
    return list;

  if( !g_slist_find_custom( list, data, func ) )
    list = g_slist_insert_sorted( list, data, func );

  return list;
}

static GSList *get_timezones_from_tzdata_zi( void )
{
  GSList *list = NULL;
  FILE   *fp   = NULL;
  gchar  *ln   = NULL, *line = NULL;

  fp = fopen( "/usr/share/zoneinfo/tzdata.zi", "r" );
  if( !fp )
    return list;

  line = (gchar *)g_malloc0( (gsize)PATH_MAX );
  if( !line )
  {
    fclose( fp );
    return list;
  }

  /**********************************************
    Zone line format is: 'Zone' 'timezone' ...
    Link line format is: 'Link' 'target' 'alias'
    See `man (8) zic' for infirmation.
   **********************************************/
  while( (ln = fgets( line, PATH_MAX, fp )) )
  {
    gchar  *p, *q;

    if( !g_ascii_strncasecmp( ln, "Z", 1 ) )
    {
      p = &ln[1];

      /* Skip spaces */
      while( (*p == ' ' || *p == '\t') && *p != '\n' )
        ++p;

      q = p;

      /* Take the first entry */
      while( *q != ' ' && *q != '\t' && *q != '\n' )
        ++q;

      *q = '\0';

      list = g_slist_insert_sorted_unique( list, (gpointer)g_strdup( p ), (GCompareFunc)comparator );
      continue;
    }
    else if( !g_ascii_strncasecmp( ln, "L", 1 ) )
    {
      p = &ln[1];

      /* Skip spaces */
      while( (*p == ' ' || *p == '\t') && *p != '\n' )
        ++p;

      q = p;

      /* Skip the first entry */
      while( *q != ' ' && *q != '\t' && *q != '\n' )
        ++q;

      p = q;

      /* Skip spaces */
      while( (*p == ' ' || *p == '\t') && *p != '\n' )
        ++p;

      q = p;

      /* Take the second entry */
      while( *q != ' ' && *q != '\t' && *q != '\n' )
        ++q;

      *q = '\0';

      list = g_slist_insert_sorted_unique( list, (gpointer)g_strdup( p ), (GCompareFunc)comparator );
    }
    else
    {
      continue;
    }
  }

  g_free( (gpointer)line );
  fclose( fp );

  return list;
}

void timezones_free( const gchar *const **list )
{
  gchar **c, **p;

  if( !list || *list == NULL )
    return;

  p = c = (gpointer)*list;

  while( *p )
  {
    g_free( (gpointer)*p );
    ++p;
  }

  g_free( (gpointer)c );

  *list = (const gchar *const *)NULL;
}

void timezones_print( const gchar *const **list )
{
  gchar **p;

  if( !list || *list == NULL )
    return;

  p = (gpointer)*list;

  while( *p )
  {
    g_print( "%s\n", *p );
    ++p;
  }
}

gboolean get_timezones( const gchar *const **list )
{
  GSList   *slist = NULL, *iterator = NULL;
  gboolean  ret   = TRUE;

  slist = get_timezones_from_tzdata_zi();
  if( !slist )
    return FALSE;

  for( iterator = slist; iterator; iterator = iterator->next )
  {
    gboolean rc = strv_append( list, (const gchar *)iterator->data );
    if( !rc )
      ret = FALSE;

    g_free( (gpointer)iterator->data );
  }
  g_slist_free(slist);

  return ret;
}