Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eef40ed

Browse files
committed
Add function to check what file is opened
Signed-off-by: Seungha Son <linuxias@gmail.com>
1 parent 0ff2c4d commit eef40ed

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

‎file/s_file.c‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ SFile *s_file_create(const char *path, int flag, int mode)
5151
}
5252

5353
file->sb = sb;
54-
55-
file->fd = open(path, flag, mode);
56-
if (file->fd == -1) {
57-
s_file_Destroy(file);
58-
return NULL;
59-
}
54+
file->status = S_FILE_STATUS_CLOSE;
6055

6156
return file;
6257
}
@@ -74,6 +69,28 @@ void s_file_destroy(SFile *file)
7469
file = NULL;
7570
}
7671

72+
s_file_is_open(SFile *file)
73+
{
74+
return file->status == S_FILE_STATUS_OPEN ? TRUE : FALSE;
75+
}
76+
77+
int s_file_is_open(SFile *file)
78+
{
79+
if S_UNLIKELY (file == NULL)
80+
return -1;
81+
82+
if (file->status == S_FILE_STATUS_OPEN)
83+
return -1;
84+
85+
file->fd = open(path, flag, mode);
86+
if (file->fd == -1)
87+
return -1;
88+
89+
file->status = S_FILE_STATUS_OPEN;
90+
91+
return 0;
92+
}
93+
7794
int s_file_is_absolute_path(const char *path)
7895
{
7996
if S_UNLIEKLY (path == NULL)

‎file/s_file.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@
33

44
#include <sys/stat.h>
55

6+
#ifndef TRUE
7+
#define TRUE 1
8+
#endif
9+
10+
#ifndef FALSE
11+
#define FASLE 0
12+
#endif
13+
614
typedef struct _SFile SFile;
715

16+
typedef enum _s_file_status s_file_status;
17+
18+
enum _s_file_status {
19+
S_FILE_STATUS_CLOSE
20+
S_FILE_STATUS_OPEN,
21+
};
22+
823
struct _SFile {
924
char *path;
1025
int fd;
26+
s_file_status status;
1127
struct stat sb;
1228
};
1329

@@ -17,6 +33,6 @@ void s_file_destroy(SFile *file);
1733

1834
int s_file_is_absolute_path(const char *path);
1935

20-
36+
ints_file_is_open(SFile*file);
2137

2238
#endif /* __S_FILE_H__ */

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /