X-Git-Url: https://pd.if.org/git/?p=pdutils;a=blobdiff_plain;f=utils%2Fdirname%2Fdirname.c;fp=utils%2Fdirname%2Fdirname.c;h=c490a56bf37fe9cfce403abf1867bbb4fc08b0d6;hp=0000000000000000000000000000000000000000;hb=4daa873acbfc240e9ecc350e74c14bbfd58fabc9;hpb=b6d847ae20c32744d508eced2be3132c3fa8c5b9 diff --git a/utils/dirname/dirname.c b/utils/dirname/dirname.c new file mode 100644 index 0000000..c490a56 --- /dev/null +++ b/utils/dirname/dirname.c @@ -0,0 +1,52 @@ +/* + * dirname.c - return the directory portion of a pathname + * + * Version: 2008-1.01 + * Build: c89 -o dirname dirname.c + * Source: + * Spec: + * + * This is free and unencumbered software released into the public domain, + * provided "as is", without warranty of any kind, express or implied. See the + * file UNLICENSE and the website for further details. + */ + + +#define _POSIX_SOURCE + +#include +#include + +#define USAGE "usage: dirname string\n" + + +int main(int argc, char **argv) +{ + char *head, *tail; + + setlocale (LC_ALL, ""); + + if (argc != 2) + { + fprintf(stderr, USAGE); + return(1); + } + + head = tail = argv[1]; + while (*tail) + tail++; + + while (tail > head && tail[-1] == '/') + tail--; + while (tail > head && tail[-1] != '/') + tail--; + while (tail > head && tail[-1] == '/') + tail--; + + if (head == tail) + printf(*head == '/' ? "/\n" : ".\n"); + else + printf("%.*s\n", (tail - head), head); + + return(0); +}