Patch from Robert de Bath to substantially simplify timing.c.
[u/mdw/putty] / unix / uxmisc.c
1 /*
2 * PuTTY miscellaneous Unix stuff
3 */
4
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <unistd.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <pwd.h>
13
14 #include "putty.h"
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 * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
26 }
27
28 Filename *filename_from_str(const char *str)
29 {
30 Filename *ret = snew(Filename);
31 ret->path = dupstr(str);
32 return ret;
33 }
34
35 Filename *filename_copy(const Filename *fn)
36 {
37 return filename_from_str(fn->path);
38 }
39
40 const char *filename_to_str(const Filename *fn)
41 {
42 return fn->path;
43 }
44
45 int filename_equal(const Filename *f1, const Filename *f2)
46 {
47 return !strcmp(f1->path, f2->path);
48 }
49
50 int filename_is_null(const Filename *fn)
51 {
52 return !fn->path[0];
53 }
54
55 void filename_free(Filename *fn)
56 {
57 sfree(fn->path);
58 sfree(fn);
59 }
60
61 int 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 }
70 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
71 {
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);
80 }
81
82 #ifdef DEBUG
83 static FILE *debug_fp = NULL;
84
85 void 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
97
98 char *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 }
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 */
143 void 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 }
155
156 /*
157 * Set FD_CLOEXEC on a file descriptor
158 */
159 int 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 }
166
167 FILE *f_open(const Filename *filename, char const *mode, int is_private)
168 {
169 if (!is_private) {
170 return fopen(filename->path, mode);
171 } else {
172 int fd;
173 assert(mode[0] == 'w'); /* is_private is meaningless for read,
174 and tricky for append */
175 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
176 if (fd < 0)
177 return NULL;
178 return fdopen(fd, mode);
179 }
180 }
181
182 FontSpec *fontspec_new(const char *name)
183 {
184 FontSpec *f = snew(FontSpec);
185 f->name = dupstr(name);
186 return f;
187 }
188 FontSpec *fontspec_copy(const FontSpec *f)
189 {
190 return fontspec_new(f->name);
191 }
192 void fontspec_free(FontSpec *f)
193 {
194 sfree(f->name);
195 sfree(f);
196 }
197 int 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 }
204 FontSpec *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 }