aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 11:39:12 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 18:16:19 +0100
commit4e8b2473bd036561105fa3fc68068b6812e08ba9 (patch)
tree2b5d78b4f9f50ec4f1e8968b607c1c2b70de4b4b
parentImprove ERR. (diff)
downloadcalp-4e8b2473bd036561105fa3fc68068b6812e08ba9.tar.gz
calp-4e8b2473bd036561105fa3fc68068b6812e08ba9.tar.xz
Add single file mode.
-rw-r--r--calendar.c60
-rw-r--r--main.c2
2 files changed, 58 insertions, 4 deletions
diff --git a/calendar.c b/calendar.c
index 5ac47f58..8fc4199b 100644
--- a/calendar.c
+++ b/calendar.c
@@ -10,6 +10,7 @@
#include <string.h>
#include <stdio.h>
#include <errno.h>
+#include <unistd.h>
#include "macro.h"
#include "parse.h"
@@ -36,8 +37,11 @@ int get_extension(const char* filename, char* ext, ssize_t max_len) {
: ext_idx;
}
-int read_vcalendar(vcalendar* cal, char* path) {
+/*
+ * TODO merge the code for files and dirs.
+ */
+int parse_dir(vcalendar* cal, char* path) {
DIR* dir = opendir(path);
struct dirent* d;
while ((d = readdir(dir)) != NULL) {
@@ -70,11 +74,61 @@ int read_vcalendar(vcalendar* cal, char* path) {
parse_file(fname, f, cal);
fclose(f);
-
- ;
}
closedir(dir);
return 0;
}
+
+int read_vcalendar(vcalendar* cal, char* path) {
+
+ struct stat statbuf;
+ if (stat (path, &statbuf) != 0) {
+ fprintf(stderr,
+ "Error opening file or directory, errno = %i\npath = [%s]\n",
+ errno, path);
+ }
+
+ int type = statbuf.st_mode & 0777000;
+ int chmod = statbuf.st_mode & 0777;
+ printf("file has mode 0%o, with chmod = 0%o\n", type, chmod);
+
+ switch (type) {
+ case S_IFREG:
+ puts("Parsing a single file");
+
+ char ext[10];
+ int has_ext = get_extension(path, ext, 9);
+ if (! has_ext || strcmp(ext, "ics") != 0) {
+ fprintf(stderr, "File doesn't have .ics extension.\n");
+ exit(1);
+ }
+
+ FILE* f = fopen(path, "r");
+ if (f == NULL) {
+ fprintf(stderr, "Error opening file [%s], errno = %i\n",
+ path, errno);
+ exit (1);
+ }
+
+ parse_file(path, f, cal);
+ fclose(f);
+ break;
+
+ case S_IFDIR:
+ puts("Parsing a directory");
+ parse_dir (cal, path);
+ break;
+
+ case S_IFLNK:
+ fputs("Found symlink, can't be bothered to check it further.", stderr);
+ break;
+
+ default:
+ ;
+ }
+
+
+ return 0;
+}
diff --git a/main.c b/main.c
index c9fa7fd8..a669f290 100644
--- a/main.c
+++ b/main.c
@@ -24,7 +24,7 @@ int main (int argc, char* argv[argc]) {
arg args = { .argc = argc, .argv = argv };
if (arg_shift(&args) == 0) {
- puts("Please give vdir as first argument");
+ puts("Please give vdir or a vcalendar file as first argument");
exit (1);
}