error_string.c revision 78527
178527Sassar/* 278527Sassar * Copyright (c) 2001 Kungliga Tekniska H�gskolan 378527Sassar * (Royal Institute of Technology, Stockholm, Sweden). 478527Sassar * All rights reserved. 578527Sassar * 678527Sassar * Redistribution and use in source and binary forms, with or without 778527Sassar * modification, are permitted provided that the following conditions 878527Sassar * are met: 978527Sassar * 1078527Sassar * 1. Redistributions of source code must retain the above copyright 1178527Sassar * notice, this list of conditions and the following disclaimer. 1278527Sassar * 1378527Sassar * 2. Redistributions in binary form must reproduce the above copyright 1478527Sassar * notice, this list of conditions and the following disclaimer in the 1578527Sassar * documentation and/or other materials provided with the distribution. 1678527Sassar * 1778527Sassar * 3. Neither the name of the Institute nor the names of its contributors 1878527Sassar * may be used to endorse or promote products derived from this software 1978527Sassar * without specific prior written permission. 2078527Sassar * 2178527Sassar * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2278527Sassar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2378527Sassar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2478527Sassar * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2578527Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2678527Sassar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2778527Sassar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2878527Sassar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2978527Sassar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3078527Sassar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3178527Sassar * SUCH DAMAGE. 3278527Sassar */ 3378527Sassar 3478527Sassar#include "krb5_locl.h" 3578527Sassar 3678527SassarRCSID("$Id: error_string.c,v 1.1 2001/05/06 23:07:22 assar Exp $"); 3778527Sassar 3878527Sassar#undef __attribute__ 3978527Sassar#define __attribute__(X) 4078527Sassar 4178527Sassarvoid 4278527Sassarkrb5_free_error_string(krb5_context context, char *str) 4378527Sassar{ 4478527Sassar if (str != context->error_buf) 4578527Sassar free(str); 4678527Sassar} 4778527Sassar 4878527Sassarvoid 4978527Sassarkrb5_clear_error_string(krb5_context context) 5078527Sassar{ 5178527Sassar if (context->error_string != NULL 5278527Sassar && context->error_string != context->error_buf) 5378527Sassar free(context->error_string); 5478527Sassar context->error_string = NULL; 5578527Sassar} 5678527Sassar 5778527Sassarkrb5_error_code 5878527Sassarkrb5_set_error_string(krb5_context context, const char *fmt, ...) 5978527Sassar __attribute__((format (printf, 2, 3))) 6078527Sassar{ 6178527Sassar krb5_error_code ret; 6278527Sassar va_list ap; 6378527Sassar 6478527Sassar va_start(ap, fmt); 6578527Sassar ret = krb5_vset_error_string (context, fmt, ap); 6678527Sassar va_end(ap); 6778527Sassar return ret; 6878527Sassar} 6978527Sassar 7078527Sassarkrb5_error_code 7178527Sassarkrb5_vset_error_string(krb5_context context, const char *fmt, va_list args) 7278527Sassar __attribute__ ((format (printf, 2, 0))) 7378527Sassar{ 7478527Sassar krb5_clear_error_string(context); 7578527Sassar vasprintf(&context->error_string, fmt, args); 7678527Sassar if(context->error_string == NULL) { 7778527Sassar vsnprintf (context->error_buf, sizeof(context->error_buf), fmt, args); 7878527Sassar context->error_string = context->error_buf; 7978527Sassar } 8078527Sassar return 0; 8178527Sassar} 8278527Sassar 8378527Sassarchar* 8478527Sassarkrb5_get_error_string(krb5_context context) 8578527Sassar{ 8678527Sassar char *ret = context->error_string; 8778527Sassar context->error_string = NULL; 8878527Sassar return ret; 8978527Sassar} 9078527Sassar 9178527Sassarkrb5_boolean 9278527Sassarkrb5_have_error_string(krb5_context context) 9378527Sassar{ 9478527Sassar return context->error_string != NULL; 9578527Sassar} 96