summary refs log tree commit diff
diff options
context:
space:
mode:
authordzwdz2024-04-09 18:18:27 +0200
committerdzwdz2024-04-09 18:18:27 +0200
commit521da79371243c230e0e2beda72bed82791c6816 (patch)
tree4a0839f73271d6e7a4a6b5c2255329f5451b2398
parent9f6c7c42254005e0873b7192d5770b8e7ff203f0 (diff)
lastlog: smaller html output, hide system users
-rw-r--r--lastlog.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/lastlog.c b/lastlog.c
index 9036527..0cb6e84 100644
--- a/lastlog.c
+++ b/lastlog.c
@@ -63,6 +63,42 @@ static struct stat statbuf;	/* fstat buffer for file size */
 // TODO just put that in a variable...
 #define	NOW	(time ((time_t *) 0))
 
+/* https://gist.github.com/ciembor/1494530
+ * under the "do what you want :)" license */
+float hue2rgb(float p, float q, float t) {
+	if (t < 0)
+		t += 1;
+	if (t > 1)
+		t -= 1;
+	if (t < 1./6)
+		return p + (q - p) * 6 * t;
+	if (t < 1./2)
+		return q;
+	if (t < 2./3)
+		return p + (q - p) * (2./3 - t) * 6;
+	return p;
+}
+int byteclamp(int n) {
+	if (n < 0) return 0;
+	if (n > 0xFF) return 0xFF;
+	return n;
+}
+int hsl2rgb(float h, float s, float l) {
+	float r, g, b;
+
+	if (0 == s) {
+		r = g = b = l; // achromatic
+	} else {
+		float q = l < 0.5 ? l * (1 + s) : l + s - l * s;
+		float p = 2 * l - q;
+		r = hue2rgb(p, q, h + 1./3) * 255;
+		g = hue2rgb(p, q, h) * 255;
+		b = hue2rgb(p, q, h - 1./3) * 255;
+	}
+
+	return (byteclamp(r) << 16) + (byteclamp(g) << 8) + byteclamp(b);
+}
+
 static void print_one(const struct passwd *pw)
 {
 	char *cp;
@@ -76,6 +112,10 @@ static void print_one(const struct passwd *pw)
 		return;
 	}
 
+	if (pw->pw_uid < 1000 || pw->pw_uid == 65534) {
+		return;
+	}
+
 	offset = (off_t) pw->pw_uid * sizeof(ll);
 	if (offset + sizeof(ll) <= statbuf.st_size) {
 		/* fseeko errors are not really relevant for us. */
@@ -114,7 +154,7 @@ static void print_one(const struct passwd *pw)
 	float b = 2.5;
 	f = MAX(f * f / b, f * a - a + 1.0);
 	float hue = f * 90.0 + 260.;
-	printf("<a href=\"/~%s\" title=\"%s\" style=\"background: hsl(%fdeg %f%% %f%%)\"></a>\n", pw->pw_name, pw->pw_name, hue, 20. + f*70., 10. + f*90.);
+	printf("<a href=\"/~%s\" style=\"background: #%06x\"></a>\n", pw->pw_name, hsl2rgb(hue / 360., .2 + f*.7, .1 + f*.9));
 }
 
 int main(void)