1 /* tag: Tom Lord Tue Dec  4 14:41:40 2001 (safe-vu-utils.c)
2  */
3 /* safe-vu-utils.c -
4  *
5  ****************************************************************
6  * Copyright (C) 2000 Tom Lord
7  *
8  * See the file "COPYING" for further information about
9  * the copyright and warranty status of this work.
10  */
11 
12 
13 #include "hackerlab/os/errno-to-string.h"
14 #include "hackerlab/bugs/panic.h"
15 #include "hackerlab/vu/printfmt.h"
16 #include "hackerlab/vu/safe-vu-utils.h"
17 
18 
19 
20 
21 /*(c safe_move_fd)
22  * int safe_move_fd (int fd, int newfd);
23  *
24  * See xref:"move_fd".
25  */
26 int
safe_move_fd(int fd,int newfd)27 safe_move_fd (int fd, int newfd)
28 {
29   int errn;
30   int answer;
31 
32   answer = vu_move_fd (&errn, fd, newfd);
33   if (0 > answer)
34     {
35       printfmt (&errn, 2, "Error calling `vu_move_fd' to move descriptor %d to descriptor %d (%s)\n", fd, newfd, errno_to_string (errn));
36       panic ("I/O error");
37     }
38   return answer;
39 }
40 
41 
42 /*(c safe_file_to_string)
43  * int safe_file_to_string (t_uchar ** buf,
44  *                          size_t * len,
45  *                          int fd);
46  *
47  * Read the entire contents of `fd' into a newly allocated string.
48  *
49  * The new string is returned in `*buf'; its length in `*len'.
50  *
51  * If `vu_fstat' is able to report the length of the file, a single call to
52  * `vu_read_retry' is used to read its contents.  Otherwise, repeated calls
53  * to `vu_read_retry' are used.
54  */
55 void
safe_file_to_string(t_uchar ** buf,size_t * len,int fd)56 safe_file_to_string (t_uchar ** buf, size_t * len, int fd)
57 {
58   int errn;
59   int answer;
60 
61   answer = vu_file_to_string (&errn, buf, len, fd);
62   if (0 > answer)
63     {
64       printfmt (&errn, 2, "Error calling `vu_file_to_string' for descriptor %d (%s)\n", fd, errno_to_string (errn));
65       panic ("I/O error");
66     }
67 }
68 
69 
70 /*(c safe_file_is_directory)
71  * int safe_file_is_directory (const t_uchar * name);
72  *
73  * Return 1 if `name' names a directory, 0 if not.
74  *
75  * (If `name' is a symbolic link, even to a directory, 0 is
76  * returned, but see also xref:"safe_file_is_directory_following".)
77  *
78  * Non-existence of any file called `name' is not an error.
79  */
80 int
safe_file_is_directory(const t_uchar * name)81 safe_file_is_directory (const t_uchar * name)
82 {
83   int errn;
84   int answer;
85 
86   answer = vu_file_is_directory (&errn, name);
87 
88   if (0 > answer)
89     {
90       printfmt (&errn, 2, "Error calling `vu_file_is_directory' (%s)\n", errno_to_string (errn));
91       printfmt (&errn, 2, "  file %s\n", name);
92       panic ("I/O error");
93     }
94   return answer;
95 }
96 
97 
98 /*(c safe_file_is_directory_following)
99  * int safe_file_is_directory_following (const t_uchar * name);
100  *
101  * Return 1 if `name' names a directory or symlink to a directory,
102  * 0 if not.
103  *
104  * Non-existence of any file called `name' is not an error.
105  */
106 int
safe_file_is_directory_following(const t_uchar * name)107 safe_file_is_directory_following (const t_uchar * name)
108 {
109   int errn;
110   int answer;
111 
112   answer = vu_file_is_directory_following (&errn, name);
113 
114   if (0 > answer)
115     {
116       printfmt (&errn, 2, "Error calling `vu_file_is_directory_following' (%s)\n", errno_to_string (errn));
117       printfmt (&errn, 2, "  file %s\n", name);
118       panic ("I/O error");
119     }
120   return answer;
121 }
122