create-read.c 740 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. int main(int argc, char **argv)
  12. {
  13. const char *path;
  14. char buf[4];
  15. int fd, rc;
  16. if (argc < 2) {
  17. fprintf(stderr, "usage: %s <path>\n", argv[0]);
  18. return EXIT_FAILURE;
  19. }
  20. path = argv[1];
  21. /* create a test variable */
  22. fd = open(path, O_RDWR | O_CREAT, 0600);
  23. if (fd < 0) {
  24. perror("open(O_WRONLY)");
  25. return EXIT_FAILURE;
  26. }
  27. rc = read(fd, buf, sizeof(buf));
  28. if (rc != 0) {
  29. fprintf(stderr, "Reading a new var should return EOF\n");
  30. close(fd);
  31. return EXIT_FAILURE;
  32. }
  33. close(fd);
  34. return EXIT_SUCCESS;
  35. }