summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortsxv478 <atci39@rambler.ru>2024-06-19 06:28:46 +0300
committertsxv478 <atci39@rambler.ru>2024-06-19 06:28:46 +0300
commit0a1c24d45a2df5ffa70f38fe177aa7264eb2dfe7 (patch)
tree2cc6d494d23d43f7c2e32a60e6439355fab48a54
parentb861d3433dc56fe1d89594d8ccb3d13009de5778 (diff)
Code
-rw-r--r--Makefile33
-rw-r--r--config.h4
-rw-r--r--note.c152
3 files changed, 189 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..fb1fe46
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,33 @@
+VERSION = 0.1
+PREFIX = /usr/local
+CC = cc
+CPPFLAGS = -D_DEFAULT_SOURCE
+CFLAGS = --std=c99 -pedantic -Wall -Os
+LDFLAGS = -s -static
+
+all: note
+
+note: note.c
+
+install: all
+ mkdir -p $(DESTDIR)$(PREFIX)/bin
+ cp -f note $(DESTDIR)$(PREFIX)/bin
+ chmod 755 $(DESTDIR)$(PREFIX)/bin/note
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/bin/note
+
+clean:
+ rm -f note note-$(VERSION).tar.gz
+
+dist: clean
+ mkdir -p note-$(VERSION)
+ cp -R TODO Makefile config.h note.c note-$(VERSION)
+ tar -cf note-$(VERSION).tar note-$(VERSION)
+ gzip note-$(VERSION).tar
+ rm -rf note-$(VERSION)
+
+.c:
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<
+
+.PHONY: all install uninstall clean dist
diff --git a/config.h b/config.h
new file mode 100644
index 0000000..a558272
--- /dev/null
+++ b/config.h
@@ -0,0 +1,4 @@
+#define TIMEFORMAT "%Y/%m/%d, %H:%M"
+
+static const char *filepath = ".local/share/notes/notes.txt";
+static const char *dmenucmd = "printf '' | dmenu -p 'Enter note:'";
diff --git a/note.c b/note.c
new file mode 100644
index 0000000..3cca833
--- /dev/null
+++ b/note.c
@@ -0,0 +1,152 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "config.h"
+
+static void append(int, char *);
+static void last(char *);
+static void edit(char *);
+static void usage(void);
+
+void
+append(int d, char *fullpath)
+{
+ FILE *fp;
+ FILE *read;
+ char *line = NULL;
+ char timebuf[100];
+ size_t len = 0;
+ time_t t;
+
+ switch(d) {
+ case 0:
+ fputs("Enter note: ", stdout);
+ if (getline(&line, &len, stdin) <= 1) {
+ puts("\nNothing entered, exiting.");
+ free(line);
+ exit(0);
+ }
+ break;
+ case 1:
+ if (!(read = popen(dmenucmd, "r"))) {
+ perror("popen");
+ exit(1);
+ } else if (getline(&line, &len, read) <= 1) {
+ pclose(read);
+ free(line);
+ exit(0);
+ } else if (pclose(read) < 0) {
+ perror("pclose");
+ exit(1);
+ }
+ break;
+ default: abort();
+ }
+
+ if (!(fp = fopen(fullpath, "a"))) {
+ perror("fopen");
+ exit(1);
+ }
+
+ t = time(NULL);
+ if (!strftime(timebuf, sizeof(timebuf), TIMEFORMAT, localtime(&t))) {
+ fputs("strftime: result string exceeds buffer size\n", stderr);
+ exit(1);
+ }
+
+ fprintf(fp, "[%s] %s\n", timebuf, line);
+
+ free(line);
+ fclose(fp);
+}
+
+void
+last(char *fullpath)
+{
+ FILE *fp;
+ int count = 0;
+ long int pos;
+ char s[100];
+
+ if (!(fp = fopen(fullpath, "r"))) {
+ perror("fopen");
+ exit(1);
+ }
+
+ fseek(fp, 0, SEEK_END);
+ pos = ftell(fp);
+
+ while (1) {
+ fseek(fp, --pos, SEEK_SET);
+ if (!pos)
+ break;
+ else if ((fgetc(fp) == '\n') && (count++ == 20))
+ break;
+ }
+
+ while (fgets(s, sizeof(s), fp) != NULL) {
+ printf("%s", s);
+ }
+
+ fclose(fp);
+}
+
+void
+edit(char *fullpath)
+{
+ char *editor;
+
+ if ((editor = getenv("EDITOR")) == NULL) {
+ editor = "vi";
+ }
+
+ char *cmd[] = { editor, fullpath, NULL };
+ execvp(cmd[0], cmd);
+}
+
+void
+usage(void)
+{
+ fputs("usage: note [option]\n"
+ " -a append to the main file\n"
+ " -d append using dmenu\n"
+ " -s show last 10 notes\n"
+ " -e edit the main file\n", stderr);
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int opt;
+ char fullpath[100];
+
+ if (argc != 2) {
+ usage();
+ }
+
+ snprintf(fullpath, sizeof(fullpath), "%s/%s", getenv("HOME"), filepath);
+
+ while ((opt = getopt(argc, argv, "adse")) != -1) {
+ switch (opt) {
+ case 'a':
+ append(0, fullpath);
+ break;
+ case 'd':
+ append(1, fullpath);
+ break;
+ case 's':
+ last(fullpath);
+ break;
+ case 'e':
+ edit(fullpath);
+ break;
+ default:
+ usage();
+ }
+ }
+
+ return 0;
+}