libzfs_util.c revision 5c7098917783942b65876f681a21342761227dad
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Internal utility routines for the ZFS library.
*/
#include <errno.h>
#include <fcntl.h>
#include <libintl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <libzfs.h>
#include "libzfs_impl.h"
int zfs_fd;
void (*error_func)(const char *, va_list);
/*
* All error handling is kept within libzfs where we have the most information
* immediately available. While this may not be suitable for a general purpose
* library, it greatly simplifies our commands. This command name is used to
* prefix all error messages appropriately.
*/
void
{
if (error_func != NULL) {
} else {
}
}
/*
* An internal error is something that we cannot recover from, and should never
* happen (such as running out of memory). It should only be used in
* exceptional circumstances.
*/
void
{
if (error_func != NULL) {
} else {
}
exit(1);
}
/*
* Consumers (such as the JNI interface) that need to capture error output can
* override the default error handler using this function.
*/
void
{
error_func = func;
}
/*
* Display an out of memory error message and abort the current program.
*/
void
no_memory(void)
{
}
/*
* A safe form of malloc() which will die if the allocation fails.
*/
void *
{
void *data;
no_memory();
return (data);
}
/*
* A safe form of strdup() which will die if the allocation fails.
*/
char *
zfs_strdup(const char *str)
{
char *ret;
no_memory();
return (ret);
}
/*
* Initialize the library. Sets the command name used when reporting errors.
* This command name is used to prefix all error messages appropriately.
*/
void
zfs_init(void)
{
"internal error: cannot open zfs device"));
"open %s\n"), MNTTAB);
}
/*
* Cleanup function for library. Simply close the file descriptors that we
* opened as part of libzfs_init().
*/
void
zfs_fini(void)
{
}
/*
* Convert a number to an appropriately human-readable output.
*/
void
{
int index = 0;
char u;
while (n >= 1024) {
n /= 1024;
index++;
}
u = " KMGTPE"[index];
if (index == 0) {
/*
* If this is an even multiple of the base, always display
* without any decimal precision.
*/
} else {
/*
* We want to choose a precision that reflects the best choice
* for fitting in 5 characters. This can get rather tricky when
* we have numbers that are very close to an order of magnitude.
* For example, when displaying 10239 (which is really 9.999K),
* we want only a single place of precision for 10.0K. We could
* develop some complex heuristics for this, but it's much
* easier just to try each combination in turn.
*/
int i;
for (i = 2; i >= 0; i--) {
break;
}
}
}