aboutsummaryrefslogtreecommitdiffstats
path: root/libasn1compiler/asn1c_compat.c
diff options
context:
space:
mode:
authorLev Walkin <vlm@lionet.info>2004-08-13 16:58:19 +0000
committerLev Walkin <vlm@lionet.info>2004-08-13 16:58:19 +0000
commit79f5495aaa51627e231c146665b28dfb42e3c13c (patch)
treebc35b7bc6a9890274b23f1cde400877bd0f9eeb5 /libasn1compiler/asn1c_compat.c
parent0ce0fd7bb4bd6127452fd213b1e2d2105ae103ea (diff)
custom basename(3) and dirname(3)
Diffstat (limited to 'libasn1compiler/asn1c_compat.c')
-rw-r--r--libasn1compiler/asn1c_compat.c94
1 files changed, 94 insertions, 0 deletions
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 <asn1c_compat.h>
+
+#include <string.h>
+#include <errno.h>
+
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h> /* 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;
+}
+