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