]> git.ozlabs.org Git - ccan/blob - ccan/noerr/_info.c
Junkcode now works.
[ccan] / ccan / noerr / _info.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * noerr - routines for cleaning up without blatting errno
7  *
8  * It is a good idea to follow the standard C convention of setting errno in
9  * your own helper functions.  Unfortunately, care must be taken in the error
10  * paths as most standard functions can (and do) overwrite errno, even if they
11  * succeed.
12  *
13  * Example:
14  *      #include <sys/types.h>
15  *      #include <sys/stat.h>
16  *      #include <fcntl.h>
17  *
18  *      bool write_string_to_file(const char *file, const char *string)
19  *      {
20  *              int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600);
21  *              if (fd < 0)
22  *                      return false;
23  *              ret = write(fd, string, strlen(string));
24  *              if (ret < 0) {
25  *                      // Preserve errno from write above.
26  *                      close_noerr(fd);
27  *                      unlink_noerr(file);
28  *                      return false;
29  *              }
30  *              if (close(fd) != 0) {
31  *                      // Again, preserve errno.
32  *                      unlink_noerr(file);
33  *                      return false;
34  *              }
35  *              // A short write means out of space.
36  *              if (ret < strlen(string)) {
37  *                      unlink(file);
38  *                      errno = ENOSPC;
39  *                      return false;
40  *              }
41  *              return true;
42  *      }
43  *
44  * Licence: LGPL (2 or any later version)
45  */
46 int main(int argc, char *argv[])
47 {
48         if (argc != 2)
49                 return 1;
50
51         if (strcmp(argv[1], "depends") == 0)
52                 /* Nothing. */
53                 return 0;
54
55         return 1;
56 }