summaryrefslogtreecommitdiff
path: root/ccan/noerr/_info
blob: 082c71e601e451cf77ef022555d25d102f054de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
 * noerr - routines for cleaning up without blatting errno
 *
 * It is a good idea to follow the standard C convention of setting errno in
 * your own helper functions.  Unfortunately, care must be taken in the error
 * paths as most standard functions can (and do) overwrite errno, even if they
 * succeed.
 *
 * Example:
 *	#include <sys/types.h>
 *	#include <sys/stat.h>
 *	#include <fcntl.h>
 *	#include <stdbool.h>
 *	#include <string.h>
 *	#include <errno.h>
 *	#include <ccan/noerr/noerr.h>
 *
 *	static bool write_string_to_file(const char *file, const char *string)
 *	{
 *		int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600);
 *		if (fd < 0)
 *			return false;
 *		ret = write(fd, string, strlen(string));
 *		if (ret < 0) {
 *			// Preserve errno from write above.
 *			close_noerr(fd);
 *			unlink_noerr(file);
 *			return false;
 *		}
 *		if (close(fd) != 0) {
 *			// Again, preserve errno.
 *			unlink_noerr(file);
 *			return false;
 *		}
 *		// A short write means out of space.
 *		if (ret < strlen(string)) {
 *			unlink(file);
 *			errno = ENOSPC;
 *			return false;
 *		}
 *		return true;
 *	}
 *
 * License: CC0 (Public domain)
 * Author: Rusty Russell <rusty@rustcorp.com.au>
 */
int main(int argc, char *argv[])
{
	if (argc != 2)
		return 1;

	if (strcmp(argv[1], "depends") == 0)
		/* Nothing. */
		return 0;

	return 1;
}