What is the problem with the code?

Y

yezi

Hi: I am coding to output the result to the tmp file in order to send
to the client. The following is the code I write to output the system
call to a tmp file "tmp.txt". However, when running it, it generate the
message like " Segmentation fault (core dumped)"

Really confused by that . Any comments is welcomed.

tmpfile = fopen("tmp.txt","a+");
if(tmpfile == NULL) {
perror("Error: can't open file.\n");
exit(1);;
}
startTm = time(NULL);
ptr = localtime(&startTm);
//Print the time of request and Time on the server side
fprintf(tmpfile,"The client IP is %s, The systime
is,",inet_ntoa(their_addr.sin_addr));
fprintf(tmpfile,asctime(ptr)); //show time problem ???
fprintf(tmpfile,"\n The command is %s, The status is
%s\n",buf,status);

sep[20]="ls";
//process the buffer content
strcat(sep,"> tmp.txt");
system(sep);
 
W

Walter Roberson

Hi: I am coding to output the result to the tmp file in order to send
to the client. The following is the code I write to output the system
call to a tmp file "tmp.txt". However, when running it, it generate the
message like " Segmentation fault (core dumped)"
fprintf(tmpfile,"The client IP is %s, The systime
is,",inet_ntoa(their_addr.sin_addr));
fprintf(tmpfile,asctime(ptr)); //show time problem ???

The second argument of fprintf is expected to be a format string.
If the result of asctime() happened to include a % then the fprintf
could try to convert a non-existant argument.

You could try,

fprintf(tmpfile,"The client IP is %s, The systime is, %s",
inet_ntoa(their_addr.sin_addr), asctime(ptr) );


As a general reminder: any time you are expecting a pointer to a
system structure or newly allocated structure to be returned, then
you should be checking for a NULL pointer return.
 
P

Peter Nilsson

Walter said:
The second argument of fprintf is expected to be a format string.
If the result of asctime() happened to include a % then the fprintf
could try to convert a non-existant argument.

You could try,

fprintf(tmpfile,"The client IP is %s, The systime is, %s",
inet_ntoa(their_addr.sin_addr), asctime(ptr) );


As a general reminder: any time you are expecting a pointer to a
system structure or newly allocated structure to be returned, then
you should be checking for a NULL pointer return.

Your points are valid in general, but note that asctime() cannot
return a null pointer, nor can it return a pointer to a string
containing a % character.

[inet_ntoa() is not a standard C function, so it is off-topic in clc.]
 
W

Walter Roberson

Your points are valid in general, but note that asctime() cannot
return a null pointer, nor can it return a pointer to a string
containing a % character.

Is it impossible for a locale to define a timezone name or day or
month name that contains a % character ?
 
K

Keith Thompson

Is it impossible for a locale to define a timezone name or day or
month name that contains a % character ?

asctime() ignores the locale; it returns a string of the form

Sun Sep 16 01:03:52 1973\n\0
 
C

Chris Dollin

yezi said:
sep[20]="ls";
//process the buffer content
strcat(sep,"> tmp.txt");

`sep` wasn't declared, but I'm having difficulty imagining a
declaration for it that allows its 20th element to be a char*
and for `sep` itself to be a char*.

Conclusion: something is broken.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top