bug-make
[Top][All Lists]
Advanced

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

Re: [PATCH] Enhance/fix VMS setting of program name, MAKE/MAKE_COMMAND,


From: Ralph Corderoy
Subject: Re: [PATCH] Enhance/fix VMS setting of program name, MAKE/MAKE_COMMAND, variables
Date: Tue, 26 Aug 2014 10:19:15 +0100

Hi becker,

    /*
     * Argv0 will be a full vms file specification, like
     * node$dka100:[utils.gnumake]make.exe;47
     * the vms progname should be ^^^^, the filename without
     * file type .exe and ;version (and there are some checks,
     * just in case something unexpected is passed and then the
     * progname is set to "make").
     */
    char *
    vms_progname(const char* argv0)
    {
        int len;
        char *progname;
        char *p;
        p = strrchr(argv0, ']');
        if (p) {
            p++;
            if (*p) {
                progname = malloc(strlen(p) + 1);

Can malloc fail on VMS?

                strcpy(progname, p);
                p = strrchr(progname, ';');
                if (p)
                    *p = '\0';
                len = strlen(progname);
                if (0 == strcasecmp(&progname[len - 4], ".exe"))

What if len is less than 4?

                    progname[len - 4] = '\0';
                if (!*progname)
                    progname = "make";
            } else
                progname = "make";
            }
        else
            progname = "make";
        return progname;
    }

I found it was a bit tedious to keep track of state in my head just so
we end up at a single return statement;  checking malloc will make that
worse.  This, untested, seems clearer to me;  return ASAP.

    const char *vms_progname(const char *argv0)
    {
        static char const fallback[] = "make";
        static char const suffix[] = ".exe";
        const size_t suflen = sizeof suffix - 1;
        char *s, *e;
        char *name;

        /* Return \1 of /^.*](.+)(?:\.exe)?(?:;[^;]*)?$/. */

        s = strrchr(argv0, ']');
        if (!s || !*++s)
            return fallback;

        e = strrchr(s, ';');
        if (!e)
            e = s + strlen(s);
        if (e - s >= suflen &&
            !strncasecmp(e - suflen, suffix, suflen))
            e -= suflen;
        if (e == s)
            return fallback;

        name = malloc(e - s + 1);
        if (!name)
            return fallback;
        strncpy(name, s, e - s);
        name[e - s] = '\0';

        return name;
    }

Cheers, Ralph.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]