static int scandir_cmp(const void *p1, const void *p2) { struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2; if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2); if ((*d1)->d_type == DT_DIR) return -1; if ((*d2)->d_type == DT_DIR) return 1; return alphasort(d1, d2); }
static int scandir_cmp(const struct dirent **p1, const struct dirent **p2) { #if defined(GCW0) return 1; #else struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2; if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2); if ((*d1)->d_type == DT_DIR) return -1; // put before if ((*d2)->d_type == DT_DIR) return 1; return alphasort(d1, d2); #endif }
/* Sort entries in a directory; directories first, and then by name */ int dirsort(const struct dirent **a, const struct dirent **b) { /* both the same type, go alphabetically */ if((*a)->d_type == (*b)->d_type) return alphasort((void*)a, (void*)b); /* a is a dir, b is not */ if((*a)->d_type == DT_DIR) return -1; /* b is a dir, a is not */ if((*b)->d_type == DT_DIR) return 1; /* different types but neither is directory, alphabetical */ return alphasort((void*)a, (void*)b); }
static int scandir_cmp(const void *p1, const void *p2) { struct dirent **d1 = (struct dirent **)p1, **d2 = (struct dirent **)p2; #else static int scandir_cmp(const dirent **p1, const dirent **p2) { const struct dirent **d1 = (const struct dirent **)p1, **d2 = (const struct dirent **)p2; #endif if ((*d1)->d_type == (*d2)->d_type) return alphasort(d1, d2); if ((*d1)->d_type == DT_DIR) return -1; if ((*d2)->d_type == DT_DIR) return 1; return alphasort(d1, d2); } static const char *filter_exts[] = { ".adf", ".gz",".rom",".adf.gz" }; static int scandir_filter(const struct dirent *ent) { const char *p; int i; return 1; if (ent == NULL || ent->d_name == NULL) return 0; if (strlen(ent->d_name) < 5) return 1; p = ent->d_name + strlen(ent->d_name) - 4; for (i = 0; i < sizeof(filter_exts)/sizeof(filter_exts[0]); i++) { if (strcmp(p, filter_exts[i]) == 0) return 0; } return 1; } static void extractFileName(char * str,char *buffer) { char *p=str+strlen(str)-1; while(*p != '/') p--; p++; strcpy(buffer,p); }
static int dirent_cmp(const struct dirent **a, const struct dirent **b) { bool dir_a = !!((*a)->d_type & DT_DIR); bool dir_b = !!((*b)->d_type & DT_DIR); /* directories first */ if (dir_a != dir_b) return dir_b - dir_a; return alphasort(a, b); }
int compare(const struct dirent **a, const struct dirent **b) { struct stat a_stat; struct stat b_stat; char path_a[PATH_MAX]; char path_b[PATH_MAX]; strcpy(path_a, cwd); strcat(path_a, "/"); strcat(path_a, (*a)->d_name); strcpy(path_b, cwd); strcat(path_b, "/"); strcat(path_b, (*b)->d_name); stat(path_a, &a_stat); stat(path_b, &b_stat); if(S_ISDIR(a_stat.st_mode) && !S_ISDIR(b_stat.st_mode)) { return -1; } else if(S_ISDIR(b_stat.st_mode) && !S_ISDIR(a_stat.st_mode)) { return 1; } else return alphasort(a,b); }
int alphasortAndroid(const dirent **a, const dirent **b) { alphasort(a, b); }
int reverseSortLogFileName(const struct dirent **e1, const struct dirent **e2) { return alphasort(e2, e1); }
static int plugins_ralphasort(const void * a, const void * b) { #else static int plugins_ralphasort(const struct dirent ** a, const struct dirent ** b) { #endif return -alphasort(a, b); } int plugins_load(Protocol * proto, TCFBroadcastGroup * bcg) { struct dirent ** files; int file_count = -1; int ret = 0; file_count = scandir(plugins_path, &files, plugins_filter, plugins_ralphasort); if (file_count < 0) { trace(LOG_PLUGIN, "plugins error: failed opening plugins directory \"%s\"", plugins_path); return -1; } while (file_count--) { char * cur_plugin_path = NULL; if (asprintf(&cur_plugin_path, "%s/%s", plugins_path, files[file_count]->d_name) == -1) { trace(LOG_PLUGIN, "plugins error: `asprintf' failed for plugin \"%s\"", files[file_count]->d_name); ret = -1; goto delete_cur_entry; } if (plugin_init(cur_plugin_path, proto, bcg)) { trace(LOG_PLUGIN, "plugins error: unable to start plugin \"%s\"", cur_plugin_path); ret = -1; /* Continue to load the rest of plugins */ } /* cur_plugin_path and files were allocated by asprintf() and scandir(), * and they should be released by free(), don't call loc_free() here. */ free(cur_plugin_path); delete_cur_entry: free(files[file_count]); } free(files); return ret; } int plugin_init(const char * name, Protocol * proto, TCFBroadcastGroup * bcg) { void * handle; char * error; InitFunc init; /* Plugin loading: */ trace(LOG_PLUGIN, "loading plugin \"%s\"", name); handle = dlopen(name, RTLD_LAZY); if (!handle) { trace(LOG_PLUGIN, "plugins error: \"%s\"", dlerror()); return -1; } /* Plugin initialization: */ init = (InitFunc)dlsym(handle, "tcf_init_plugin"); if ((error = dlerror()) != NULL) { trace(LOG_PLUGIN, "plugins error: \"%s\"", error); return -1; } trace(LOG_PLUGIN, "initializing plugin \"%s\"", name); init(proto, bcg, NULL); /* Handles table update: */ plugins_handles = (void **) loc_realloc(plugins_handles, ++plugins_count * sizeof(void *)); plugins_handles[plugins_count - 1] = handle; return 0; }
static int my_alphasort (const void *a, const void *b) { return alphasort(b,a); }
static void url_enc(char *s, int f, int chunk) { for(; *s; s++) { char *c = (char[4]){ *s, 0 }; if(!isalnum(*s) && *s != '-' && *s != '_' && *s != '.' && *s != '~' && *s != '/') sprintf(c, "%%%.2X", (unsigned)*s); if(chunk) dprintf(f, "%x\r\n%s\r\n", (int)strlen(c), c); else dprintf(f, "%s", c); } } static int alphasort_q(const void *a, const void *b) { return alphasort((const struct dirent**)a, (const struct dirent**)b); } static void do_dir_list(char *s, int c, int f, char *http) { DIR *d = fdopendir(f); struct dirent *de, **names=0, **tmp; size_t cnt=0, len=0; int chunk = 0; if(!d) { http_error(c, 503, http); close(f); return; } while((errno=0), (de = readdir(d))) { if(de->d_name[0] == '.') continue; if(cnt >= len) { len = 2*len+1; if (len > SIZE_MAX/sizeof *names) break; tmp = realloc(names, len * sizeof *names); if (!tmp) break; names = tmp; } names[cnt] = malloc(de->d_reclen); if (!names[cnt]) break; memcpy(names[cnt++], de, de->d_reclen); } if(errno) { closedir(d); http_error(c, 503, http); if (names) while(cnt-->0) free(names[cnt]); free(names); } qsort(names, cnt, sizeof *names, alphasort_q); if(strcmp(http, "HTTP/1.1") == 0) chunk = 1; dprintf(c, "%s<!DOCTYPE html>\r\n%s", chunk ? "11\r\n" : "", chunk ? "\r\n" : ""); for(size_t i = 0; i < cnt; i++) { dprintf(c, "%s<a href=\"/%s", chunk ? "a\r\n" : "", chunk ? "\r\n" : ""); url_enc(s, c, chunk); dprintf(c, "%s/%s", chunk ? "1\r\n" : "", chunk ? "\r\n" : ""); url_enc(names[i]->d_name, c, chunk); dprintf(c, "%s\">%s", chunk ? "2\r\n" : "", chunk ? "\r\n" : ""); for(size_t j = 0; names[i]->d_name[j]; j++) { switch(names[i]->d_name[j]) { case '<': dprintf(c, "%s<%s", chunk ? "4\r\n" : "", chunk ? "\r\n" : ""); break; case '>': dprintf(c, "%s>%s", chunk ? "4\r\n" : "", chunk ? "\r\n" : ""); break; case '&': dprintf(c, "%s&%s", chunk ? "5\r\n" : "", chunk ? "\r\n" : ""); break; case '"': dprintf(c, "%s"%s", chunk ? "6\r\n" : "", chunk ? "\r\n" : ""); break; case '\'': dprintf(c, "%s'%s", chunk ? "6\r\n" : "", chunk ? "\r\n" : ""); break; default: dprintf(c, "%s%c%s", chunk ? "1\r\n" : "", names[i]->d_name[j], chunk ? "\r\n" : ""); } } dprintf(c, "%s</a><br/>\r\n%s", chunk ? "b\r\n" : "", chunk ? "\r\n" : ""); free(names[i]); } if(chunk) dprintf(c, "0\r\n\r\n"); free(names); closedir(d); }
int main(void) { /* dummy variables to prevent null warnings */ char path[] = ""; char mode[] = ""; char buf[1]; struct stat st; struct statfs stfs; struct statvfs stvfs; fpos_t fpos; off_t off; struct rlimit rlim; glob_t glb; int result; DIR* dir; struct dirent direntry; struct dirent* pdirentry; struct dirent** ppdirentry; const struct dirent *pconstdirentry; dir = 0; result = alphasort(&pconstdirentry, &pconstdirentry); creat(path, 0); fgetpos(0, &fpos); fopen(path, mode); freopen(path, mode, 0); fseeko(0, 0, 0); fsetpos(0, &fpos); fstat(0, &st); fstatat(0, path, &st, 0); fstatfs(0, &stfs); fstatvfs(0, &stvfs); ftello(0); ftruncate(0, 0); ftw(path, ftw_stub, 0); getdirentries(0, buf, 0, &off); getrlimit(0, &rlim); glob(path, 0, glob_stub, &glb); lockf(0, 0, 0); lseek(0, 0, 0); lstat(path, &st); mkostemp(path, 0); mkstemp(path); mmap(buf, 0, 0, 0, 0, 0); nftw(path, nftw_stub, 0, 0); open(path, 0); open(path, 0, 0); openat(0, path, 0); openat(0, path, 0, 0); posix_fadvise(0, 0, 0, 0); posix_fallocate(0, 0, 0); pread(0, buf, 0, 0); pwrite(0, buf, 0, 0); readdir(dir); readdir_r(dir, &direntry, &pdirentry); scandir(path, &ppdirentry, scandir_filter_stub, scandir_compare_stub); sendfile(0, 0, &off, 0); setrlimit(0, &rlim); stat(path, &st); statfs(path, &stfs); statvfs(path, &stvfs); tmpfile(); truncate(path, 0); result = versionsort(&pconstdirentry, &pconstdirentry); return result; }
static int cmpdir(const char* a, const char* b) { struct dirent **namelist1, **namelist2; int n1, n2; int i1 = 0, i2 = 0; n1 = scandir(a, &namelist1, filter, alphasort); if (n1 < 0) perror("scandir"); //printf("%d entries in %s\n", n1, a); n2 = scandir(b, &namelist2, filter, alphasort); if (n2 < 0) perror("scandir"); //printf("%d entries in %s\n", n2, b); while (i1 < n1 || i2 < n2) { int ret; if (i1 >= n1) ret = 1; else if (i2 >= n2) ret = -1; else ret = alphasort((const struct dirent **)namelist1+i1, (const struct dirent **)namelist2+i2); if (ret < 0) { do_delete(namelist1[i1]); free(namelist1[i1]); i1++; } else if (ret > 0) { do_add(namelist2[i2]); free(namelist2[i2]); i2++; } else { char buf1[1024], buf2[1024]; //printf("%s match (recurse)\n", namelist1[i1]->d_name); sprintf(buf1, "%s/%s", a, namelist1[i1]->d_name); sprintf(buf2, "%s/%s", b, namelist2[i2]->d_name); if(namelist1[i1]->d_type != namelist2[i2]->d_type) { fprintf(stderr, "%s types differ, delete then add?\n", namelist1[i1]->d_name); } else if(namelist1[i1]->d_type == DT_LNK) { int len1, len2; len1 = readlink(buf1, buf1, 1024); len2 = readlink(buf2, buf2, 1024); //printf("SYMLINK %d %d\n", len1, len2); if(len1 != len2 || strncmp(buf1, buf2, len1)) { fprintf(stderr, "%s symlink target mismatch\n", namelist1[i1]->d_name); } } else if(namelist1[i1]->d_type == DT_DIR) { int len; len = strlen(prefix); strcat(prefix, "/"); strcat(prefix, namelist1[i1]->d_name); cmpdir(buf1,buf2); prefix[len] = 0; } else { // stat files and compare struct stat sb1, sb2; ret = stat(buf1, &sb1); if(ret != 0) fprintf(stderr, "couldn't stat %s\n", buf1); ret = stat(buf2, &sb2); if(ret != 0) fprintf(stderr, "couldn't stat %s\n", buf2); if(sb1.st_size != sb2.st_size || cmpfiles(buf1, buf2, sb1.st_size)) { do_diff(namelist1[i1], namelist2[i2]); } } free(namelist1[i1]); free(namelist2[i2]); i1++, i2++; } } free(namelist1); free(namelist2); return 0; }
//swp half thumb fastmult vfp edsp neon vfpv3 tls //example: // bool fhasneon = hasFeature("neon"); static int cpu_sort (const struct dirent ** d1, const struct dirent ** d2) { return alphasort(d1,d2); }