On Tue 11 Mar 2003, jw schultz wrote:
> 
> That or char fscratch[MAXPATHLEN];
> Just don't use malloc.

How about this:


static int get_tmpname(char *fnametmp, char *fname)
{
        char    *f;
        char    *dir = "";      /* what dir to put the temp file in */
        char    namepart[NAME_MAX-10];  /* we never need more than this, if   */
                                        /* the name is longer, we would end   */
                                        /* up having to shorten it anyway     */
        if (tmpdir)
                dir = tmpdir;

        f = strrchr(fname,'/'); /* is there a directory to skip in fname? */
        if (f == NULL) {                /* no */
                /* the strlcpy takes care of making the name short enough */
                /* and null-terminating it at the same time. */
                strlcpy(namepart, fname, sizeof(namepart));
        }
        else {                          /* yes */
                strlcpy(namepart, f+1, sizeof(namepart));
        }

        if (strlen(dir)+strlen(namepart)+1 > MAXPATHLEN) {
                /* how often will this happen... the temp dir would have to */
                /* a very long name, and hence the fault of the user who    */
                /* specified it. Let him fix the problem.                   */
                rprintf(FERROR,"filename too long\n");
                return 0;
        }

        /* construct temp name template */
        snprintf(fnametmp, MAXPATHLEN, "%s%s.%s.XXXXXX",
                                        dir, dir[0]?"/":"", namepart);

        return 1;
}


Even shorter than it was, it should now be difficult to mess this up :-)
I thought of letting the default value of dir be "./", but then
clean_fname would end up shifting that off again, thus wasting
resources.


Paul Slootman
-- 
To unsubscribe or change options: http://lists.samba.org/mailman/listinfo/rsync
Before posting, read: http://www.tuxedo.org/~esr/faqs/smart-questions.html

Reply via email to