views:

28

answers:

1

How can we get and handle the sftp return codes in a C program if sftp fails to put the file in a server?

We need to transfer the files again if sftp fails to put it in the sftp server.

A: 

Assuming you want to use the command line sftp from within a C program, this program illustrates one way to do it (it's rather ugly but shows the idea):

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    int ret;
    ret = system(argv[1]);
    if (ret == -1) {
        printf("Error in system!");
        /* *** */
        exit(-1);
    }
    ret = (signed char)WEXITSTATUS(ret);
    printf("%d\n", ret);
    return 0;
}

Use man system, and then man 2 wait for info on WEXITSTATUS macro.

The value of ret is the return code of sftp and can be used to determine failure in sftp and decide what to do next.

Michał Trybus