These are probably cosmetic comments, but here they are anyway...
At 09:54 13/01/01 +0000, W.H.Scholten wrote:
>+char *dirname(char *path) {
>+ char *slash;
>+
>+ while (path[ strlen(path)-1 ] == '/') path[ strlen(path)-1 ] = 0;
if path is an empty string, you are accessing path[-1], which is dangerous.
Also, you're computing strlen too many times, and strlen is a loop
(while (*ptr) ptr++). so I suggest using a pointer to the string as in
/usr/bin/dirname/dirname.c. mainly:
if (*path == '\0') return "/"; /* if const is not ok, strdup("/") */
ptr = path;
while (*ptr) ptr++;
while ((*ptr == '/') && (ptr > path)) ptr--;
...
>+
>+ slash = strrchr(path, '/');
>+ if (slash) {
>+ *slash = 0;
>+ while (path[ strlen(path)-1 ] == '/') path[ strlen(path)-1
>] = 0;
you already did that (I mean trimming the trailing slashes).
Finally, I'd propose adding such a function (dirname) in a library (either libc
or say libfile) to allow code reuse. such a lib would contain functions
such as basename, dir_create (mkdir -p), .... so that the commands
mkdir, rmdir, cp, mv, ... etc call the lib functions instead of rewriting code.
regards,
mouss
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message