You are partially right, there is no need to test for the "." and ".." at all.
The real difference between your approach and mine is that you step each filename one dot at a time until you get to the end, while I only check whether the filename contains exactly the "cl" after the
first dot. It is not about which functions are used, but the logic.
What if filename has less than 3 characters? The len-3 goes haywire.
Your line 2:
The return type of strlen is
not unsigned char. You
must read the documentation of the functions that you do use.
Your line 3:
d_name is a pointer or array, so
(*ent->d_name)
means same as
ent->d_name[0]
, i.e. one character. What happens, if you write:
1 2
|
char a[8] = "Hello";
a[3] = 7;
|
Your line 3 does something very similar.
Lets assume that ent->d_name is a pointer. If you do advance it, then it will not point to the beginning of the filename any more. What you want is a
new pointer:
1 2
|
size_t len = strlen( ent->d_name );
char * p = ent->d_name + len - 3;
|
However, the strchr does practically the same thing, and is safer.
Your line 4:
Why copy at all? If you do have a pointer to third character from end, then you effectively have a 3-character string that you can compare with ".cl" (like my code does).
Besides, if you just look at the end, then you don't test whether there are more dots earlier.
Do not try to optimize. Seriously. You have enough challenge on writing both syntactically and logically correct code. Thinking about whether this or that standard function uses one less CPU cycle is utter waste of your time.
Unrelated note, you return either
EXIT_FAILURE
or
true
. That looks inconsistent. What are the values of those? Are they both 1?