After googling everything I could, the best I could do with this is get the following warning with -Wall:
Here's what I have for my code:
If Apache or Nginx are running it should grab the pid from /var/run and list its directory in /proc/$pid/fd. Right now, replacing the code in the while loop with "printf("%s%s\n", pro, ep->d_name);" works just fine. However, my current code dies after listing one directory at that location.
I tried casting, dereferncing pointers and a lot of other things I could think of, but kept getting segmentation faults. Any suggestions greatly appreciated.
There are lots of problems here. One immediately obvious problem (as explicitly pointed out by the warning gcc gave you) is that the 2nd argument in the call:
should be a pointer to a string, not a pointer to a pointer to a string. (Change &str to str to fix that problem and get rid of the warning.)
Then you have a string of correctly "formatted" strncat() function calls that can easily overflow the arrays you're using and are, in some cases, appending strings to the end of uninitialized arrays of characters. Any of these operations might produce a segmentation fault. The last argument given to strncat() should NEVER be larger than the minimum of the two
values:
the size of the array to which you are copying data minus the length of the string in that array minus the length of the string you are appending to that array minus one, or
the size of the array from which you are copying characters to the destination string (unless you absolutely, positively, know that array contains a null terminated string that is no longer than the size of the array in which it is located).
In the following list of statements in your code:
Notes:
Lines 3, 4, and 5 define arrays of characters but do not initialize those arrays. The probability that the 1st byte of any of these arrays will be a NUL character is about 1 in 256.
If filename can't be opened for reading, file will be set to a NULL pointer on line 7, every call to fgets() on line 8 will return a NULL pointer leaving the contents of the pid[] array uninitialized, which could cause line 9 to randomly change a <newline> character somewhere in memory to a NUL character or drop core with a segmentation violation. (And, don't tell me you have already verified that it can be opened for reading; you call fopen() which consumes a stream and a file descriptor in main() and you do not ever close that stream nor release that file descriptor. So a second attempt to open the same file using another stream may fail.) Instead of passing in a file name, why don't you save the stream pointer returned by the fopen calls in main() and pass in that stream pointer instead of the file's name?)
On many systems, the type pid_t is bigger than a short integer. If that is true on your system, the PID found on the last line of the file named by filename may be longer than five characters. If it is six or more characters long, fgets() won't insert a <newline> character in pid[] and the strtok() on line 9 may again wipe out a <newline> character beyond the limits of the pid[] array or drop core with a segmentation violation.
Since pid[] may be an uninitialized array, line 10 will copy up to 10 bytes from a 7 byte array which could also drop core on a segmentation fault.
If lines 9, 10, and 11 didn't drop core, line 12 will append no more than 9 bytes from the string /proc/xxxxx/fd/ (where xxxx is a 1 to 5 digit PID or a zero to 9 byte random string) to the end of the string in the uninitialized array dir[]. This would seem to give you a 0% chance of having anything useful in dir[] and could also give you another chance for a core dump with a segmentation violation. Should line 12 use strncpy() instead of strncat()? Why do you need dir[]; why not just use pro[] instead of making a copy of it?
Then, let us move down a little further in your code where we find:
which is more clearly written as:
which I assume is not at all what you want. And, as mentioned before the &str is clearly wrong. But, even if you had written it as:
there are still problems. Why are you copying around a filename twice? Although the name of a file in /proc/pid/fd is likely to be no more than 6 characters long (including the NUL terminator), copying 60 bytes from an array of at least NAME_MAX + 1 bytes is dangerous. And then appending up to 60 bytes from the string that you just copied to a string that could be close to 20 bytes long into an array allocated to hold 60 bytes is asking for another chance to drop core on a segmentation violation.
Why not use the much simpler, much less error-prone, more efficient:
This User Gave Thanks to Don Cragun For This Post:
I must apologize, I meant to take out &str before posting this. I knew it did not work, but before asking for help I went through a phase of trying to brute-force something that would work.
I kept the character arrays small to try and keep things lean. I guess that was wishful thinking. I went ahead and bumped them all up to at least 20, but saw no change.
The multiple times copying were from frustration and looking at the code too long I think. I was out of ideas and kept trying new things in hopes of making it work.
I took your advice and added curly braces to that while loop and took out unneed stuff:
Now I have this problem:
The reason I don't just use:
Is because I need to eventually loop through each directory name to and call each one with another function that is already working.
I shall work on closing fopen, replacing strtok() with something more stable and your other suggestions. Thank you for the thorough response.
Yep, I was not interested in "." or "..", but was just trying to focus on one problem at a time. That worked!
Much appreciated Don Cragun. I've been trying to get back into C after a long break from it. I will definitely re-read all the suggestions you've made. Thanks again!
Note also that unless you are absolutely positive that that your arrays are large enough to hold the desired text, you should verify that the snprintf(), strncat(), etc. calls didn't truncate your buffer (instead of silently continuing with truncated data). For example, with the last example loop in post #4, consider adding error checking as follows:
This User Gave Thanks to Don Cragun For This Post:
Pointers are seeming to get the best of me and I get that error in my program.
Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n"
#define REPORTHEADING2 " Name ... (1 Reply)
Hi,
I have received an application that stores some properties in a file. The existing struct looks like this:
struct TData
{
UINT uSizeIncludingStrings;
// copy of Telnet data struct
UINT uSize;
// basic properties:
TCHAR szHost; //defined in Sshconfig
UINT iPortNr;
TCHAR... (2 Replies)
Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially):
xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’:
xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’
The same error with all c++ constructors - gcc 4.4.4.
If anyone can throw any light on... (8 Replies)
in C i am using this code to get the c time or a time or m time
struct dirent *dir;
struct stat my;
stat(what, &my);
thetime = my.st_ctime;
How can i check if i have permission to check the c time of the file? (1 Reply)
Can someone tell me how to do this?
Just a thought that entered my mind when learning about structs.
First thought was:
struct one
{
struct two;
}
struct two
{
three;
}
one->two->three
would this be how you would access "three"? (1 Reply)
hello everybody!
i have aproblem! i dont know how to concatenate const char* with char
const char *buffer;
char *b;
sprintf(b,"result.txt");
strcat(buffer,b);
thanx in advance (4 Replies)
Hello,
I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it:
char* getext(char *file)
{
char *extension;
int i, j;... (5 Replies)
Hi,
How to replace any character in a file with a newline character using sed ..
Ex:
To replace ',' with newline
Input:
abcd,efgh,ijkl,mnop
Output:
abcd
efgh
ijkl
mnop
Thnx in advance.
Regards,
Sasidhar (5 Replies)
I receive an integer as argument for a function.
within function definition i want it to be of type struct tm.
eg..
main()
{
int a;
......
}
function(...,..,a,..)
int a;
{
struct tm tm;
if(!a)
^ time(&a);
^ ... (4 Replies)