Files
heimdal/appl/test/common.c
Assar Westerlund fc1956b05f new arg_printusage
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@5130 ec53bebd-3082-4978-b11e-865c3cabbd6b
1998-08-18 21:17:01 +00:00

123 lines
3.8 KiB
C

/*
* Copyright (c) 1997, 1998 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Kungliga Tekniska
* Högskolan and its contributors.
*
* 4. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "test_locl.h"
RCSID("$Id$");
static int help_flag;
static int version_flag;
static char *port_str;
char *service = SERVICE;
static struct getargs args[] = {
{ "port", 'p', arg_string, &port_str, "port to listen to", "port" },
{ "service", 's', arg_string, &service, "service to use", "service" },
{ "help", 'h', arg_flag, &help_flag },
{ "version", 0, arg_flag, &version_flag }
};
static int num_args = sizeof(args) / sizeof(args[0]);
static void
server_usage(int code, struct getargs *args, int num_args)
{
arg_printusage(args, num_args, NULL, "");
exit(code);
}
static void
client_usage(int code, struct getargs *args, int num_args)
{
arg_printusage(args, num_args, NULL, "host");
exit(code);
}
static int
common_setup(krb5_context *context, int *argc, char **argv,
void (*usage)(int, struct getargs*, int))
{
int port = 0;
*argc = krb5_program_setup(context, *argc, argv, args, num_args, usage);
if(help_flag)
(*usage)(0, args, num_args);
if(version_flag)
krb5_errx(*context, 0, "%s", heimdal_version);
if(port_str){
struct servent *s = roken_getservbyname(port_str, "tcp");
if(s)
port = s->s_port;
else {
char *ptr;
port = strtol (port_str, &ptr, 10);
if (port == 0 && ptr == port_str)
errx (1, "Bad port `%s'", port_str);
port = htons(port);
}
}
if (port == 0)
port = krb5_getportbyname (*context, PORT, "tcp", 4711);
return port;
}
int
server_setup(krb5_context *context, int argc, char **argv)
{
int port = common_setup(context, &argc, argv, server_usage);
if(argv[argc] != NULL)
server_usage(1, args, num_args);
return port;
}
int
client_setup(krb5_context *context, int *argc, char **argv)
{
int optind = *argc;
int port = common_setup(context, &optind, argv, client_usage);
if(*argc - optind != 1)
client_usage(1, args, num_args);
*argc = optind;
return port;
}