-
Notifications
You must be signed in to change notification settings - Fork 1
/
procdir.c
97 lines (80 loc) · 2.45 KB
/
procdir.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright (C) 2019-2021 Torge Matthies */
/*
* This file is part of osu-handler-wine.
*
* osu-handler-wine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* osu-handler-wine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Author contact info:
* E-Mail address: openglfreak@googlemail.com
* GPG key fingerprint: 0535 3830 2F11 C888 9032 FAD2 7C95 CD70 C9E8 438D
*/
#define _POSIX_C_SOURCE 200809L /* dirfd */
#define _DEFAULT_SOURCE /* DT_DIR, DT_UNKNOWN, dirfd */
#include "is_number.h" /* is_number */
#include <dirent.h> /* DIR, DT_DIR, DT_UNKNOWN, _DIRENT_HAVE_D_TYPE, closedir,
struct dirent, dirfd, opendir, readdir */
#include <errno.h> /* ENOMEM, errno */
#include <stdlib.h> /* free, malloc */
#ifndef PROCFS_PATH
#define PROCFS_PATH "/proc"
#endif
typedef struct procdir_struct {
DIR* dirptr;
int dirfd;
} procdir_struct;
int open_procdir(procdir_struct** const out_pdhandle)
{
procdir_struct* p;
p = (procdir_struct*)malloc(sizeof(procdir_struct));
if (!p)
return ENOMEM;
p->dirptr = opendir(PROCFS_PATH);
p->dirfd = -1;
if (!p->dirptr)
{
free(p);
return errno;
}
*out_pdhandle = p;
return 0;
}
int procdir_dirfd(procdir_struct* const p)
{
if (p->dirfd == -1)
p->dirfd = dirfd(p->dirptr);
return p->dirfd;
}
#ifdef _DIRENT_HAVE_D_TYPE
#define may_be_dir(dent) \
((dent)->d_type == DT_DIR || (dent)->d_type == DT_UNKNOWN)
#define is_process_dir(dent) (may_be_dir((dent)) && is_number((dent)->d_name))
#else
#define is_process_dir(dent) (is_number((dent)->d_name))
#endif
int procdir_next_process(procdir_struct* const p, struct dirent** const out_dent)
{
struct dirent* dent;
errno = 0;
do {
dent = readdir(p->dirptr);
} while (errno == 0 && dent && !is_process_dir(dent));
*out_dent = dent;
return errno;
}
void close_procdir(procdir_struct* const p)
{
closedir(p->dirptr);
free(p);
}