2 * dirname.c - return the directory portion of a pathname
5 * Build: c89 -o dirname dirname.c
6 * Source: <http://pdcore.sourceforge.net/>
7 * Spec: <http://www.opengroup.org/onlinepubs/9699919799/utilities/dirname.html>
9 * This is free and unencumbered software released into the public domain,
10 * provided "as is", without warranty of any kind, express or implied. See the
11 * file UNLICENSE and the website <http://unlicense.org> for further details.
20 #define USAGE "usage: dirname string\n"
23 int main(int argc, char **argv)
27 setlocale (LC_ALL, "");
31 fprintf(stderr, USAGE);
35 head = tail = argv[1];
39 while (tail > head && tail[-1] == '/')
41 while (tail > head && tail[-1] != '/')
43 while (tail > head && tail[-1] == '/')
47 printf(*head == '/' ? "/\n" : ".\n");
49 printf("%.*s\n", (tail - head), head);