Re: [PATCH 01/11] last: include libgen.h for basename(3p)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday 06 December 2016, Karel Zak wrote:
> On Thu, Dec 01, 2016 at 09:52:04AM +0100, Ruediger Meier wrote:
> > From: Ruediger Meier <ruediger.meier@xxxxxxxxxxx>
> >
> > Signed-off-by: Ruediger Meier <ruediger.meier@xxxxxxxxxxx>
> > ---
> >  login-utils/last.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/login-utils/last.c b/login-utils/last.c
> > index 6d0e892..90eee68 100644
> > --- a/login-utils/last.c
> > +++ b/login-utils/last.c
> > @@ -40,6 +40,7 @@
> >  #include <netinet/in.h>
> >  #include <netdb.h>
> >  #include <arpa/inet.h>
> > +#include <libgen.h>
> >
> >  #include "c.h"
> >  #include "nls.h"
> > @@ -877,7 +878,11 @@ static void process_wtmp_file(const struct
> > last_control *ctl, }
> >  	}
> >
> > -	printf(_("\n%s begins %s"), basename(filename),
> > ctime(&begintime)); +	{
> > +		char* tmp = xstrdup(filename);
> > +		printf(_("\n%s begins %s"), basename(tmp), ctime(&begintime));
> > +		free(tmp);
>
>  This seems like pretty common pattern, what about to introduce
>  xbasename(filename, &tmp)?

Or the comfort version below? Caller has to
free the returned string only.

char *xbasename(const char *path)
{
        char *tmp, *p, *ret;

        tmp = xstrdup(path);
        p = basename(tmp);
        ret = xstrdup(p);

        free(tmp);
        return ret;
}

... plus we could also import musl's basename code to get rid of this
libgen.h/GNU thing at all:

char *basename(char *s)
{
        size_t i;
        if (!s || !*s) return ".";
        i = strlen(s)-1;
        for (; i&&s[i]=='/'; i--) s[i] = 0;
        for (; i&&s[i-1]!='/'; i--);
        return s+i;
}

cu,
Rudi
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux