wall.c 829 B

123456789101112131415161718192021222324252627282930313233343536
  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. #include "libbb.h"
  9. #include <utmp.h>
  10. int wall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  11. int wall_main(int argc UNUSED_PARAM, char **argv)
  12. {
  13. struct utmp *ut;
  14. char *msg;
  15. int fd = argv[1] ? xopen(argv[1], O_RDONLY) : STDIN_FILENO;
  16. msg = xmalloc_read(fd, NULL);
  17. if (ENABLE_FEATURE_CLEAN_UP && argv[1])
  18. close(fd);
  19. setutent();
  20. while ((ut = getutent()) != NULL) {
  21. char *line;
  22. if (ut->ut_type != USER_PROCESS)
  23. continue;
  24. line = concat_path_file("/dev", ut->ut_line);
  25. xopen_xwrite_close(line, msg);
  26. free(line);
  27. }
  28. if (ENABLE_FEATURE_CLEAN_UP) {
  29. endutent();
  30. free(msg);
  31. }
  32. return EXIT_SUCCESS;
  33. }