LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I think that is problem with convertin char to const char* (https://www.linuxquestions.org/questions/programming-9/i-think-that-is-problem-with-convertin-char-to-const-char%2A-674044/)

shifter 10-03-2008 04:37 PM

I think that is problem with convertin char to const char*
 
How can I to convert char* to const char*. I tried with cast but system output error.

My code is:

Quote:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/inotify.h>

#define _GNU_SOURCE

void daemonize() {

pid_t pid;

pid = fork();
if (pid < 0) perror("Error in fork()");
if (pid > 0) exit(0);
setsid();

}

int main(int argc, char* argv[]) {

int fd, wd;
size_t lsize = 0;
FILE* conf_file;
char* line = NULL;
char* token;

if (argc == 1) {
printf("\nCommand syntax:\n");
printf("\n\tfsmond [ start | stop ]\n");
printf("\t\tstart: start fsmon daemon\n");
printf("\t\tstop: stop fsmon daemon\n");
}
else if (strcmp(argv[1], "start") == 0) {
daemonize();
fd = inotify_init();
if (fd < 0) perror("Error in inotify_init()");
conf_file = fopen("/etc/fsmon/fsmon.conf", "r");
fseek(conf_file, 0, SEEK_SET);
while (!feof(conf_file))
while (getline(&line, &lsize, conf_file) != -1)
if (strncmp(&line[0], "#", 1) != 0) {
token = strtok(line, "=");
if (strcmp(token, " ADD") == 0) {
token = strtok(NULL, "=");
printf("fd = %d, dir = %s", fd, token);
wd = inotify_add_watch(fd, (const char*)token, IN_CREATE | IN_MODIFY | IN_DELETE);
if (wd < 0) printf("wd = %d Error = %s\n", wd, strerror(errno));

}
}
}

return(0);

}
The output of program is:

Quote:

bash-3.1# gcc fsmond.c -o fsmond
bash-3.1# ./fsmond start
fd = 3, dir = "/tmp"
wd = -1 Error = No such file or directory
fd = 3, dir = "/var/www"
wd = -1 Error = No such file or directory
bash-3.1# gcc fsmond.c -ansi -Wall -pedantic -D_GNU_SOURCE
fsmond.c:8:1: warning: "_GNU_SOURCE" redefined
<command-line>: warning: this is the location of the previous definition
bash-3.1# gcc fsmond.c -o fsmond
bash-3.1# ./fsmond start
fd = 3, dir = "/tmp"
wd = -1 Error = No such file or directory
fd = 3, dir = "/var/www"
wd = -1 Error = No such file or directory
bash-3.1# gcc fsmond.c -ansi -Wall -pedantic -D_GNU_SOURCE
fsmond.c:8:1: warning: "_GNU_SOURCE" redefined
<command-line>: warning: this is the location of the previous definition
bash-3.1#
The inotify_add_watch prototipe is

int inotify_add_watch(int fd, const char *pathname, uint32_t mask);

If I write in inotify_add_watch function directly any directory string:

wd = inotify_add_watch(fd, "/tmp", IN_CREATE | IN_MODIFY | IN_DELETE);

then program run without problems.

I want program in C, then I cannot use c_str() C++ function.
How can I solve this problem?

Thanks,
Savio

keefaz 10-03-2008 05:37 PM

Maybe you have to delete the new line character "\n" from dir, just a thought

shifter 10-03-2008 05:42 PM

How can I split and truncate dir from "\n"?

keefaz 10-03-2008 05:49 PM

I don't know, you could replace the newline char with a null:

dir[strlen(dir) -1] = '\0';

ErV 10-03-2008 05:53 PM

Quote:

Originally Posted by shifter (Post 3299478)
How can I split and truncate dir from "\n"?

use strrchr or strchr and insert zero at the position of '\n'

dmail 10-03-2008 06:03 PM

I think keefaz is correct you do not add a new line to the printf statements yet there is one there in the output. The cast from char* to char const* is implicit, the warning about _GNU_SOURCE is due to adding it on the command line and also in the code, one of these should be removed. This is a much better request for help :)

edit: Oops I never refreshed the screen before posting

shifter 10-03-2008 06:06 PM

I typed:

Quote:

dir = malloc(strlen(token)-1);
strncpy(dir, token, strlen(token) - 1);
printf("fd = %d, dir = %s", fd, token);
wd = inotify_add_watch(fd, dir, IN_CREATE | IN_MODIFY | IN_DELETE);
if (wd < 0) printf("wd = %d Error = %s\n", wd, strerror(errno));
It seems works!

keefaz 10-03-2008 08:13 PM

Note however, that with you code, you use non-terminated with '\0' strings, that means weird bugs could happen...
strlen returns the number of chars not including the terminating \0, you have to malloc the number of chars you want + the terminating \0

and man strncpy:
Quote:

char *strncpy(char *dest, const char *src, size_t n);

If there is no null byte among the first n bytes of src, the string placed in dest will not be null terminated.
try:
dir = malloc(strlen(token));
strncpy(dir, token, strlen(token) - 1);

shifter 10-04-2008 06:00 AM

Quote:

try:
dir = malloc(strlen(token));
strncpy(dir, token, strlen(token) - 1);
Ok, thanks, it works


All times are GMT -5. The time now is 04:18 PM.