-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpidfile.c
39 lines (31 loc) · 804 Bytes
/
pidfile.c
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
// pidfile.c make a file with the pid in the name
//
// $ gcc -o pidfile pidfile.c
//
// $ ./pidfile
///////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <libgen.h>
#include <string.h>
int main(int argc, char *argv[])
{
pid_t pid;
char pids[20];
FILE *fp = NULL;
char *path;
path = malloc(sizeof(argv[0]+1));
strcpy(path, argv[0]);
//printf("path: %s\n", path);
printf("basename(path): %s\n", basename(path));
pid = getpid();
sprintf(pids, "/tmp/%s.%d", basename(path), pid);
if ((fp = fopen(pids, "w+" )) != NULL) {
fprintf(fp, "hello pidfile\n");
printf("file created\n");
}
if (fp)
fclose(fp);
return EXIT_SUCCESS;
}