Drastically reduce the warnings in utilbox

This commit is contained in:
Pierre Pronchery 2009-10-13 10:19:32 +00:00
parent 3f43762bf8
commit a3491d5fe8
7 changed files with 58 additions and 27 deletions

View File

@ -71,9 +71,9 @@ dist:
$(PACKAGE)-$(VERSION)/src/common.c \
$(PACKAGE)-$(VERSION)/src/project.conf \
$(PACKAGE)-$(VERSION)/tools/utilbox.c \
$(PACKAGE)-$(VERSION)/tools/utils.c \
$(PACKAGE)-$(VERSION)/tools/Makefile \
$(PACKAGE)-$(VERSION)/tools/utils.sh \
$(PACKAGE)-$(VERSION)/tools/utils.c \
$(PACKAGE)-$(VERSION)/tools/project.conf \
$(PACKAGE)-$(VERSION)/Makefile \
$(PACKAGE)-$(VERSION)/COPYING \

View File

@ -15,16 +15,19 @@ INSTALL = install
all: $(TARGETS)
utilbox_OBJS = utilbox.o
utilbox_OBJS = utilbox.o utils.o
utilbox_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
utilbox_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
utilbox: $(utilbox_OBJS) utils.c
utilbox: $(utilbox_OBJS)
$(CC) -o utilbox $(utilbox_OBJS) $(utilbox_LDFLAGS)
utilbox.o: utilbox.c
$(CC) $(utilbox_CFLAGS) -c utilbox.c
utils.o: utils.c
$(CC) $(utilbox_CFLAGS) -c utils.c
clean:
$(RM) $(utilbox_OBJS)

View File

@ -1,9 +1,8 @@
targets=utilbox
cflags_force=-W
cflags=-Wall -g -O2 -pedantic
dist=Makefile,utils.sh,utils.c
dist=Makefile,utils.sh
[utilbox]
type=binary
sources=utilbox.c
depends=utils.c
sources=utilbox.c,utils.c

View File

@ -20,22 +20,11 @@
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include "utilbox.h"
/* utilbox */
/* private */
/* types */
typedef struct _Call
{
char const * name;
int (*call)(int argc, char * argv[]);
} Call;
/* variables */
#include "utils.c"
/* prototypes */
static int _error(char const * message, int ret);
static int _list(Call * calls);
@ -86,25 +75,25 @@ int main(int argc, char * argv[])
if((p = strdup(argv[0])) == NULL)
return _error(NULL, 2);
q = basename(p);
for(i = 0; _calls[i].name != NULL; i++)
if(strcmp(_calls[i].name, q) == 0)
for(i = 0; calls[i].name != NULL; i++)
if(strcmp(calls[i].name, q) == 0)
{
free(p);
return _calls[i].call(argc, argv);
return calls[i].call(argc, argv);
}
free(p);
while((o = getopt(argc, argv, "l")) != -1)
switch(o)
{
case 'l':
return _list(_calls);
return _list(calls);
default:
return _usage();
}
if(optind == argc)
return _usage();
for(i = 0; _calls[i].name != NULL; i++)
if(strcmp(_calls[i].name, argv[optind]) == 0)
return _calls[i].call(argc - optind, &argv[optind]);
for(i = 0; calls[i].name != NULL; i++)
if(strcmp(calls[i].name, argv[optind]) == 0)
return calls[i].call(argc - optind, &argv[optind]);
return 0;
}

34
tools/utilbox.h Normal file
View File

@ -0,0 +1,34 @@
/* $Id$ */
/* Copyright (c) 2009 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Unix utils */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef UTILS_UTILBOX_H
# define UTILS_UTILBOX_H
/* utilbox */
/* types */
typedef struct _Call
{
char const * name;
int (*call)(int argc, char * argv[]);
} Call;
/* variables */
extern Call calls[];
#endif /* UTILS_UTILBOX_H */

View File

@ -15,6 +15,9 @@
#include "utilbox.h"
/* basename.c */
#define main _basename_main
#define _usage _basename_usage
@ -706,7 +709,7 @@
#undef _who
static Call _calls[] =
Call calls[] =
{
{ "basename", _basename_main },

View File

@ -60,6 +60,9 @@ cat > "utils.c" << EOF
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "utilbox.h"
EOF
for i in ../src/*.c; do
BASENAME=`basename $i`
@ -73,7 +76,7 @@ done >> "utils.c"
cat >> "utils.c" << EOF
static Call _calls[] =
Call calls[] =
{
$CALLS
{ NULL, NULL }