Last active
August 13, 2022 05:04
-
-
Save hiroqn/f2d3c236ebd8a8139ce15d1cd4246049 to your computer and use it in GitHub Desktop.
dir.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include <ctype.h> | |
//#include <locale.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
//#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <dirent.h> | |
typedef DIR directory_type; | |
#define FILESEP '/' | |
static int | |
isdirectory(char *filename) | |
{ | |
struct stat statbuf; | |
if (stat(filename, &statbuf) < 0) | |
return 0; /* In the expectation that opening as a file will fail */ | |
return (statbuf.st_mode & S_IFMT) == S_IFDIR; | |
} | |
static directory_type * | |
opendirectory(char *filename) | |
{ | |
return opendir(filename); | |
} | |
static char * | |
readdirectory(directory_type *dir) | |
{ | |
for (;;) | |
{ | |
struct dirent *dent = readdir(dir); | |
if (dent == NULL) return NULL; | |
if (strcmp(dent->d_name, ".") != 0 && strcmp(dent->d_name, "..") != 0) | |
return dent->d_name; | |
} | |
/* Control never reaches here */ | |
} | |
static void | |
closedirectory(directory_type *dir) | |
{ | |
closedir(dir); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
char buffer[1024]; | |
char *nextfile; | |
char *pathname; | |
pathname = argv[1]; | |
puts(argv[1]); | |
if(isdirectory(pathname)) { | |
puts("dir"); | |
directory_type *dir = opendirectory(pathname); | |
if (dir == NULL) { | |
puts("err"); | |
} | |
while ((nextfile = readdirectory(dir)) != NULL) | |
{ | |
int frc; | |
puts(nextfile); | |
sprintf(buffer, "%.512s%c%.128s", pathname, FILESEP, nextfile); | |
puts(buffer); | |
} | |
} else { | |
puts("file"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment