mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-07-19 07:03:12 +03:00
examples: Fix code style in samplesftp.c
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Andreas Schneider
parent
7390db6bbb
commit
f246e31ca0
@ -33,6 +33,7 @@ static int verbosity;
|
||||
static char *destination;
|
||||
|
||||
#define DATALEN 65536
|
||||
|
||||
static void do_sftp(ssh_session session) {
|
||||
sftp_session sftp = sftp_new(session);
|
||||
sftp_dir dir;
|
||||
@ -53,6 +54,7 @@ static void do_sftp(ssh_session session){
|
||||
ssh_get_error(session));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sftp_init(sftp)) {
|
||||
fprintf(stderr, "error initialising sftp: %s\n",
|
||||
ssh_get_error(session));
|
||||
@ -69,8 +71,10 @@ static void do_sftp(ssh_session session){
|
||||
|
||||
/* test symlink and readlink */
|
||||
if (sftp_symlink(sftp, "/tmp/this_is_the_link",
|
||||
"/tmp/sftp_symlink_test") < 0) {
|
||||
fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session));
|
||||
"/tmp/sftp_symlink_test") < 0)
|
||||
{
|
||||
fprintf(stderr, "Could not create link (%s)\n",
|
||||
ssh_get_error(session));
|
||||
goto end;
|
||||
}
|
||||
|
||||
@ -153,6 +157,7 @@ static void do_sftp(ssh_session session){
|
||||
fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session));
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* reading the whole directory, file by file */
|
||||
while ((file = sftp_readdir(sftp, dir))) {
|
||||
fprintf(stderr, "%30s(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes\n",
|
||||
@ -165,17 +170,21 @@ static void do_sftp(ssh_session session){
|
||||
(long long unsigned int) file->size);
|
||||
sftp_attributes_free(file);
|
||||
}
|
||||
/* when file=NULL, an error has occured OR the directory listing is end of file */
|
||||
|
||||
/* when file = NULL, an error has occured OR the directory listing is end of
|
||||
* file */
|
||||
if (!sftp_dir_eof(dir)) {
|
||||
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (sftp_closedir(dir)) {
|
||||
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
||||
goto end;
|
||||
}
|
||||
/* this will open a file and copy it into your /home directory */
|
||||
/* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */
|
||||
/* the small buffer size was intended to stress the library. of course, you
|
||||
* can use a buffer till 20kbytes without problem */
|
||||
|
||||
fichier = sftp_open(sftp, "/usr/bin/ssh", O_RDONLY, 0);
|
||||
if (!fichier) {
|
||||
@ -183,6 +192,7 @@ static void do_sftp(ssh_session session){
|
||||
ssh_get_error(session));
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* open a file for writing... */
|
||||
to = sftp_open(sftp, "ssh-copy", O_WRONLY | O_CREAT, 0700);
|
||||
if (!to) {
|
||||
@ -191,6 +201,7 @@ static void do_sftp(ssh_session session){
|
||||
sftp_close(fichier);
|
||||
goto end;
|
||||
}
|
||||
|
||||
while ((len = sftp_read(fichier, data, 4096)) > 0) {
|
||||
if (sftp_write(to, data, len) != len) {
|
||||
fprintf(stderr, "Error writing %d bytes: %s\n",
|
||||
@ -200,13 +211,17 @@ static void do_sftp(ssh_session session){
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
printf("finished\n");
|
||||
if(len<0)
|
||||
if (len < 0) {
|
||||
fprintf(stderr, "Error reading file: %s\n", ssh_get_error(session));
|
||||
}
|
||||
|
||||
sftp_close(fichier);
|
||||
sftp_close(to);
|
||||
printf("fichiers ferm\n");
|
||||
to = sftp_open(sftp, "/tmp/grosfichier", O_WRONLY|O_CREAT, 0644);
|
||||
|
||||
for (i = 0; i < 1000; ++i) {
|
||||
len = sftp_write(to, data, DATALEN);
|
||||
printf("wrote %d bytes\n", len);
|
||||
@ -234,6 +249,7 @@ static void usage(const char *argv0){
|
||||
|
||||
static int opts(int argc, char **argv) {
|
||||
int i;
|
||||
|
||||
while ((i = getopt(argc, argv, "v")) != -1) {
|
||||
switch(i) {
|
||||
case 'v':
|
||||
@ -256,17 +272,20 @@ static int opts(int argc, char **argv){
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
ssh_session session;
|
||||
if(opts(argc,argv)<0)
|
||||
|
||||
if (opts(argc, argv) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
session = connect_ssh(destination, NULL, verbosity);
|
||||
if(session == NULL)
|
||||
if (session == NULL) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
do_sftp(session);
|
||||
ssh_disconnect(session);
|
||||
ssh_free(session);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user