Patch from Robert de Bath to substantially simplify timing.c.
[u/mdw/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>
c6940f12 8#include <assert.h>
d0912d1f 9#include <unistd.h>
f7f27309 10#include <sys/time.h>
799dfcfa 11#include <sys/types.h>
12#include <pwd.h>
f7f27309 13
9a30e26b 14#include "putty.h"
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 */
c2eb6cb7 25 return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
f7f27309 26}
9a30e26b 27
962468d4 28Filename *filename_from_str(const char *str)
9a30e26b 29{
962468d4 30 Filename *ret = snew(Filename);
31 ret->path = dupstr(str);
9a30e26b 32 return ret;
33}
34
962468d4 35Filename *filename_copy(const Filename *fn)
36{
37 return filename_from_str(fn->path);
38}
39
9fab77dc 40const char *filename_to_str(const Filename *fn)
9a30e26b 41{
9fab77dc 42 return fn->path;
9a30e26b 43}
44
962468d4 45int filename_equal(const Filename *f1, const Filename *f2)
46{
47 return !strcmp(f1->path, f2->path);
48}
49
50int filename_is_null(const Filename *fn)
51{
52 return !fn->path[0];
53}
54
55void filename_free(Filename *fn)
9a30e26b 56{
962468d4 57 sfree(fn->path);
58 sfree(fn);
9a30e26b 59}
60
962468d4 61int filename_serialise(const Filename *f, void *vdata)
62{
63 char *data = (char *)vdata;
64 int len = strlen(f->path) + 1; /* include trailing NUL */
65 if (data) {
66 strcpy(data, f->path);
67 }
68 return len;
69}
70Filename *filename_deserialise(void *vdata, int maxsize, int *used)
9a30e26b 71{
962468d4 72 char *data = (char *)vdata;
73 char *end;
74 end = memchr(data, '\0', maxsize);
75 if (!end)
76 return NULL;
77 end++;
78 *used = end - data;
79 return filename_from_str(data);
9a30e26b 80}
d0912d1f 81
82#ifdef DEBUG
83static FILE *debug_fp = NULL;
84
85void dputs(char *buf)
86{
87 if (!debug_fp) {
88 debug_fp = fopen("debug.log", "w");
89 }
90
91 write(1, buf, strlen(buf));
92
93 fputs(buf, debug_fp);
94 fflush(debug_fp);
95}
96#endif
799dfcfa 97
98char *get_username(void)
99{
100 struct passwd *p;
101 uid_t uid = getuid();
102 char *user, *ret = NULL;
103
104 /*
105 * First, find who we think we are using getlogin. If this
106 * agrees with our uid, we'll go along with it. This should
107 * allow sharing of uids between several login names whilst
108 * coping correctly with people who have su'ed.
109 */
110 user = getlogin();
111 setpwent();
112 if (user)
113 p = getpwnam(user);
114 else
115 p = NULL;
116 if (p && p->pw_uid == uid) {
117 /*
118 * The result of getlogin() really does correspond to
119 * our uid. Fine.
120 */
121 ret = user;
122 } else {
123 /*
124 * If that didn't work, for whatever reason, we'll do
125 * the simpler version: look up our uid in the password
126 * file and map it straight to a name.
127 */
128 p = getpwuid(uid);
129 if (!p)
130 return NULL;
131 ret = p->pw_name;
132 }
133 endpwent();
134
135 return dupstr(ret);
136}
2285d016 137
138/*
139 * Display the fingerprints of the PGP Master Keys to the user.
140 * (This is here rather than in uxcons because it's appropriate even for
141 * Unix GUI apps.)
142 */
143void pgp_fingerprints(void)
144{
145 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
146 "be used to establish a trust path from this executable to another\n"
147 "one. See the manual for more information.\n"
148 "(Note: these fingerprints have nothing to do with SSH!)\n"
149 "\n"
150 "PuTTY Master Key (RSA), 1024-bit:\n"
151 " " PGP_RSA_MASTER_KEY_FP "\n"
152 "PuTTY Master Key (DSA), 1024-bit:\n"
153 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
154}
db9b7dce 155
156/*
157 * Set FD_CLOEXEC on a file descriptor
158 */
159int cloexec(int fd) {
160 int fdflags;
161
162 fdflags = fcntl(fd, F_GETFD);
163 if (fdflags == -1) return -1;
164 return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
165}
c6940f12 166
962468d4 167FILE *f_open(const Filename *filename, char const *mode, int is_private)
c6940f12 168{
169 if (!is_private) {
962468d4 170 return fopen(filename->path, mode);
c6940f12 171 } else {
deb0e1cf 172 int fd;
b078de41 173 assert(mode[0] == 'w'); /* is_private is meaningless for read,
174 and tricky for append */
e41356ff 175 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
c6940f12 176 if (fd < 0)
177 return NULL;
178 return fdopen(fd, mode);
179 }
180}
ae62eaeb 181
182FontSpec *fontspec_new(const char *name)
183{
184 FontSpec *f = snew(FontSpec);
185 f->name = dupstr(name);
186 return f;
187}
188FontSpec *fontspec_copy(const FontSpec *f)
189{
190 return fontspec_new(f->name);
191}
192void fontspec_free(FontSpec *f)
193{
194 sfree(f->name);
195 sfree(f);
196}
197int fontspec_serialise(FontSpec *f, void *data)
198{
199 int len = strlen(f->name);
200 if (data)
201 strcpy(data, f->name);
202 return len + 1; /* include trailing NUL */
203}
204FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
205{
206 char *data = (char *)vdata;
207 char *end = memchr(data, '\0', maxsize);
208 if (!end)
209 return NULL;
210 *used = end - data + 1;
211 return fontspec_new(data);
212}