stdbuf_cmdline.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @file stdbuf_cmdline.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Builds command line for running a program via stdbuf.
  25. */
  26. #ifndef BADVPN_STDBUF_CMDLINE_H
  27. #define BADVPN_STDBUF_CMDLINE_H
  28. #include <misc/debug.h>
  29. #include <misc/cmdline.h>
  30. #include <misc/concat_strings.h>
  31. #define STDBUF_EXEC "/usr/bin/stdbuf"
  32. /**
  33. * Builds the initial part of command line for calling a program via stdbuf
  34. * with standard output buffering set to line-buffered.
  35. *
  36. * @param out {@link CmdLine} to append the result to. Note than on failure, only
  37. * some part of the cmdline may have been appended.
  38. * @param exec path to the executable
  39. * @return 1 on success, 0 on failure
  40. */
  41. static int build_stdbuf_cmdline (CmdLine *out, const char *exec) WARN_UNUSED;
  42. int build_stdbuf_cmdline (CmdLine *out, const char *exec)
  43. {
  44. if (!CmdLine_AppendMulti(out, 3, STDBUF_EXEC, "-o", "L")) {
  45. goto fail1;
  46. }
  47. if (exec[0] == '/') {
  48. if (!CmdLine_Append(out, exec)) {
  49. goto fail1;
  50. }
  51. } else {
  52. char *real_exec = concat_strings(2, "./", exec);
  53. if (!real_exec) {
  54. goto fail1;
  55. }
  56. int res = CmdLine_Append(out, real_exec);
  57. free(real_exec);
  58. if (!res) {
  59. goto fail1;
  60. }
  61. }
  62. return 1;
  63. fail1:
  64. return 0;
  65. }
  66. #endif