Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (95 sloc) 2.078 kb
5425669 @pkrumins added AsyncStackedVideos - node-video just got asynchronous
authored
1 #include <stdio.h>
2 #include <dirent.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7
8 #include "common.h"
9 #include "utils.h"
10
11 char **
12 find_files(const char *path)
13 {
14 char **files = NULL;
15 DIR *dp;
16 struct dirent *dirp;
17 int i = 0;
18
19 if ((dp = opendir(path)) == NULL) {
20 return NULL;
21 }
22 int nfiles = 0;
23 while ((dirp = readdir(dp)) != NULL) {
24 if (str_eq(dirp->d_name, ".") || str_eq(dirp->d_name, ".."))
25 continue;
26 nfiles++;
27 }
28 files = (char **)malloc(sizeof(char *) * (nfiles + 1));
29 if (!files)
30 goto err_1;
31 files[nfiles] = NULL;
32 rewinddir(dp);
33 while ((dirp = readdir(dp)) != NULL) {
34 if (str_eq(dirp->d_name, ".") || str_eq(dirp->d_name, ".."))
35 continue;
36 int dir_name_len = strlen(dirp->d_name);
37 files[i] = (char *)malloc(sizeof(char) * (dir_name_len + 1));
38 if (!files[i]) {
39 int j;
40 for (j=0;j<i;j++) free(files[j]);
41 files = NULL;
42 goto err_1;
43 }
44 strcpy(files[i], dirp->d_name);
45 i++;
46 }
47
48 err_1:
49 closedir(dp);
50 return files;
51 }
52
53 void
54 free_file_list(char **file_list)
55 {
56 char **p = file_list;
57 while (*p)
58 free(*p++);
59 free(file_list);
60 }
61
62 int
63 file_list_length(char **file_list)
64 {
65 char **p = file_list;
66 int i = 0;
67 while (*p++)
68 i++;
69 return i;
70 }
71
72 char **
73 filter_file_list(char **file_list, int (*f)(const char *file))
74 {
75 char **p = file_list;
76 int nfiles = 0;
77 while (*p)
78 if (f(*p++)) nfiles++;
79 char **files = (char **)malloc(sizeof(char *)*(nfiles+1));
80 if (!files)
81 return NULL;
82 p = file_list;
83 int i = 0;
84 while (*p)
85 if (f(*p))
86 files[i++] = *p++;
87 return files;
88 }
89
90 int
91 file_size(const char *path)
92 {
93 struct stat moo;
94 if (stat(path, &moo) == -1) return -1;
95 return moo.st_size;
96 }
97
98 bool
99 is_dir(const char *path)
100 {
101 struct stat moo;
102 if (stat(path, &moo) == -1) return false;
103 return S_ISDIR(moo.st_mode);
104 }
105
Something went wrong with that request. Please try again.