How do you obtain the file name from Windows? Which API function call are you using? If the documentation for that API function call specifies that the name is null terminated, then you should not be getting the garbage. But some old file systems set off a fixed length buffer for the file name - even in Unix! The original Unix directory was 16 bytes per entry: two bytes inode, 14 chars filename. The orginal FAT had room for 8 char file name, 3 char extension. If the field was filled with a maximum length file name (in either Unix or FAT), there was no room for a terminating nul. If you reach the end of the field before finding a null, it is nevertheless the end of the field!
Remember that nul terminated strings is an idea conceived for the C language. It directoy conflicts with the ISO character set standard, whether ISO 646, 8859 or other variants. Other languages have used other solutions, e.g. in Pascal, a string is (at least in most implementations) represented by a length field associated with the string buffer (so the ISO NUL character can be used in the ISO standard way!). Many other languages to the same.
DOS was developed before C and Unix. Windows was developed before Unix had become very important, at least in the market segments Windows was aimed at. So you should not take for granted that all sorts of C / Unix conventions have been adopeted in every other segment.
You are making a wrong assumption. The sizeof(dir_struct->directory) operator will return 2048. But what if the string in that table is less than that? You need an actual count of the number of valid characters in the directory field (assuming that it is correctly null terminated).
You are also mixing the dot (.) and arrow (->) operators on your struct reference.
But all this is largely irrelevant. You need to go back a stage to the code that fills the directory structure in the first place, as that is where the problem is most likely to be found.
"You are also mixing the dot (.) and arrow (->) operators on your struct reference."
Yes, you are right, I taken those lines of codes from different function (in one of them was reference, in other was pointer), that why those mixing are there.
Well you should not be doing so, since the reference in both cases is a structure, not a pointer. The dot operator is the correct one.
But more importantly you have still not shown or explained when and where this structure gets initialised, and the directory name copied in. Until you give us that information anything we pass on is pure guesswork.
You have actually identified your problem, you just haven't realized it.
strncpy(dir_strut->directory, "/", sizeof(dir_struct_directory)); // here name is good
strcat(dir_struct->directory, current_file->name); // here name is bad
It would seem that current_file->name is not a null terminated C string. You have not given us the details of current_file, or how it is instantiated, so we can't further diagnose the problem at the moment. Perhaps current_file has a member that tells you how long the name member is, in which case you should use that and strncat to append the file name to the directory name.
I have just tried to reproduce what you have done and the code works fine. I can only conclude that the code you are showing does not match what you are actually running. But since you have only shown small pieces and not the complete method where this occurs, it is difficult to say more.
What you need to display is the assignment to dir_struct.directory - nothing more. It may be as simple as a single statement (/line). I guess that confidentiality issues won't preclude that.
How did you get hold of that value? Which system call or library function provided it?
If you say "None, we built it from pieces ourselves, and that code is confidential", then you have got the answer: In that string building code you forgot to add the terminating nul. Otherwise: Tell us who provided the directory string, in which way.
I have managed to create a sample of all that code, although I find the #define list_entry_const somewhat difficult to deconstruct. However, with a valid name in the file_info entry it works fine. I can only assume that somewhere in the actual code that you are running there is some corruption of your data, or a pointer is not set up correctly.
I also could not find the TRACE macro in Windows.
I am really puzzled by how difficult it seem to be to explain how the value of that .directory was initialized. Where its value came from. How it was created.
Obviously, whatever created the value did not follow the C convention of terminating it with a nul - most likely because it doesn't have a C origin. Fair enough. But why is it so difficult to show how the .directory value was obtained?
As it now stands, it is like complaining "This variable should have av value between 0 and 100, but it is above 100. Why???" If you can't figure that out yourself, you must reveal how you calculate it, you cannot simply tell us how you use it, after it has gotten that out-of-range value-
Your directory string is another out-of-range value. How you use it, after it got this illegal value is of no interest. The important thing is how it got that value, not how you later use it. Like if you ask "Why does my value exceed 100?" Noone can tell unless you tell how you got or calculated that value. String values are no different: If they have an illegal value, such as not being nul-terminated in contexts where that is expected, the problem is in the source, not in the use of the value.
If it is as difficult as it seems to trace the source, identify where that directory sting came from, then you should not expect others to be able to help you. "I have an illegal value, but I can't tell where it came from - why is it not legal?" - noone can give you a reasonable answer to that.
So come on, tell us how the .directory value was obtained/created! Until you do that, you cannot expect any useful help from other forum members
Last Visit: 26-Sep-20 2:58 Last Update: 26-Sep-20 2:58