1

我正在尝试展示Ubuntuusing下的任何文件的详细信息C

例如,如果我正在寻找一个名为 的文件/文件夹12,我会得到:

a@ubuntu:~/Desktop$ ./exer4 . 12
drwxrwxr-x 3 1000 1000 4096 2012-06-19 10:08 
drwxrwxr-x 3 1000 1000 4096 2012-06-16 14:09 

但我想这样显示它:

a@ubuntu:~/Desktop$ find . -name 12 -exec ls \-lnd {} \; | sort
drwxrwxr-x 3 1000 1000 4096 Jun 16 14:09 
drwxrwxr-x 3 1000 1000 4096 Jun 19 10:08

意思是,我想把这个:改成2012-06-19 10:08那个Jun 19 10:08,或者这个

2012-06-16 14:09到那个 Jun 16 14:09

我正在使用以下方法来呈现任何文件的详细信息:

void displayFileProperties(struct stat* file,char*  outputProperties)
{


    char partOfOutput[BUFFER];
    mode_t mode = file->st_mode;
    struct tm* time;
    int month, day, hour, min;

    // First take care of the file details , e.g permission

    // this is a regular file
    if (S_ISREG(mode)) strcat(outputProperties,"-");

    // the is a pipe file
    else if (S_ISFIFO(mode)) strcat(outputProperties,"p");

    // for a link file
    else if (S_ISLNK(mode)) strcat(outputProperties,"l");

    // for a directory file
    else if (S_ISDIR(mode)) strcat(outputProperties,"d");

    // this is for a socket file
    else if (S_ISSOCK(mode)) strcat(outputProperties,"s");

    // for a block device file
    else if (S_ISBLK(mode)) strcat(outputProperties,"b");

    // and this is one for a character device file
    else if (S_ISCHR(mode)) strcat(outputProperties,"c");


    // Permissions

    (mode & S_IRUSR)? strcat(outputProperties,"r"): strcat(outputProperties,"-");
    (mode & S_IWUSR)? strcat(outputProperties,"w"): strcat(outputProperties,"-");
    (mode & S_IXUSR)? strcat(outputProperties,"x"): strcat(outputProperties,"-");


    // Group permission

    (mode & S_IRGRP) ?strcat(outputProperties,"r"):strcat(outputProperties,"-");
    (mode & S_IWGRP) ?strcat(outputProperties,"w"):strcat(outputProperties,"-");
    (mode & S_IXGRP) ?strcat(outputProperties,"x"):strcat(outputProperties,"-");


    (mode & S_IROTH)? strcat(outputProperties,"r"):strcat(outputProperties,"-");
    (mode & S_IWOTH) ?strcat(outputProperties,"w"):strcat(outputProperties,"-");
    (mode & S_IXOTH) ?strcat(outputProperties,"x"):strcat(outputProperties,"-");



    //print other information

    //print num of hard link
    sprintf(partOfOutput," %d ",file->st_nlink);
    strcat(outputProperties,partOfOutput);

    //print num of hard link
    sprintf(partOfOutput,"%d ",file->st_uid);
    strcat(outputProperties,partOfOutput);

    //print num of hard link
    sprintf(partOfOutput,"%d ",file->st_gid);
    strcat(outputProperties,partOfOutput);

    //print num of hard link
    sprintf(partOfOutput,"%d ",(int) file->st_size);
    strcat(outputProperties,partOfOutput);


    // From here take care of the time properties

    time = localtime(&file->st_mtim);

    month = time->tm_mon + 1;
    day = time->tm_mday;
    hour = time->tm_hour;
    min = time->tm_min;

    sprintf(partOfOutput,"%d-",time->tm_year + 1900);
    strcat(outputProperties,partOfOutput);

    if(month < 10)
        strcat(outputProperties,"0");

    sprintf(partOfOutput,"%d-",month);
    strcat(outputProperties,partOfOutput);

    if(day < 10)
        strcat(outputProperties,"0");

    sprintf(partOfOutput,"%d ",day);
    strcat(outputProperties,partOfOutput);

    if(hour < 10)
        strcat(outputProperties,"0");

    sprintf(partOfOutput,"%d:",hour);
    strcat(outputProperties,partOfOutput);

    if(min < 10)
      strcat(outputProperties,"0");

    sprintf(partOfOutput,"%d ",min);
    strcat(outputProperties,partOfOutput);

}

我怎样才能做到这一点 ?我几乎可以肯定它需要一些小的更改,但是我在网上搜索过,但没有发现任何可以澄清这一点的东西。

谢谢

4

2 回答 2

4

strftime()功能在日期格式方面非常灵活。http://linux.die.net/man/3/strftime

strftime(partOfOutput, BUFFER, "%b %d %H:%M", time);
于 2012-06-26T11:10:12.630 回答
4

你可以使用strftime

time = localtime(&file->st_mtim);
strftime(partOfOutput, BUFFER, "%b %d %H:%M", time);

检查文档以获取strftime. 它可用于解决日期和时间格式的大多数问题。

于 2012-06-26T11:13:25.047 回答