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;
|
static char *destination;
|
||||||
|
|
||||||
#define DATALEN 65536
|
#define DATALEN 65536
|
||||||
|
|
||||||
static void do_sftp(ssh_session session) {
|
static void do_sftp(ssh_session session) {
|
||||||
sftp_session sftp = sftp_new(session);
|
sftp_session sftp = sftp_new(session);
|
||||||
sftp_dir dir;
|
sftp_dir dir;
|
||||||
@ -53,6 +54,7 @@ static void do_sftp(ssh_session session){
|
|||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sftp_init(sftp)) {
|
if (sftp_init(sftp)) {
|
||||||
fprintf(stderr, "error initialising sftp: %s\n",
|
fprintf(stderr, "error initialising sftp: %s\n",
|
||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
@ -69,8 +71,10 @@ static void do_sftp(ssh_session session){
|
|||||||
|
|
||||||
/* test symlink and readlink */
|
/* test symlink and readlink */
|
||||||
if (sftp_symlink(sftp, "/tmp/this_is_the_link",
|
if (sftp_symlink(sftp, "/tmp/this_is_the_link",
|
||||||
"/tmp/sftp_symlink_test") < 0) {
|
"/tmp/sftp_symlink_test") < 0)
|
||||||
fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session));
|
{
|
||||||
|
fprintf(stderr, "Could not create link (%s)\n",
|
||||||
|
ssh_get_error(session));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,6 +157,7 @@ static void do_sftp(ssh_session session){
|
|||||||
fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session));
|
fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reading the whole directory, file by file */
|
/* reading the whole directory, file by file */
|
||||||
while ((file = sftp_readdir(sftp, dir))) {
|
while ((file = sftp_readdir(sftp, dir))) {
|
||||||
fprintf(stderr, "%30s(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes\n",
|
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);
|
(long long unsigned int) file->size);
|
||||||
sftp_attributes_free(file);
|
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)) {
|
if (!sftp_dir_eof(dir)) {
|
||||||
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sftp_closedir(dir)) {
|
if (sftp_closedir(dir)) {
|
||||||
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
fprintf(stderr, "Error: %s\n", ssh_get_error(session));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
/* this will open a file and copy it into your /home directory */
|
/* 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);
|
fichier = sftp_open(sftp, "/usr/bin/ssh", O_RDONLY, 0);
|
||||||
if (!fichier) {
|
if (!fichier) {
|
||||||
@ -183,6 +192,7 @@ static void do_sftp(ssh_session session){
|
|||||||
ssh_get_error(session));
|
ssh_get_error(session));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* open a file for writing... */
|
/* open a file for writing... */
|
||||||
to = sftp_open(sftp, "ssh-copy", O_WRONLY | O_CREAT, 0700);
|
to = sftp_open(sftp, "ssh-copy", O_WRONLY | O_CREAT, 0700);
|
||||||
if (!to) {
|
if (!to) {
|
||||||
@ -191,6 +201,7 @@ static void do_sftp(ssh_session session){
|
|||||||
sftp_close(fichier);
|
sftp_close(fichier);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((len = sftp_read(fichier, data, 4096)) > 0) {
|
while ((len = sftp_read(fichier, data, 4096)) > 0) {
|
||||||
if (sftp_write(to, data, len) != len) {
|
if (sftp_write(to, data, len) != len) {
|
||||||
fprintf(stderr, "Error writing %d bytes: %s\n",
|
fprintf(stderr, "Error writing %d bytes: %s\n",
|
||||||
@ -200,13 +211,17 @@ static void do_sftp(ssh_session session){
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("finished\n");
|
printf("finished\n");
|
||||||
if(len<0)
|
if (len < 0) {
|
||||||
fprintf(stderr, "Error reading file: %s\n", ssh_get_error(session));
|
fprintf(stderr, "Error reading file: %s\n", ssh_get_error(session));
|
||||||
|
}
|
||||||
|
|
||||||
sftp_close(fichier);
|
sftp_close(fichier);
|
||||||
sftp_close(to);
|
sftp_close(to);
|
||||||
printf("fichiers ferm\n");
|
printf("fichiers ferm\n");
|
||||||
to = sftp_open(sftp, "/tmp/grosfichier", O_WRONLY|O_CREAT, 0644);
|
to = sftp_open(sftp, "/tmp/grosfichier", O_WRONLY|O_CREAT, 0644);
|
||||||
|
|
||||||
for (i = 0; i < 1000; ++i) {
|
for (i = 0; i < 1000; ++i) {
|
||||||
len = sftp_write(to, data, DATALEN);
|
len = sftp_write(to, data, DATALEN);
|
||||||
printf("wrote %d bytes\n", len);
|
printf("wrote %d bytes\n", len);
|
||||||
@ -234,6 +249,7 @@ static void usage(const char *argv0){
|
|||||||
|
|
||||||
static int opts(int argc, char **argv) {
|
static int opts(int argc, char **argv) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
while ((i = getopt(argc, argv, "v")) != -1) {
|
while ((i = getopt(argc, argv, "v")) != -1) {
|
||||||
switch(i) {
|
switch(i) {
|
||||||
case 'v':
|
case 'v':
|
||||||
@ -256,17 +272,20 @@ static int opts(int argc, char **argv){
|
|||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
ssh_session session;
|
ssh_session session;
|
||||||
if(opts(argc,argv)<0)
|
|
||||||
|
if (opts(argc, argv) < 0) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
session = connect_ssh(destination, NULL, verbosity);
|
session = connect_ssh(destination, NULL, verbosity);
|
||||||
if(session == NULL)
|
if (session == NULL) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
do_sftp(session);
|
do_sftp(session);
|
||||||
ssh_disconnect(session);
|
ssh_disconnect(session);
|
||||||
ssh_free(session);
|
ssh_free(session);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user