Hi,
I'm currently working on an FTP client written in C, and it's working pretty good. I was successful in writing a function that connects to an FTP server and logs in with a username or password, but I'm having a problem with returning errors. I have setup a struct FTPError {};
with 3 fields:
int
An error codeint
An FTP error domain (specific to my function)char[256]
A user readable description
The caller of the function passes a structure by reference to the function and I fill it with the data. But I'm struggling with filling the user readable string (char[256]
). I fill the string with strcpy
, but when I call it, my program signals a SIGABRT. I present you a simplified piece of my code:
struct FTPError {
int status;
int domain;
char message[FTP_ERROR_MAX];
};
typedef int FTPConnection;
FTPConnection FTPConnect(const char *hostname, const char *username, const char *password, struct FTPError *errn) {
// Very much code I wrote that connects to the server,
// sends the USER command to the FTP server, sends the
// PASS command to the FTP server and succeeds in
// logging in
int socket = /* socket file descriptor */
// Success
if(success == 1) {
if(errn) {
// when I comment the line below, no signal is sent
strcpy(errn->message, "User successfully loged in");
errn->status = 230;
errn->domain = kServerReplyDomain;
}
}
// return the file descriptor
return sockfd;
}
I thank all the dedicated people here to help me again, ief2
PS: This what Xcode gives me in the error console:
Program loaded.
run
[Switching to process 2566]
Running…
SOCK: 3
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
kill
quit
0x00007fff824c03cc <+0000> mov $0x2000025,%eax
0x00007fff824c03d1 <+0005> mov %rcx,%r10
0x00007fff824c03d4 <+0008> syscall
0x00007fff824c03d6 <+0010> jae 0x7fff824c03dd <__kill+17> --> (points this line)
0x00007fff824c03d8 <+0012> jmpq 0x7fff82560a8c <cerror>
0x00007fff824c03dd <+0017> retq
3 __kill
2 __abort
1 __stack_chk_fail
0 main
PPS: I was asked to call the code that calls the function:
int main (int argc, const char * argv[]) {
struct FTPError reply;
FTPConnection socket;
socket = FTPConnect("ftp.belnet.be", "anonymous", "pwd", &reply);
printf("SOCK: %d\n", socket);
return 0;
}