logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git

memsys.c (760B)


  1. // Collection of Unix tools, comparable to coreutils
  2. // SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
  4. #define _POSIX_C_SOURCE 200809L
  5. #include <fcntl.h> /* open() */
  6. #include <stdio.h> /* perror() */
  7. #include <unistd.h> /* close(), write() */
  8. #define SYS_POWER_STATE "/sys/power/state"
  9. int
  10. main(void)
  11. {
  12. int fd, err = 0;
  13. char *entry = "mem";
  14. size_t entry_size = 3;
  15. fd = open(SYS_POWER_STATE, O_WRONLY);
  16. if(fd == -1)
  17. {
  18. perror("memsys: open(\"" SYS_POWER_STATE "\")");
  19. err++;
  20. }
  21. else
  22. {
  23. if(write(fd, entry, entry_size) < (ssize_t)entry_size)
  24. {
  25. perror("memsys: write(fd, \"mem\", 3)");
  26. err++;
  27. }
  28. close(fd);
  29. }
  30. return err;
  31. }