lazybox.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <unistd.h>
  2. #include <dirent.h>
  3. #include <stdarg.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. struct applet {
  8. const char* name;
  9. int (*func)(const char** args);
  10. };
  11. int putchar(int c)
  12. {
  13. write(STDOUT_FILENO, &c, 1);
  14. return c;
  15. }
  16. int printf(const char* fmt, ...)
  17. {
  18. va_list args;
  19. va_start(args, fmt);
  20. char buf[128];
  21. int n = vsnprintf(buf, sizeof(buf), fmt, args);
  22. n = write(STDOUT_FILENO, buf, n);
  23. va_end(args);
  24. return n;
  25. }
  26. int lazybox_version(const char** _)
  27. {
  28. (void)_;
  29. printf("lazybox by greatbridf\n");
  30. return 0;
  31. }
  32. int pwd(const char** _)
  33. {
  34. (void)_;
  35. char buf[256];
  36. if (getcwd(buf, sizeof(buf)) == 0) {
  37. printf("cannot get cwd\n");
  38. return -1;
  39. }
  40. puts(buf);
  41. return 0;
  42. }
  43. int ls(const char** args)
  44. {
  45. const char* path = args[0];
  46. DIR* dir = NULL;
  47. if (path == NULL) {
  48. char buf[256];
  49. if (getcwd(buf, sizeof(buf)) == 0)
  50. return -1;
  51. dir = opendir(buf);
  52. } else {
  53. dir = opendir(args[0]);
  54. }
  55. if (!dir)
  56. return -1;
  57. struct dirent* dp = NULL;
  58. while ((dp = readdir(dir)) != NULL) {
  59. printf("%s ", dp->d_name);
  60. }
  61. printf("\n");
  62. return 0;
  63. }
  64. struct applet applets[] = {
  65. {
  66. "lazybox",
  67. lazybox_version,
  68. },
  69. {
  70. "pwd",
  71. pwd,
  72. },
  73. {
  74. "ls",
  75. ls,
  76. }
  77. };
  78. static inline int tolower(int c)
  79. {
  80. if (c >= 'A' && c <= 'Z')
  81. return c - 'A' + 'a';
  82. return c;
  83. }
  84. int strcmpi(const char* a, const char* b)
  85. {
  86. int ret = 0;
  87. while (*a && *b) {
  88. if (tolower(*a) != tolower(*b)) {
  89. ret = 1;
  90. break;
  91. }
  92. ++a, ++b;
  93. }
  94. if ((*a && !*b) || (*b && !*a)) {
  95. ret = 1;
  96. }
  97. return ret;
  98. }
  99. const char* find_file_name(const char* path)
  100. {
  101. const char* last = path + strlen(path);
  102. for (; last != path; --last) {
  103. if (*last == '/')
  104. break;
  105. }
  106. return last == path ? path : last + 1;
  107. }
  108. int parse_applet(const char* name)
  109. {
  110. if (!name)
  111. return -1;
  112. for (size_t i = 0; i < (sizeof(applets) / sizeof(struct applet)); ++i) {
  113. if (strcmpi(applets[i].name, name) == 0) {
  114. return i;
  115. }
  116. }
  117. return -1;
  118. }
  119. int main(int argc, const char** argv)
  120. {
  121. (void)argc;
  122. int offset = 0;
  123. const char* name = find_file_name(argv[offset++]);
  124. int type = -1;
  125. run:
  126. type = parse_applet(name);
  127. if (type == -1) {
  128. printf("applet not found: %s\n", name);
  129. return -1;
  130. }
  131. if (type == 0 && offset == 1 && argv[1]) {
  132. name = argv[offset++];
  133. goto run;
  134. }
  135. return applets[type].func(argv + offset);
  136. }