Sponsored Content
Top Forums Programming Concatenating Struct Value With Char In C Post 302974356 by Azrael on Sunday 29th of May 2016 02:09:46 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STRCAT(3)						   BSD Library Functions Manual 						 STRCAT(3)

NAME
strcat, strncat -- concatenate strings LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <string.h> char * strcat(char *restrict s1, const char *restrict s2); char * strncat(char *restrict s1, const char *restrict s2, size_t n); DESCRIPTION
The strcat() and strncat() functions append a copy of the null-terminated string s2 to the end of the null-terminated string s1, then add a terminating ''. The string s1 must have sufficient space to hold the result. The strncat() function appends not more than n characters from s2, and then adds a terminating ''. The source and destination strings should not overlap, as the behavior is undefined. RETURN VALUES
The strcat() and strncat() functions return the pointer s1. SECURITY CONSIDERATIONS
The strcat() function is easily misused in a manner which enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack. (See the FSA.) Avoid using strcat(). Instead, use strncat() or strlcat() and ensure that no more characters are copied to the destination buffer than it can hold. Note that strncat() can also be problematic. It may be a security concern for a string to be truncated at all. Since the truncated string will not be as long as the original, it may refer to a completely different resource and usage of the truncated resource could result in very incorrect behavior. Example: void foo(const char *arbitrary_string) { char onstack[8] = ""; #if defined(BAD) /* * This first strcat is bad behavior. Do not use strcat! */ (void)strcat(onstack, arbitrary_string); /* BAD! */ #elif defined(BETTER) /* * The following two lines demonstrate better use of * strncat(). */ (void)strncat(onstack, arbitrary_string, sizeof(onstack) - strlen(onstack) - 1); #elif defined(BEST) /* * These lines are even more robust due to testing for * truncation. */ if (strlen(arbitrary_string) + 1 > sizeof(onstack) - strlen(onstack)) err(1, "onstack would be truncated"); (void)strncat(onstack, arbitrary_string, sizeof(onstack) - strlen(onstack) - 1); #endif } SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), strcpy(3), strlcat(3), strlcpy(3), wcscat(3) STANDARDS
The strcat() and strncat() functions conform to ISO/IEC 9899:1990 (``ISO C90''). BSD
December 1, 2009 BSD
All times are GMT -4. The time now is 10:24 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy