]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxmisc.c
28ae83a2af29d5d90bf142113d1615885e6beba1
[PuTTY.git] / unix / uxmisc.c
1 /*
2  * PuTTY miscellaneous Unix stuff
3  */
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <sys/time.h>
8 #include <sys/types.h>
9 #include <pwd.h>
10
11 #include "putty.h"
12
13 unsigned long getticks(void)
14 {
15     struct timeval tv;
16     gettimeofday(&tv, NULL);
17     /*
18      * We want to use milliseconds rather than microseconds,
19      * because we need a decent number of them to fit into a 32-bit
20      * word so it can be used for keepalives.
21      */
22     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
23 }
24
25 Filename filename_from_str(const char *str)
26 {
27     Filename ret;
28     strncpy(ret.path, str, sizeof(ret.path));
29     ret.path[sizeof(ret.path)-1] = '\0';
30     return ret;
31 }
32
33 const char *filename_to_str(const Filename *fn)
34 {
35     return fn->path;
36 }
37
38 int filename_equal(Filename f1, Filename f2)
39 {
40     return !strcmp(f1.path, f2.path);
41 }
42
43 int filename_is_null(Filename fn)
44 {
45     return !*fn.path;
46 }
47
48 #ifdef DEBUG
49 static FILE *debug_fp = NULL;
50
51 void dputs(char *buf)
52 {
53     if (!debug_fp) {
54         debug_fp = fopen("debug.log", "w");
55     }
56
57     write(1, buf, strlen(buf));
58
59     fputs(buf, debug_fp);
60     fflush(debug_fp);
61 }
62 #endif
63
64 char *get_username(void)
65 {
66     struct passwd *p;
67     uid_t uid = getuid();
68     char *user, *ret = NULL;
69
70     /*
71      * First, find who we think we are using getlogin. If this
72      * agrees with our uid, we'll go along with it. This should
73      * allow sharing of uids between several login names whilst
74      * coping correctly with people who have su'ed.
75      */
76     user = getlogin();
77     setpwent();
78     if (user)
79         p = getpwnam(user);
80     else
81         p = NULL;
82     if (p && p->pw_uid == uid) {
83         /*
84          * The result of getlogin() really does correspond to
85          * our uid. Fine.
86          */
87         ret = user;
88     } else {
89         /*
90          * If that didn't work, for whatever reason, we'll do
91          * the simpler version: look up our uid in the password
92          * file and map it straight to a name.
93          */
94         p = getpwuid(uid);
95         if (!p)
96             return NULL;
97         ret = p->pw_name;
98     }
99     endpwent();
100
101     return dupstr(ret);
102 }
103
104 /*
105  * Display the fingerprints of the PGP Master Keys to the user.
106  * (This is here rather than in uxcons because it's appropriate even for
107  * Unix GUI apps.)
108  */
109 void pgp_fingerprints(void)
110 {
111     fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
112           "be used to establish a trust path from this executable to another\n"
113           "one. See the manual for more information.\n"
114           "(Note: these fingerprints have nothing to do with SSH!)\n"
115           "\n"
116           "PuTTY Master Key (RSA), 1024-bit:\n"
117           "  " PGP_RSA_MASTER_KEY_FP "\n"
118           "PuTTY Master Key (DSA), 1024-bit:\n"
119           "  " PGP_DSA_MASTER_KEY_FP "\n", stdout);
120 }