Change the semantics of 'FontSpec' so that it's a dynamically
[u/mdw/putty] / unix / uxmisc.c
CommitLineData
f7f27309 1/*
2 * PuTTY miscellaneous Unix stuff
3 */
4
db9b7dce 5#include <fcntl.h>
f7f27309 6#include <stdio.h>
2ac3322e 7#include <stdlib.h>
c6940f12 8#include <assert.h>
d0912d1f 9#include <unistd.h>
f7f27309 10#include <sys/time.h>
799dfcfa 11#include <sys/types.h>
12#include <pwd.h>
f7f27309 13
9a30e26b 14#include "putty.h"
15
2ac3322e 16long tickcount_offset = 0;
17
f7f27309 18unsigned long getticks(void)
19{
20 struct timeval tv;
21 gettimeofday(&tv, NULL);
fe7b25bd 22 /*
23 * We want to use milliseconds rather than microseconds,
24 * because we need a decent number of them to fit into a 32-bit
25 * word so it can be used for keepalives.
26 */
2ac3322e 27 return tv.tv_sec * 1000 + tv.tv_usec / 1000 + tickcount_offset;
f7f27309 28}
9a30e26b 29
9fab77dc 30Filename filename_from_str(const char *str)
9a30e26b 31{
32 Filename ret;
33 strncpy(ret.path, str, sizeof(ret.path));
34 ret.path[sizeof(ret.path)-1] = '\0';
35 return ret;
36}
37
9fab77dc 38const char *filename_to_str(const Filename *fn)
9a30e26b 39{
9fab77dc 40 return fn->path;
9a30e26b 41}
42
43int filename_equal(Filename f1, Filename f2)
44{
45 return !strcmp(f1.path, f2.path);
46}
47
48int filename_is_null(Filename fn)
49{
50 return !*fn.path;
51}
d0912d1f 52
53#ifdef DEBUG
54static FILE *debug_fp = NULL;
55
56void dputs(char *buf)
57{
58 if (!debug_fp) {
59 debug_fp = fopen("debug.log", "w");
60 }
61
62 write(1, buf, strlen(buf));
63
64 fputs(buf, debug_fp);
65 fflush(debug_fp);
66}
67#endif
799dfcfa 68
69char *get_username(void)
70{
71 struct passwd *p;
72 uid_t uid = getuid();
73 char *user, *ret = NULL;
74
75 /*
76 * First, find who we think we are using getlogin. If this
77 * agrees with our uid, we'll go along with it. This should
78 * allow sharing of uids between several login names whilst
79 * coping correctly with people who have su'ed.
80 */
81 user = getlogin();
82 setpwent();
83 if (user)
84 p = getpwnam(user);
85 else
86 p = NULL;
87 if (p && p->pw_uid == uid) {
88 /*
89 * The result of getlogin() really does correspond to
90 * our uid. Fine.
91 */
92 ret = user;
93 } else {
94 /*
95 * If that didn't work, for whatever reason, we'll do
96 * the simpler version: look up our uid in the password
97 * file and map it straight to a name.
98 */
99 p = getpwuid(uid);
100 if (!p)
101 return NULL;
102 ret = p->pw_name;
103 }
104 endpwent();
105
106 return dupstr(ret);
107}
2285d016 108
109/*
110 * Display the fingerprints of the PGP Master Keys to the user.
111 * (This is here rather than in uxcons because it's appropriate even for
112 * Unix GUI apps.)
113 */
114void pgp_fingerprints(void)
115{
116 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
117 "be used to establish a trust path from this executable to another\n"
118 "one. See the manual for more information.\n"
119 "(Note: these fingerprints have nothing to do with SSH!)\n"
120 "\n"
121 "PuTTY Master Key (RSA), 1024-bit:\n"
122 " " PGP_RSA_MASTER_KEY_FP "\n"
123 "PuTTY Master Key (DSA), 1024-bit:\n"
124 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
125}
db9b7dce 126
127/*
128 * Set FD_CLOEXEC on a file descriptor
129 */
130int cloexec(int fd) {
131 int fdflags;
132
133 fdflags = fcntl(fd, F_GETFD);
134 if (fdflags == -1) return -1;
135 return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
136}
c6940f12 137
138FILE *f_open(struct Filename filename, char const *mode, int is_private)
139{
140 if (!is_private) {
141 return fopen(filename.path, mode);
142 } else {
deb0e1cf 143 int fd;
b078de41 144 assert(mode[0] == 'w'); /* is_private is meaningless for read,
145 and tricky for append */
deb0e1cf 146 fd = open(filename.path, O_WRONLY | O_CREAT | O_TRUNC,
c6940f12 147 0700);
148 if (fd < 0)
149 return NULL;
150 return fdopen(fd, mode);
151 }
152}
ae62eaeb 153
154FontSpec *fontspec_new(const char *name)
155{
156 FontSpec *f = snew(FontSpec);
157 f->name = dupstr(name);
158 return f;
159}
160FontSpec *fontspec_copy(const FontSpec *f)
161{
162 return fontspec_new(f->name);
163}
164void fontspec_free(FontSpec *f)
165{
166 sfree(f->name);
167 sfree(f);
168}
169int fontspec_serialise(FontSpec *f, void *data)
170{
171 int len = strlen(f->name);
172 if (data)
173 strcpy(data, f->name);
174 return len + 1; /* include trailing NUL */
175}
176FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
177{
178 char *data = (char *)vdata;
179 char *end = memchr(data, '\0', maxsize);
180 if (!end)
181 return NULL;
182 *used = end - data + 1;
183 return fontspec_new(data);
184}