Render timing.c robust in the face of strangeness. The strangenesses
[u/mdw/putty] / unix / uxmisc.c
CommitLineData
f7f27309 1/*
2 * PuTTY miscellaneous Unix stuff
3 */
4
5#include <stdio.h>
2ac3322e 6#include <stdlib.h>
d0912d1f 7#include <unistd.h>
f7f27309 8#include <sys/time.h>
799dfcfa 9#include <sys/types.h>
10#include <pwd.h>
f7f27309 11
9a30e26b 12#include "putty.h"
13
2ac3322e 14long tickcount_offset = 0;
15
f7f27309 16unsigned long getticks(void)
17{
18 struct timeval tv;
19 gettimeofday(&tv, NULL);
fe7b25bd 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 */
2ac3322e 25 return tv.tv_sec * 1000 + tv.tv_usec / 1000 + tickcount_offset;
f7f27309 26}
9a30e26b 27
9fab77dc 28Filename filename_from_str(const char *str)
9a30e26b 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
9fab77dc 36const char *filename_to_str(const Filename *fn)
9a30e26b 37{
9fab77dc 38 return fn->path;
9a30e26b 39}
40
41int filename_equal(Filename f1, Filename f2)
42{
43 return !strcmp(f1.path, f2.path);
44}
45
46int filename_is_null(Filename fn)
47{
48 return !*fn.path;
49}
d0912d1f 50
51#ifdef DEBUG
52static FILE *debug_fp = NULL;
53
54void 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
799dfcfa 66
67char *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}
2285d016 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 */
112void 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}