view toys/basename.c @ 408:8506c538f26a

Comment changes, and add a blank line to the help text.
author Rob Landley <rob@landley.net>
date Wed, 18 Jan 2012 20:32:52 -0600
parents a8b14410e784
children 55598a9b8f21
line wrap: on
line source

/* vi: set sw=4 ts=4:
 *
 * basename.c - Return non-directory portion of a pathname
 *
 * Copyright 2012 Tryn Mirell <tryn@mirell.org>
 *
 * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html


USE_BASENAME(NEWTOY(basename, NULL, TOYFLAG_USR|TOYFLAG_BIN))

config BASENAME
	bool "basename"
	default n
	help
        usage: basename string [suffix]

        Return non-directory portion of a pathname
*/

#include "toys.h"

void basename_main(void)
{
    char *arg, *suffix, *base;
    int arglen;

    arg = toys.optargs[0];
    suffix = toys.optargs[1];

    // return null string if nothing provided
    if (!arg) return; 

    arglen = strlen(arg);
 
    // handle the case where we only have single slash
    if (arglen == 1 && arg[0] == '/') {
        puts("/");
        return;
    }

    // remove trailing slash
    if (arg[arglen - 1] == '/') {
        arg[arglen - 1] = 0;
    }

    // get everything past the last /
    base = strrchr(arg, '/');

    if (!base) base = arg;
    else base++;
   
    // handle the case where we have all slashes
    if (base[0] == 0) base = "/";  
    
    // chop off the suffix if provided
    if (suffix) {
        strstr(base, suffix)[0] = 0;
    }

    puts(base);
}