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