3
0

wall.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * wall - write a message to all logged-in users
  4. * Copyright (c) 2009 Bernhard Reutner-Fischer
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //usage:#define wall_trivial_usage
  9. //usage: "[FILE]"
  10. //usage:#define wall_full_usage "\n\n"
  11. //usage: "Write content of FILE or stdin to all logged-in users"
  12. //usage:
  13. //usage:#define wall_sample_usage
  14. //usage: "echo foo | wall\n"
  15. //usage: "wall ./mymessage"
  16. #include "libbb.h"
  17. int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18. int wall_main(int argc UNUSED_PARAM, char **argv)
  19. {
  20. struct utmp *ut;
  21. char *msg;
  22. int fd = argv[1] ? xopen(argv[1], O_RDONLY) : STDIN_FILENO;
  23. msg = xmalloc_read(fd, NULL);
  24. if (ENABLE_FEATURE_CLEAN_UP && argv[1])
  25. close(fd);
  26. setutent();
  27. while ((ut = getutent()) != NULL) {
  28. char *line;
  29. if (ut->ut_type != USER_PROCESS)
  30. continue;
  31. line = concat_path_file("/dev", ut->ut_line);
  32. xopen_xwrite_close(line, msg);
  33. free(line);
  34. }
  35. if (ENABLE_FEATURE_CLEAN_UP) {
  36. endutent();
  37. free(msg);
  38. }
  39. return EXIT_SUCCESS;
  40. }