LCFTP


  • language
    C
  • pĂ©riode
    Juillet 2020
  • lien

LCFTP (Light Client FTP) is a client library for FTP, written in C.

LCFTP allows you to easily use FTP in your programs.

API

Struct

struct ftp_t {
  int cmd;
  int data;
  char *host;
  int port;
};

Structure representing the FTP connection.

Initialize a struct

FTP_INITIALIZER(host, port)

Create a ftp_t struct ready to login.

struct ftp_t ftp = FTP_INITIALIZER("ftp.host.com", 21);

Login to a FTP server

int ftp_login(struct ftp_t *ftp, const char *username, const char *password)
ftp_login(&ftp, "webmaster@domain.com", "password");

Delete file

// macro
ftp_delete(struct ftp_t *ftp, const char *path)
ftp_delete(&ftp, "cocolapin.txt");

Create directory

// macro
ftp_mkd(struct ftp_t *ftp, const char *path)
ftp_mkd(&ftp, "foobar");

Change working directory

// macro
ftp_cwd(struct ftp_t *ftp, const char *path)
ftp_cwd(&ftp, "foobar");

Remove directory

// macro
ftp_rmd(struct ftp_t *ftp, const char *path)
ftp_rmd(&ftp, "foobar");

Upload file

int ftp_upload_file(struct ftp_t *ftp, const char *path, FILE **fp)
FILE *f;
f = fopen("cocolapin.txt", "r");
ftp_upload_file(&ftp, "cocolapin.txt", &f);
fclose(f);

Download file

int ftp_download_file(struct ftp_t *ftp, const char *path, FILE **fp, void (*progress)(int a, int b))

Add a callback to track the download, or NULL.

void progress(int a, int b)
{
  int percentage = a * 100 / b;
  if (a == b) {
    printf("completed !\n");
  } else if (a == -1) {
    printf("error\n");
  } else {
    printf("%d/%d (%d%%)\n", a, b, percentage);
  }
}
FILE *f;
f = fopen("cocolapin.txt", "w");
ftp_download_file(&ftp, "cocolapin.txt", &f, progress);
fclose(f);

Size of a file

int ftp_filesize(struct ftp_t *ftp, const char *path)

Size of a directory

int int ftp_dir_size(struct ftp_t *ftp, const char *path)

File exists

int ftp_file_exists(struct ftp_t *ftp, const char *path)

Directory exists

int ftp_dir_exists(struct ftp_t *ftp, const char *path)

Quit

int ftp_quit(struct ftp_t *ftp)

Example

int main(int argc, char *argv[])
{
  struct ftp_t ftp = FTP_INITIALIZER("ftp.host.com", 21);
  ftp_login(&ftp, "webmaster@domain.com", "password");

  ftp_mkd(&ftp, "foobar");
  ftp_cwd(&ftp, "foobar");

  FILE *f;
  f = fopen("cocolapin.txt", "r");
  ftp_upload_file(&ftp, "cocolapin.txt", &f);
  fclose(f);

  ftp_delete(&ftp, "cocolapin.txt");
  ftp_rmd(&ftp, "foobar");

  ftp_quit(&ftp);
  return 0;
}

TODO

  • Allow all files in a directory to be downloaded at once
  • Add error detection (when upload/download)

License

GPL

LCFTP, a light client FTP

Copyright (C) 2020 Pierre-Loup Gosse

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.