aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2020-03-28 10:38:18 +0100
committerCamil Staps2020-03-28 10:38:18 +0100
commitc44bc1445731fafc73a6cb42d9d3d9afd8b276bf (patch)
tree166a0b6ee2eccdec606c4d5b1d4ba39d184c212f
parentFix #1 (in some cases string garbage was read for file names) (diff)
Code styleHEADmaster
-rw-r--r--Inotify.icl17
-rw-r--r--inotify_c.c92
2 files changed, 50 insertions, 59 deletions
diff --git a/Inotify.icl b/Inotify.icl
index fcd1d60..c07377d 100644
--- a/Inotify.icl
+++ b/Inotify.icl
@@ -92,29 +92,32 @@ where
check :: [(Int,Int,String)] (INWatch, INCallback st) *(*Inotify st, *World)
-> *(*Inotify st, *World)
check infos (watch,f) (st,w)
- # (st,w) = seq [\(st,w) -> f mask (toMaybe name) st w
- \\ (wd,mask,name) <- infos | wd == watch] (st,w)
+ # (st,w) = seq
+ [ \(st,w) -> f mask (toMaybe name) st w
+ \\ (wd,mask,name) <- infos
+ | wd == watch
+ ]
+ (st,w)
= (st,w)
where
toMaybe :: String -> Maybe String
- toMaybe s = if (s=="") Nothing (Just s)
+ toMaybe s = if (size s==0) Nothing (Just s)
bytesToInt :: {#Char} -> Int
bytesToInt cs = sum [toInt c << p \\ c <-: cs & p <- [0,8..]]
split :: Int String -> [String]
split n s
- | size s > n = [s % (0,n-1) : split n (s % (n, size s - 1))]
+ | size s > n = [s % (0,n-1) : split n (s % (n, size s - 1))]
| size s == n = [s]
- | s == "" = []
+ | size s == 0 = []
splitOn :: Char String -> [String]
splitOn c s = map toString (split` c [c \\ c <-: s])
where
split` :: Char [Char] -> [[Char]]
split` c [] = []
- split` c cs=:[x:xs]
- = let (l,r) = span ((<>)c) cs in [l:split` c (removeMember c r)]
+ split` c cs=:[x:xs] = [l:split` c r] where (l,[_:r]) = span ((<>)c) cs
c_check :: !Int -> (!Bool, !String, !String, !String, !Int)
c_check fd = code {
diff --git a/inotify_c.c b/inotify_c.c
index 28432f2..5a6987b 100644
--- a/inotify_c.c
+++ b/inotify_c.c
@@ -1,17 +1,11 @@
#include <errno.h>
-#include <fcntl.h>
#include <poll.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>
#include "Clean.h"
-/** The empty string, as a CleanString */
-static struct {int length; char chars[1]; } empty_string = {0,""};
-
/**
* Poll an inotify file descriptor
*
@@ -20,19 +14,20 @@ static struct {int length; char chars[1]; } empty_string = {0,""};
* re_nrevents Will be set to the number of polled events
* re_fd Will be set to fd (needed for uniqueness)
*/
-void clean_poll(int fd, int timeout, int *re_nrevents, int *re_fd) {
- struct pollfd pfd = {fd, POLLIN, 0};
- *re_nrevents = poll(&pfd, 1, timeout);
- *re_fd = fd;
+void clean_poll (int fd,int timeout,int *re_nrevents,int *re_fd)
+{
+ struct pollfd pfd={fd,POLLIN,0};
+ *re_nrevents=poll (&pfd,1,timeout);
+ *re_fd=fd;
}
/**
* CleanStrings that are returned from clean_inotify_check (so that we don't
* have to malloc all the time.)
*/
-static CleanStringVariable(wds_string, 1024);
-static CleanStringVariable(masks_string, 1024);
-static CleanStringVariable(names_string, 4096);
+static CleanStringVariable (wds_string,1024);
+static CleanStringVariable (masks_string,1024);
+static CleanStringVariable (names_string,4096);
/**
* Check for events on an inotify file descriptor.
@@ -49,58 +44,55 @@ static CleanStringVariable(names_string, 4096);
* at a time and casting that to an int. The string array can be read by
* splitting on \0 (since they are filenames, \0 cannot occur).
*/
-void clean_inotify_check(int fd,
- int *re_ok, CleanString* re_wds, CleanString* re_masks,
- CleanString* re_fnames, int *re_fd) {
+void clean_inotify_check (int fd,
+ int *re_ok,CleanString *re_wds,CleanString *re_masks,
+ CleanString *re_fnames, int *re_fd)
+{
char buf[4096] __attribute__((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *ev;
ssize_t len;
char *ptr;
- struct pollfd pfd = {fd, POLLIN, 0};
+ struct pollfd pfd={fd,POLLIN,0};
int poll_n;
- char *wds_ptr = CleanStringCharacters(wds_string);
- char *masks_ptr = CleanStringCharacters(masks_string);
- char *names_ptr = CleanStringCharacters(names_string);
- CleanStringLength(wds_string) = 0;
- CleanStringLength(masks_string) = 0;
- CleanStringLength(names_string) = 0;
+ char *wds_ptr=CleanStringCharacters (wds_string);
+ char *masks_ptr=CleanStringCharacters (masks_string);
+ char *names_ptr=CleanStringCharacters (names_string);
+ CleanStringLength (wds_string)=0;
+ CleanStringLength (masks_string)=0;
+ CleanStringLength (names_string)=0;
- *re_ok = 0;
- *re_fd = fd;
+ *re_ok=0;
+ *re_fd=fd;
- *re_wds = (CleanString) &empty_string;
- *re_masks = (CleanString) &empty_string;
- *re_fnames = (CleanString) &empty_string;
+ *re_wds=(CleanString) wds_string;
+ *re_masks=(CleanString) masks_string;
+ *re_fnames=(CleanString) names_string;
- for (;;) {
- poll_n = poll(&pfd, 1, 0);
- if (poll_n < 0) {
+ for (;;){
+ poll_n=poll (&pfd,1,0);
+ if (poll_n<0)
return;
- } else if (poll_n == 0) {
+ if (poll_n==0)
break;
- }
- len = read(fd, buf, sizeof buf);
- if (len == -1 && errno != EAGAIN) {
+ len=read (fd,buf,sizeof (buf));
+ if (len==-1 && errno!=EAGAIN)
return;
- }
- if (len <= 0) {
+ if (len<=0)
break;
- }
- for (ptr = buf; ptr < buf + len;
- ptr += sizeof(struct inotify_event) + ev->len) {
- ev = (const struct inotify_event*) ptr;
+ for (ptr=buf; ptr<buf+len; ptr+=sizeof (struct inotify_event)+ev->len){
+ ev=(const struct inotify_event*) ptr;
- memcpy(masks_ptr, &ev->mask, 4);
- masks_ptr += 4;
- CleanStringLength(masks_string) += 4;
+ memcpy (masks_ptr,&ev->mask,4);
+ masks_ptr+=4;
+ CleanStringLength (masks_string)+=4;
- memcpy(wds_ptr, &ev->wd, sizeof(int));
- wds_ptr += sizeof(int);
- CleanStringLength(wds_string) += sizeof(int);
+ memcpy (wds_ptr,&ev->wd,sizeof (int));
+ wds_ptr+=sizeof (int);
+ CleanStringLength (wds_string)+=sizeof (int);
memcpy (names_ptr,&ev->name,ev->len);
names_ptr+=ev->len+1;
@@ -109,9 +101,5 @@ void clean_inotify_check(int fd,
}
}
- *re_wds = (CleanString) wds_string;
- *re_masks = (CleanString) masks_string;
- *re_fnames = (CleanString) names_string;
-
- *re_ok = 1;
+ *re_ok=1;
}