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