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
Dist::Metadata::Struct(3pm)				User Contributed Perl Documentation			       Dist::Metadata::Struct(3pm)

NAME
Dist::Metadata::Struct - Enable Dist::Metadata for a data structure VERSION
version 0.923 SYNOPSIS
my $dm = Dist::Metadata->new(struct => { files => { 'lib/Mod.pm' => 'package Mod; sub something { ... }', 'README' => 'this is a fake dist, useful for testing', } }); DESCRIPTION
This is a subclass of Dist::Metadata::Dist to enable mocking up a dist from perl data structures. This is mostly used for testing but might be useful if you already have an in-memory representation of a dist that you'd like to examine. It's probably not very useful on it's own though, and should be used from "new" in Dist::Metadata. METHODS
new $dist = Dist::Metadata::Struct->new(files => { 'lib/Mod.pm' => 'package Mod; sub something { ... }', }); Accepts a "files" parameter that should be a hash of "{ name => content, }". Content can be a string, a reference to a string, or an IO object. default_file_spec "Unix" is the default for consistency/simplicity but "file_spec" can be overridden in the constructor. file_content Returns the string content for the specified name. find_files Returns the keys of the "files" hash. AUTHOR
Randy Stauner <rwstauner@cpan.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Randy Stauner. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-06-19 Dist::Metadata::Struct(3pm)
All times are GMT -4. The time now is 03:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy