util.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * calmwm - the calm window manager
  3. *
  4. * Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. * $OpenBSD: util.c,v 1.17 2015/01/19 14:54:16 okan Exp $
  19. */
  20. #include <sys/types.h>
  21. #include <sys/queue.h>
  22. #include <err.h>
  23. #include <errno.h>
  24. #include <limits.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "calmwm.h"
  30. #define MAXARGLEN 20
  31. void
  32. u_spawn(char *argstr)
  33. {
  34. switch (fork()) {
  35. case 0:
  36. u_exec(argstr);
  37. err(1, "%s", argstr);
  38. break;
  39. case -1:
  40. warn("fork");
  41. default:
  42. break;
  43. }
  44. }
  45. void
  46. u_exec(char *argstr)
  47. {
  48. char *args[MAXARGLEN], **ap = args;
  49. char **end = &args[MAXARGLEN - 1], *tmp;
  50. while (ap < end && (*ap = strsep(&argstr, " \t")) != NULL) {
  51. if (**ap == '\0')
  52. continue;
  53. ap++;
  54. if (argstr != NULL) {
  55. /* deal with quoted strings */
  56. switch(argstr[0]) {
  57. case '"':
  58. case '\'':
  59. if ((tmp = strchr(argstr + 1, argstr[0]))
  60. != NULL) {
  61. *(tmp++) = '\0';
  62. *(ap++) = ++argstr;
  63. argstr = tmp;
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
  71. *ap = NULL;
  72. (void)setsid();
  73. (void)execvp(args[0], args);
  74. }