Mercurial > hg-old > index.cgi
changeset 114:c65fcec346cd
Handle input files on command line and add some memory management utility functions
author | lost |
---|---|
date | Sat, 17 Jan 2009 20:54:58 +0000 |
parents | f4a489ebd44a |
children | 776d8bea5b46 |
files | lwlink/trunk/src/Makefile.am lwlink/trunk/src/lwlink.c lwlink/trunk/src/lwlink.h lwlink/trunk/src/main.c lwlink/trunk/src/util.c lwlink/trunk/src/util.h |
diffstat | 6 files changed, 155 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/lwlink/trunk/src/Makefile.am Sat Jan 17 20:54:20 2009 +0000 +++ b/lwlink/trunk/src/Makefile.am Sat Jan 17 20:54:58 2009 +0000 @@ -1,3 +1,3 @@ bin_PROGRAMS = lwlink -lwlink_SOURCES = main.c lwlink.c -EXTRA_DIST = lwlink.h +lwlink_SOURCES = main.c lwlink.c util.c +EXTRA_DIST = lwlink.h util.h
--- a/lwlink/trunk/src/lwlink.c Sat Jan 17 20:54:20 2009 +0000 +++ b/lwlink/trunk/src/lwlink.c Sat Jan 17 20:54:58 2009 +0000 @@ -33,8 +33,18 @@ #include <stdlib.h> #include "lwlink.h" +#include "util.h" int debug_level = 0; int outformat = OUTPUT_DECB; char *outfile = NULL; +char **inputfiles = NULL; +int ninputfiles = 0; + +void add_input_file(char *fn) +{ + inputfiles = lw_realloc(inputfiles, sizeof(char *) * (ninputfiles + 1)); + inputfiles[ninputfiles++] = lw_strdup(fn); +} +
--- a/lwlink/trunk/src/lwlink.h Sat Jan 17 20:54:20 2009 +0000 +++ b/lwlink/trunk/src/lwlink.h Sat Jan 17 20:54:58 2009 +0000 @@ -32,7 +32,15 @@ extern int debug_level; extern int outformat; extern char *outfile; +extern int ninputfiles; +extern char **inputfiles; +#define __lwlink_E__ extern +#else +#define __lwlink_E__ #endif // __lwlink_c_seen__ +__lwlink_E__ void add_input_file(char *fn); + +#undef __lwlink_E__ #endif //__lwlink_h_seen__
--- a/lwlink/trunk/src/main.c Sat Jan 17 20:54:20 2009 +0000 +++ b/lwlink/trunk/src/main.c Sat Jan 17 20:54:58 2009 +0000 @@ -43,9 +43,6 @@ { case 'o': // output - if (outfile) - { - } outfile = arg; break; @@ -83,7 +80,7 @@ break; case ARGP_KEY_ARG: - // FIXME: input files! + add_input_file(arg); break; default: @@ -120,6 +117,11 @@ int main(int argc, char **argv) { argp_parse(&argp, argc, argv, 0, 0, NULL); - + if (ninputfiles == 0) + { + fprintf(stderr, "No input files\n"); + exit(1); + } + exit(0); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwlink/trunk/src/util.c Sat Jan 17 20:54:58 2009 +0000 @@ -0,0 +1,84 @@ +/* +util.c +Copyright © 2008 William Astle + +This file is part of LWLINK. + +LWLINK 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, either version 3 of the License, or (at your option) any later +version. + +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/>. +*/ + +/* +Utility functions +*/ + +#define __util_c_seen__ + +#include <malloc.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +void *lw_malloc(int size) +{ + void *ptr; + + ptr = malloc(size); + if (!ptr) + { + // bail out; memory allocation error + fprintf(stderr, "lw_malloc(): Memory allocation error\n"); + exit(1); + } + return ptr; +} + +void *lw_realloc(void *optr, int size) +{ + void *ptr; + + if (size == 0) + { + lw_free(optr); + return; + } + + ptr = realloc(optr, size); + if (!ptr) + { + fprintf(stderr, "lw_realloc(): memory allocation error\n"); + exit(1); + } +} + +void lw_free(void *ptr) +{ + if (ptr) + free(ptr); +} + +char *lw_strdup(const char *s) +{ + char *d; + + d = strdup(s); + if (!d) + { + fprintf(stderr, "lw_strdup(): memory allocation error\n"); + exit(1); + } + + return d; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwlink/trunk/src/util.h Sat Jan 17 20:54:58 2009 +0000 @@ -0,0 +1,44 @@ +/* +util.h +Copyright © 2008 William Astle + +This file is part of LWLINK. + +LWLINK 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, either version 3 of the License, or (at your option) any later +version. + +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/>. +*/ + +/* +Utility functions +*/ + +#ifndef __util_h_seen__ +#define __util_h_seen__ + +#ifndef __util_c_seen__ +#define __util_E__ extern +#else +#define __util_E__ +#endif + +// allocate memory +__util_E__ void *lw_malloc(int size); +__util_E__ void lw_free(void *ptr); +__util_E__ void *lw_realloc(void *optr, int size); + +// string stuff +__util_E__ char *lw_strdup(const char *s); + +#undef __util_E__ + +#endif // __util_h_seen__