A patch from Vladimir to simplify concat_path_file() and
authorEric Andersen <andersen@codepoet.org>
Mon, 7 May 2001 23:01:32 +0000 (23:01 -0000)
committerEric Andersen <andersen@codepoet.org>
Mon, 7 May 2001 23:01:32 +0000 (23:01 -0000)
make it less likely to break.

libbb/concat_path_file.c

index ce92310eaa7a139a4f15f5abd1cce28d1b826c6d..6b7abf24ba9508a61099a89faa099f3070e266da 100644 (file)
 extern char *concat_path_file(const char *path, const char *filename)
 {
        char *outbuf;
-       int  l;
-       int  flg_slash = 1;
-
-       l = strlen(path);
-       if (l>0 && path[l-1] == '/')
-               flg_slash--;
-       l += strlen(filename);
-       if (l>0 && filename[0] == '/')
-               flg_slash--;
-       outbuf = xmalloc(l+1+flg_slash);
-       sprintf(outbuf, (flg_slash ? "%s/%s" : "%s%s"), path, filename);
+       const char *lc;
+       
+       lc = last_char_is((char*)path, '/');
+       if (filename[0] == '/')
+               filename++;
+       outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL));
+       sprintf(outbuf, (lc==NULL ? "%s/%s" : "%s%s"), path, filename);
        return outbuf;
 }