Concatenating Struct Value With Char In C


 
Thread Tools Search this Thread
Top Forums Programming Concatenating Struct Value With Char In C
# 1  
Old 05-29-2016
Concatenating Struct Value With Char In C

After googling everything I could, the best I could do with this is get the following warning with -Wall:

Code:
gcc -Wall help.c -o help
help.c: In function ‘findpid':
help.c:29:25: warning: passing argument 2 of ‘strncat' from incompatible pointer type
           strncat( pro, &str, 60);
                         ^
In file included from help.c:3:0:
/usr/include/string.h:140:14: note: expected ‘const char * __restrict__' but argument is of type ‘char (*)[60]'
 extern char *strncat (char *__restrict __dest, const char *__restrict __src,
              ^

Here's what I have for my code:

Code:
#include <stdio.h>
#include <dirent.h>
#include <string.h>

void findpid(char *filename){

    char pro[60] = "/proc/";
    char fd[5] = "/fd/";
    char pid[7];
    char str[60];
    char dir[60];
    FILE *file;
    file = fopen(filename,"r");
    while(fgets(pid, sizeof(pid), file)!=NULL)
    strtok(pid, "\n"); // Strip newline.
    strncat( pro, pid, 10 );
    strncat( pro, fd, 10 );
    strncat( dir, pro, 10);
    fclose(file);

    DIR *dp;
    struct dirent *ep;

    dp = opendir (pro);
    if (dp != NULL)
      {
        while ((ep = readdir (dp)) != NULL)
          memcpy(str, ep->d_name, 60);
          strncat( pro, &str, 60);
          printf("%s\n", pro);
        (void) closedir (dp);
      }
    else
      perror ("Couldn't open the directory");

}

int main(void)
{
    if (fopen("/var/run/apache2/apache2.pid", "r") != NULL) {
        char filename[] = "/var/run/apache2/apache2.pid";
        findpid(filename);
    }

    else if (fopen("/var/run/httpd/httpd.pid", "r") != NULL) {
        char filename[] = "/var/run/httpd/httpd.pid";
        findpid(filename);
    }
    else if (fopen("/var/run/nginx.pid", "r") != NULL) {
        char filename[] = "/var/run/nginx.pid";
        findpid(filename);
    }
    else {
        printf("Nothing found.\n");
    }

    return (0);
}

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.
# 2  
Old 05-29-2016
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:
Code:
          strncat( pro, &str, 60);

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:
  1. 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
  2. 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:
Code:
 1   char pro[60] = "/proc/";
 2   char fd[5] = "/fd/";
 3   char pid[7];
 4   char str[60];
 5   char dir[60];
 6   FILE *file;
 7   file = fopen(filename,"r");
 8   while(fgets(pid, sizeof(pid), file)!=NULL)
 9   strtok(pid, "\n"); // Strip newline.
10   strncat( pro, pid, 10 );
11   strncat( pro, fd, 10 );
12   strncat( dir, pro, 10);

Notes:
  1. 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.
  2. 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?)
  3. 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.
  4. 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.
  5. 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:
Code:
        while ((ep = readdir (dp)) != NULL)
          memcpy(str, ep->d_name, 60);
          strncat( pro, &str, 60);
          printf("%s\n", pro);
        (void) closed (dp);

which is more clearly written as:
Code:
        while ((ep = readdir (dp)) != NULL) {
          memcpy(str, ep->d_name, 60);
        }
          strncat( pro, &str, 60);
          printf("%s\n", pro);
        (void) closed (dp);

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:
Code:
1        while ((ep = reader (dp)) != NULL) {
2          memcpy(str, ep->d_name, 60);
3          strncat( pro, str, 60);
4          printf("%s\n", pro);
5        }
6        (void) closed (dp);

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:
Code:
        while ((ep = readdir (dp)) != NULL)
          printf("%s%s\n", pro, ep->d_name);
        (void) closedir (dp);

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-30-2016
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:

Code:
         while ((ep = readdir (dp)) != NULL) {
          strncat( pro, ep->d_name, 60);
          printf("%s\n", pro);
        }

        (void) closedir (dp);

Now I have this problem:
Code:
$ /proc/21480/fd/.
/proc/21480/fd/...
/proc/21480/fd/...0
/proc/21480/fd/...01
/proc/21480/fd/...012
/proc/21480/fd/...0123
/proc/21480/fd/...01234
/proc/21480/fd/...012345
/proc/21480/fd/...0123456
/proc/21480/fd/...01234567
/proc/21480/fd/...012345678
/proc/21480/fd/...0123456789
/proc/21480/fd/...012345678910
/proc/21480/fd/...01234567891011
/proc/21480/fd/...0123456789101112
/proc/21480/fd/...012345678910111213
/proc/21480/fd/...01234567891011121314
/proc/21480/fd/...0123456789101112131415
/proc/21480/fd/...012345678910111213141516

The reason I don't just use:
Code:
printf("%s%s\n", pro, ep->d_name);

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.
# 4  
Old 05-30-2016
Maybe something more like:
Code:
        while ((ep = readdir (dp)) != NULL) {
          snprintf( dir, sizeof(dir), "%s%s", pro, ep->d_name );
          printf("%s\n", dir);
        }

        (void) close (dp);

or (if you aren't interested in dot and dot-dot):
Code:
        while ((ep = readdir (dp)) != NULL) {
          if( strcmp( ".", ep->d_name ) && strcmp( "..", ep->d_name ) ) {
            snprintf( dir, sizeof(dir), "%s%s", pro, ep->d_name);
            printf("%s\n", dir);
          }
        }

        (void) close (dp);

would come closer to what you're trying to do.

Last edited by Don Cragun; 05-30-2016 at 05:51 PM.. Reason: Fix typos: s/closed/close/
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-30-2016
Yep, I was not interested in "." or "..", but was just trying to focus on one problem at a time. That worked!

Code:
/proc/21480/fd/0
/proc/21480/fd/1
/proc/21480/fd/2
/proc/21480/fd/3
/proc/21480/fd/4
/proc/21480/fd/5
/proc/21480/fd/6
/proc/21480/fd/7
/proc/21480/fd/8
/proc/21480/fd/9
/proc/21480/fd/10
/proc/21480/fd/11
/proc/21480/fd/12
/proc/21480/fd/13
/proc/21480/fd/14
/proc/21480/fd/15
/proc/21480/fd/16

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!
# 6  
Old 05-30-2016
I'm glad my comments have helped.

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:
Code:
        while((ep = readdir(dp)) != NULL) {
          if(strcmp(".", ep->d_name) && strcmp("..", ep->d_name)) {
            if(snprintf(dir, sizeof(dir), "%s%s", pro, ep->d_name) >=
                sizeof(dir)) {
              fprintf(stderr, "Processing for %s%s skipped (buffer overflow)\n",
                  pro, ep->d_name);
              continue;
            } 
            printf("%s\n", dir);
          }
        }
        (void)close(dp);

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Invalid conversion from char* to char

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)
Discussion started by: Plum
1 Replies

2. Programming

Storing C++-struct in file - problem when adding new item in struct

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)
Discussion started by: Powerponken
2 Replies

3. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

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)
Discussion started by: GSO
8 Replies

4. Programming

help with struct command in C

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)
Discussion started by: omega666
1 Replies

5. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

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)
Discussion started by: unbelievable21
1 Replies

6. Programming

concat const char * with char *

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)
Discussion started by: nicos
4 Replies

7. Programming

Adding a single char to a char pointer.

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)
Discussion started by: pallak7
5 Replies

8. Shell Programming and Scripting

How to replace any char with newline char.

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)
Discussion started by: mightysam
5 Replies

9. Programming

struct tm problem

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)
Discussion started by: bankpro
4 Replies

10. Programming

save a struct

hi all , can i save a structure in c in a file? how ? help me , thx. :) (2 Replies)
Discussion started by: kall_ANSI
2 Replies
Login or Register to Ask a Question