1#include <fcntl.h>
2#include <libgen.h>
3#include <stdbool.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/errno.h>
9#include <sys/param.h>
10#include <sys/stat.h>
11#include <unistd.h>
12
13int get_return_value();
14int get_return_value2();
15
16// Create \a file_name with the c-string of our
17// pid in it. Initially open & write the contents
18// to a temporary file, then move it to the actual
19// filename once writing is completed.
20bool writePid(const char *file_name, const pid_t pid) {
21 char *tmp_file_name = (char *)malloc(size: strlen(s: file_name) + 16);
22 strcpy(dest: tmp_file_name, src: file_name);
23 strcat(dest: tmp_file_name, src: "_tmp");
24 int fd = open(file: tmp_file_name, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
25 if (fd == -1) {
26 fprintf(stderr, format: "open(%s) failed: %s\n", tmp_file_name, strerror(errno));
27 free(ptr: tmp_file_name);
28 return false;
29 }
30 char buffer[64];
31 snprintf(s: buffer, maxlen: sizeof(buffer), format: "%ld", (long)pid);
32 bool res = true;
33 if (write(fd: fd, buf: buffer, n: strlen(s: buffer)) == -1) {
34 fprintf(stderr, format: "write(%s) failed: %s\n", buffer, strerror(errno));
35 res = false;
36 }
37 close(fd: fd);
38
39 if (rename(old: tmp_file_name, new: file_name) == -1) {
40 fprintf(stderr, format: "rename(%s, %s) failed: %s\n", tmp_file_name, file_name,
41 strerror(errno));
42 res = false;
43 }
44 free(ptr: tmp_file_name);
45
46 return res;
47}
48
49int main(int argc, char **argv) {
50 if (writePid(file_name: argv[1], pid: getpid())) {
51 // we've signaled lldb we are ready to be attached to,
52 // this sleep() call will be interrupted when lldb
53 // attaches.
54 sleep(seconds: 200);
55 } else {
56 printf(format: "Error writing pid to '%s', exiting.\n", argv[1]);
57 exit(status: 3);
58 }
59
60 int retval = get_return_value();
61 return retval + get_return_value2();
62}
63

source code of lldb/test/API/macosx/no-nlist-memory-module/main.c