~drizzle-trunk/drizzle/development

641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
1
/*****************************************************************************
2
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
4
Copyright (c) 2009, Sun Microsystems, Inc.
5
6
Portions of this file contain modifications contributed and copyrighted by
7
Sun Microsystems, Inc. Those modifications are gratefully acknowledged and
8
are described briefly in the InnoDB documentation. The contributions by
9
Sun Microsystems are incorporated with their permission, and subject to the
10
conditions contained in the file COPYING.Sun_Microsystems.
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
11
12
This program is free software; you can redistribute it and/or modify it under
13
the terms of the GNU General Public License as published by the Free Software
14
Foundation; version 2 of the License.
15
16
This program is distributed in the hope that it will be useful, but WITHOUT
17
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19
20
You should have received a copy of the GNU General Public License along with
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22
Place, Suite 330, Boston, MA 02111-1307 USA
23
24
*****************************************************************************/
25
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
26
/***************************************************************//**
27
@file ut/ut0ut.c
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
28
Various utilities for Innobase.
29
30
Created 5/11/1994 Heikki Tuuri
31
********************************************************************/
32
33
#include "ut0ut.h"
34
35
#ifdef UNIV_NONINL
36
#include "ut0ut.ic"
37
#endif
38
39
#include <stdarg.h>
40
#include <string.h>
41
#include <ctype.h>
42
43
#ifndef UNIV_HOTBACKUP
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
44
# include "trx0trx.h"
641.1.4 by Monty Taylor
Merged in InnoDB changes.
45
# if defined(BUILD_DRIZZLE)
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
46
#  include "drizzled/common.h"
641.1.4 by Monty Taylor
Merged in InnoDB changes.
47
#  if TIME_WITH_SYS_TIME
48
#   include <sys/time.h>
49
#   include <time.h>
50
#  else
51
#   if HAVE_SYS_TIME_H
52
#    include <sys/time.h>
53
#   else
54
#    include <time.h>
55
#   endif
56
#  endif
57
# else
1114.1.1 by Monty Taylor
Merged InnoDB Plugin 1.0.4
58
#  include "ha_prototypes.h"
641.1.4 by Monty Taylor
Merged in InnoDB changes.
59
#  include "mysql_com.h" /* NAME_LEN */
60
# endif /* DRIZZLE */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
61
#endif /* UNIV_HOTBACKUP */
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
62
#include <errno.h>
63
#include <assert.h>
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
64
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
65
/** A constant to prevent the compiler from optimizing ut_delay() away. */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
66
UNIV_INTERN ibool	ut_always_false	= FALSE;
67
68
#ifdef __WIN__
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
69
/*****************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
70
NOTE: The Windows epoch starts from 1601/01/01 whereas the Unix
71
epoch starts from 1970/1/1. For selection of constant see:
72
http://support.microsoft.com/kb/167296/ */
73
#define WIN_TO_UNIX_DELTA_USEC  ((ib_int64_t) 11644473600000000ULL)
74
75
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
76
/*****************************************************************//**
77
This is the Windows version of gettimeofday(2).
78
@return	0 if all OK else -1 */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
79
static
80
int
81
ut_gettimeofday(
82
/*============*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
83
	struct timeval*	tv,	/*!< out: Values are relative to Unix epoch */
84
	void*		tz)	/*!< in: not used */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
85
{
86
	FILETIME	ft;
87
	ib_int64_t	tm;
88
89
	if (!tv) {
90
		errno = EINVAL;
91
		return(-1);
92
	}
93
94
	GetSystemTimeAsFileTime(&ft);
95
96
	tm = (ib_int64_t) ft.dwHighDateTime << 32;
97
	tm |= ft.dwLowDateTime;
98
99
	ut_a(tm >= 0);	/* If tm wraps over to negative, the quotient / 10
100
			does not work */
101
102
	tm /= 10;	/* Convert from 100 nsec periods to usec */
103
104
	/* If we don't convert to the Unix epoch the value for
105
	struct timeval::tv_sec will overflow.*/
106
	tm -= WIN_TO_UNIX_DELTA_USEC;
107
108
	tv->tv_sec  = (long) (tm / 1000000L);
109
	tv->tv_usec = (long) (tm % 1000000L);
110
111
	return(0);
112
}
113
#else
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
114
/** An alias for gettimeofday(2).  On Microsoft Windows, we have to
115
reimplement this function. */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
116
#define	ut_gettimeofday		gettimeofday
117
#endif
118
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
119
/********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
120
Gets the high 32 bits in a ulint. That is makes a shift >> 32,
121
but since there seem to be compiler bugs in both gcc and Visual C++,
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
122
we do this by a special conversion.
123
@return	a >> 32 */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
124
UNIV_INTERN
125
ulint
126
ut_get_high32(
127
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
128
	ulint	a)	/*!< in: ulint */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
129
{
130
	ib_int64_t	i;
131
132
	i = (ib_int64_t)a;
133
134
	i = i >> 32;
135
136
	return((ulint)i);
137
}
138
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
139
/**********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
140
Returns system time. We do not specify the format of the time returned:
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
141
the only way to manipulate it is to use the function ut_difftime.
142
@return	system time */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
143
UNIV_INTERN
144
ib_time_t
145
ut_time(void)
146
/*=========*/
147
{
148
	return(time(NULL));
149
}
150
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
151
/**********************************************************//**
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
152
Returns system time.
153
Upon successful completion, the value 0 is returned; otherwise the
154
value -1 is returned and the global variable errno is set to indicate the
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
155
error.
156
@return	0 on success, -1 otherwise */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
157
UNIV_INTERN
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
158
int
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
159
ut_usectime(
160
/*========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
161
	ulint*	sec,	/*!< out: seconds since the Epoch */
162
	ulint*	ms)	/*!< out: microseconds since the Epoch+*sec */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
163
{
164
	struct timeval	tv;
641.2.1 by Monty Taylor
InnoDB Plugin 1.0.2
165
	int		ret;
166
	int		errno_gettimeofday;
167
	int		i;
168
169
	for (i = 0; i < 10; i++) {
170
171
		ret = ut_gettimeofday(&tv, NULL);
172
173
		if (ret == -1) {
174
			errno_gettimeofday = errno;
175
			ut_print_timestamp(stderr);
176
			fprintf(stderr, "  InnoDB: gettimeofday(): %s\n",
177
				strerror(errno_gettimeofday));
178
			os_thread_sleep(100000);  /* 0.1 sec */
179
			errno = errno_gettimeofday;
180
		} else {
181
			break;
182
		}
183
	}
184
185
	if (ret != -1) {
186
		*sec = (ulint) tv.tv_sec;
187
		*ms  = (ulint) tv.tv_usec;
188
	}
189
190
	return(ret);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
191
}
192
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
193
/**********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
194
Returns the number of microseconds since epoch. Similar to
195
time(3), the return value is also stored in *tloc, provided
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
196
that tloc is non-NULL.
197
@return	us since epoch */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
198
UNIV_INTERN
199
ullint
200
ut_time_us(
201
/*=======*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
202
	ullint*	tloc)	/*!< out: us since epoch, if non-NULL */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
203
{
204
	struct timeval	tv;
205
	ullint		us;
206
207
	ut_gettimeofday(&tv, NULL);
208
209
	us = (ullint) tv.tv_sec * 1000000 + tv.tv_usec;
210
211
	if (tloc != NULL) {
212
		*tloc = us;
213
	}
214
215
	return(us);
216
}
217
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
218
/**********************************************************//**
219
Returns the difference of two times in seconds.
220
@return	time2 - time1 expressed in seconds */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
221
UNIV_INTERN
222
double
223
ut_difftime(
224
/*========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
225
	ib_time_t	time2,	/*!< in: time */
226
	ib_time_t	time1)	/*!< in: time */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
227
{
228
	return(difftime(time2, time1));
229
}
230
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
231
/**********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
232
Prints a timestamp to a file. */
233
UNIV_INTERN
234
void
235
ut_print_timestamp(
236
/*===============*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
237
	FILE*  file) /*!< in: file where to print */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
238
{
239
#ifdef __WIN__
240
	SYSTEMTIME cal_tm;
241
242
	GetLocalTime(&cal_tm);
243
244
	fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
245
		(int)cal_tm.wYear % 100,
246
		(int)cal_tm.wMonth,
247
		(int)cal_tm.wDay,
248
		(int)cal_tm.wHour,
249
		(int)cal_tm.wMinute,
250
		(int)cal_tm.wSecond);
251
#else
252
	struct tm  cal_tm;
253
	struct tm* cal_tm_ptr;
254
	time_t	   tm;
255
256
	time(&tm);
257
258
#ifdef HAVE_LOCALTIME_R
259
	localtime_r(&tm, &cal_tm);
260
	cal_tm_ptr = &cal_tm;
261
#else
262
	cal_tm_ptr = localtime(&tm);
263
#endif
264
	fprintf(file,"%02d%02d%02d %2d:%02d:%02d",
265
		cal_tm_ptr->tm_year % 100,
266
		cal_tm_ptr->tm_mon + 1,
267
		cal_tm_ptr->tm_mday,
268
		cal_tm_ptr->tm_hour,
269
		cal_tm_ptr->tm_min,
270
		cal_tm_ptr->tm_sec);
271
#endif
272
}
273
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
274
/**********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
275
Sprintfs a timestamp to a buffer, 13..14 chars plus terminating NUL. */
276
UNIV_INTERN
277
void
278
ut_sprintf_timestamp(
279
/*=================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
280
	char*	buf) /*!< in: buffer where to sprintf */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
281
{
282
#ifdef __WIN__
283
	SYSTEMTIME cal_tm;
284
285
	GetLocalTime(&cal_tm);
286
287
	sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
288
		(int)cal_tm.wYear % 100,
289
		(int)cal_tm.wMonth,
290
		(int)cal_tm.wDay,
291
		(int)cal_tm.wHour,
292
		(int)cal_tm.wMinute,
293
		(int)cal_tm.wSecond);
294
#else
295
	struct tm  cal_tm;
296
	struct tm* cal_tm_ptr;
297
	time_t	   tm;
298
299
	time(&tm);
300
301
#ifdef HAVE_LOCALTIME_R
302
	localtime_r(&tm, &cal_tm);
303
	cal_tm_ptr = &cal_tm;
304
#else
305
	cal_tm_ptr = localtime(&tm);
306
#endif
307
	sprintf(buf, "%02d%02d%02d %2d:%02d:%02d",
308
		cal_tm_ptr->tm_year % 100,
309
		cal_tm_ptr->tm_mon + 1,
310
		cal_tm_ptr->tm_mday,
311
		cal_tm_ptr->tm_hour,
312
		cal_tm_ptr->tm_min,
313
		cal_tm_ptr->tm_sec);
314
#endif
315
}
316
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
317
#ifdef UNIV_HOTBACKUP
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
318
/**********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
319
Sprintfs a timestamp to a buffer with no spaces and with ':' characters
320
replaced by '_'. */
321
UNIV_INTERN
322
void
323
ut_sprintf_timestamp_without_extra_chars(
324
/*=====================================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
325
	char*	buf) /*!< in: buffer where to sprintf */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
326
{
327
#ifdef __WIN__
328
	SYSTEMTIME cal_tm;
329
330
	GetLocalTime(&cal_tm);
331
332
	sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
333
		(int)cal_tm.wYear % 100,
334
		(int)cal_tm.wMonth,
335
		(int)cal_tm.wDay,
336
		(int)cal_tm.wHour,
337
		(int)cal_tm.wMinute,
338
		(int)cal_tm.wSecond);
339
#else
340
	struct tm  cal_tm;
341
	struct tm* cal_tm_ptr;
342
	time_t	   tm;
343
344
	time(&tm);
345
346
#ifdef HAVE_LOCALTIME_R
347
	localtime_r(&tm, &cal_tm);
348
	cal_tm_ptr = &cal_tm;
349
#else
350
	cal_tm_ptr = localtime(&tm);
351
#endif
352
	sprintf(buf, "%02d%02d%02d_%2d_%02d_%02d",
353
		cal_tm_ptr->tm_year % 100,
354
		cal_tm_ptr->tm_mon + 1,
355
		cal_tm_ptr->tm_mday,
356
		cal_tm_ptr->tm_hour,
357
		cal_tm_ptr->tm_min,
358
		cal_tm_ptr->tm_sec);
359
#endif
360
}
361
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
362
/**********************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
363
Returns current year, month, day. */
364
UNIV_INTERN
365
void
366
ut_get_year_month_day(
367
/*==================*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
368
	ulint*	year,	/*!< out: current year */
369
	ulint*	month,	/*!< out: month */
370
	ulint*	day)	/*!< out: day */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
371
{
372
#ifdef __WIN__
373
	SYSTEMTIME cal_tm;
374
375
	GetLocalTime(&cal_tm);
376
377
	*year = (ulint)cal_tm.wYear;
378
	*month = (ulint)cal_tm.wMonth;
379
	*day = (ulint)cal_tm.wDay;
380
#else
381
	struct tm  cal_tm;
382
	struct tm* cal_tm_ptr;
383
	time_t	   tm;
384
385
	time(&tm);
386
387
#ifdef HAVE_LOCALTIME_R
388
	localtime_r(&tm, &cal_tm);
389
	cal_tm_ptr = &cal_tm;
390
#else
391
	cal_tm_ptr = localtime(&tm);
392
#endif
393
	*year = (ulint)cal_tm_ptr->tm_year + 1900;
394
	*month = (ulint)cal_tm_ptr->tm_mon + 1;
395
	*day = (ulint)cal_tm_ptr->tm_mday;
396
#endif
397
}
641.2.2 by Monty Taylor
InnoDB Plugin 1.0.3
398
#endif /* UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
399
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
400
#ifndef UNIV_HOTBACKUP
401
/*************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
402
Runs an idle loop on CPU. The argument gives the desired delay
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
403
in microseconds on 100 MHz Pentium + Visual C++.
404
@return	dummy value */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
405
UNIV_INTERN
406
ulint
407
ut_delay(
408
/*=====*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
409
	ulint	delay)	/*!< in: delay in microseconds on 100 MHz Pentium */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
410
{
411
	ulint	i, j;
412
413
	j = 0;
414
415
	for (i = 0; i < delay * 50; i++) {
416
		j += i;
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
417
		UT_RELAX_CPU();
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
418
	}
419
420
	if (ut_always_false) {
421
		ut_always_false = (ibool) j;
422
	}
423
424
	return(j);
425
}
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
426
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
427
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
428
/*************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
429
Prints the contents of a memory buffer in hex and ascii. */
430
UNIV_INTERN
431
void
432
ut_print_buf(
433
/*=========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
434
	FILE*		file,	/*!< in: file where to print */
435
	const void*	buf,	/*!< in: memory buffer */
436
	ulint		len)	/*!< in: length of the buffer */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
437
{
438
	const byte*	data;
439
	ulint		i;
440
441
	UNIV_MEM_ASSERT_RW(buf, len);
442
443
	fprintf(file, " len %lu; hex ", len);
444
445
	for (data = (const byte*)buf, i = 0; i < len; i++) {
446
		fprintf(file, "%02lx", (ulong)*data++);
447
	}
448
449
	fputs("; asc ", file);
450
451
	data = (const byte*)buf;
452
453
	for (i = 0; i < len; i++) {
454
		int	c = (int) *data++;
455
		putc(isprint(c) ? c : ' ', file);
456
	}
457
458
	putc(';', file);
459
}
460
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
461
/*************************************************************//**
462
Calculates fast the number rounded up to the nearest power of 2.
463
@return	first power of 2 which is >= n */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
464
UNIV_INTERN
465
ulint
466
ut_2_power_up(
467
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
468
	ulint	n)	/*!< in: number != 0 */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
469
{
470
	ulint	res;
471
472
	res = 1;
473
474
	ut_ad(n > 0);
475
476
	while (res < n) {
477
		res = res * 2;
478
	}
479
480
	return(res);
481
}
482
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
483
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
484
Outputs a NUL-terminated file name, quoted with apostrophes. */
485
UNIV_INTERN
486
void
487
ut_print_filename(
488
/*==============*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
489
	FILE*		f,	/*!< in: output stream */
490
	const char*	name)	/*!< in: name to print */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
491
{
492
	putc('\'', f);
493
	for (;;) {
494
		int	c = *name++;
495
		switch (c) {
496
		case 0:
497
			goto done;
498
		case '\'':
499
			putc(c, f);
500
			/* fall through */
501
		default:
502
			putc(c, f);
503
		}
504
	}
505
done:
506
	putc('\'', f);
507
}
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
508
#ifndef UNIV_HOTBACKUP
509
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
510
Outputs a fixed-length string, quoted as an SQL identifier.
511
If the string contains a slash '/', the string will be
512
output as two identifiers separated by a period (.),
513
as in SQL database_name.identifier. */
514
UNIV_INTERN
515
void
516
ut_print_name(
517
/*==========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
518
	FILE*		f,	/*!< in: output stream */
519
	trx_t*		trx,	/*!< in: transaction */
520
	ibool		table_id,/*!< in: TRUE=print a table name,
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
521
				FALSE=print other identifier */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
522
	const char*	name)	/*!< in: name to print */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
523
{
524
	ut_print_namel(f, trx, table_id, name, strlen(name));
525
}
526
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
527
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
528
Outputs a fixed-length string, quoted as an SQL identifier.
529
If the string contains a slash '/', the string will be
530
output as two identifiers separated by a period (.),
531
as in SQL database_name.identifier. */
532
UNIV_INTERN
533
void
534
ut_print_namel(
535
/*===========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
536
	FILE*		f,	/*!< in: output stream */
537
	trx_t*		trx,	/*!< in: transaction (NULL=no quotes) */
538
	ibool		table_id,/*!< in: TRUE=print a table name,
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
539
				FALSE=print other identifier */
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
540
	const char*	name,	/*!< in: name to print */
541
	ulint		namelen)/*!< in: length of name */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
542
{
543
	/* 2 * NAME_LEN for database and table name,
544
	and some slack for the #mysql50# prefix and quotes */
545
	char		buf[3 * NAME_LEN];
546
	const char*	bufend;
547
548
	bufend = innobase_convert_name(buf, sizeof buf,
549
				       name, namelen,
550
				       trx ? trx->mysql_thd : NULL,
551
				       table_id);
552
641.1.4 by Monty Taylor
Merged in InnoDB changes.
553
	ssize_t ret= fwrite(buf, 1, bufend - buf, f);
660.1.4 by Eric Herman
un-fixing whitespace changes to storage/innobase
554
	assert(ret==bufend-buf);  
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
555
}
556
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
557
/**********************************************************************//**
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
558
Catenate files. */
559
UNIV_INTERN
560
void
561
ut_copy_file(
562
/*=========*/
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
563
	FILE*	dest,	/*!< in: output file */
564
	FILE*	src)	/*!< in: input file to be appended to output */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
565
{
566
	long	len = ftell(src);
567
	char	buf[4096];
568
569
	rewind(src);
570
	do {
571
		size_t	maxs = len < (long) sizeof buf
572
			? (size_t) len
573
			: sizeof buf;
574
		size_t	size = fread(buf, 1, maxs, src);
641.1.4 by Monty Taylor
Merged in InnoDB changes.
575
		size_t ret= fwrite(buf, 1, size, dest);
576
		assert(ret==size);
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
577
		len -= (long) size;
578
		if (size < maxs) {
579
			break;
580
		}
581
	} while (len > 0);
582
}
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
583
#endif /* !UNIV_HOTBACKUP */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
584
585
#ifdef __WIN__
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
586
# include <stdarg.h>
587
/**********************************************************************//**
588
A substitute for snprintf(3), formatted output conversion into
589
a limited buffer.
590
@return number of characters that would have been printed if the size
591
were unlimited, not including the terminating '\0'. */
592
UNIV_INTERN
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
593
int
594
ut_snprintf(
641.2.3 by Monty Taylor
InnoDB Plugin 1.0.4
595
/*========*/
596
	char*		str,	/*!< out: string */
597
	size_t		size,	/*!< in: str size */
598
	const char*	fmt,	/*!< in: format */
599
	...)			/*!< in: format values */
641.1.2 by Monty Taylor
Imported 1.0.1 with clean - with no changes.
600
{
601
	int	res;
602
	va_list	ap1;
603
	va_list	ap2;
604
605
	va_start(ap1, fmt);
606
	va_start(ap2, fmt);
607
608
	res = _vscprintf(fmt, ap1);
609
	ut_a(res != -1);
610
611
	if (size > 0) {
612
		_vsnprintf(str, size, fmt, ap2);
613
614
		if ((size_t) res >= size) {
615
			str[size - 1] = '\0';
616
		}
617
	}
618
619
	va_end(ap1);
620
	va_end(ap2);
621
622
	return(res);
623
}
624
#endif /* __WIN__ */