-
Notifications
You must be signed in to change notification settings - Fork 0
/
namedpipe.h
192 lines (165 loc) · 3.55 KB
/
namedpipe.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// function declarations
void create_pipe(char *file_name);
void read_pipe(char *file_name);
void write_pipe(char *file_name);
void info_pipe(char *file_name);
// driver code
void named_pipe(char *file_name, char *mode)
{
if (strcmp(mode, "create") == 0)
{
create_pipe(file_name);
}
else if (strcmp(mode, "write") == 0)
{
write_pipe(file_name);
}
else if (strcmp(mode, "read") == 0)
{
read_pipe(file_name);
}
else if (strcmp(mode, "info") == 0)
{
printf("\nInfo of the pipe:");
info_pipe(file_name);
}
else
{
printf("Wrong Mode Selected");
}
}
// function definitions
void create_pipe(char *file_name)
{
int fd;
int access_check = access(file_name, F_OK);
if (access_check == 0)
{
printf("\nPipe with the name: %s already exists. \nPlease try a different name or use this one in future.", file_name);
return;
}
int mk = mknod(file_name, 010777, 0);
if (mk < 0)
{
printf("\nError in creating the pipe %s.", file_name);
return;
}
else
{
printf("\nPipe %s created successfully!!!", file_name);
}
}
void write_pipe(char *file_name)
{
int fd;
int access_check = access(file_name, F_OK);
if (access_check != 0)
{
printf("\nPipe %s doesn't exists, please create one before.", file_name);
return;
}
access_check = access(file_name, W_OK);
if (access_check != 0)
{
printf("\nPipe %s doesn't have write access.", file_name);
return;
}
fd = open(file_name, O_WRONLY);
if (fd < 0)
{
printf("\nError opening the pipe %s in write mode", file_name);
return;
}
int nw;
printf("\nEnter the contents to be written in the pipe (Press # to stop writing)\n");
char c;
scanf("%c", &c);
while (c != '#')
{
nw = write(fd, &c, 1);
scanf("%c", &c);
if (nw <= 0)
{
printf("\nError! While writing the pipe");
close(fd);
return;
}
}
close(fd);
}
void read_pipe(char *file_name)
{
int fd;
int access_check = access(file_name, F_OK);
if (access_check != 0)
{
printf("\nPipe %s doesn't exists, please create one before", file_name);
return;
}
access_check = access(file_name, R_OK);
if (access_check != 0)
{
printf("\nPipe %s doesn't have read access", file_name);
return;
}
fd = open(file_name, O_RDONLY);
if (fd < 0)
{
printf("\nError opening the pipe %s in read mode.", file_name);
return;
}
char c;
int nr;
printf("\nContents of the pipe:\n");
do
{
nr = read(fd, &c, 1);
printf("%c", c);
if (nr < 0)
{
printf("\nPipe either ended or encountered an error.");
close(fd);
return;
}
} while (nr > 0);
close(fd);
}
void info_pipe(char *file_name)
{
struct stat stats;
struct tm dt;
if (stat(file_name, &stats) == 0)
{
// File permissions
printf("\nFile access: ");
if (stats.st_mode & R_OK)
printf("read ");
if (stats.st_mode & W_OK)
printf("write ");
if (stats.st_mode & X_OK)
printf("execute");
// File size
printf("\nFile size: %ld", stats.st_size);
// Get file creation time in seconds and
// convert seconds to date and time format
dt = *(gmtime(&stats.st_ctime));
printf("\nCreated on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon, dt.tm_year + 1900,
dt.tm_hour, dt.tm_min, dt.tm_sec);
// File modification time
dt = *(gmtime(&stats.st_mtime));
printf("\nModified on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon, dt.tm_year + 1900,
dt.tm_hour, dt.tm_min, dt.tm_sec);
}
else
{
printf("Unable to get file properties.\n");
printf("Please check whether '%s' file exists.\n", file_name);
}
}