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 write(1, buf
, strlen(buf
));
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 (RSA), 1024-bit:\n"
166 " " PGP_RSA_MASTER_KEY_FP
"\n"
167 "PuTTY Master Key (DSA), 1024-bit:\n"
168 " " PGP_DSA_MASTER_KEY_FP
"\n", stdout
);
172 * Set and clear fcntl options on a file descriptor. We don't
173 * realistically expect any of these operations to fail (the most
174 * plausible error condition is EBADF, but we always believe ourselves
175 * to be passing a valid fd so even that's an assertion-fail sort of
176 * response), so we don't make any effort to return sensible error
177 * codes to the caller - we just log to standard error and die
178 * unceremoniously. However, nonblock and no_nonblock do return the
179 * previous state of O_NONBLOCK.
181 void cloexec(int fd
) {
184 fdflags
= fcntl(fd
, F_GETFD
);
186 fprintf(stderr
, "%d: fcntl(F_GETFD): %s\n", fd
, strerror(errno
));
189 if (fcntl(fd
, F_SETFD
, fdflags
| FD_CLOEXEC
) < 0) {
190 fprintf(stderr
, "%d: fcntl(F_SETFD): %s\n", fd
, strerror(errno
));
194 void noncloexec(int fd
) {
197 fdflags
= fcntl(fd
, F_GETFD
);
199 fprintf(stderr
, "%d: fcntl(F_GETFD): %s\n", fd
, strerror(errno
));
202 if (fcntl(fd
, F_SETFD
, fdflags
& ~FD_CLOEXEC
) < 0) {
203 fprintf(stderr
, "%d: fcntl(F_SETFD): %s\n", fd
, strerror(errno
));
207 int nonblock(int fd
) {
210 fdflags
= fcntl(fd
, F_GETFL
);
212 fprintf(stderr
, "%d: fcntl(F_GETFL): %s\n", fd
, strerror(errno
));
215 if (fcntl(fd
, F_SETFL
, fdflags
| O_NONBLOCK
) < 0) {
216 fprintf(stderr
, "%d: fcntl(F_SETFL): %s\n", fd
, strerror(errno
));
220 return fdflags
& O_NONBLOCK
;
222 int no_nonblock(int fd
) {
225 fdflags
= fcntl(fd
, F_GETFL
);
227 fprintf(stderr
, "%d: fcntl(F_GETFL): %s\n", fd
, strerror(errno
));
230 if (fcntl(fd
, F_SETFL
, fdflags
& ~O_NONBLOCK
) < 0) {
231 fprintf(stderr
, "%d: fcntl(F_SETFL): %s\n", fd
, strerror(errno
));
235 return fdflags
& O_NONBLOCK
;
238 FILE *f_open(const Filename
*filename
, char const *mode
, int is_private
)
241 return fopen(filename
->path
, mode
);
244 assert(mode
[0] == 'w'); /* is_private is meaningless for read,
245 and tricky for append */
246 fd
= open(filename
->path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600);
249 return fdopen(fd
, mode
);
253 FontSpec
*fontspec_new(const char *name
)
255 FontSpec
*f
= snew(FontSpec
);
256 f
->name
= dupstr(name
);
259 FontSpec
*fontspec_copy(const FontSpec
*f
)
261 return fontspec_new(f
->name
);
263 void fontspec_free(FontSpec
*f
)
268 int fontspec_serialise(FontSpec
*f
, void *data
)
270 int len
= strlen(f
->name
);
272 strcpy(data
, f
->name
);
273 return len
+ 1; /* include trailing NUL */
275 FontSpec
*fontspec_deserialise(void *vdata
, int maxsize
, int *used
)
277 char *data
= (char *)vdata
;
278 char *end
= memchr(data
, '\0', maxsize
);
281 *used
= end
- data
+ 1;
282 return fontspec_new(data
);