void change_directory(char *root) {
	ftp_connect();
	if (!FtpChdir(root, conn)) {
		fprintf(stderr, "Change directory failed\n%s", FtpLastResponse(conn));
		exit(EX_REMCMD);
	}
}
Exemple #2
0
void site_cmd(char *cmd)
{
    ftp_connect();
    if (!FtpSite( cmd, conn ))
    {
	fprintf(stderr,"SITE command failed\n%s", FtpLastResponse(conn));
	exit(EX_REMCMD);
    }
}
Exemple #3
0
void ftp_connect(void)
{
    if (conn)
        return;
    if (host == NULL)
    {
	fprintf(stderr,"Host name not specified\n");
	usage();
	exit(EX_SYNTAX);
    }
    if (!logged_in)
    {
    	if (user == NULL)
    	{
	    user = "******";
	    if (pass == NULL)
	    {
	    	char *u,h[64];
	    	u = getenv("USER");
	    	if (gethostname(h,64) < 0)
	    	{
		    perror("gethostname");
		    exit(EX_NETDB);
	    	}
	    	if ((u != NULL) && (h != NULL))
	    	{
		    static char xxx[256];
		    sprintf(xxx,"%s@%s",u,h);
		    pass = xxx;
	    	}
	    }
    	}
	else if (pass == NULL)
#if defined(_WIN32) || defined(VMS)
	    exit(EX_LOGIN);
#else
	    if ((pass = getpass("Password: "******"Unable to connect to node %s\n",host);
	    exit(EX_CONNECT);
    	}
    	if (!FtpLogin(user,pass,conn))
    	{
	    fprintf(stderr,"Login failure\n%s",FtpLastResponse(conn));
	    exit(EX_LOGIN);
    	}
	logged_in++;
    }
}
Exemple #4
0
void process_file(char *fnm)
{
    int sts=0;
    fsz_t fsz;
    struct REMFILE *filelist = NULL;
    struct REMFILE rem;

    ftp_connect();
    FtpClearCallback(conn);
    if ((action == FTP_SEND) || (action == FTP_GET) || (action == FTP_RM))
    {
	if (action == FTP_SEND)
	{
	    struct stat info;
	    if (stat(fnm,&info) == -1)
	    {
	    	perror(fnm);
		return;
	    }
	    if (S_ISDIR(info.st_mode))
	    {
		if (!FtpMkdir(fnm, conn))
		    fprintf(stderr,"mkdir %s failed\n%s",fnm,FtpLastResponse(conn));
		else
		    if (ftplib_debug)
			fprintf(stderr,"Directory %s created\n",fnm);
		return;
	    }
            fsz = info.st_size;
	}
        else
        {
	    if (!wildcard)
	    {
		struct REMFILE *f;
		f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
		memset(f,0,sizeof(struct REMFILE));
		f->next = filelist;
		filelist = f;
		f->fnm = strdup(fnm);
	    } else {
		netbuf *dir;
		char *buf;
		if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir))
		{
		    fprintf(stderr,"error requesting directory of %s\n%s\n",
			    fnm, FtpLastResponse(conn));
		    return;
		}
		buf = malloc(DIRBUF_SIZE);
		while (FtpRead(buf, DIRBUF_SIZE, dir) > 0)
		{
		    struct REMFILE *f;
		    char *p;
		    f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
		    memset(f,0,sizeof(struct REMFILE));
		    f->next = filelist;
		    p = strchr(buf,'\n');
		    if (p)
			*p = '\0';
		    f->fnm = strdup(buf);
		    filelist = f;
		}
		free(buf);
		FtpClose(dir);
	    }
        }
    }
    switch (action)
    {
      case FTP_DIR :
	sts = FtpDir(NULL, fnm, conn);
	break;
      case FTP_LIST :
	sts = FtpNlst(NULL, fnm, conn);
	break;
      case FTP_SEND :
	rem.next = NULL;
	rem.fnm = fnm;
	rem.fsz = fsz;
	fsz /= 100;
	if (fsz > 100000)
	    fsz = 100000;
        if (ftplib_debug && fsz)
        {
	    FtpCallbackOptions opt;
	    opt.cbFunc = log_progress;
	    opt.cbArg = &rem;
	    opt.idleTime = 1000;
	    opt.bytesXferred = fsz;
	    FtpSetCallback(&opt,conn);
        }
	sts = FtpPut(fnm,strippath ? basename(fnm) : fnm,mode,conn);
	if (ftplib_debug && sts)
	    printf("%s sent\n",fnm);
	break;
      case FTP_GET :
	while (filelist)
	{
	    struct REMFILE *f = filelist;
	    filelist = f->next;
#if defined(__UINT64_MAX)
	    if (!FtpSizeLong(f->fnm, &fsz, mode, conn))
#else
	    if (!FtpSize(f->fnm, &fsz, mode, conn))
#endif
		fsz = 0;
	    f->fsz = fsz;
	    fsz /= 100;
	    if (fsz > 100000)
		fsz = 100000;
	    if ( fsz == 0 )
		fsz = 32768;
	    if (ftplib_debug)
	    {
		FtpCallbackOptions opt;
		opt.cbFunc = log_progress;
		opt.cbArg = f;
		opt.idleTime = 1000;
		opt.bytesXferred = fsz;
		FtpSetCallback(&opt,conn);
	    }
	    sts = FtpGet(f->fnm,f->fnm,mode,conn);
	    if (ftplib_debug && sts)
		printf("%s retrieved\n",f->fnm);
	    free(f->fnm);
	    free(f);
	}
	break;
      case FTP_RM :
	while (filelist)
	{
	    struct REMFILE *f = filelist;
	    filelist = f->next;
	    sts = FtpDelete(f->fnm,conn);
	    if (ftplib_debug && sts)
		printf("%s deleted\n", f->fnm);
	    free(f->fnm);
	    free(f);
	}
	break;
    }
    if (!sts)
	printf("ftp error\n%s\n",FtpLastResponse(conn));
    return;
}
Exemple #5
0
void process_file(char *fnm)
{
    int i;
    int fsz;

    ftp_connect();
    FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);
    if ((action == FTP_SEND) || (action == FTP_GET))
    {
	if (action == FTP_SEND)
	{
	    struct stat info;
	    if (stat(fnm,&info) == -1)
	    {
	    	perror(fnm);
		return;
	    }
	    if (S_ISDIR(info.st_mode))
	    {
		if (!ftpMkdir(fnm))
		    fprintf(stderr,"mkdir %s failed\n%s",fnm,FtpLastResponse(conn));
		else
		    if (ftplib_debug)
			fprintf(stderr,"Directory %s created\n",fnm);
		return;
	    }
            fsz = info.st_size;
	}
        else
        {
            if (!FtpSize(fnm, &fsz, mode, conn))
                fsz = 0;
        }
        if (ftplib_debug && fsz)
        {
            FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
            FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
            FtpOptions(FTPLIB_CALLBACKARG, (long) &fsz, conn);
            FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz/10, conn);
        }
    }
    switch (action)
    {
      case FTP_DIR :
	i = FtpDir(NULL, fnm, conn);
	break;
      case FTP_LIST :
	i = FtpNlst(NULL, fnm, conn);
	break;
      case FTP_SEND :
	i = FtpPut(fnm,fnm,mode,conn);
	if (ftplib_debug && i)
	    printf("%s sent\n",fnm);
	break;
      case FTP_GET :
	i = FtpGet(fnm,fnm,mode,conn);
	if (ftplib_debug && i)
	    printf("%s retrieved\n",fnm);
	break;
      case FTP_RM :
	i = FtpDelete(fnm,conn);
	if (ftplib_debug && i)
	    printf("%s deleted\n", fnm);
	break;
    }
    if (!i)
	printf("ftp error\n%s\n",FtpLastResponse(conn));
}
void process_file(char *fnm) {
	int sts = 0;
	int fsz;
	struct REMFILE *filelist = NULL;
	struct REMFILE rem;

	ftp_connect();
	FtpOptions(FTPLIB_CALLBACK, (long) NULL, conn);
	if ((action == FTP_SEND) || (action == FTP_GET)) {
		if (action == FTP_SEND) {
			struct stat info;
			if (stat(fnm, &info) == -1) {
				perror(fnm);
				return;
			}
			if (S_ISDIR(info.st_mode)) {
				if (!ftpMkdir(fnm))
					fprintf(stderr, "mkdir %s failed\n%s", fnm,
							FtpLastResponse(conn));
				else if (ftplib_debug)
					fprintf(stderr, "Directory %s created\n", fnm);
				return;
			}
			fsz = info.st_size;
		} else {
			if (!wildcard) {
				struct REMFILE *f;
				f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
				memset(f, 0, sizeof(struct REMFILE));
				f->next = filelist;
				filelist = f;
				f->fnm = strdup(fnm);
			} else {
				netbuf * dir;
				char *buf;
				if (!FtpAccess(fnm, FTPLIB_DIR, FTPLIB_ASCII, conn, &dir)) {
					fprintf(stderr, "error requesting directory of %s\n%s\n",
							fnm, FtpLastResponse(conn));
					return;
				}
				buf = malloc(DIRBUF_SIZE);
				while (FtpRead(buf, DIRBUF_SIZE, dir) > 0) {
					struct REMFILE *f;
					char *p;
					f = (struct REMFILE *) malloc(sizeof(struct REMFILE));
					memset(f, 0, sizeof(struct REMFILE));
					f->next = filelist;
					p = strchr(buf, '\n');
					if (p)
						*p = '\0';
					f->fnm = strdup(buf);
					filelist = f;
				}
				free(buf);
				FtpClose(dir);
			}
		}
	}
	switch (action) {
	case FTP_DIR:
		sts = FtpDir(NULL, fnm, conn);
		break;
	case FTP_LIST:
		sts = FtpNlst(NULL, fnm, conn);
		break;
	case FTP_SEND:
		rem.next = NULL;
		rem.fnm = fnm;
		rem.fsz = fsz;
		fsz /= 10;
		if (fsz > 100000)
			fsz = 100000;
		if (ftplib_debug && fsz) {
			FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
			FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
			FtpOptions(FTPLIB_CALLBACKARG, (long) &rem, conn);
			FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);
		}
		sts = FtpPut(fnm, fnm, mode, conn);
		if (ftplib_debug && sts)
			printf("%s sent\n", fnm);
		break;
	case FTP_GET:
		while (filelist) {
			struct REMFILE *f = filelist;
			filelist = f->next;
			if (!FtpSize(f->fnm, &fsz, mode, conn))
				fsz = 0;
			f->fsz = fsz;
			fsz /= 10;
			if (fsz > 100000)
				fsz = 100000;
			if (ftplib_debug && fsz) {
				FtpOptions(FTPLIB_CALLBACK, (long) log_progress, conn);
				FtpOptions(FTPLIB_IDLETIME, (long) 1000, conn);
				FtpOptions(FTPLIB_CALLBACKARG, (long) f, conn);
				FtpOptions(FTPLIB_CALLBACKBYTES, (long) fsz, conn);
			}
			sts = FtpGet(f->fnm, f->fnm, mode, conn);
			if (ftplib_debug && sts)
				printf("%s retrieved\n", f->fnm);
			free(f->fnm);
			free(f);
		}
		break;
	case FTP_RM:
		while (filelist) {
			struct REMFILE *f = filelist;
			filelist = f->next;
			sts = FtpDelete(f->fnm, conn);
			if (ftplib_debug && sts)
				printf("%s deleted\n", f->fnm);
			free(f->fnm);
			free(f);
		}
		break;
	}
	if (!sts)
		printf("ftp error\n%s\n", FtpLastResponse(conn));
	return;
}