]> mj.ucw.cz Git - misc.git/blob - sig.c
Merge branch 'master' of git+ssh://git.ucw.cz/home/mj/GIT/misc
[misc.git] / sig.c
1 #include <stdio.h>
2 #include <pwd.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <time.h>
7
8 int main(int argc, char **argv)
9 {
10         struct passwd *p;
11         int n, len, f;
12         char buf[256], *z, *i;
13
14         p = getpwuid(getuid());
15         if (!p) { puts("You don't exist and thus you don't have a signature!"); return 1; }
16         chdir(p->pw_dir);
17         f = open((argc > 1) ? argv[1] : ".sigbase", O_RDONLY);
18         if (f < 0) { puts("No file, no signature!"); return 1; }
19         while ((n = read(f, buf, sizeof(buf))) > 0) write(1, buf, n);
20         close(f);
21         f = open(".misfortune", O_RDONLY);
22         if (!f || (len = lseek(f, 0, SEEK_END)) <= 0) {
23                 puts("No fortune, no luck, no sig!");
24                 return 1;
25         }
26         lseek(f, 0, SEEK_SET);
27         z = malloc(len+1);
28         if (!z) { puts("ENOMEM"); return 1; }
29         read(f, z, len);
30         z[len] = 0;
31         n = 0;
32         for(i=z; i<z+len; i++)
33                 if (*i == '\n')
34                         n++;
35         if (!n) { puts("Oh no! More lemmings!"); return 1; }
36         srandom(time(NULL));
37         n = random() % n;
38         while (n--) {
39                 while (*z && *z != '\n') z++;
40                 if (*z) z++;
41         }
42         while (*z && *z != '\n') putchar(*z++);
43         putchar('\n');
44         return 0;
45 }