2 * PuTTY miscellaneous Unix stuff
13 #include <sys/types.h>
18 unsigned long getticks(void)
21 * We want to use milliseconds rather than the microseconds or
22 * nanoseconds given by the underlying clock functions, because we
23 * need a decent number of them to fit into a 32-bit word so it
24 * can be used for keepalives.
26 #if defined HAVE_CLOCK_GETTIME && defined HAVE_DECL_CLOCK_MONOTONIC
28 /* Use CLOCK_MONOTONIC if available, so as to be unconfused if
29 * the system clock changes. */
31 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
32 return ts.tv_sec * TICKSPERSEC +
33 ts.tv_nsec / (1000000000 / TICKSPERSEC);
38 gettimeofday(&tv, NULL);
39 return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
43 Filename *filename_from_str(const char *str)
45 Filename *ret = snew(Filename);
46 ret->path = dupstr(str);
50 Filename *filename_copy(const Filename *fn)
52 return filename_from_str(fn->path);
55 const char *filename_to_str(const Filename *fn)
60 int filename_equal(const Filename *f1, const Filename *f2)
62 return !strcmp(f1->path, f2->path);
65 int filename_is_null(const Filename *fn)
70 void filename_free(Filename *fn)
76 int filename_serialise(const Filename *f, void *vdata)
78 char *data = (char *)vdata;
79 int len = strlen(f->path) + 1; /* include trailing NUL */
81 strcpy(data, f->path);
85 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
87 char *data = (char *)vdata;
89 end = memchr(data, '\0', maxsize);
94 return filename_from_str(data);
98 static FILE *debug_fp = NULL;
100 void dputs(char *buf)
103 debug_fp = fopen("debug.log", "w");
106 if (write(1, buf, strlen(buf)) < 0) {} /* 'error check' to placate gcc */
108 fputs(buf, debug_fp);
113 char *get_username(void)
116 uid_t uid = getuid();
117 char *user, *ret = NULL;
120 * First, find who we think we are using getlogin. If this
121 * agrees with our uid, we'll go along with it. This should
122 * allow sharing of uids between several login names whilst
123 * coping correctly with people who have su'ed.
131 if (p && p->pw_uid == uid) {
133 * The result of getlogin() really does correspond to
139 * If that didn't work, for whatever reason, we'll do
140 * the simpler version: look up our uid in the password
141 * file and map it straight to a name.
154 * Display the fingerprints of the PGP Master Keys to the user.
155 * (This is here rather than in uxcons because it's appropriate even for
158 void pgp_fingerprints(void)
160 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
161 "be used to establish a trust path from this executable to another\n"
162 "one. See the manual for more information.\n"
163 "(Note: these fingerprints have nothing to do with SSH!)\n"
165 "PuTTY Master Key as of 2015 (RSA, 4096-bit):\n"
166 " " PGP_MASTER_KEY_FP "\n\n"
167 "Original PuTTY Master Key (RSA, 1024-bit):\n"
168 " " PGP_RSA_MASTER_KEY_FP "\n"
169 "Original PuTTY Master Key (DSA, 1024-bit):\n"
170 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
174 * Set and clear fcntl options on a file descriptor. We don't
175 * realistically expect any of these operations to fail (the most
176 * plausible error condition is EBADF, but we always believe ourselves
177 * to be passing a valid fd so even that's an assertion-fail sort of
178 * response), so we don't make any effort to return sensible error
179 * codes to the caller - we just log to standard error and die
180 * unceremoniously. However, nonblock and no_nonblock do return the
181 * previous state of O_NONBLOCK.
183 void cloexec(int fd) {
186 fdflags = fcntl(fd, F_GETFD);
188 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
191 if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
192 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
196 void noncloexec(int fd) {
199 fdflags = fcntl(fd, F_GETFD);
201 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
204 if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
205 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
209 int nonblock(int fd) {
212 fdflags = fcntl(fd, F_GETFL);
214 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
217 if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
218 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
222 return fdflags & O_NONBLOCK;
224 int no_nonblock(int fd) {
227 fdflags = fcntl(fd, F_GETFL);
229 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
232 if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
233 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
237 return fdflags & O_NONBLOCK;
240 FILE *f_open(const Filename *filename, char const *mode, int is_private)
243 return fopen(filename->path, mode);
246 assert(mode[0] == 'w'); /* is_private is meaningless for read,
247 and tricky for append */
248 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
251 return fdopen(fd, mode);
255 FontSpec *fontspec_new(const char *name)
257 FontSpec *f = snew(FontSpec);
258 f->name = dupstr(name);
261 FontSpec *fontspec_copy(const FontSpec *f)
263 return fontspec_new(f->name);
265 void fontspec_free(FontSpec *f)
270 int fontspec_serialise(FontSpec *f, void *data)
272 int len = strlen(f->name);
274 strcpy(data, f->name);
275 return len + 1; /* include trailing NUL */
277 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
279 char *data = (char *)vdata;
280 char *end = memchr(data, '\0', maxsize);
283 *used = end - data + 1;
284 return fontspec_new(data);