]> git.ozlabs.org Git - ccan/blob - ccan/noerr/_info
web: make sure to include symlinks in tarballs
[ccan] / ccan / noerr / _info
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  *      #include <stdbool.h>
18  *      #include <string.h>
19  *      #include <errno.h>
20  *      #include <ccan/noerr/noerr.h>
21  *
22  *      bool write_string_to_file(const char *file, const char *string)
23  *      {
24  *              int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600);
25  *              if (fd < 0)
26  *                      return false;
27  *              ret = write(fd, string, strlen(string));
28  *              if (ret < 0) {
29  *                      // Preserve errno from write above.
30  *                      close_noerr(fd);
31  *                      unlink_noerr(file);
32  *                      return false;
33  *              }
34  *              if (close(fd) != 0) {
35  *                      // Again, preserve errno.
36  *                      unlink_noerr(file);
37  *                      return false;
38  *              }
39  *              // A short write means out of space.
40  *              if (ret < strlen(string)) {
41  *                      unlink(file);
42  *                      errno = ENOSPC;
43  *                      return false;
44  *              }
45  *              return true;
46  *      }
47  *
48  * Licence: LGPL (2 or any later version)
49  * Author: Rusty Russell <rusty@rustcorp.com.au>
50  */
51 int main(int argc, char *argv[])
52 {
53         if (argc != 2)
54                 return 1;
55
56         if (strcmp(argv[1], "depends") == 0)
57                 /* Nothing. */
58                 return 0;
59
60         return 1;
61 }