aboutsummaryrefslogtreecommitdiffhomepage
path: root/wutil.c
blob: 6a1a41220c43a11b72220a5a66ddc48a4920d30c (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
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
/** \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 <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <wchar.h>
#include <string.h>
#include <dirent.h>
#include <stdarg.h>
#include <limits.h>

#include "util.h"
#include "common.h"
#include "wutil.h"

static char *tmp=0;
static size_t tmp_len=0;

int c = 0;

void wutil_destroy()
{
	free( tmp );
	tmp=0;
	tmp_len=0;
	debug( 3, L"wutil functions called %d times", c );
}

static char *wutil_wcs2str( const wchar_t *in )
{
	c++;
	
	size_t new_sz =MAX_UTF8_BYTES*wcslen(in)+1;
	if( tmp_len < new_sz )
	{
		free( tmp );
		tmp = malloc( new_sz );
		if( !tmp )
		{
			die_mem();
		}
		tmp_len = new_sz;
	}
	
	wcstombs( tmp, in, tmp_len );
	return tmp;
}

wchar_t *wgetcwd( wchar_t *buff, size_t sz )
{
	char buffc[sz*MAX_UTF8_BYTES];
	char *res = getcwd( buffc, sz*MAX_UTF8_BYTES );
	if( !res )
		return 0;
	
	if( (size_t)-1 == mbstowcs( buff, buffc, sizeof( wchar_t ) * sz ) )
	{
		return 0;		
	}	
	return buff;
}

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 )
	{
		va_start( argp, flags );
		
		if( ! (flags & O_CREAT) )
			res = open(tmp, flags);
		else
			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)
{
	if( s != 0 )
	{
		fwprintf( stderr, L"%ls: ", s );
	}
	fwprintf( stderr, L"%s\n", strerror( errno ) );
}


#if !HAVE_WPRINTF
/*
  Here is my own implementation of *wprintf, included since NetBSD does
  not provide one of it's own.
*/

/**
   This function is defined to help vgwprintf when it wants to call
   itself recursively
*/
static int gwprintf( void (*writer)(wchar_t), 
					 const wchar_t *filter, 
					 ... );


/**
   Generic formatting function. All other formatting functions are
   secretly a wrapper around this function.
*/
static int vgwprintf( void (*writer)(wchar_t), 
					  const wchar_t *filter, 
					  va_list va )
{
	const wchar_t *filter_org=filter;
	int count=0;
	
	for( ;*filter; filter++)
	{
		if(*filter == L'%')
		{
			int i;
			int is_long=0;
			int width = 0;
			filter++;
			int loop=1;
			int precision=INT_MAX;
			
			while( loop )
			{
				switch(*filter)
				{
					case L'l':
						/* Long variable */
						is_long++;
						filter++;
						break;
					case L'*':
						/* Set minimum field width */
						width = va_arg( va, int );
						filter++;
						break;
					case L'.':
						/* 
						   Set precision.
						   Hasn't been tested enough yet, so I don't really trust it.
						*/
						filter++;
						if( *filter == L'*' )
						{
							precision = va_arg( va, int );
						}
						else
						{
							while( (*filter >= L'0') && (*filter <= L'9'))
							{
								precision=10*precision+(*filter - L'0');
							}							
						}
						break;
						
					default:
						loop=0;
						break;
				}
			}
			switch( *filter )
			{
				case L'c':
				{
					wchar_t c;
					
					c = is_long?va_arg(va, wchar_t):btowc(va_arg(va, int));
					if( width )
					{
						int i;
						for( i=1; i<width; i++ )
						{
							writer( L' ' );
							count++;
						}
					}
					if( precision != 0 )
						writer( c );
					count++;
					
					break;
				}
				case L's':
				{
					wchar_t *ss = is_long?va_arg(va, wchar_t*):str2wcs(va_arg(va, char*));
					
					if( !ss )
						return -1;
					
					if( width )
					{
						int i;
						for( i=wcslen(ss); i<width; i++ )
						{
							writer( L' ' );
							count++;
						}
					}

					wchar_t *s=ss;
					int precount = count;
					
					while( *s )
					{
						if( (precision <= (count-precount) ) )
							break;

						writer( *(s++) );
						count++;
					}
					
					if( !is_long )
						free( ss );
					
					break;
				}

				case L'd':
				case L'i':
				{
					char str[32];

					switch( is_long )
					{
						case 0:
						{
							int d = va_arg( va, int );
							snprintf( str, 32, "%.*d", precision, d );
							break;
						}
						
						case 1:
						{
							long d = va_arg( va, long );
							snprintf( str, 32, "%.*ld", precision, d );
							break;
						}
						
						case 2:
						{
							long long d = va_arg( va, long long );
							snprintf( str, 32, "%.*lld", precision, d );
							break;
						}
						
						default:
							return -1;
					}
					
					if( width )
					{
						int i;
						for( i=strlen(str); i<width; i++ )
						{
							writer( L' ' );
							count++;
						}
					}
										
					int c = gwprintf( writer, L"%s", str );
					if( c==-1 )
						return -1;
					else
						count += c;
					
					break;
				}

				case L'u':
				{
					char str[32];
					

					switch( is_long )
					{
						case 0:
						{
							unsigned d = va_arg( va, unsigned );
							snprintf( str, 32, "%d", d );
							break;
						}
						
						case 1:
						{
							unsigned long d = va_arg( va, unsigned long );
							snprintf( str, 32, "%ld", d );
							break;
						}
						
						case 2:
						{
							unsigned long long d = va_arg( va, unsigned long long );
							snprintf( str, 32, "%lld", d );
							break;
						}
						
						default:
							return -1;
					}
					
					if( width )
					{
						int i;
						for( i=strlen(str); i<width; i++ )
						{
							writer( L' ' );
							count++;
						}
					}

					int c = gwprintf( writer, L"%s", str );
					if( c==-1 )
						return -1;
					else
						count += c;
					
					break;
				}

				case L'n':
				{
					int *n = va_arg( va, int *);
					
					*n = count;					
					break;
				}
				default:
					debug( 0, L"Unknown switch %lc in string %ls\n", *filter, filter_org );
					exit(1);
			}
		}
		else
		{
			writer( *filter );
			count++;
		}
	}

	return count;
}

static int gwprintf( void (*writer)(wchar_t), 
					 const wchar_t *filter, 
					 ... )
{
	va_list va;
	va_start( va, filter );
	int written=vgwprintf( writer,
						   filter,
						   va );
	va_end( va );
	return written;
}

/**
   Holds data for swprintf writer
*/
static struct
{
	int count;
	int max;
	wchar_t *pos;
}
sw_data;

/**
   Writer for swprintf
*/
static void sw_writer( wchar_t c )
{
	if( sw_data.count < sw_data.max )
		*(sw_data.pos++)=c;	
	sw_data.count++;
}


int swprintf( wchar_t *out, size_t n, const wchar_t *filter, ... )
{
	va_list va;
	va_start( va, filter );
	sw_data.pos=out;
	sw_data.max=n;
	sw_data.count=0;
	int written=vgwprintf( &sw_writer,
						   filter,
						   va );
	if( written < n )
	{
		*sw_data.pos = 0;
	}
	else
	{
		written=-1;
	}
	
	va_end( va );
	return written;
}

/**
   Holds auxiliary data for fwprintf and wprintf writer
*/
static FILE *fw_data;

static void fw_writer( wchar_t c )
{
	putw( c, fw_data );
}

/**
   Writer for fwprintf and wprintf
*/
int fwprintf( FILE *f, const wchar_t *filter, ... )
{
	va_list va;
	va_start( va, filter );
	fw_data = f;
	
	int written=vgwprintf( &fw_writer, filter, va );
	va_end( va );
	return written;
}

int wprintf( const wchar_t *filter, ... )
{
	va_list va;
	va_start( va, filter );
	fw_data = stdout;
	
	int written=vgwprintf( &fw_writer, filter, va );
	va_end( va );
	return written;
}

#endif