Pi66

sta
minimal & simple status monitoring app.
git clone https://git.pi66.xyz/sta

← back to log

Init

author: pi66
date: 2025-09-18 14:02
hash: 5aedf5d657abce946082dce81a621f77ec71aed9

Diffstat:

A

Makefile
8 +

A

main.c
162 ++++++++++++++++++++++++++++++

A

usage.sh
11 ++
  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
193
194
195
196
197
198
199
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..33a1ec7
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,8 @@
+compile:
+   cc -o sta main.c
+
+install: compile
+   cp sta /usr/local/bin
+
+uninstall:
+   rm /usr/local/bin/sta
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..9a74044
--- /dev/null
+++ b/main.c
@@ -0,0 +1,162 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <stdarg.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
+
+#define SOCKET_PATH "/tmp/sta"
+
+typedef struct {
+    int id;
+    char *name;
+} Pro;
+
+typedef struct {
+    Pro *process;
+    int count;
+} Pros;
+
+void error(char *t,char *m,...){
+    va_list args;
+    va_start(args,m);
+    fprintf(stderr, "\033[1;31m[E]\033[0m \033[1;33m%s\033[0m:", t);
+    vfprintf(stderr, m,args);
+    fprintf(stderr, "\n");
+    va_end(args);
+}
+
+Pros *init(){
+    Pros *list = malloc(sizeof(Pros));
+    list->process = malloc(sizeof(Pro));
+    list->count = 0;
+    return list;
+}
+
+void add_pro(Pros *list,int id,char *name){
+   int checker = 0;
+   for (int i = 0;i<list->count;++i){
+       if (id == list->process[i].id){
+           list->process[i].name = strdup(name);
+           checker = 1;
+       }
+   }
+   if (checker) return;
+
+    list->count++;
+    list->process = realloc(list->process, sizeof(Pro) * list->count);
+    if (!list->process) error("Memory","memory allocation failed.");
+    list->process[list->count -1].name = strdup(name);
+    list->process[list->count -1].id = id;
+}
+
+void show_pros(Pros *list)
+{
+    for(int a = 0; a<list->count;++a){
+        printf("%s",list->process[a].name);
+    }
+   printf("\n");
+    fflush(stdout);
+}
+
+void show_help()
+{
+   printf("Usage:\n");
+   printf("\t-id        <number>       => process id (id >= 1)\n");
+   printf("\t-name      <string>       => process name\n");
+   printf("\t-h -help                  => print this help menu\n");
+
+   exit(1);
+}
+
+void args(int *id,const char **name,int argc, char const* argv[])
+{
+   for (int i = 1; i < argc; i += 2) {
+       if (!strcmp("-h", argv[i]) ||!strcmp("-help", argv[i])) 
+           show_help();
+
+       if (i + 1 >= argc || argv[i+1][0] == '-') 
+           error("Args", "missing value for %s\nrun: sta -h", argv[i]);
+
+       if (!strcmp("-id", argv[i])) 
+           *id = atoi(argv[i + 1]);
+
+       else if (!strcmp("-name", argv[i])) 
+           *name = argv[i + 1];
+
+   }
+
+   if (*id <= 0 || !*name) 
+       show_help();
+
+}
+
+void clean_up(){
+    unlink(SOCKET_PATH);
+    exit(0);
+}
+
+int main(int argc, char const* argv[]){
+
+    struct sockaddr_un addr;
+    memset(&addr, 0, sizeof(addr));
+    addr.sun_family = AF_UNIX;
+    strcpy(addr.sun_path, SOCKET_PATH);
+    int sock = socket(AF_UNIX, SOCK_STREAM , 0);
+    if (sock < 0) error("Socket","failed to create socket: %s", strerror(errno));
+
+    Pros *list = init();
+
+    if (access(SOCKET_PATH, F_OK) == 0) {
+       int id;
+       const char *name;
+       char buf[256];
+       args(&id, &name,argc, argv);
+       snprintf(buf,sizeof(buf),"%d|+|%s",id,name);
+
+        if (connect(sock, (struct sockaddr*)&addr , sizeof(addr)) < 0)
+            error("Connect","failed: %s", strerror(errno));
+
+        write(sock, buf, strlen(buf));
+        close(sock);
+        return 0;
+    } else {
+        signal(SIGTERM, clean_up);
+        signal(SIGINT, clean_up);
+
+        unlink(SOCKET_PATH);
+        if (bind(sock, (struct sockaddr*)&addr , sizeof(addr)) < 0)
+            error("Bind","failed: %s", strerror(errno));
+
+        if (listen(sock, 5) < 0)
+            error("Listen","failed: %s", strerror(errno));
+
+        while (1) {
+            int client = accept(sock, NULL, NULL);
+            if (client < 0) continue;
+
+            char buf[256], name[256];
+            int n,id;
+
+            while ((n = recv(client, buf, sizeof(buf)-1, 0)) > 0) {
+                buf[n] = '\0';
+               char *s = strstr(buf,"|+|");
+               if (s){
+                   *s = '\0';
+                   id = atoi(buf);
+                   strncpy(name,s+3,sizeof(name)-1);
+                   add_pro(list,  id, name);
+                   show_pros(list);
+               }
+            }
+            close(client);
+        }
+        close(sock);
+        unlink(SOCKET_PATH);
+        return 0;
+    }
+}
+
diff --git a/usage.sh b/usage.sh
new file mode 100644
index 0000000..6c28cb4
--- /dev/null
+++ b/usage.sh
@@ -0,0 +1,11 @@
+./sta -id 1 -name " $(date +%H:%M) "
+./sta -id 2 -name " $(date +'%b %d')                                    "
+./sta -id 3 -name " $(cat /sys/class/power_supply/BAT1/status) "
+./sta -id 4 -name "   $(cat /sys/class/power_supply/BAT1/capacity)% │"
+./sta -id 5 -name "   <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>l</mi><mi>i</mi><mi>g</mi><mi>h</mi><mi>t</mi><mo>&#x02212;</mo><mi>G</mi><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">&#x00028;</mo></mrow></mrow></math>1)}')% │"
+./sta -id 6 -name "   $(pamixer --get-volume) │"
+./sta -id 7 -name "  <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>t</mi><mi>o</mi><mi>p</mi><mo>&#x02212;</mo><mi>b</mi><mi>n</mi><mn>1</mn><mo stretchy="false">&#x0007C;</mo><mi>g</mi><mi>r</mi><mi>e</mi><mi>p</mi><mi>"</mi><mi>C</mi><mi>p</mi><mi>u</mi><mo stretchy="false">&#x00028;</mo><mi>s</mi><mo stretchy="false">&#x00029;</mo><mi>"</mi><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">&#x00028;</mo></mrow></mrow></math>2 + $4)}')% /"
+./sta -id 8 -name "  <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mo>&#x0002F;</mo><mi>s</mi><mi>y</mi><mi>s</mi><mo>&#x0002F;</mo><mi>c</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>s</mi><mo>&#x0002F;</mo><mi>t</mi><mi>h</mi><mi>e</mi><mi>r</mi><mi>m</mi><mi>a</mi><mi>l</mi><mo>&#x0002F;</mo><mi>t</mi><mi>h</mi><mi>e</mi><mi>r</mi><mi>m</mi><mi>a</mi><msub><mi>l</mi><mi>z</mi></msub><mi>o</mi><mi>n</mi><mi>e</mi><mn>3</mn><mo>&#x0002F;</mo><mi>t</mi><mi>e</mi><mi>m</mi><mi>p</mi><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">&#x00028;</mo></mrow></mrow></math>1/1000)}')󰔄 │"
+./sta -id 9 -name "   <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>f</mi><mi>r</mi><mi>e</mi><mi>e</mi><mo>&#x02212;</mo><mi>h</mi><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><msup><mo>&#x0002F;</mo><mi>M</mi></msup><mi>e</mi><mi>m</mi><mi>:</mi><mo>&#x0002F;</mo><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi></mrow></mrow></math>3}') │"
+./sta -id 10 -name "  <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>d</mi><mi>f</mi><mo>&#x02212;</mo><mi>h</mi><mo>&#x0002F;</mo><mi>h</mi><mi>o</mi><mi>m</mi><mi>e</mi><mo>&#x0002F;</mo><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><mi>N</mi><mi>R</mi><mo>&#x0003D;</mo><mo>&#x0003D;</mo><mn>2</mn><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi></mrow></mrow></math>5}') │"
+./sta -id 11 -name "   <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow><mo stretchy="false">&#x00028;</mo><mi>c</mi><mi>a</mi><mi>t</mi><mo>&#x0002F;</mo><mi>p</mi><mi>r</mi><mi>o</mi><mi>c</mi><mo>&#x0002F;</mo><mi>n</mi><mi>e</mi><mi>t</mi><mo>&#x0002F;</mo><mi>w</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>l</mi><mi>e</mi><mi>s</mi><mi>s</mi><mo stretchy="false">&#x0007C;</mo><mi>a</mi><mi>w</mi><msup><mi>k</mi><mi>&#x02032;</mi></msup><mi>N</mi><mi>R</mi><mo>&#x0003E;</mo><mn>2</mn><mrow><mi>p</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>t</mi></mrow></mrow></math>1}' | head -n1)  "