Skip to content

Commit 88d05af

Browse files
Include What You Use, use namespace std in util library
1 parent 76c8007 commit 88d05af

File tree

2 files changed

+91
-82
lines changed

2 files changed

+91
-82
lines changed

util/env.cpp

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
*/
33
/* This software is copyrighted as detailed in the LICENSE file. */
44

5-
#include <config/pipe_io.h>
6-
7-
#include "config/common.h"
85
#include "util/env-internal.h"
96

10-
#include "trn/init.h"
11-
#include "trn/util.h"
12-
#include "util/util2.h"
7+
#include <config/common.h>
8+
#include <config/pipe_io.h>
9+
#include <trn/init.h>
10+
#include <trn/util.h>
11+
#include <util/util2.h>
1312

1413
#ifdef HAS_RES_INIT
15-
#include <netinet/in.h>
1614
#include <arpa/nameser.h>
15+
#include <netinet/in.h>
1716
#include <resolv.h>
1817
#endif
1918

@@ -28,6 +27,11 @@
2827
#endif
2928

3029
#include <algorithm>
30+
#include <cctype>
31+
#include <cstdio>
32+
#include <cstdlib>
33+
#include <cstring>
34+
#include <functional>
3135
#include <memory>
3236

3337
char *g_home_dir{}; /* login directory */
@@ -42,7 +46,7 @@ std::string g_p_host_name; /* host name in a posting */
4246
char *g_local_host{}; /* local host name */
4347
int g_net_speed{20}; /* how fast our net-connection is */
4448

45-
static std::function<char *(const char *name)> s_getenv_fn = getenv;
49+
static std::function<char *(const char *name)> s_getenv_fn = std::getenv;
4650

4751
static void env_init2();
4852
static int envix(const char *nam, int len);
@@ -57,7 +61,7 @@ void set_environment(std::function<char *(const char *)> getenv_fn)
5761
}
5862
else
5963
{
60-
s_getenv_fn = getenv;
64+
s_getenv_fn = std::getenv;
6165
}
6266
}
6367

@@ -121,8 +125,8 @@ bool env_init(char *tcbuf, bool lax, const std::function<bool(char *tmpbuf)> &se
121125
char *home_path = s_getenv_fn("HOMEPATH");
122126
if (home_drive != nullptr && home_path != nullptr)
123127
{
124-
strcpy(tcbuf, home_drive);
125-
strcat(tcbuf, home_path);
128+
std::strcpy(tcbuf, home_drive);
129+
std::strcat(tcbuf, home_path);
126130
g_home_dir = savestr(tcbuf);
127131
}
128132
}
@@ -157,7 +161,7 @@ bool env_init(char *tcbuf, bool lax, const std::function<bool(char *tmpbuf)> &se
157161
g_net_speed = 1;
158162
}
159163
else {
160-
g_net_speed = atoi(cp);
164+
g_net_speed = std::atoi(cp);
161165
g_net_speed = std::max(g_net_speed, 1);
162166
}
163167
}
@@ -242,34 +246,34 @@ static bool set_user_name(char *tmpbuf)
242246
s++;
243247
}
244248
#endif
245-
c = strchr(s, ',');
249+
c = std::strchr(s, ',');
246250
if (c != nullptr)
247251
{
248252
*c = '\0';
249253
}
250-
c = strchr(s, ';');
254+
c = std::strchr(s, ';');
251255
if (c != nullptr)
252256
{
253257
*c = '\0';
254258
}
255259
s = cpytill(g_buf,s,'&');
256260
if (*s == '&') { /* whoever thought this one up was */
257261
c = g_buf + strlen(g_buf); /* in the middle of the night */
258-
strcat(c, g_login_name.c_str()); /* before the morning after */
259-
strcat(c,s+1);
260-
if (islower(*c))
262+
std::strcat(c, g_login_name.c_str()); /* before the morning after */
263+
std::strcat(c,s+1);
264+
if (std::islower(*c))
261265
{
262-
*c = toupper(*c); /* gack and double gack */
266+
*c = std::toupper(*c); /* gack and double gack */
263267
}
264268
}
265269
g_real_name = g_buf;
266270
#else /* !BERKNAMES */
267-
c = strchr(s, '(');
271+
c = std::strchr(s, '(');
268272
if (c != nullptr)
269273
{
270274
*c = '\0';
271275
}
272-
c = strchr(s, '-');
276+
c = std::strchr(s, '-');
273277
if (c != nullptr)
274278
{
275279
s = c;
@@ -280,11 +284,11 @@ static bool set_user_name(char *tmpbuf)
280284
#ifndef PASSNAMES
281285
{
282286
env_init2(); /* Make sure g_home_dir/g_dot_dir/etc. are set. */
283-
FILE *fp = fopen(filexp(FULLNAMEFILE), "r");
287+
std::FILE *fp = std::fopen(filexp(FULLNAMEFILE), "r");
284288
if (fp != nullptr)
285289
{
286-
fgets(g_buf,sizeof g_buf,fp);
287-
fclose(fp);
290+
std::fgets(g_buf,sizeof g_buf,fp);
291+
std::fclose(fp);
288292
g_buf[strlen(g_buf)-1] = '\0';
289293
g_real_name = g_buf;
290294
}
@@ -336,7 +340,7 @@ static bool set_user_name(char *tmpbuf)
336340

337341
static bool set_p_host_name(char *tmpbuf)
338342
{
339-
FILE* fp;
343+
std::FILE* fp;
340344
bool hostname_ok = true;
341345

342346
/* Find the local hostname */
@@ -352,17 +356,17 @@ static bool set_p_host_name(char *tmpbuf)
352356
# ifdef HAS_UNAME
353357
/* get sysname */
354358
uname(&utsn);
355-
strcpy(tmpbuf,utsn.nodename);
359+
std::strcpy(tmpbuf,utsn.nodename);
356360
# else
357361
# ifdef PHOSTCMD
358362
{
359-
FILE* pipefp = popen(PHOSTCMD,"r");
363+
std::FILE* pipefp = popen(PHOSTCMD,"r");
360364

361365
if (pipefp == nullptr) {
362-
printf("Can't find hostname\n");
366+
std::printf("Can't find hostname\n");
363367
finalize(1);
364368
}
365-
fgets(tmpbuf,TCBUF_SIZE,pipefp);
369+
std::fgets(tmpbuf,TCBUF_SIZE,pipefp);
366370
tmpbuf[strlen(tmpbuf)-1] = '\0'; /* wipe out newline */
367371
pclose(pipefp);
368372
}
@@ -378,15 +382,15 @@ static bool set_p_host_name(char *tmpbuf)
378382
const char *filename{PHOSTNAME};
379383
if (FILE_REF(filename) || filename[0] == '~')
380384
{
381-
fp = fopen(filexp(filename), "r");
385+
fp = std::fopen(filexp(filename), "r");
382386
if (fp == nullptr)
383387
{
384-
strcpy(tmpbuf, ".");
388+
std::strcpy(tmpbuf, ".");
385389
}
386390
else
387391
{
388-
fgets(tmpbuf, TCBUF_SIZE, fp);
389-
fclose(fp);
392+
std::fgets(tmpbuf, TCBUF_SIZE, fp);
393+
std::fclose(fp);
390394
char *end = tmpbuf + strlen(tmpbuf) - 1;
391395
if (*end == '\n')
392396
{
@@ -396,26 +400,26 @@ static bool set_p_host_name(char *tmpbuf)
396400
}
397401
else
398402
{
399-
strcpy(tmpbuf, PHOSTNAME);
403+
std::strcpy(tmpbuf, PHOSTNAME);
400404
}
401405

402406
if (tmpbuf[0] == '.') {
403407
if (tmpbuf[1] != '\0')
404408
{
405-
strcpy(g_buf,tmpbuf);
409+
std::strcpy(g_buf,tmpbuf);
406410
}
407411
else
408412
{
409413
g_buf[0] = '\0';
410414
}
411-
strcpy(tmpbuf,g_local_host);
412-
strcat(tmpbuf,g_buf);
415+
std::strcpy(tmpbuf,g_local_host);
416+
std::strcat(tmpbuf,g_buf);
413417
}
414418

415-
if (!strchr(tmpbuf,'.')) {
419+
if (!std::strchr(tmpbuf,'.')) {
416420
if (tmpbuf[0])
417421
{
418-
strcat(tmpbuf, ".");
422+
std::strcat(tmpbuf, ".");
419423
}
420424
#ifdef HAS_RES_INIT
421425
if (!(_res.options & RES_INIT))
@@ -424,19 +428,19 @@ static bool set_p_host_name(char *tmpbuf)
424428
}
425429
if (_res.defdname != nullptr)
426430
{
427-
strcat(tmpbuf,_res.defdname);
431+
std::strcat(tmpbuf,_res.defdname);
428432
}
429433
else
430434
#endif
431435
#ifdef HAS_GETDOMAINNAME
432436
if (getdomainname(g_buf,LBUFLEN) == 0)
433437
{
434-
strcat(tmpbuf,g_buf);
438+
std::strcat(tmpbuf,g_buf);
435439
}
436440
else
437441
#endif
438442
{
439-
strcat(tmpbuf,"UNKNOWN.HOST");
443+
std::strcat(tmpbuf,"UNKNOWN.HOST");
440444
hostname_ok = false;
441445
}
442446
}
@@ -476,7 +480,7 @@ char *export_var(const char *nam, const char *val)
476480
putenv(buff);
477481
return buff;
478482
#else
479-
int namlen = strlen(nam);
483+
int namlen = std::strlen(nam);
480484
int i=envix(nam,namlen); /* where does it go? */
481485
482486
if (!environ[i]) { /* does not exist yet */
@@ -508,7 +512,7 @@ char *export_var(const char *nam, const char *val)
508512
environ[i] = safemalloc((MEM_SIZE)(namlen + strlen(val) + 2));
509513
/* this may or may not be in */
510514
/* the old environ structure */
511-
sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
515+
std::sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
512516
return environ[i] + namlen + 1;
513517
#endif
514518
}
@@ -536,7 +540,7 @@ static int envix(const char *nam, int len)
536540
int i;
537541

538542
for (i = 0; environ[i]; i++) {
539-
if (!strncmp(environ[i],nam,len) && environ[i][len] == '=')
543+
if (!std::strncmp(environ[i],nam,len) && environ[i][len] == '=')
540544
{
541545
break; /* strncmp must come first to avoid */
542546
}

0 commit comments

Comments
 (0)