mirror of
https://github.com/Soccera1/init.git
synced 2026-02-04 06:43:40 +01:00
Remove duplicate code
This commit is contained in:
parent
12f9c4274b
commit
0f1dcb7039
2 changed files with 6 additions and 33 deletions
|
|
@ -1 +1,2 @@
|
||||||
#define INIT_DIRECTORY "/home/lily/init/examples"
|
#define INIT_DIRECTORY "/home/lily/init/examples"
|
||||||
|
#define SHELL "/bin/sh"
|
||||||
38
src/main.c
38
src/main.c
|
|
@ -19,38 +19,10 @@ void check_init() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int has_char_before_dot(const char *filename, char c) {
|
void run(int runlevel) {
|
||||||
int found_c = 0;
|
char command[1024];
|
||||||
for (int i = 0; filename[i] != '\0'; i++) {
|
snprintf(command, sizeof(command), "%s %s/%d", SHELL, INIT_DIRECTORY, runlevel);
|
||||||
if (filename[i] == '.') return found_c;
|
system(command);
|
||||||
if (filename[i] == c) {
|
|
||||||
found_c = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void parse_names(char runlevel) {
|
|
||||||
DIR *d;
|
|
||||||
struct dirent *dir;
|
|
||||||
d = opendir(INIT_DIRECTORY);
|
|
||||||
if (d) {
|
|
||||||
while ((dir = readdir(d)) != NULL) {
|
|
||||||
// Use 8 if DT_REG is not defined (standard on most Linux systems)
|
|
||||||
#ifndef DT_REG
|
|
||||||
#define DT_REG 8
|
|
||||||
#endif
|
|
||||||
#ifndef DT_LNK
|
|
||||||
#define DT_LNK 10
|
|
||||||
#endif
|
|
||||||
if (dir->d_type == DT_REG || dir->d_type == DT_LNK) {
|
|
||||||
if (has_char_before_dot(dir->d_name, runlevel)) {
|
|
||||||
printf("File: %s, Number: %c\n", dir->d_name, runlevel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
closedir(d);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
@ -61,6 +33,6 @@ int main(int argc, char* argv[]) {
|
||||||
char runlevel = argv[1][0];
|
char runlevel = argv[1][0];
|
||||||
|
|
||||||
check_init();
|
check_init();
|
||||||
parse_names(runlevel);
|
run;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue