aux.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "common.h"
  2. /* expand a path relative to some `.' */
  3. extern String *
  4. abspath(char *path, char *dot, String *to)
  5. {
  6. if (*path == '/') {
  7. to = s_append(to, path);
  8. } else {
  9. to = s_append(to, dot);
  10. to = s_append(to, "/");
  11. to = s_append(to, path);
  12. }
  13. return to;
  14. }
  15. /* return a pointer to the base component of a pathname */
  16. extern char *
  17. basename(char *path)
  18. {
  19. char *cp;
  20. cp = strrchr(path, '/');
  21. return cp==0 ? path : cp+1;
  22. }
  23. /* append a sub-expression match onto a String */
  24. extern void
  25. append_match(Resub *subexp, String *sp, int se)
  26. {
  27. char *cp, *ep;
  28. cp = subexp[se].sp;
  29. ep = subexp[se].ep;
  30. for (; cp < ep; cp++)
  31. s_putc(sp, *cp);
  32. s_terminate(sp);
  33. }
  34. /*
  35. * check for shell characters in a String
  36. */
  37. static char *illegalchars = "\r\n";
  38. extern int
  39. shellchars(char *cp)
  40. {
  41. char *sp;
  42. for(sp=illegalchars; *sp; sp++)
  43. if(strchr(cp, *sp))
  44. return 1;
  45. return 0;
  46. }
  47. static char *specialchars = " ()<>{};=\\'\`^&|";
  48. static char *escape = "%%";
  49. int
  50. hexchar(int x)
  51. {
  52. x &= 0xf;
  53. if(x < 10)
  54. return '0' + x;
  55. else
  56. return 'A' + x - 10;
  57. }
  58. /*
  59. * rewrite a string to escape shell characters
  60. */
  61. extern String*
  62. escapespecial(String *s)
  63. {
  64. String *ns;
  65. char *sp;
  66. for(sp = specialchars; *sp; sp++)
  67. if(strchr(s_to_c(s), *sp))
  68. break;
  69. if(*sp == 0)
  70. return s;
  71. ns = s_new();
  72. for(sp = s_to_c(s); *sp; sp++){
  73. if(strchr(specialchars, *sp)){
  74. s_append(ns, escape);
  75. s_putc(ns, hexchar(*sp>>4));
  76. s_putc(ns, hexchar(*sp));
  77. } else
  78. s_putc(ns, *sp);
  79. }
  80. s_terminate(ns);
  81. s_free(s);
  82. return ns;
  83. }
  84. int
  85. hex2uint(char x)
  86. {
  87. if(x >= '0' && x <= '9')
  88. return x - '0';
  89. if(x >= 'A' && x <= 'F')
  90. return (x - 'A') + 10;
  91. if(x >= 'a' && x <= 'f')
  92. return (x - 'a') + 10;
  93. return -512;
  94. }
  95. /*
  96. * rewrite a string to remove shell characters escapes
  97. */
  98. extern String*
  99. unescapespecial(String *s)
  100. {
  101. int c;
  102. String *ns;
  103. char *sp;
  104. uint n;
  105. if(strstr(s_to_c(s), escape) == 0)
  106. return s;
  107. n = strlen(escape);
  108. ns = s_new();
  109. for(sp = s_to_c(s); *sp; sp++){
  110. if(strncmp(sp, escape, n) == 0){
  111. c = (hex2uint(sp[n])<<4) | hex2uint(sp[n+1]);
  112. if(c < 0)
  113. s_putc(ns, *sp);
  114. else {
  115. s_putc(ns, c);
  116. sp += n+2-1;
  117. }
  118. } else
  119. s_putc(ns, *sp);
  120. }
  121. s_terminate(ns);
  122. s_free(s);
  123. return ns;
  124. }
  125. int
  126. returnable(char *path)
  127. {
  128. return strcmp(path, "/dev/null") != 0;
  129. }