forked from dagwieers/vsftpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysutil.c
2861 lines (2659 loc) · 60.5 KB
/
sysutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Part of Very Secure FTPd
* Licence: GPL v2
* Author: Chris Evans
*
* sysutil.c
*
* Routines to make the libc/syscall API more pleasant to use. Long term,
* more libc/syscalls will go in here to reduce the number of .c files with
* dependencies on libc or syscalls.
*/
#define PRIVATE_HANDS_OFF_syscall_retval syscall_retval
#define PRIVATE_HANDS_OFF_exit_status exit_status
#include "sysutil.h"
#include "utility.h"
#include "tunables.h"
#include "sysdeputil.h"
/* Activate 64-bit file support on Linux/32bit plus others */
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#define _LARGE_FILES 1
/* For Linux, this adds nothing :-) */
#include "port/porting_junk.h"
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <dirent.h>
#include <time.h>
#include <arpa/inet.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/time.h>
/* Must be before netinet/ip.h. Found on FreeBSD, Solaris */
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <limits.h>
#include <syslog.h>
#include <utime.h>
#include <netdb.h>
#include <sys/resource.h>
/* Private variables to this file */
/* Current umask() */
static unsigned int s_current_umask;
/* Cached time */
static struct timeval s_current_time;
/* Current pid */
static int s_current_pid = -1;
/* Exit function */
static exitfunc_t s_exit_func;
/* Difference in timezone from GMT in seconds */
static long s_timezone;
/* Our internal signal handling implementation details */
static struct vsf_sysutil_sig_details
{
vsf_sighandle_t sync_sig_handler;
void* p_private;
volatile sig_atomic_t pending;
int running;
int use_alarm;
} s_sig_details[NSIG];
static vsf_context_io_t s_io_handler;
static void* s_p_io_handler_private;
static int s_io_handler_running;
struct vsf_sysutil_sockaddr
{
union
{
struct sockaddr u_sockaddr;
struct sockaddr_in u_sockaddr_in;
struct sockaddr_in6 u_sockaddr_in6;
} u;
};
/* File locals */
static void vsf_sysutil_common_sighandler(int signum);
static void vsf_sysutil_alrm_sighandler(int signum);
static int vsf_sysutil_translate_sig(const enum EVSFSysUtilSignal sig);
static void vsf_sysutil_set_sighandler(int sig, void (*p_handlefunc)(int));
static int vsf_sysutil_translate_memprot(
const enum EVSFSysUtilMapPermission perm);
static int vsf_sysutil_translate_openmode(
const enum EVSFSysUtilOpenMode mode);
static void vsf_sysutil_alloc_statbuf(struct vsf_sysutil_statbuf** p_ptr);
void vsf_sysutil_sockaddr_alloc(struct vsf_sysutil_sockaddr** p_sockptr);
static int lock_internal(int fd, int lock_type);
static void
vsf_sysutil_alrm_sighandler(int signum)
{
(void) signum;
alarm(1);
}
static void
vsf_sysutil_common_sighandler(int signum)
{
if (signum < 0 || signum >= NSIG)
{
/* "cannot happen" */
return;
}
if (s_sig_details[signum].sync_sig_handler)
{
s_sig_details[signum].pending = 1;
/* Since this synchronous signal framework has a small race (signal coming
* in just before we start a blocking call), there's the option to fire an
* alarm repeatedly until the signal is handled.
*/
if (s_sig_details[signum].use_alarm)
{
alarm(1);
}
}
}
/* Notes. This signal check is evaluated after potentially blocking system
* calls, i.e. at highly defined points in the code. Since we only interrupt
* at these definite locations, the signal handlers can be non-trivial
* without us having to worry about re-entrancy.
*
* We guarantee that a handler for a given signal is not re-entrant. This
* is taken care of by the "running" flag.
*
* This call itself can only be re-entered once we dereference the actual
* hander function pointer, so we are safe with respect to races modifying
* the "running" flag.
*/
void
vsf_sysutil_check_pending_actions(
const enum EVSFSysUtilInterruptContext context, int retval, int fd)
{
unsigned int i;
/* Check the i/o handler before the signal handlers */
if (s_io_handler && !s_io_handler_running && context == kVSFSysUtilIO)
{
s_io_handler_running = 1;
(*s_io_handler)(retval, fd, s_p_io_handler_private);
s_io_handler_running = 0;
}
for (i=0; i < NSIG; ++i)
{
if (s_sig_details[i].pending && !s_sig_details[i].running)
{
s_sig_details[i].running = 1;
if (s_sig_details[i].use_alarm)
{
alarm(0);
}
if (s_sig_details[i].sync_sig_handler)
{
s_sig_details[i].pending = 0;
(*(s_sig_details[i].sync_sig_handler))(s_sig_details[i].p_private);
}
s_sig_details[i].running = 0;
}
}
}
static int
vsf_sysutil_translate_sig(const enum EVSFSysUtilSignal sig)
{
int realsig = 0;
switch (sig)
{
case kVSFSysUtilSigALRM:
realsig = SIGALRM;
break;
case kVSFSysUtilSigTERM:
realsig = SIGTERM;
break;
case kVSFSysUtilSigCHLD:
realsig = SIGCHLD;
break;
case kVSFSysUtilSigPIPE:
realsig = SIGPIPE;
break;
case kVSFSysUtilSigURG:
realsig = SIGURG;
break;
case kVSFSysUtilSigHUP:
realsig = SIGHUP;
break;
default:
bug("unknown signal in vsf_sysutil_translate_sig");
break;
}
if (realsig < 0 || realsig >= NSIG)
{
bug("signal out of range in vsf_sysutil_translate_sig");
}
return realsig;
}
void
vsf_sysutil_install_sighandler(const enum EVSFSysUtilSignal sig,
vsf_sighandle_t handler,
void* p_private,
int use_alarm)
{
int realsig = vsf_sysutil_translate_sig(sig);
s_sig_details[realsig].p_private = p_private;
s_sig_details[realsig].sync_sig_handler = handler;
s_sig_details[realsig].use_alarm = use_alarm;
vsf_sysutil_set_sighandler(realsig, vsf_sysutil_common_sighandler);
if (use_alarm && realsig != SIGALRM)
{
vsf_sysutil_set_sighandler(SIGALRM, vsf_sysutil_alrm_sighandler);
}
}
void
vsf_sysutil_default_sig(const enum EVSFSysUtilSignal sig)
{
int realsig = vsf_sysutil_translate_sig(sig);
vsf_sysutil_set_sighandler(realsig, SIG_DFL);
s_sig_details[realsig].p_private = NULL;
s_sig_details[realsig].sync_sig_handler = NULL;
}
void
vsf_sysutil_install_null_sighandler(const enum EVSFSysUtilSignal sig)
{
int realsig = vsf_sysutil_translate_sig(sig);
s_sig_details[realsig].p_private = NULL;
s_sig_details[realsig].sync_sig_handler = NULL;
vsf_sysutil_set_sighandler(realsig, vsf_sysutil_common_sighandler);
}
void
vsf_sysutil_install_async_sighandler(const enum EVSFSysUtilSignal sig,
vsf_async_sighandle_t handler)
{
int realsig = vsf_sysutil_translate_sig(sig);
s_sig_details[realsig].p_private = NULL;
s_sig_details[realsig].sync_sig_handler = NULL;
vsf_sysutil_block_sig(sig);
vsf_sysutil_set_sighandler(realsig, handler);
}
static void
vsf_sysutil_set_sighandler(int sig, void (*p_handlefunc)(int))
{
int retval;
struct sigaction sigact;
vsf_sysutil_memclr(&sigact, sizeof(sigact));
sigact.sa_handler = p_handlefunc;
retval = sigfillset(&sigact.sa_mask);
if (retval != 0)
{
die("sigfillset");
}
retval = sigaction(sig, &sigact, NULL);
if (retval != 0)
{
die("sigaction");
}
}
void
vsf_sysutil_block_sig(const enum EVSFSysUtilSignal sig)
{
sigset_t sset;
int retval;
int realsig = vsf_sysutil_translate_sig(sig);
retval = sigemptyset(&sset);
if (retval != 0)
{
die("sigemptyset");
}
retval = sigaddset(&sset, realsig);
if (retval != 0)
{
die("sigaddset");
}
retval = sigprocmask(SIG_BLOCK, &sset, NULL);
if (retval != 0)
{
die("sigprocmask");
}
}
void
vsf_sysutil_unblock_sig(const enum EVSFSysUtilSignal sig)
{
sigset_t sset;
int retval;
int realsig = vsf_sysutil_translate_sig(sig);
retval = sigemptyset(&sset);
if (retval != 0)
{
die("sigemptyset");
}
retval = sigaddset(&sset, realsig);
if (retval != 0)
{
die("sigaddset");
}
retval = sigprocmask(SIG_UNBLOCK, &sset, NULL);
if (retval != 0)
{
die("sigprocmask");
}
}
void
vsf_sysutil_install_io_handler(vsf_context_io_t handler, void* p_private)
{
if (s_io_handler != NULL)
{
bug("double register of i/o handler");
}
s_io_handler = handler;
s_p_io_handler_private = p_private;
}
void
vsf_sysutil_uninstall_io_handler(void)
{
if (s_io_handler == NULL)
{
bug("no i/o handler to unregister!");
}
s_io_handler = NULL;
s_p_io_handler_private = NULL;
}
void
vsf_sysutil_set_alarm(const unsigned int trigger_seconds)
{
(void) alarm(trigger_seconds);
}
void
vsf_sysutil_clear_alarm(void)
{
vsf_sysutil_set_alarm(0);
}
int
vsf_sysutil_read(const int fd, void* p_buf, const unsigned int size)
{
while (1)
{
int retval = read(fd, p_buf, size);
int saved_errno = errno;
vsf_sysutil_check_pending_actions(kVSFSysUtilIO, retval, fd);
if (retval < 0 && saved_errno == EINTR)
{
continue;
}
return retval;
}
}
int
vsf_sysutil_write(const int fd, const void* p_buf, const unsigned int size)
{
while (1)
{
int retval = write(fd, p_buf, size);
int saved_errno = errno;
vsf_sysutil_check_pending_actions(kVSFSysUtilIO, retval, fd);
if (retval < 0 && saved_errno == EINTR)
{
continue;
}
return retval;
}
}
int
vsf_sysutil_read_loop(const int fd, void* p_buf, unsigned int size)
{
int retval;
int num_read = 0;
if (size > INT_MAX)
{
die("size too big in vsf_sysutil_read_loop");
}
while (1)
{
retval = vsf_sysutil_read(fd, (char*)p_buf + num_read, size);
if (retval < 0)
{
return retval;
}
else if (retval == 0)
{
/* Read all we're going to read.. */
return num_read;
}
if ((unsigned int) retval > size)
{
die("retval too big in vsf_sysutil_read_loop");
}
num_read += retval;
size -= (unsigned int) retval;
if (size == 0)
{
/* Hit the read target, cool. */
return num_read;
}
}
}
int
vsf_sysutil_write_loop(const int fd, const void* p_buf, unsigned int size)
{
int retval;
int num_written = 0;
if (size > INT_MAX)
{
die("size too big in vsf_sysutil_write_loop");
}
while (1)
{
retval = vsf_sysutil_write(fd, (const char*)p_buf + num_written, size);
if (retval < 0)
{
/* Error */
return retval;
}
else if (retval == 0)
{
/* Written all we're going to write.. */
return num_written;
}
if ((unsigned int) retval > size)
{
die("retval too big in vsf_sysutil_write_loop");
}
num_written += retval;
size -= (unsigned int) retval;
if (size == 0)
{
/* Hit the write target, cool. */
return num_written;
}
}
}
filesize_t
vsf_sysutil_get_file_offset(const int file_fd)
{
filesize_t retval = lseek(file_fd, 0, SEEK_CUR);
if (retval < 0)
{
die("lseek");
}
return retval;
}
void
vsf_sysutil_lseek_to(const int fd, filesize_t seek_pos)
{
filesize_t retval;
if (seek_pos < 0)
{
die("negative seek_pos in vsf_sysutil_lseek_to");
}
retval = lseek(fd, seek_pos, SEEK_SET);
if (retval < 0)
{
die("lseek");
}
}
void
vsf_sysutil_lseek_end(const int fd)
{
filesize_t retval;
retval = lseek(fd, 0, SEEK_END);
if (retval < 0)
{
die("lseek");
}
}
void*
vsf_sysutil_malloc(unsigned int size)
{
void* p_ret;
/* Paranoia - what if we got an integer overflow/underflow? */
if (size == 0 || size > INT_MAX)
{
bug("zero or big size in vsf_sysutil_malloc");
}
p_ret = malloc(size);
if (p_ret == NULL)
{
die("malloc");
}
return p_ret;
}
void*
vsf_sysutil_realloc(void* p_ptr, unsigned int size)
{
void* p_ret;
if (size == 0 || size > INT_MAX)
{
bug("zero or big size in vsf_sysutil_realloc");
}
p_ret = realloc(p_ptr, size);
if (p_ret == NULL)
{
die("realloc");
}
return p_ret;
}
void
vsf_sysutil_free(void* p_ptr)
{
if (p_ptr == NULL)
{
bug("vsf_sysutil_free got a null pointer");
}
free(p_ptr);
}
unsigned int
vsf_sysutil_getpid(void)
{
if (s_current_pid == -1)
{
s_current_pid = vsf_sysutil_getpid_nocache();
}
return (unsigned int) s_current_pid;
}
int
vsf_sysutil_fork(void)
{
int retval = vsf_sysutil_fork_failok();
if (retval < 0)
{
die("fork");
}
return retval;
}
int
vsf_sysutil_fork_failok(void)
{
int retval;
retval = fork();
if (retval == 0)
{
vsf_sysutil_post_fork();
}
return retval;
}
void
vsf_sysutil_set_exit_func(exitfunc_t exitfunc)
{
s_exit_func = exitfunc;
}
void
vsf_sysutil_exit(int exit_code)
{
if (s_exit_func)
{
exitfunc_t curr_func = s_exit_func;
/* Prevent recursion */
s_exit_func = 0;
(*curr_func)();
}
_exit(exit_code);
}
struct vsf_sysutil_wait_retval
vsf_sysutil_wait(void)
{
struct vsf_sysutil_wait_retval retval;
vsf_sysutil_memclr(&retval, sizeof(retval));
while (1)
{
int sys_ret = wait(&retval.exit_status);
if (sys_ret < 0 && errno == EINTR)
{
vsf_sysutil_check_pending_actions(kVSFSysUtilUnknown, 0, 0);
continue;
}
retval.syscall_retval = sys_ret;
return retval;
}
}
int
vsf_sysutil_wait_reap_one(void)
{
int retval = waitpid(-1, NULL, WNOHANG);
if (retval == 0 || (retval < 0 && errno == ECHILD))
{
/* No more children */
return 0;
}
if (retval < 0)
{
die("waitpid");
}
/* Got one */
return retval;
}
int
vsf_sysutil_wait_get_retval(const struct vsf_sysutil_wait_retval* p_waitret)
{
return p_waitret->syscall_retval;
}
int
vsf_sysutil_wait_exited_normally(
const struct vsf_sysutil_wait_retval* p_waitret)
{
int status = ((struct vsf_sysutil_wait_retval*) p_waitret)->exit_status;
return WIFEXITED(status);
}
int
vsf_sysutil_wait_get_exitcode(const struct vsf_sysutil_wait_retval* p_waitret)
{
int status;
if (!vsf_sysutil_wait_exited_normally(p_waitret))
{
bug("not a normal exit in vsf_sysutil_wait_get_exitcode");
}
status = ((struct vsf_sysutil_wait_retval*) p_waitret)->exit_status;
return WEXITSTATUS(status);
}
void
vsf_sysutil_activate_keepalive(int fd)
{
int keepalive = 1;
int retval = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive,
sizeof(keepalive));
if (retval != 0)
{
die("setsockopt: keepalive");
}
}
void
vsf_sysutil_activate_reuseaddr(int fd)
{
int reuseaddr = 1;
int retval = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
sizeof(reuseaddr));
if (retval != 0)
{
die("setsockopt: reuseaddr");
}
}
void
vsf_sysutil_set_nodelay(int fd)
{
int nodelay = 1;
int retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay,
sizeof(nodelay));
if (retval != 0)
{
die("setsockopt: nodelay");
}
}
void
vsf_sysutil_activate_sigurg(int fd)
{
int retval = fcntl(fd, F_SETOWN, vsf_sysutil_getpid());
if (retval != 0)
{
die("fcntl");
}
}
void
vsf_sysutil_activate_oobinline(int fd)
{
int oob_inline = 1;
int retval = setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &oob_inline,
sizeof(oob_inline));
if (retval != 0)
{
die("setsockopt: oobinline");
}
}
void
vsf_sysutil_set_iptos_throughput(int fd)
{
int tos = IPTOS_THROUGHPUT;
/* Ignore failure to set (maybe this IP stack demands privilege for this) */
(void) setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
}
void
vsf_sysutil_activate_linger(int fd)
{
int retval;
struct linger the_linger;
vsf_sysutil_memclr(&the_linger, sizeof(the_linger));
the_linger.l_onoff = 1;
the_linger.l_linger = 60 * 10;
retval = setsockopt(fd, SOL_SOCKET, SO_LINGER, &the_linger,
sizeof(the_linger));
if (retval != 0)
{
die("setsockopt: linger");
}
}
void
vsf_sysutil_deactivate_linger_failok(int fd)
{
struct linger the_linger;
the_linger.l_onoff = 0;
the_linger.l_linger = 0;
(void) setsockopt(fd, SOL_SOCKET, SO_LINGER, &the_linger, sizeof(the_linger));
}
void
vsf_sysutil_activate_noblock(int fd)
{
int retval;
int curr_flags = fcntl(fd, F_GETFL);
if (vsf_sysutil_retval_is_error(curr_flags))
{
die("fcntl");
}
curr_flags |= O_NONBLOCK;
retval = fcntl(fd, F_SETFL, curr_flags);
if (retval != 0)
{
die("fcntl");
}
}
void
vsf_sysutil_deactivate_noblock(int fd)
{
int retval;
int curr_flags = fcntl(fd, F_GETFL);
if (vsf_sysutil_retval_is_error(curr_flags))
{
die("fcntl");
}
curr_flags &= ~O_NONBLOCK;
retval = fcntl(fd, F_SETFL, curr_flags);
if (retval != 0)
{
die("fcntl");
}
}
int
vsf_sysutil_recv_peek(const int fd, void* p_buf, unsigned int len)
{
while (1)
{
int retval = recv(fd, p_buf, len, MSG_PEEK);
int saved_errno = errno;
vsf_sysutil_check_pending_actions(kVSFSysUtilIO, retval, fd);
if (retval < 0 && saved_errno == EINTR)
{
continue;
}
return retval;
}
}
int
vsf_sysutil_atoi(const char* p_str)
{
return atoi(p_str);
}
filesize_t
vsf_sysutil_a_to_filesize_t(const char* p_str)
{
/* atoll() is C99 standard - but even modern FreeBSD, OpenBSD don't have
* it, so we'll supply our own
*/
filesize_t result = 0;
filesize_t mult = 1;
unsigned int len = vsf_sysutil_strlen(p_str);
unsigned int i;
/* Bail if the number is excessively big (petabytes!) */
if (len > 15)
{
return 0;
}
for (i=0; i<len; ++i)
{
char the_char = p_str[len-(i+1)];
filesize_t val;
if (the_char < '0' || the_char > '9')
{
return 0;
}
val = the_char - '0';
val *= mult;
result += val;
mult *= 10;
}
return result;
}
const char*
vsf_sysutil_ulong_to_str(unsigned long the_ulong)
{
static char ulong_buf[32];
(void) snprintf(ulong_buf, sizeof(ulong_buf), "%lu", the_ulong);
return ulong_buf;
}
const char*
vsf_sysutil_filesize_t_to_str(filesize_t the_filesize)
{
static char filesize_buf[32];
if (sizeof(long) == 8)
{
/* Avoid using non-standard %ll if we can */
(void) snprintf(filesize_buf, sizeof(filesize_buf), "%ld",
(long) the_filesize);
}
else
{
(void) snprintf(filesize_buf, sizeof(filesize_buf), "%lld", the_filesize);
}
return filesize_buf;
}
const char*
vsf_sysutil_double_to_str(double the_double)
{
static char double_buf[32];
(void) snprintf(double_buf, sizeof(double_buf), "%.2f", the_double);
return double_buf;
}
const char*
vsf_sysutil_uint_to_octal(unsigned int the_uint)
{
static char octal_buf[32];
if (the_uint == 0)
{
octal_buf[0] = '0';
octal_buf[1] = '\0';
}
else
{
(void) snprintf(octal_buf, sizeof(octal_buf), "0%o", the_uint);
}
return octal_buf;
}
unsigned int
vsf_sysutil_octal_to_uint(const char* p_str)
{
/* NOTE - avoiding using sscanf() parser */
unsigned int result = 0;
int seen_non_zero_digit = 0;
while (*p_str != '\0')
{
int digit = *p_str;
if (!isdigit(digit) || digit > '7')
{
break;
}
if (digit != '0')
{
seen_non_zero_digit = 1;
}
if (seen_non_zero_digit)
{
result <<= 3;
result += (digit - '0');
}
p_str++;
}
return result;
}
int
vsf_sysutil_toupper(int the_char)
{
return toupper((unsigned char) the_char);
}
int
vsf_sysutil_isspace(int the_char)
{
return isspace((unsigned char) the_char);
}
int
vsf_sysutil_isprint(int the_char)
{
/* From Solar - we know better than some libc's! Don't let any potential
* control chars through
*/
unsigned char uc = (unsigned char) the_char;
if (uc <= 31)
{
return 0;
}
if (uc == 177)
{
return 0;
}
if (uc >= 128 && uc <= 159)
{
return 0;
}
return isprint(the_char);
}
int
vsf_sysutil_isalnum(int the_char)
{
return isalnum((unsigned char) the_char);
}
int
vsf_sysutil_isdigit(int the_char)
{
return isdigit((unsigned char) the_char);
}
char*
vsf_sysutil_getcwd(char* p_dest, const unsigned int buf_size)
{
char* p_retval;
if (buf_size == 0) {
return p_dest;
}
p_retval = getcwd(p_dest, buf_size);
p_dest[buf_size - 1] = '\0';
return p_retval;
}
int
vsf_sysutil_mkdir(const char* p_dirname, const unsigned int mode)
{
return mkdir(p_dirname, mode);
}
int
vsf_sysutil_rmdir(const char* p_dirname)
{
return rmdir(p_dirname);
}
int
vsf_sysutil_chdir(const char* p_dirname)
{
return chdir(p_dirname);
}
int
vsf_sysutil_rename(const char* p_from, const char* p_to)
{
return rename(p_from, p_to);
}
struct vsf_sysutil_dir*
vsf_sysutil_opendir(const char* p_dirname)
{
return (struct vsf_sysutil_dir*) opendir(p_dirname);
}
void
vsf_sysutil_closedir(struct vsf_sysutil_dir* p_dir)
{
DIR* p_real_dir = (DIR*) p_dir;