ex-2.c

来自「掌握如何用C来实现各种算法」· C语言 代码 · 共 74 行

C
74
字号
/*****************************************************************************
*                                                                            *
*  ex-2.c                                                                    *
*  ======                                                                    *
*                                                                            *
*  Description: Illustrates sorting a directory listing (see Chapter 12).    *
*                                                                            *
*****************************************************************************/

#include <stdio.h>
#include <sys/param.h>
#include <unistd.h>

#include "directls.h"

/*****************************************************************************
*                                                                            *
*  --------------------------------- main ---------------------------------  *
*                                                                            *
*****************************************************************************/

int main(int argc, char **argv) {

Directory          *dir;

char               buffer[MAXPATHLEN];

int                count,
                   i;

/*****************************************************************************
*                                                                            *
*  Get the directory listing.                                                *
*                                                                            *
*****************************************************************************/

if (argc > 1) {

   if ((count = directls(argv[1], &dir)) < 0) {

      fprintf(stdout, "Could not read directory\n");
      exit(1);

   }

   }

else {
   
   if ((count = directls(getcwd(buffer, MAXPATHLEN), &dir)) < 0) {

      fprintf(stdout, "Could not read directory\n");
      exit(1);

   }

}

/*****************************************************************************
*                                                                            *
*  Display the directory listing.                                            *
*                                                                            *
*****************************************************************************/

for (i = 0; i < count; i++)
   fprintf(stdout, "%s\n", dir[i].name);

fprintf(stdout, "%d found\n", count);
free(dir);

return 0;

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?