ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
It sounds like the delimiter is (char)0 where the original command line had one or more blanks.
Then I'm not sure whether your fgets is stopping on that (char)0 so you need another to get more. Or whether the fgets was OK and you just thought you got too little. Simple methods of displaying what you read would of course stop on the (char)0.
Such discussion as I have seen appears says fgets does not do anything special with '\0' in its input, which makes it quite difficult to distinguish an embedded '\0' in the string it read from the '\0' it adds at the end. Probably difficult enough that you should not use fgets for this purpose. Use some other function for reading.
I don't think changing the way you open the file helps.
How can i read it with another function?
The obvious (though certainly not only) choice is fread().
Assuming the requested length is large enough and there is no '\n' character in the buffer, I think fgets and fread would read the same thing into the buffer. The important difference is the return value from fread tells you how much it read.
If you just look in the buffer, the first '\0' you find is just the end of the argv part. Just past that '\0' is the argv part and so on for however many args there are. But you need to know where to stop. Knowing how much was read is the advantage of fread over fgets.
This holds the complete command line for the process, unless the process is a zombie.
In the latter case, there is nothing in this file: that is, a read on this file will
return 0 characters. The command-line arguments appear in this file as a set of null-
separated strings, with a further null byte ('\0') after the last string.
That tells me that read (with open proper) is the most appropriate because it doesn't involve a libc buffer and it returns the number of bytes read, a combination that isn't provided by other functions.
2) I should have looked for the documentation you just quoted, rather than guessing based on just the info the OP provided.
Now that I've seen the documentation, I think even fgets() would be fine for reading this file. The trick is what to do with the result.
Each part will be terminated with '\0' so a '\0' in the buffer doesn't mark the end of the whole command line, just the end of each part. The end of the whole command line will be marked with three '\0\ in a row (two in a row from the file, plus one more added by fgets).
I suggested read because it's a system call and the file isn't a text file, per se, because of the embedded null characters. For that reason, buffered operations, e.g. fgets, might interpret the input incorrectly, especially if for some reason an argument contained a newline. It's essentially just a binary data file that happens to be text and null characters merely by the convenience of the shell being a textual world. You gain nothing over read by using a libc stream operation, other than overhead.