2 * PuTTY miscellaneous Unix stuff
11 #include <sys/types.h>
16 unsigned long getticks(void)
19 gettimeofday(&tv, NULL);
21 * We want to use milliseconds rather than microseconds,
22 * because we need a decent number of them to fit into a 32-bit
23 * word so it can be used for keepalives.
25 return tv.tv_sec * TICKSPERSEC + tv.tv_usec / (1000000 / TICKSPERSEC);
28 Filename *filename_from_str(const char *str)
30 Filename *ret = snew(Filename);
31 ret->path = dupstr(str);
35 Filename *filename_copy(const Filename *fn)
37 return filename_from_str(fn->path);
40 const char *filename_to_str(const Filename *fn)
45 int filename_equal(const Filename *f1, const Filename *f2)
47 return !strcmp(f1->path, f2->path);
50 int filename_is_null(const Filename *fn)
55 void filename_free(Filename *fn)
61 int filename_serialise(const Filename *f, void *vdata)
63 char *data = (char *)vdata;
64 int len = strlen(f->path) + 1; /* include trailing NUL */
66 strcpy(data, f->path);
70 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
72 char *data = (char *)vdata;
74 end = memchr(data, '\0', maxsize);
79 return filename_from_str(data);
83 static FILE *debug_fp = NULL;
88 debug_fp = fopen("debug.log", "w");
91 write(1, buf, strlen(buf));
98 char *get_username(void)
101 uid_t uid = getuid();
102 char *user, *ret = NULL;
105 * First, find who we think we are using getlogin. If this
106 * agrees with our uid, we'll go along with it. This should
107 * allow sharing of uids between several login names whilst
108 * coping correctly with people who have su'ed.
116 if (p && p->pw_uid == uid) {
118 * The result of getlogin() really does correspond to
124 * If that didn't work, for whatever reason, we'll do
125 * the simpler version: look up our uid in the password
126 * file and map it straight to a name.
139 * Display the fingerprints of the PGP Master Keys to the user.
140 * (This is here rather than in uxcons because it's appropriate even for
143 void pgp_fingerprints(void)
145 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
146 "be used to establish a trust path from this executable to another\n"
147 "one. See the manual for more information.\n"
148 "(Note: these fingerprints have nothing to do with SSH!)\n"
150 "PuTTY Master Key (RSA), 1024-bit:\n"
151 " " PGP_RSA_MASTER_KEY_FP "\n"
152 "PuTTY Master Key (DSA), 1024-bit:\n"
153 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
157 * Set FD_CLOEXEC on a file descriptor
159 int cloexec(int fd) {
162 fdflags = fcntl(fd, F_GETFD);
163 if (fdflags == -1) return -1;
164 return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
167 FILE *f_open(const Filename *filename, char const *mode, int is_private)
170 return fopen(filename->path, mode);
173 assert(mode[0] == 'w'); /* is_private is meaningless for read,
174 and tricky for append */
175 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
178 return fdopen(fd, mode);
182 FontSpec *fontspec_new(const char *name)
184 FontSpec *f = snew(FontSpec);
185 f->name = dupstr(name);
188 FontSpec *fontspec_copy(const FontSpec *f)
190 return fontspec_new(f->name);
192 void fontspec_free(FontSpec *f)
197 int fontspec_serialise(FontSpec *f, void *data)
199 int len = strlen(f->name);
201 strcpy(data, f->name);
202 return len + 1; /* include trailing NUL */
204 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
206 char *data = (char *)vdata;
207 char *end = memchr(data, '\0', maxsize);
210 *used = end - data + 1;
211 return fontspec_new(data);