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);
97 char filename_char_sanitise(char c)
105 static FILE *debug_fp = NULL;
107 void dputs(char *buf)
110 debug_fp = fopen("debug.log", "w");
113 if (write(1, buf, strlen(buf)) < 0) {} /* 'error check' to placate gcc */
115 fputs(buf, debug_fp);
120 char *get_username(void)
123 uid_t uid = getuid();
124 char *user, *ret = NULL;
127 * First, find who we think we are using getlogin. If this
128 * agrees with our uid, we'll go along with it. This should
129 * allow sharing of uids between several login names whilst
130 * coping correctly with people who have su'ed.
138 if (p && p->pw_uid == uid) {
140 * The result of getlogin() really does correspond to
146 * If that didn't work, for whatever reason, we'll do
147 * the simpler version: look up our uid in the password
148 * file and map it straight to a name.
161 * Display the fingerprints of the PGP Master Keys to the user.
162 * (This is here rather than in uxcons because it's appropriate even for
165 void pgp_fingerprints(void)
167 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
168 "be used to establish a trust path from this executable to another\n"
169 "one. See the manual for more information.\n"
170 "(Note: these fingerprints have nothing to do with SSH!)\n"
172 "PuTTY Master Key as of 2015 (RSA, 4096-bit):\n"
173 " " PGP_MASTER_KEY_FP "\n\n"
174 "Original PuTTY Master Key (RSA, 1024-bit):\n"
175 " " PGP_RSA_MASTER_KEY_FP "\n"
176 "Original PuTTY Master Key (DSA, 1024-bit):\n"
177 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
181 * Set and clear fcntl options on a file descriptor. We don't
182 * realistically expect any of these operations to fail (the most
183 * plausible error condition is EBADF, but we always believe ourselves
184 * to be passing a valid fd so even that's an assertion-fail sort of
185 * response), so we don't make any effort to return sensible error
186 * codes to the caller - we just log to standard error and die
187 * unceremoniously. However, nonblock and no_nonblock do return the
188 * previous state of O_NONBLOCK.
190 void cloexec(int fd) {
193 fdflags = fcntl(fd, F_GETFD);
195 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
198 if (fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) < 0) {
199 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
203 void noncloexec(int fd) {
206 fdflags = fcntl(fd, F_GETFD);
208 fprintf(stderr, "%d: fcntl(F_GETFD): %s\n", fd, strerror(errno));
211 if (fcntl(fd, F_SETFD, fdflags & ~FD_CLOEXEC) < 0) {
212 fprintf(stderr, "%d: fcntl(F_SETFD): %s\n", fd, strerror(errno));
216 int nonblock(int fd) {
219 fdflags = fcntl(fd, F_GETFL);
221 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
224 if (fcntl(fd, F_SETFL, fdflags | O_NONBLOCK) < 0) {
225 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
229 return fdflags & O_NONBLOCK;
231 int no_nonblock(int fd) {
234 fdflags = fcntl(fd, F_GETFL);
236 fprintf(stderr, "%d: fcntl(F_GETFL): %s\n", fd, strerror(errno));
239 if (fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) {
240 fprintf(stderr, "%d: fcntl(F_SETFL): %s\n", fd, strerror(errno));
244 return fdflags & O_NONBLOCK;
247 FILE *f_open(const Filename *filename, char const *mode, int is_private)
250 return fopen(filename->path, mode);
253 assert(mode[0] == 'w'); /* is_private is meaningless for read,
254 and tricky for append */
255 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
258 return fdopen(fd, mode);
262 FontSpec *fontspec_new(const char *name)
264 FontSpec *f = snew(FontSpec);
265 f->name = dupstr(name);
268 FontSpec *fontspec_copy(const FontSpec *f)
270 return fontspec_new(f->name);
272 void fontspec_free(FontSpec *f)
277 int fontspec_serialise(FontSpec *f, void *data)
279 int len = strlen(f->name);
281 strcpy(data, f->name);
282 return len + 1; /* include trailing NUL */
284 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
286 char *data = (char *)vdata;
287 char *end = memchr(data, '\0', maxsize);
290 *used = end - data + 1;
291 return fontspec_new(data);