Annotation of coherent/d/bin/basename.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Strip filename suffix and directory prefix.
        !             3:  */
        !             4: 
        !             5: #include <stdio.h>
        !             6: 
        !             7: char   *basename();
        !             8: 
        !             9: main(argc, argv)
        !            10: char *argv[];
        !            11: {
        !            12:        if (argc < 2 || argc > 3) {
        !            13:                fprintf(stderr, "Usage: basename string [suffix]\n");
        !            14:                exit(1);
        !            15:        }
        !            16:        printf("%s\n", basename(argv[1], argc==2 ? NULL : argv[2]));
        !            17:        exit(0);
        !            18: }
        !            19: 
        !            20: /*
        !            21:  * Do the work of producing a stripping prefixes and
        !            22:  * a suffix (if not NULL).
        !            23:  */
        !            24: char *
        !            25: basename(s, suffix)
        !            26: char *s;
        !            27: char *suffix;
        !            28: {
        !            29:        register char *cp, *ep, *sp;
        !            30:        char *p;
        !            31: 
        !            32:        for (ep = cp = s; *ep != '\0'; ep++)
        !            33:                if (*ep == '/')
        !            34:                        cp = ep+1;
        !            35:        if ((sp = suffix) != NULL) {
        !            36:                ep = cp;
        !            37:                while (*ep++ != '\0')
        !            38:                        ;
        !            39:                ep--;
        !            40:                while (*sp++)
        !            41:                        ep--;
        !            42:                if (ep < cp)
        !            43:                        return (cp);
        !            44:                p = ep;
        !            45:                for (sp = suffix; *sp!='\0' && *ep!='\0'; )
        !            46:                        if (*sp++ != *ep++)
        !            47:                                return (cp);
        !            48:                if (*sp != '\0')
        !            49:                        return (cp);
        !            50:                *p = '\0';
        !            51:        }
        !            52:        return (cp);
        !            53: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.