layout
This commit is contained in:
parent
1f38fcdc74
commit
760db962ea
12 changed files with 273 additions and 154 deletions
1
core/CMakeLists.txt
Normal file
1
core/CMakeLists.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
setup_lib(core Core)
|
||||
28
core/directory.c
Normal file
28
core/directory.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <MDE/Core/Directory.h>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
void MDEDirectoryScan(const char* path, void (*call)(const char* name, int dir, int symlink, void* user), void* user) {
|
||||
DIR* dir = opendir(path);
|
||||
if(dir != NULL) {
|
||||
struct dirent* d;
|
||||
while((d = readdir(dir)) != NULL) {
|
||||
char* p;
|
||||
struct stat s;
|
||||
if(strcmp(d->d_name, "..") == 0 || strcmp(d->d_name, ".") == 0) continue;
|
||||
|
||||
p = malloc(strlen(path) + 1 + strlen(d->d_name) + 1);
|
||||
strcpy(p, path);
|
||||
if(path[strlen(path) - 1] != '/') strcat(p, "/");
|
||||
strcat(p, d->d_name);
|
||||
|
||||
if(lstat(p, &s) == 0) {
|
||||
call(p, S_ISDIR(s.st_mode) ? 1 : 0, S_ISLNK(s.st_mode) ? 1 : 0, user);
|
||||
}
|
||||
|
||||
free(p);
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
}
|
||||
21
core/file.c
Normal file
21
core/file.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include <MDE/Core/File.h>
|
||||
|
||||
void MDEFileCopy(const char* src, const char* dst) {
|
||||
char buffer[4096];
|
||||
FILE* in;
|
||||
FILE* out;
|
||||
int r;
|
||||
if((in = fopen(src, "rb")) == NULL) return;
|
||||
if((out = fopen(dst, "wb")) == NULL) {
|
||||
fclose(in);
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
r = fread(buffer, 1, sizeof(buffer), in);
|
||||
fwrite(buffer, 1, r, out);
|
||||
} while(r == sizeof(buffer));
|
||||
|
||||
fclose(in);
|
||||
fclose(out);
|
||||
}
|
||||
9
core/string.c
Normal file
9
core/string.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#include <MDE/Core/String.h>
|
||||
|
||||
char* MDEStringDuplicate(const char* src) {
|
||||
char* s = malloc(strlen(src) + 1);
|
||||
|
||||
strcpy(s, src);
|
||||
|
||||
return s;
|
||||
}
|
||||
39
core/users.c
Normal file
39
core/users.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <MDE/Core/Users.h>
|
||||
#include <MDE/Core/String.h>
|
||||
|
||||
#include <pwd.h>
|
||||
|
||||
void MDEUsersList(void (*call)(const char* name, void* user), void* user) {
|
||||
struct passwd* pwd;
|
||||
|
||||
setpwent();
|
||||
while((pwd = getpwent()) != NULL) {
|
||||
char* dir;
|
||||
char* shell;
|
||||
|
||||
/* this is BAD check. and i cannot do anything about it
|
||||
*
|
||||
* i appreciate the insanity
|
||||
*/
|
||||
|
||||
if(pwd->pw_dir == NULL || pwd->pw_shell == NULL) continue;
|
||||
if(pwd->pw_name[0] == '_') continue;
|
||||
|
||||
if(pwd->pw_uid != 0 && strstr(pwd->pw_dir, "/home/") == NULL) continue;
|
||||
|
||||
shell = strrchr(pwd->pw_shell, '/');
|
||||
if(shell == NULL) break;
|
||||
shell++;
|
||||
|
||||
if(strcmp(shell, "nologin") == 0) continue;
|
||||
|
||||
dir = strrchr(pwd->pw_dir, '/');
|
||||
if(dir == NULL) break;
|
||||
dir++;
|
||||
|
||||
if(strcmp(dir, pwd->pw_name) != 0) continue;
|
||||
|
||||
call(pwd->pw_name, user);
|
||||
}
|
||||
endpwent();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue