lazybox.c 2.8 KB

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