Deleted Added
full compact
1/*
2 * Miscellaneous support routines..
3 *
4 * $Id: misc.c,v 1.11.2.2 1995/06/01 22:32:06 jkh Exp $
4 * $Id: misc.c,v 1.12.2.8 1995/11/04 15:08:21 jkh Exp $
5 *
6 * Copyright (c) 1995
7 * Jordan Hubbard. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright

--- 27 unchanged lines hidden (view full) ---

40
41#include "sysinstall.h"
42#include <ctype.h>
43#include <unistd.h>
44#include <sys/stat.h>
45#include <sys/errno.h>
46#include <sys/file.h>
47#include <sys/types.h>
48#include <dirent.h>
49#include <sys/wait.h>
50#include <sys/param.h>
51#include <sys/mount.h>
52#include <sys/reboot.h>
53#include <sys/dkbad.h>
54#include <sys/disklabel.h>
55
56/* Quick check to see if a file is readable */

--- 15 unchanged lines hidden (view full) ---

72}
73
74/* Concatenate two strings into static storage */
75char *
76string_concat(char *one, char *two)
77{
78 static char tmp[FILENAME_MAX];
79
80 /* Yes, we're deliberately cavalier about not checking for overflow */
81 strcpy(tmp, one);
82 strcat(tmp, two);
83 return tmp;
84}
85
86/* Concatenate three strings into static storage */
87char *
88string_concat3(char *one, char *two, char *three)
89{
90 static char tmp[FILENAME_MAX];
91
92 /* Yes, we're deliberately cavalier about not checking for overflow */
93 strcpy(tmp, one);
94 strcat(tmp, two);
95 strcat(tmp, three);
96 return tmp;
97}
98
99/* Clip the whitespace off the end of a string */
100char *
101string_prune(char *str)
102{
103 int len = str ? strlen(str) : 0;
104
105 while (len && isspace(str[len - 1]))
106 str[--len] = '\0';

--- 4 unchanged lines hidden (view full) ---

111char *
112string_skipwhite(char *str)
113{
114 while (*str && isspace(*str))
115 ++str;
116 return str;
117}
118
119/* copy optionally and allow second arg to be null */
120char *
121string_copy(char *s1, char *s2)
122{
123 if (!s1)
124 return NULL;
125 if (!s2)
126 s1[0] = '\0';
127 else
128 strcpy(s1, s2);
129 return s1;
130}
131
132Boolean
133directoryExists(const char *dirname)
134{
135 DIR *tptr;
136
137 if (!dirname)
138 return FALSE;
139 if (!strlen(dirname))
140 return FALSE;
141
142 tptr = opendir(dirname);
143 if (!tptr)
144 return (FALSE);
145
146 closedir(tptr);
147 return (TRUE);
148}
149
150char *
151pathBaseName(const char *path)
152{
153 char *pt;
154 char *ret = (char *)path;
155
156 pt = strrchr(path,(int)'/');
157
158 if (pt != 0) /* if there is a slash */
159 {
160 ret = ++pt; /* start the file after it */
161 }
162
163 return(ret);
164}
165
166/* A free guaranteed to take NULL ptrs */
167void
168safe_free(void *ptr)
169{
170 if (ptr)
171 free(ptr);
172}
173
174/* A malloc that checks errors */
175void *
176safe_malloc(size_t size)
177{
178 void *ptr;
179
180 if (size <= 0)
181 msgFatal("Invalid malloc size of %d!", size);
182 ptr = malloc(size);
183 if (!ptr)
184 msgFatal("Out of memory!");
185 bzero(ptr, size);
186 return ptr;
187}
188
189/* A realloc that checks errors */
190void *
191safe_realloc(void *orig, size_t size)
192{
193 void *ptr;

--- 43 unchanged lines hidden (view full) ---

237
238int
239Mkdir(char *ipath, void *data)
240{
241 struct stat sb;
242 int final=0;
243 char *p, *path;
244
182 if (access(ipath, R_OK) == 0)
183 return 0;
245 if (file_readable(ipath))
246 return RET_SUCCESS;
247
248 path = strdup(ipath);
249 if (isDebug())
250 msgDebug("mkdir(%s)\n", path);
251 p = path;
252 if (p[0] == '/') /* Skip leading '/'. */
253 ++p;
254 for (;!final; ++p) {
255 if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0'))
256 final++;
257 else if (p[0] != '/')
258 continue;
259 *p = '\0';
260 if (stat(path, &sb)) {
261 if (errno != ENOENT) {
262 dialog_clear();
263 msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno));
200 return 1;
264 return RET_FAIL;
265 }
266 if (isDebug())
267 msgDebug("mkdir(%s..)\n", path);
268 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
269 dialog_clear();
270 msgConfirm("Couldn't create directory %s: %s", path,strerror(errno));
206 return 1;
271 return RET_FAIL;
272 }
273 }
274 *p = '/';
275 }
276 free(path);
212 return 0;
277 return RET_SUCCESS;
278}
279
280int
281Mount(char *mountp, void *dev)
282{
283 struct ufs_args ufsargs;
284 char device[80];
285 char mountpoint[FILENAME_MAX];

--- 4 unchanged lines hidden (view full) ---

290 }
291 else {
292 strcpy(device, dev);
293 strcpy(mountpoint, mountp);
294 }
295 memset(&ufsargs,0,sizeof ufsargs);
296
297 if (Mkdir(mountpoint, NULL)) {
298 dialog_clear();
299 msgConfirm("Unable to make directory mountpoint for %s!", mountpoint);
234 return 1;
300 return RET_FAIL;
301 }
302 if (isDebug())
303 msgDebug("mount %s %s\n", device, mountpoint);
304 bzero(&ufsargs, sizeof(ufsargs));
305 ufsargs.fspec = device;
306 if (mount(MOUNT_UFS, mountpoint, 0, (caddr_t)&ufsargs) == -1) {
241 msgConfirm("Error mounting %s on %s : %s\n", device, mountpoint, strerror(errno));
242 return 1;
307 dialog_clear();
308 msgConfirm("Error mounting %s on %s : %s", device, mountpoint, strerror(errno));
309 return RET_FAIL;
310 }
244 return 0;
311 return RET_SUCCESS;
312}