#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#define _GNU_SOURCE
#include <getopt.h>
#define BUFFER_SIZE 1024
#define BF_SIZE 10
static void print_file(const char *path);
static void print_file_lib(const char *path);
static void print_dir(char *path);
static void find_file_lib(const char *path, regex_t pat);
static void find_dir(const char *path, regex_t pat);
static int filetype(const char *path);
static void out(const char *s, FILE *f, int fd, DIR *d);
static struct option longopts[] = {
{"find", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[]){
int i, opt;
if(argc < 2) out("usage: <commend> <name>", NULL, 0, NULL);
while ((opt = getopt_long(argc, argv, "f:h", longopts, NULL)) != -1){
switch(opt){
case 'f':
break;
case 'h':
printf("usage: [-f] [FILE/DIR ...]\n");
exit(0);
case '?':
printf("usage: [-option] [FILE/DIR ...]\n");
exit(1);
}
}
if(optind == 1){
for(i=1; i<argc; i++){
int result;
result = filetype(argv[i]);
if(result == 0) print_dir(argv[i]);
else if(result == 1) print_file_lib(argv[i]);
}
} else {
regex_t pat;
for(i=optind; i<argc; i++){
int err = 0, result;
char buf[BUFFER_SIZE];
err = regcomp(&pat, argv[2], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
if(err != 0){
regerror(err, &pat, buf, sizeof buf);
puts(buf);
exit(1);
}
result = filetype(argv[i]);
if(result == 0) find_dir(argv[i], pat);
else if(result == 1) find_file_lib(argv[i], pat);
}
regfree(&pat);
}
exit(0);
}
static void print_file(const char *path){
int fd, n;
unsigned char buf[BUFFER_SIZE];
fd = open(path, O_RDONLY);
if(fd < 0) out("print_file: open error", NULL, fd, NULL);
for(;;){
n = read(fd, buf, sizeof buf);
if(n < 0) out("print_file: read error", NULL, fd, NULL);
if(n == 0) break;
if(write(STDOUT_FILENO, buf, n) < 0) out("print_file: write error", NULL, fd, NULL);
}
if(close(fd) < 0) out("print_file: close error", NULL, fd, NULL);
}
static void print_file_lib(const char *path){
char *n;
unsigned char buf[BUFFER_SIZE];
FILE *f = fopen(path, "r");
if(!f) out("print_file_lib: fopen error", f, 0, NULL);
while(1){
n = fgets(buf, BUFFER_SIZE, f);
if(!n) break;
printf("%s", buf);
}
if(fclose(f) == EOF) out("print_file_lib: fclose error", f, 0, NULL);
}
static void print_dir(char *path) {
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("print_dir: opendir error", NULL, 0, d);
while(ent = readdir(d)){
char fpath[BUFFER_SIZE] = { };
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
int result = filetype(fpath);
if(result == 0) printf("%s/\n", ent->d_name);
else if(result == 1) printf("%s*\n", ent->d_name);
}
closedir(d);
}
static void find_file_lib(const char *path, regex_t pat){
int cnt = 0;
FILE *f;
char buf[BUFFER_SIZE];
f = fopen(path, "r");
if(!f) out("find_file_lib: fopen error", f, 0, NULL);
//성공 buf 반환, 실패 NULL 반환
while(fgets(buf, BUFFER_SIZE, f)){
//while((fread(buf, sizeof(char), 1, f)) > 0){
if(ferror(buf)) out("fgets error", f, 0, NULL);
cnt++;
if(regexec(&pat, buf, 1, NULL, 0) == 0){
fprintf(stdout, "%d: %s\n", cnt, buf);
}
}
fclose(f);
}
static void find_dir(const char *path, regex_t pat){
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("opendir error", NULL, 0, d);
while(ent = readdir(d)){
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
char fpath[BUFFER_SIZE];
fpath[0] = '\0';
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
if(regexec(&pat, ent->d_name, 1, NULL, 0) == 0){
int result = filetype(fpath);
if(result == 0){
printf("%s:\t%s/\n", path, ent->d_name);
find_dir(fpath, pat);
}
else if(result == 1) printf("%s:\t%s*\n", path, ent->d_name);
}
}
closedir(d);
}
static int filetype(const char *path){
struct stat st;
int result;
if(stat(path, &st) < 0) out("stat error", NULL, 0, NULL);
if(S_ISDIR(st.st_mode)){
result = 0;
}else if(S_ISREG(st.st_mode)){
result = 1;
}
return result;
}
static void out(const char *s, FILE *f, int fd, DIR *d){
perror(s);
if(f) fclose(f);
if(fd) close(fd);
if(d) closedir(d);
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 2048
static void print_file(const char *path);
static void print_file_lib(const char *path);
static void print_dir(char *path);
static void out(const char *s, FILE *f, int fd, DIR *d);
int main(int argc, char *argv[]){
int i;
struct stat statbuf;
char path[BUFFER_SIZE];
if(argc < 2) out("usage: <commend> <name>", NULL, 0, NULL);
for(i=1; i<argc; i++){
if(stat(argv[i], &statbuf) < 0) out("stat error", NULL, 0, NULL);
if(S_ISREG(statbuf.st_mode)){
print_file_lib(argv[i]);
}
else if(S_ISDIR(statbuf.st_mode)){
print_dir(argv[i]);
}
}
exit(0);
}
static void print_file(const char *path){
int fd, n;
unsigned char buf[BUFFER_SIZE];
fd = open(path, O_RDONLY);
if(fd < 0) out("open error", NULL, fd, NULL);
for(;;){
n = read(fd, buf, sizeof buf);
if(n < 0) out("read error", NULL, fd, NULL);
if(n == 0) break;
if(write(STDOUT_FILENO, buf, n) < 0) out("write error", NULL, fd, NULL);
}
if(close(fd) < 0) out("close error", NULL, fd, NULL);
}
static void print_file_lib(const char *path){
char *n;
unsigned char buf[BUFFER_SIZE];
FILE *f = fopen(path, "r");
if(!f) out("fopen error", f, 0, NULL);
while(1){
n = fgets(buf, BUFFER_SIZE, f);
if(!n) break;
printf("%s", buf);
}
if(fclose(f) == EOF) out("fclose error", f, 0, NULL);
}
static void print_dir(char *path) {
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("opendir error", NULL, 0, d);
while(ent = readdir(d)){
char fpath[BUFFER_SIZE] = { };
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
if(stat(fpath, &st) < 0) out("dir: stat error", NULL, 0, NULL);
if(S_ISDIR(st.st_mode)){
printf("%s/\n", ent->d_name);
}else if(S_ISREG(st.st_mode)) printf("%s*\n", ent->d_name);
}
closedir(d);
}
static void out(const char *s, FILE *f, int fd, DIR *d){
perror(s);
if(f) fclose(f);
if(fd) close(fd);
if(d) closedir(d);
exit(1);
}
근데 argv[0]에 뭐 들어가야 정상 작동함..
//char *cmd = strtok(command, " ");
//int cnt = 0;
//while(cmd != NULL){
// cnt++;
// printf("cmd: %s\n", cmd);
// cmd = strtok(NULL, " ");
//}
```
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
#define BF_SIZE 10
void *sub_thread(void *command);
int makeargv(const char *s, const char *delimiters, char ***argvp);
void freemakeargv(char **argv);
static void print_file(const char *path);
static void print_file_lib(const char *path);
static void print_dir(char *path);
static void find_file_lib(const char *path, regex_t pat);
static void find_dir(const char *path, regex_t pat);
static int filetype(const char *path);
static void out(const char *s, FILE *f, int fd, DIR *d);
static struct option longopts[] = {
{"find", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[]){
int i, opt;
if(argc != 1) out("usage: <commend>", NULL, 0, NULL);
char str[BUFFER_SIZE];
printf("file or directory name: ");
scanf("%[^\n]s", str);
pthread_t thread;
pthread_create(&thread,NULL,sub_thread, (void *) str);
sleep(6);
printf("input name: %s\n", str);
exit(0);
}
void *sub_thread(void *command){
printf("sub_thread in\n");
pthread_t tid;
tid=pthread_self();
printf("\ttid:%lx\n",tid);
printf("sub: %s\n", (const char *)command);
int myargc;
char **myargv;
if ((myargc = makeargv(command, " ", &myargv)) == -1) {
out("makeargv error", NULL, 0, NULL);
}
int i, opt;
while ((opt = getopt_long(myargc, myargv, "f:h", longopts, NULL)) != -1){
switch(opt){
case 'f':
break;
case 'h':
printf("usage: [-f] [FILE/DIR ...]\n");
exit(0);
case '?':
printf("usage: [-option] [FILE/DIR ...]\n");
exit(1);
}
}
printf("optind: %d, myargc: %d\n", optind, myargc);
if(optind == 1){
for(i=0; i<myargc; i++){
printf("myargv[%d]: %s\n", i, myargv[i]);
int result;
result = filetype(myargv[i]);
if(result == 0) print_dir(myargv[i]);
else if(result == 1) print_file_lib(myargv[i]);
}
} else {
for(int i=0; i<myargc; i++){
printf("myargc[%d]: %s\n", i, myargv[i]);
}
regex_t pat;
for(i=optind; i<myargc; i++){
int err = 0, result;
char buf[BUFFER_SIZE];
err = regcomp(&pat, myargv[2], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
if(err != 0){
regerror(err, &pat, buf, sizeof buf);
puts(buf);
exit(1);
}
result = filetype(myargv[i]);
if(result == 0) find_dir(myargv[i], pat);
else if(result == 1) find_file_lib(myargv[i], pat);
}
regfree(&pat);
}
freemakeargv(myargv);
}
int makeargv(const char *s, const char *delimiters, char ***argvp) {
int error;
int i;
int numtokens;
const char *snew;
char *t;
if ((s == NULL) || (delimiters == NULL) || (argvp == NULL)) {
errno = EINVAL;
return -1;
}
*argvp = NULL;
/* snew is real start of string */
snew = s + strspn(s, delimiters);
if ((t = malloc(strlen(snew) + 1)) == NULL)
return -1;
strcpy(t, snew);
numtokens=0;
/* count the number of tokens in s */
if (strtok(t, delimiters) != NULL)
for (numtokens=1; strtok(NULL, delimiters) != NULL; numtokens++) ;
/* create argument array for ptrs to the tokens */
if ((*argvp = malloc((numtokens + 1) * sizeof(char *))) == NULL) {
error = errno;
free(t);
errno = error;
return -1;
}
/* insert pointers to tokens into the argument array */
if (numtokens == 0) {
free(t);
} else {
strcpy(t, snew);
**argvp = strtok(t, delimiters);
for(i=1; i<numtokens; i++)
*((*argvp)+i) = strtok(NULL, delimiters);
}
/* put in final NULL pointer */
*((*argvp)+numtokens) = NULL;
return numtokens;
}
void freemakeargv(char **argv) {
if (argv == NULL)
return;
if (*argv != NULL)
free(*argv);
free(argv);
argv=NULL;
}
static void print_file(const char *path){
int fd, n;
unsigned char buf[BUFFER_SIZE];
fd = open(path, O_RDONLY);
if(fd < 0) out("print_file: open error", NULL, fd, NULL);
for(;;){
n = read(fd, buf, sizeof buf);
if(n < 0) out("print_file: read error", NULL, fd, NULL);
if(n == 0) break;
if(write(STDOUT_FILENO, buf, n) < 0) out("print_file: write error", NULL, fd, NULL);
}
if(close(fd) < 0) out("print_file: close error", NULL, fd, NULL);
}
static void print_file_lib(const char *path){
char *n;
unsigned char buf[BUFFER_SIZE];
FILE *f = fopen(path, "r");
if(!f) out("print_file_lib: fopen error", f, 0, NULL);
while(1){
n = fgets(buf, BUFFER_SIZE, f);
if(!n) break;
printf("%s", buf);
}
if(fclose(f) == EOF) out("print_file_lib: fclose error", f, 0, NULL);
}
static void print_dir(char *path) {
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("print_dir: opendir error", NULL, 0, d);
while(ent = readdir(d)){
char fpath[BUFFER_SIZE] = { };
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
int result = filetype(fpath);
if(result == 0) printf("%s/\n", ent->d_name);
else if(result == 1) printf("%s*\n", ent->d_name);
}
closedir(d);
}
static void find_file_lib(const char *path, regex_t pat){
int cnt = 0;
FILE *f;
char buf[BUFFER_SIZE];
f = fopen(path, "r");
if(!f) out("find_file_lib: fopen error", f, 0, NULL);
//성공 buf 반환, 실패 NULL 반환
while(fgets(buf, BUFFER_SIZE, f)){
//while((fread(buf, sizeof(char), 1, f)) > 0){
if(ferror(buf)) out("fgets error", f, 0, NULL);
cnt++;
if(regexec(&pat, buf, 1, NULL, 0) == 0){
fprintf(stdout, "%d: %s\n", cnt, buf);
}
}
fclose(f);
}
static void find_dir(const char *path, regex_t pat){
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("opendir error", NULL, 0, d);
while(ent = readdir(d)){
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
char fpath[BUFFER_SIZE];
fpath[0] = '\0';
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
if(regexec(&pat, ent->d_name, 1, NULL, 0) == 0){
int result = filetype(fpath);
if(result == 0){
printf("%s:\t%s/\n", path, ent->d_name);
find_dir(fpath, pat);
}
else if(result == 1) printf("%s:\t%s*\n", path, ent->d_name);
}
}
closedir(d);
}
static int filetype(const char *path){
struct stat st;
int result;
if(stat(path, &st) < 0) out("stat error", NULL, 0, NULL);
if(S_ISDIR(st.st_mode)){
result = 0;
}else if(S_ISREG(st.st_mode)){
result = 1;
}
return result;
}
static void out(const char *s, FILE *f, int fd, DIR *d){
perror(s);
if(f) fclose(f);
if(fd) close(fd);
if(d) closedir(d);
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#define BUFFER_SIZE 2048
void *sub_thread(void *command);
static void print_file(const char *path);
static void print_file_lib(const char *path);
static void print_dir(char *path);
static void out(const char *s, FILE *f, int fd, DIR *d);
int main(int argc, char *argv[]){
struct stat statbuf;
if(argc != 1) out("usage: <commend> <name>", NULL, 0, NULL);
char str[BUFFER_SIZE];
printf("file or directory name: ");
scanf("%[^\n]s", str);
pthread_t thread;
pthread_create(&thread,NULL,sub_thread, (void *) str);
sleep(4);
exit(0);
}
void *sub_thread(void *command){
struct stat statbuf;
printf("sub_thread in\n");
pthread_t tid;
tid=pthread_self();
printf("\ttid:%lx\n",tid);
printf("sub: %s\n", (const char *)command);
if(stat(command, &statbuf) < 0) out("stat error", NULL, 0, NULL);
if(S_ISREG(statbuf.st_mode)){
print_file_lib(command);
}
else if(S_ISDIR(statbuf.st_mode)){
print_dir(command);
}
return command;
}
static void print_file(const char *path){
int fd, n;
unsigned char buf[BUFFER_SIZE];
fd = open(path, O_RDONLY);
if(fd < 0) out("open error", NULL, fd, NULL);
for(;;){
n = read(fd, buf, sizeof buf);
if(n < 0) out("read error", NULL, fd, NULL);
if(n == 0) break;
if(write(STDOUT_FILENO, buf, n) < 0) out("write error", NULL, fd, NULL);
}
if(close(fd) < 0) out("close error", NULL, fd, NULL);
}
static void print_file_lib(const char *path){
char *n;
unsigned char buf[BUFFER_SIZE];
FILE *f = fopen(path, "r");
if(!f) out("fopen error", f, 0, NULL);
while(1){
n = fgets(buf, BUFFER_SIZE, f);
if(!n) break;
printf("%s", buf);
}
if(fclose(f) == EOF) out("fclose error", f, 0, NULL);
}
static void print_dir(char *path) {
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("opendir error", NULL, 0, d);
while(ent = readdir(d)){
char fpath[BUFFER_SIZE] = { };
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
if(stat(fpath, &st) < 0) out("dir: stat error", NULL, 0, NULL);
if(S_ISDIR(st.st_mode)){
printf("%s/\n", ent->d_name);
}else if(S_ISREG(st.st_mode)) printf("%s*\n", ent->d_name);
}
closedir(d);
}
static void out(const char *s, FILE *f, int fd, DIR *d){
perror(s);
if(f) fclose(f);
if(fd) close(fd);
if(d) closedir(d);
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
#define BF_SIZE 10
void *sub_thread(void *command);
int makeargv(const char *s, const char *delimiters, char ***argvp);
void freemakeargv(char **argv);
static void print_file(const char *path);
static void print_file_lib(const char *path);
static void print_dir(char *path);
static void find_file_lib(const char *path, regex_t pat);
static void find_dir(const char *path, regex_t pat);
static int filetype(const char *path);
static void out(const char *s, FILE *f, int fd, DIR *d);
static struct option longopts[] = {
{"find", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[]){
int i, opt;
if(argc != 1) out("usage: <commend>", NULL, 0, NULL);
char str[BUFFER_SIZE];
printf("file or directory name: ");
scanf("%[^\n]s", str);
pthread_t thread;
pthread_create(&thread, NULL, sub_thread, (void *) str);
sleep(6);
printf("\nEND\n");
exit(0);
}
void *sub_thread(void *command){
pthread_t tid;
tid=pthread_self();
printf("\ttid:%lx\n",tid);
printf("receive command: %s\n", (const char *)command);
char cmd[] = {"a.out ", };
strcat(cmd, command);
int myargc;
char **myargv;
if ((myargc = makeargv(cmd, " ", &myargv)) == -1) {
out("makeargv error", NULL, 0, NULL);
}
int i, opt;
while ((opt = getopt_long(myargc, myargv, "f:h", longopts, NULL)) != -1){
switch(opt){
case 'f':
break;
case 'h':
printf("usage: [-f] [FILE/DIR ...]\n");
exit(0);
case '?':
printf("usage: [-option] [FILE/DIR ...]\n");
exit(1);
}
}
if(optind == 1){
for(i=0; i<myargc; i++){
int result;
result = filetype(myargv[i]);
if(result == 0) print_dir(myargv[i]);
else if(result == 1) print_file_lib(myargv[i]);
}
} else {
//for(int i=0; i<myargc; i++){
// printf("myargc[%d]: %s\n", i, myargv[i]);
//}
regex_t pat;
int optcnt = 0;
for(i=optind; i<myargc; i++){
optcnt++;
int err = 0, result;
char buf[BUFFER_SIZE];
err = regcomp(&pat, myargv[2*optcnt], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
if(err != 0){
regerror(err, &pat, buf, sizeof buf);
puts(buf);
exit(1);
}
result = filetype(myargv[i]);
if(result == 0) find_dir(myargv[i], pat);
else if(result == 1) find_file_lib(myargv[i], pat);
printf("\n");
}
regfree(&pat);
}
freemakeargv(myargv);
}
int makeargv(const char *s, const char *delimiters, char ***argvp) {
int i;
int myargc;
const char *snew;
char *t;
if ((s == NULL) || (delimiters == NULL) || (argvp == NULL)) {
errno = EINVAL;
return -1;
}
*argvp = NULL;
//strspn: s에서 delimiters가 아닌 첫번째 표시 찾아서 그 문자의 index return
snew = s + strspn(s, delimiters);
//문자열 분석을 위한 버퍼 t는 malloc으로 할당
if ((t = malloc(strlen(snew) + 1)) == NULL)
return -1;
//s를 t에 복사
strcpy(t, snew);
myargc=0;
//strtok를 이용하여 myargc 계산
if (strtok(t, delimiters) != NULL)
for (myargc=1; strtok(NULL, delimiters) != NULL; myargc++);
//myargc를 이용하여 argvp 배열의 크기 할당
if ((*argvp = malloc((myargc + 1) * sizeof(char *))) == NULL) {
free(t);
return -1;
}
if (myargc == 0) {
free(t);
} else {
//s를 다시 t에 복사
strcpy(t, snew);
//strtok를 사용하여 각각의 토큰에 대한 포인터를 만들고, t를 분석하기 좋은 형태로 수정
**argvp = strtok(t, delimiters);
for(i=1; i<myargc; i++)
//i번째 argvp에 delimiters로 나뉜 토큰 저장
*((*argvp)+i) = strtok(NULL, delimiters);
}
//마지막에 null 넣음
*((*argvp) + myargc) = NULL;
return myargc;
}
void freemakeargv(char **argv) {
if (argv == NULL)
return;
if (*argv != NULL)
free(*argv);
free(argv);
argv=NULL;
}
static void print_file(const char *path){
int fd, n;
unsigned char buf[BUFFER_SIZE];
fd = open(path, O_RDONLY);
if(fd < 0) out("print_file: open error", NULL, fd, NULL);
for(;;){
n = read(fd, buf, sizeof buf);
if(n < 0) out("print_file: read error", NULL, fd, NULL);
if(n == 0) break;
if(write(STDOUT_FILENO, buf, n) < 0) out("print_file: write error", NULL, fd, NULL);
}
if(close(fd) < 0) out("print_file: close error", NULL, fd, NULL);
}
static void print_file_lib(const char *path){
char *n;
unsigned char buf[BUFFER_SIZE];
FILE *f = fopen(path, "r");
if(!f) out("print_file_lib: fopen error", f, 0, NULL);
while(1){
n = fgets(buf, BUFFER_SIZE, f);
if(!n) break;
printf("%s", buf);
}
if(fclose(f) == EOF) out("print_file_lib: fclose error", f, 0, NULL);
}
static void print_dir(char *path) {
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("print_dir: opendir error", NULL, 0, d);
while(ent = readdir(d)){
char fpath[BUFFER_SIZE] = { };
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
int result = filetype(fpath);
if(result == 0) printf("%s/\n", ent->d_name);
else if(result == 1) printf("%s*\n", ent->d_name);
}
closedir(d);
}
static void find_file_lib(const char *path, regex_t pat){
int cnt = 0;
FILE *f;
char buf[BUFFER_SIZE];
f = fopen(path, "r");
if(!f) out("find_file_lib: fopen error", f, 0, NULL);
//성공 buf 반환, 실패 NULL 반환
while(fgets(buf, BUFFER_SIZE, f)){
//while((fread(buf, sizeof(char), 1, f)) > 0){
if(ferror(buf)) out("fgets error", f, 0, NULL);
cnt++;
if(regexec(&pat, buf, 1, NULL, 0) == 0){
fprintf(stdout, "%d: %s\n", cnt, buf);
}
}
fclose(f);
}
static void find_dir(const char *path, regex_t pat){
DIR *d;
struct dirent *ent;
struct stat st;
d = opendir(path);
if(!d) out("opendir error", NULL, 0, d);
while(ent = readdir(d)){
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
char fpath[BUFFER_SIZE];
fpath[0] = '\0';
strcat(fpath, path);
strcat(fpath, "/");
strcat(fpath, ent->d_name);
if(regexec(&pat, ent->d_name, 1, NULL, 0) == 0){
int result = filetype(fpath);
if(result == 0){
printf("%s:\t%s/\n", path, ent->d_name);
find_dir(fpath, pat);
}
else if(result == 1) printf("%s:\t%s*\n", path, ent->d_name);
}
}
closedir(d);
}
static int filetype(const char *path){
struct stat st;
int result;
if(stat(path, &st) < 0) out("stat error", NULL, 0, NULL);
if(S_ISDIR(st.st_mode)){
result = 0;
}else if(S_ISREG(st.st_mode)){
result = 1;
}
return result;
}
static void out(const char *s, FILE *f, int fd, DIR *d){
perror(s);
if(f) fclose(f);
if(fd) close(fd);
if(d) closedir(d);
exit(1);
}