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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
/** \file wutil.c
Wide character equivalents of various standard unix
functions.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <wchar.h>
#include <wctype.h>
#include <string.h>
#include <dirent.h>
#include <stdarg.h>
#include <limits.h>
#include <libgen.h>
#if HAVE_LIBINTL_H
#include <libintl.h>
#endif
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "halloc.h"
#include "halloc_util.h"
/**
Minimum length of the internal covnersion buffers
*/
#define TMP_LEN_MIN 256
#ifndef PATH_MAX
#ifdef MAXPATHLEN
#define PATH_MAX MAXPATHLEN
#else
/**
Fallback length of MAXPATHLEN. Just a hopefully sane value...
*/
#define PATH_MAX 4096
#endif
#endif
/**
Buffer for converting wide arguments to narrow arguments, used by
the \c wutil_wcs2str() function.
*/
static char *tmp=0;
/**
Buffer for converting narrow results to wide ones, used by the \c
wutil_str2wcs() function. Avoid usign this without thinking about
it, since subsequent calls will overwrite previous values.
*/
static wchar_t *tmp2;
/**
Length of the \c tmp buffer.
*/
static size_t tmp_len=0;
/**
Length of the \c tmp2 buffer
*/
static size_t tmp2_len;
/**
Counts the number of calls to the wutil wrapper functions
*/
static int wutil_calls = 0;
/**
Storage for the wreaddir function
*/
static struct wdirent my_wdirent;
/**
For wgettext: Number of string_buffer_t in the ring of buffers
*/
#define BUFF_COUNT 4
/**
For wgettext: The ring of string_buffer_t
*/
static string_buffer_t buff[BUFF_COUNT];
/**
For wgettext: Current position in the ring
*/
static int curr_buff=0;
/**
For wgettext: Buffer used by translate_wcs2str
*/
static char *wcs2str_buff=0;
/**
For wgettext: Size of buffer used by translate_wcs2str
*/
static size_t wcs2str_buff_count=0;
/**
For wgettext: Flag to tell whether the translation library has been initialized
*/
static int wgettext_is_init = 0;
void wutil_init()
{
}
void wutil_destroy()
{
free( tmp );
free( tmp2 );
tmp=0;
tmp_len=0;
debug( 3, L"wutil functions called %d times", wutil_calls );
}
/**
Convert the specified wide aharacter string to a narrow character
string. This function uses an internal temporary buffer for storing
the result so subsequent results will overwrite previous results.
*/
static char *wutil_wcs2str( const wchar_t *in )
{
size_t new_sz;
wutil_calls++;
new_sz =MAX_UTF8_BYTES*wcslen(in)+1;
if( tmp_len < new_sz )
{
new_sz = maxi( new_sz, TMP_LEN_MIN );
tmp = realloc( tmp, new_sz );
if( !tmp )
{
DIE_MEM();
}
tmp_len = new_sz;
}
return wcs2str_internal( in, tmp );
}
/**
Convert the specified wide character string to a narrow character
string. This function uses an internal temporary buffer for storing
the result so subsequent results will overwrite previous results.
*/
static wchar_t *wutil_str2wcs( const char *in )
{
size_t new_sz;
wutil_calls++;
new_sz = sizeof(wchar_t)*(strlen(in)+1);
if( tmp2_len < new_sz )
{
new_sz = maxi( new_sz, TMP_LEN_MIN );
tmp2 = realloc( tmp2, new_sz );
if( !tmp2 )
{
DIE_MEM();
}
tmp2_len = new_sz;
}
return str2wcs_internal( in, tmp2 );
}
struct wdirent *wreaddir(DIR *dir )
{
struct dirent *d = readdir( dir );
if( !d )
return 0;
my_wdirent.d_name = wutil_str2wcs( d->d_name );
return &my_wdirent;
}
wchar_t *wgetcwd( wchar_t *buff, size_t sz )
{
char *buffc = malloc( sz*MAX_UTF8_BYTES);
char *res;
wchar_t *ret = 0;
if( !buffc )
{
errno = ENOMEM;
return 0;
}
res = getcwd( buffc, sz*MAX_UTF8_BYTES );
if( res )
{
if( (size_t)-1 != mbstowcs( buff, buffc, sizeof( wchar_t ) * sz ) )
{
ret = buff;
}
}
free( buffc );
return ret;
}
int wchdir( const wchar_t * dir )
{
char *tmp = wutil_wcs2str(dir);
return chdir( tmp );
}
FILE *wfopen(const wchar_t *path, const char *mode)
{
char *tmp =wutil_wcs2str(path);
FILE *res=0;
if( tmp )
{
res = fopen(tmp, mode);
}
return res;
}
FILE *wfreopen(const wchar_t *path, const char *mode, FILE *stream)
{
char *tmp =wutil_wcs2str(path);
FILE *res=0;
if( tmp )
{
res = freopen(tmp, mode, stream);
}
return res;
}
int wopen(const wchar_t *pathname, int flags, ...)
{
char *tmp =wutil_wcs2str(pathname);
int res=-1;
va_list argp;
if( tmp )
{
if( ! (flags & O_CREAT) )
{
res = open(tmp, flags);
}
else
{
va_start( argp, flags );
res = open(tmp, flags, va_arg(argp, int) );
va_end( argp );
}
}
return res;
}
int wcreat(const wchar_t *pathname, mode_t mode)
{
char *tmp =wutil_wcs2str(pathname);
int res = -1;
if( tmp )
{
res= creat(tmp, mode);
}
return res;
}
DIR *wopendir(const wchar_t *name)
{
char *tmp =wutil_wcs2str(name);
DIR *res = 0;
if( tmp )
{
res = opendir(tmp);
}
return res;
}
int wstat(const wchar_t *file_name, struct stat *buf)
{
char *tmp =wutil_wcs2str(file_name);
int res = -1;
if( tmp )
{
res = stat(tmp, buf);
}
return res;
}
int lwstat(const wchar_t *file_name, struct stat *buf)
{
char *tmp =wutil_wcs2str(file_name);
int res = -1;
if( tmp )
{
res = lstat(tmp, buf);
}
return res;
}
int waccess(const wchar_t *file_name, int mode)
{
char *tmp =wutil_wcs2str(file_name);
int res = -1;
if( tmp )
{
res= access(tmp, mode);
}
return res;
}
void wperror(const wchar_t *s)
{
int e = errno;
if( s != 0 )
{
fwprintf( stderr, L"%ls: ", s );
}
fwprintf( stderr, L"%s\n", strerror( e ) );
}
#ifdef HAVE_REALPATH_NULL
wchar_t *wrealpath(const wchar_t *pathname, wchar_t *resolved_path)
{
char *tmp = wutil_wcs2str(pathname);
char *narrow_res = realpath( tmp, 0 );
wchar_t *res;
if( !narrow_res )
return 0;
if( resolved_path )
{
wchar_t *tmp2 = str2wcs( narrow_res );
wcslcpy( resolved_path, tmp2, PATH_MAX );
free( tmp2 );
res = resolved_path;
}
else
{
res = str2wcs( narrow_res );
}
free( narrow_res );
return res;
}
#else
wchar_t *wrealpath(const wchar_t *pathname, wchar_t *resolved_path)
{
char *tmp = wutil_wcs2str(pathname);
char narrow_buff[PATH_MAX];
char *narrow_res = realpath( tmp, narrow_buff );
wchar_t *res;
if( !narrow_res )
return 0;
if( resolved_path )
{
wchar_t *tmp2 = str2wcs( narrow_res );
wcslcpy( resolved_path, tmp2, PATH_MAX );
free( tmp2 );
res = resolved_path;
}
else
{
res = str2wcs( narrow_res );
}
return res;
}
#endif
wchar_t *wdirname( wchar_t *path )
{
static string_buffer_t *sb = 0;
if( sb )
sb_clear(sb);
else
sb = sb_halloc( global_context );
char *tmp =wutil_wcs2str(path);
char *narrow_res = dirname( tmp );
if( !narrow_res )
return 0;
sb_printf( sb, L"%s", narrow_res );
wcscpy( path, (wchar_t *)sb->buff );
return path;
}
wchar_t *wbasename( const wchar_t *path )
{
static string_buffer_t *sb = 0;
if( sb )
sb_clear(sb);
else
sb = sb_halloc( global_context );
char *tmp =wutil_wcs2str(path);
char *narrow_res = basename( tmp );
if( !narrow_res )
return 0;
sb_printf( sb, L"%s", narrow_res );
return (wchar_t *)sb->buff;
}
/**
For wgettext: Internal shutdown function. Automatically called on shutdown if the library has been initialized.
*/
static void wgettext_destroy()
{
int i;
if( !wgettext_is_init )
return;
wgettext_is_init = 0;
for(i=0; i<BUFF_COUNT; i++ )
sb_destroy( &buff[i] );
free( wcs2str_buff );
}
/**
For wgettext: Internal init function. Automatically called when a translation is first requested.
*/
static void wgettext_init()
{
int i;
wgettext_is_init = 1;
for( i=0; i<BUFF_COUNT; i++ )
{
sb_init( &buff[i] );
}
halloc_register_function_void( global_context, &wgettext_destroy );
bindtextdomain( PACKAGE_NAME, LOCALEDIR );
textdomain( PACKAGE_NAME );
}
/**
For wgettext: Wide to narrow character conversion. Internal implementation that
avoids exessive calls to malloc
*/
static char *wgettext_wcs2str( const wchar_t *in )
{
size_t len = MAX_UTF8_BYTES*wcslen(in)+1;
if( len > wcs2str_buff_count )
{
wcs2str_buff = realloc( wcs2str_buff, len );
if( !wcs2str_buff )
{
DIE_MEM();
}
}
return wcs2str_internal( in, wcs2str_buff);
}
const wchar_t *wgettext( const wchar_t *in )
{
if( !in )
return in;
if( !wgettext_is_init )
wgettext_init();
char *mbs_in = wgettext_wcs2str( in );
char *out = gettext( mbs_in );
wchar_t *wres=0;
sb_clear( &buff[curr_buff] );
sb_printf( &buff[curr_buff], L"%s", out );
wres = (wchar_t *)buff[curr_buff].buff;
curr_buff = (curr_buff+1)%BUFF_COUNT;
return wres;
}
wchar_t *wgetenv( const wchar_t *name )
{
char *name_narrow =wutil_wcs2str(name);
char *res_narrow = getenv( name_narrow );
static string_buffer_t *out = 0;
if( !res_narrow )
return 0;
if( !out )
{
out = sb_halloc( global_context );
}
else
{
sb_clear( out );
}
sb_printf( out, L"%s", res_narrow );
return (wchar_t *)out->buff;
}
int wmkdir( const wchar_t *name, int mode )
{
char *name_narrow =wutil_wcs2str(name);
return mkdir( name_narrow, mode );
}
int wrename( const wchar_t *old, const wchar_t *new )
{
char *old_narrow =wutil_wcs2str(old);
char *new_narrow =wcs2str(new);
int res;
res = rename( old_narrow, new_narrow );
free( new_narrow );
return res;
}
|