コード例 #1
0
ファイル: test_ls.c プロジェクト: zhanjl/Myftpserver
int main()
{
    DIR *dir = opendir(".");    //打开当前目录文件
    if (dir == NULL)
        ERR_EXIT("opendir");

    struct dirent *dr;
    while ((dr = readdir(dir))) {   //读取目录文件中的每一项
        const char *filename = dr->d_name;
        if (filename[0] == '.')      //忽略.和..这两项
            continue;

        char buf[1024] = {0};
        struct stat sbuf;

        if (lstat(filename, &sbuf) == -1) {
            ERR_EXIT("lstat");
        }

        strcpy(buf, statbuf_get_perms(&sbuf));
        strcat(buf, " ");
        strcat(buf, statbuf_get_user_info(&sbuf));
        strcat(buf, " ");
        strcat(buf, statbuf_get_date(&sbuf));
        strcat(buf, " ");
        strcat(buf, statbuf_get_size(&sbuf));
        strcat(buf, " ");
        strcat(buf, statbuf_get_filename(&sbuf, filename));

        printf("%s\n", buf);
    }
    closedir(dir);
    return 0;
}
コード例 #2
0
ファイル: ftpproto.c プロジェクト: dansutj/miniftpd
int list_common(session_t* sess, int detail)
{
	DIR* dir = opendir(".");
	if(dir == NULL)
		return 0;

	struct dirent* dt;
	struct stat sbuf;
	while((dt = readdir(dir)) != NULL)
	{
		if(lstat(dt->d_name, &sbuf) < 0)
			continue;

		if(dt->d_name[0] == '.')
			continue;	
		
		char buf[1024] = {0};

                if(detail)
                {
                        const char *perms = statbuf_get_perms(&sbuf);

                        int off = 0;
                        off += sprintf(buf, "%s ", perms);
                        off += sprintf(buf + off, " %3d %-8d %8d ",
                        sbuf.st_nlink, sbuf.st_uid, sbuf.st_gid);
                        off += sprintf(buf + off, "%8lu ", (unsigned long)sbuf.st_size);

                        const char *datebuf = statbuf_get_date(&sbuf);
                        off += sprintf(buf + off, "%s ", datebuf);
			
			if(S_ISLNK(sbuf.st_mode))
			{
				char tmp[1024] = {0};
				readlink(dt->d_name, tmp, sizeof(tmp));
				sprintf(buf + off, "%s -> %s\r\n", dt->d_name, tmp);
			}
			else 
                		sprintf(buf + off, "%s\r\n", dt->d_name);
                }
                else
                        sprintf(buf, "%s\r\n", dt->d_name);

                writen(sess->data_fd, buf, strlen(buf));
        }

        closedir(dir);
        return 1;
}