2 * PuTTY miscellaneous Unix stuff
12 #include <sys/types.h>
17 unsigned long getticks(void)
20 * We want to use milliseconds rather than the microseconds or
21 * nanoseconds given by the underlying clock functions, because we
22 * need a decent number of them to fit into a 32-bit word so it
23 * can be used for keepalives.
25 #if defined HAVE_CLOCK_GETTIME && defined HAVE_DECL_CLOCK_MONOTONIC
27 /* Use CLOCK_MONOTONIC if available, so as to be unconfused if
28 * the system clock changes. */
30 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
31 return ts.tv_sec * TICKSPERSEC +
32 ts.tv_nsec / (1000000000 / TICKSPERSEC);
37 gettimeofday(&tv, NULL);
38 return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
42 Filename *filename_from_str(const char *str)
44 Filename *ret = snew(Filename);
45 ret->path = dupstr(str);
49 Filename *filename_copy(const Filename *fn)
51 return filename_from_str(fn->path);
54 const char *filename_to_str(const Filename *fn)
59 int filename_equal(const Filename *f1, const Filename *f2)
61 return !strcmp(f1->path, f2->path);
64 int filename_is_null(const Filename *fn)
69 void filename_free(Filename *fn)
75 int filename_serialise(const Filename *f, void *vdata)
77 char *data = (char *)vdata;
78 int len = strlen(f->path) + 1; /* include trailing NUL */
80 strcpy(data, f->path);
84 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
86 char *data = (char *)vdata;
88 end = memchr(data, '\0', maxsize);
93 return filename_from_str(data);
97 static FILE *debug_fp = NULL;
102 debug_fp = fopen("debug.log", "w");
105 write(1, buf, strlen(buf));
107 fputs(buf, debug_fp);
112 char *get_username(void)
115 uid_t uid = getuid();
116 char *user, *ret = NULL;
119 * First, find who we think we are using getlogin. If this
120 * agrees with our uid, we'll go along with it. This should
121 * allow sharing of uids between several login names whilst
122 * coping correctly with people who have su'ed.
130 if (p && p->pw_uid == uid) {
132 * The result of getlogin() really does correspond to
138 * If that didn't work, for whatever reason, we'll do
139 * the simpler version: look up our uid in the password
140 * file and map it straight to a name.
153 * Display the fingerprints of the PGP Master Keys to the user.
154 * (This is here rather than in uxcons because it's appropriate even for
157 void pgp_fingerprints(void)
159 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
160 "be used to establish a trust path from this executable to another\n"
161 "one. See the manual for more information.\n"
162 "(Note: these fingerprints have nothing to do with SSH!)\n"
164 "PuTTY Master Key (RSA), 1024-bit:\n"
165 " " PGP_RSA_MASTER_KEY_FP "\n"
166 "PuTTY Master Key (DSA), 1024-bit:\n"
167 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
171 * Set FD_CLOEXEC on a file descriptor
173 int cloexec(int fd) {
176 fdflags = fcntl(fd, F_GETFD);
177 if (fdflags == -1) return -1;
178 return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
181 FILE *f_open(const Filename *filename, char const *mode, int is_private)
184 return fopen(filename->path, mode);
187 assert(mode[0] == 'w'); /* is_private is meaningless for read,
188 and tricky for append */
189 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
192 return fdopen(fd, mode);
196 FontSpec *fontspec_new(const char *name)
198 FontSpec *f = snew(FontSpec);
199 f->name = dupstr(name);
202 FontSpec *fontspec_copy(const FontSpec *f)
204 return fontspec_new(f->name);
206 void fontspec_free(FontSpec *f)
211 int fontspec_serialise(FontSpec *f, void *data)
213 int len = strlen(f->name);
215 strcpy(data, f->name);
216 return len + 1; /* include trailing NUL */
218 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
220 char *data = (char *)vdata;
221 char *end = memchr(data, '\0', maxsize);
224 *used = end - data + 1;
225 return fontspec_new(data);