test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. /* For readability reasons, define true, false */
  14. int true = 0;
  15. int false = 1;
  16. int main(int argc, char *argv[]) {
  17. char param[256];
  18. struct stat file_status;
  19. for (int i = 0; i < 256; i++) param[i] = 0;
  20. if (!strcmp(argv[0], "[")) {
  21. if (strcmp(argv[argc - 1], "]")) {
  22. /* FIXME: Is printing an error message POSIX-compliant? */
  23. fprintf(stderr, "[: No matching ]\n");
  24. return 1;
  25. }
  26. argc--;
  27. argv[argc] = 0;
  28. }
  29. if (argc < 2) return false;
  30. if (!strcmp(argv[1], "!")) {
  31. true = 1;
  32. false = 0;
  33. argc--;
  34. argv++;
  35. }
  36. if (argc == 3) {
  37. param[(uint8_t)argv[1][1]] = argv[1][1];
  38. /* Files */
  39. lstat(argv[2], &file_status);
  40. if (!errno) {
  41. if (param['b'] && S_ISBLK(file_status.st_mode) != 0) return true;
  42. else if (param['c'] && S_ISCHR(file_status.st_mode) != 0) return true;
  43. else if (param['d'] && S_ISDIR(file_status.st_mode) != 0) return true;
  44. /* Just return true, if -e is used. If it was false, stat() would
  45. * have failed. If it failed, there's a check about which value
  46. * to return. Return false if -e is used, otherwise, return errno
  47. */
  48. else if (param['e']) return true;
  49. else if (param['f'] && S_ISREG(file_status.st_mode) != 0) return true;
  50. else if (param['g'] && file_status.st_mode & S_ISGID) return true;
  51. else if ((param['h'] || param['L'])
  52. && S_ISLNK(file_status.st_mode) != 0) return true;
  53. else if (param['p'] && S_ISFIFO(file_status.st_mode) != 0) return true;
  54. /* TODO: Better check for -r/-w. */
  55. else if (param['r'] && (file_status.st_mode & S_IRUSR
  56. || file_status.st_mode & S_IROTH)) return true;
  57. else if (param['S'] && S_ISSOCK(file_status.st_mode) != 0) return true;
  58. else if (param['s'] && file_status.st_size > 0) return true;
  59. else if (param['u'] && S_ISDIR(file_status.st_mode)
  60. && file_status.st_mode & S_ISUID) return true;
  61. else if (param['w'] && (file_status.st_mode & S_IWUSR
  62. || file_status.st_mode & S_IWOTH)) return true;
  63. else if (param['x'] && (file_status.st_mode & S_IXUSR
  64. || file_status.st_mode & S_IXOTH)) return true;
  65. }
  66. else if (errno && param['e']) return false;
  67. if (param['t']) {
  68. if (isatty(strtol(argv[2], NULL, 10))) return true;
  69. else return false;
  70. }
  71. /* Strings */
  72. if (param['n'] && strlen(argv[2])) return true;
  73. else if (param['z'] && !strlen(argv[2])) return true;
  74. }
  75. else if (argc == 4) {
  76. if (!strcmp(argv[2], "=") && !strcmp(argv[1], argv[3])) return true;
  77. else if (!strcmp(argv[2], "!=") && strcmp(argv[1], argv[3])) return true;
  78. else if (!strcmp(argv[2], "-eq") && !strcmp(argv[1], argv[3])) return true;
  79. else if (!strcmp(argv[2], "-ne") && strcmp(argv[1], argv[3])) return true;
  80. else if (!strcmp(argv[2], "-gt") &&
  81. strtol(argv[1], NULL, 10) > strtol(argv[3], NULL, 10)) return true;
  82. else if (!strcmp(argv[2], "-ge") &&
  83. strtol(argv[1], NULL, 10)>= strtol(argv[3], NULL, 10)) return true;
  84. else if (!strcmp(argv[2], "-lt") &&
  85. strtol(argv[1], NULL, 10) < strtol(argv[3], NULL, 10)) return true;
  86. else if (!strcmp(argv[2], "-le") &&
  87. strtol(argv[1], NULL, 10)<= strtol(argv[3], NULL, 10)) return true;
  88. }
  89. /* TODO: Better error handling. */
  90. return false;
  91. }