From 1f1d8cb956c24a0f76b09019a2971eebb5fc3d52 Mon Sep 17 00:00:00 2001 From: vlm Date: Fri, 13 Aug 2004 16:58:19 +0000 Subject: [PATCH] custom basename(3) and dirname(3) git-svn-id: https://asn1c.svn.sourceforge.net/svnroot/asn1c/trunk@114 59561ff5-6e30-0410-9f3c-9617f08c8826 --- asn1c/asn1c.c | 9 +-- configure | 2 +- configure.in | 2 +- libasn1compiler/Makefile.am | 1 + libasn1compiler/Makefile.in | 6 +- libasn1compiler/asn1c_compat.c | 94 ++++++++++++++++++++++++++++++++ libasn1compiler/asn1c_compat.h | 11 ++++ libasn1compiler/asn1c_internal.h | 5 +- libasn1compiler/asn1c_save.c | 8 ++- libasn1parser/Makefile.in | 2 +- 10 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 libasn1compiler/asn1c_compat.c create mode 100644 libasn1compiler/asn1c_compat.h diff --git a/asn1c/asn1c.c b/asn1c/asn1c.c index 086b4caf..0fd40dbc 100644 --- a/asn1c/asn1c.c +++ b/asn1c/asn1c.c @@ -8,7 +8,6 @@ #include #include /* for stat(2) */ #include -#include /* for basename(3) */ #include /* for EX_USAGE */ #include #include @@ -18,6 +17,8 @@ #include /* Print the ASN.1 tree */ #include /* Compile the ASN.1 tree */ +#include /* Portable basename(3) and dirname(3) */ + static void usage(char *av0); /* Print the Usage screen and exit(EX_USAGE) */ int @@ -118,7 +119,7 @@ main(int ac, char **av) { av += optind; } else { fprintf(stderr, "%s: No input files specified\n", - basename(av[0])); + a1c_basename(av[0])); exit(1); } @@ -201,7 +202,7 @@ main(int ac, char **av) { char *p; int len; - p = dirname(av[-optind]); + p = a1c_dirname(av[-optind]); len = strlen(p) + sizeof("/../skeletons"); skeletons_dir = alloca(len); @@ -261,7 +262,7 @@ usage(char *av0) { "\t-Wdebug-fixer\tDebug ASN.1 semantics processor\n" "\t-Wdebug-compiler\tDebug ASN.1 compiler\n" , - basename(av0), DATADIR + a1c_basename(av0), DATADIR ); exit(EX_USAGE); } diff --git a/configure b/configure index 373ae218..793e9867 100755 --- a/configure +++ b/configure @@ -21588,7 +21588,7 @@ _ACEOF fi -for ac_header in errno.h +for ac_header in sys/param.h do as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh` if eval "test \"\${$as_ac_Header+set}\" = set"; then diff --git a/configure.in b/configure.in index 05d03ca2..7585cede 100644 --- a/configure.in +++ b/configure.in @@ -67,7 +67,7 @@ esac dnl Checks for header files. AC_HEADER_STDC -AC_CHECK_HEADERS(errno.h) +AC_CHECK_HEADERS(sys/param.h) dnl Checks for typedefs, structures, and compiler characteristics. AC_C_BIGENDIAN diff --git a/libasn1compiler/Makefile.am b/libasn1compiler/Makefile.am index 6b2babdf..301bbaa5 100644 --- a/libasn1compiler/Makefile.am +++ b/libasn1compiler/Makefile.am @@ -13,6 +13,7 @@ libasn1compiler_la_SOURCES = \ asn1c_lang.c asn1c_lang.h \ asn1c_save.c asn1c_save.h \ asn1c_C.c asn1c_C.h \ + asn1c_compat.c asn1c_compat.h \ asn1c_internal.h libasn1compiler_la_LIBADD = \ diff --git a/libasn1compiler/Makefile.in b/libasn1compiler/Makefile.in index 74f98934..64c00377 100644 --- a/libasn1compiler/Makefile.in +++ b/libasn1compiler/Makefile.in @@ -54,7 +54,8 @@ libasn1compiler_la_DEPENDENCIES = \ ${top_builddir}/libasn1parser/libasn1parser.la \ ${top_builddir}/libasn1fix/libasn1fix.la am_libasn1compiler_la_OBJECTS = asn1compiler.lo asn1c_misc.lo \ - asn1c_out.lo asn1c_lang.lo asn1c_save.lo asn1c_C.lo + asn1c_out.lo asn1c_lang.lo asn1c_save.lo asn1c_C.lo \ + asn1c_compat.lo libasn1compiler_la_OBJECTS = $(am_libasn1compiler_la_OBJECTS) check_compiler_SOURCES = check_compiler.c check_compiler_OBJECTS = check_compiler.$(OBJEXT) @@ -68,6 +69,7 @@ DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) depcomp = $(SHELL) $(top_srcdir)/depcomp am__depfiles_maybe = depfiles @AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/asn1c_C.Plo \ +@AMDEP_TRUE@ ./$(DEPDIR)/asn1c_compat.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/asn1c_lang.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/asn1c_misc.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/asn1c_out.Plo \ @@ -210,6 +212,7 @@ libasn1compiler_la_SOURCES = \ asn1c_lang.c asn1c_lang.h \ asn1c_save.c asn1c_save.h \ asn1c_C.c asn1c_C.h \ + asn1c_compat.c asn1c_compat.h \ asn1c_internal.h libasn1compiler_la_LIBADD = \ @@ -281,6 +284,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asn1c_C.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asn1c_compat.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asn1c_lang.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asn1c_misc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asn1c_out.Plo@am__quote@ diff --git a/libasn1compiler/asn1c_compat.c b/libasn1compiler/asn1c_compat.c new file mode 100644 index 00000000..3d8280a0 --- /dev/null +++ b/libasn1compiler/asn1c_compat.c @@ -0,0 +1,94 @@ +#include + +#include +#include + +#ifdef HAVE_SYS_PARAM_H +#include /* For MAXPATHLEN */ +#endif + +#ifndef MAXPATHLEN +#define MAXPATHLEN 1024 +#endif + +char * +a1c_basename(const char *path) { + static char strbuf[MAXPATHLEN]; + const char *pend; + const char *name; + + pend = path + strlen(path); + if(pend == path) { + strcpy(strbuf, "."); + return strbuf; + } + + /* Skip tailing slashes */ + for(pend--; pend > path && *pend == '/'; pend--); + + if(pend == path && *path == '/') { + strcpy(strbuf, "/"); + return strbuf; + } + + for(name = pend; name > path && name[-1] != '/'; name--); + + if((pend - name) >= sizeof(strbuf) - 1) { + errno = ENAMETOOLONG; + return 0; + } + + memcpy(strbuf, name, pend - name + 1); + strbuf[pend - name + 1] = '\0'; + + return strbuf; +} + + +char * +a1c_dirname(const char *path) { + static char strbuf[MAXPATHLEN]; + const char *pend; + const char *last = 0; + int in_slash = 0; + + /* One-pass determination of the last char of the pathname */ + for(pend = path; ; pend++) { + printf("-%c", *pend); + switch(*pend) { + case '\0': break; + case '/': + if(!in_slash) { + last = pend; + in_slash = 1; + } + continue; + default: + if(in_slash) in_slash = 0; + continue; + } + break; + } + printf("\n"); + + if(last <= path) { + strcpy(strbuf, *path == '/' ? "/" : "."); + return strbuf; + } + + if(!last) { + strcpy(strbuf, "/"); + return strbuf; + } + + if((last - path) >= sizeof(strbuf)) { + errno = ENAMETOOLONG; + return 0; + } + + memcpy(strbuf, path, last - path); + strbuf[last - path] = '\0'; + + return strbuf; +} + diff --git a/libasn1compiler/asn1c_compat.h b/libasn1compiler/asn1c_compat.h new file mode 100644 index 00000000..9461b781 --- /dev/null +++ b/libasn1compiler/asn1c_compat.h @@ -0,0 +1,11 @@ +#ifndef ASN1C_COMPAT_H +#define ASN1C_COMPAT_H + +#ifdef HAVE_CONFIG_H +#include +#endif + +char *a1c_basename(const char *path); +char *a1c_dirname(const char *path); + +#endif /* ASN1C_COMPAT_H */ diff --git a/libasn1compiler/asn1c_internal.h b/libasn1compiler/asn1c_internal.h index 6192650c..08a490fb 100644 --- a/libasn1compiler/asn1c_internal.h +++ b/libasn1compiler/asn1c_internal.h @@ -1,13 +1,16 @@ #ifndef _ASN1_COMPILER_INTERNAL_H_ #define _ASN1_COMPILER_INTERNAL_H_ +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include /* for fstat(2) */ #include /* for unlink(2) */ #include /* for open(2) */ #include /* for glob(3) */ -#include /* for basename(3) */ #include #include /* for isalnum(3) */ #include diff --git a/libasn1compiler/asn1c_save.c b/libasn1compiler/asn1c_save.c index fd1356b7..15d1b676 100644 --- a/libasn1compiler/asn1c_save.c +++ b/libasn1compiler/asn1c_save.c @@ -1,4 +1,5 @@ #include "asn1c_internal.h" +#include "asn1c_compat.h" static int asn1c_dump_streams(arg_t *arg); static int asn1c_print_streams(arg_t *arg); @@ -73,7 +74,7 @@ asn1c_save_compiled_output(arg_t *arg, const char *datadir) { return -1; } else { fprintf(mkf, "\t\\\n\t%s", - basename(pg.gl_pathv[i])); + a1c_basename(pg.gl_pathv[i])); } } @@ -213,11 +214,12 @@ asn1c_save_streams(arg_t *arg) { static int asn1c_copy_over(arg_t *arg, char *path) { - char *fname = basename(path); + char *fname; (void)arg; /* Unused argument */ - if(symlink(path, fname)) { + fname = a1c_basename(path); + if(!fname || symlink(path, fname)) { if(errno == EEXIST) { struct stat sb1, sb2; if(stat(path, &sb1) == 0 diff --git a/libasn1parser/Makefile.in b/libasn1parser/Makefile.in index 759122ef..375d37e2 100644 --- a/libasn1parser/Makefile.in +++ b/libasn1parser/Makefile.in @@ -546,8 +546,8 @@ distclean-generic: maintainer-clean-generic: @echo "This command is intended for maintainers to use" @echo "it deletes files that may require special tools to rebuild." - -rm -f asn1p_y.c -rm -f asn1p_y.h + -rm -f asn1p_y.c -rm -f asn1p_l.c clean: clean-am