misc.c (9202) | misc.c (12661) |
---|---|
1/* 2 * Miscellaneous support routines.. 3 * | 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> | 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> |
|
48#include <sys/wait.h> 49#include <sys/param.h> 50#include <sys/mount.h> 51#include <sys/reboot.h> 52#include <sys/dkbad.h> 53#include <sys/disklabel.h> 54 55/* Quick check to see if a file is readable */ --- 15 unchanged lines hidden (view full) --- 71} 72 73/* Concatenate two strings into static storage */ 74char * 75string_concat(char *one, char *two) 76{ 77 static char tmp[FILENAME_MAX]; 78 | 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 */ |
|
79 strcpy(tmp, one); 80 strcat(tmp, two); 81 return tmp; 82} 83 | 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 |
|
84/* Clip the whitespace off the end of a string */ 85char * 86string_prune(char *str) 87{ 88 int len = str ? strlen(str) : 0; 89 90 while (len && isspace(str[len - 1])) 91 str[--len] = '\0'; --- 4 unchanged lines hidden (view full) --- 96char * 97string_skipwhite(char *str) 98{ 99 while (*str && isspace(*str)) 100 ++str; 101 return str; 102} 103 | 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 |
|
104/* A free guaranteed to take NULL ptrs */ 105void 106safe_free(void *ptr) 107{ 108 if (ptr) 109 free(ptr); 110} 111 112/* A malloc that checks errors */ 113void * 114safe_malloc(size_t size) 115{ 116 void *ptr; 117 118 if (size <= 0) 119 msgFatal("Invalid malloc size of %d!", size); 120 ptr = malloc(size); 121 if (!ptr) 122 msgFatal("Out of memory!"); | 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); |
|
123 return ptr; 124} 125 126/* A realloc that checks errors */ 127void * 128safe_realloc(void *orig, size_t size) 129{ 130 void *ptr; --- 43 unchanged lines hidden (view full) --- 174 175int 176Mkdir(char *ipath, void *data) 177{ 178 struct stat sb; 179 int final=0; 180 char *p, *path; 181 | 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; |
184 185 path = strdup(ipath); 186 if (isDebug()) 187 msgDebug("mkdir(%s)\n", path); 188 p = path; 189 if (p[0] == '/') /* Skip leading '/'. */ 190 ++p; 191 for (;!final; ++p) { 192 if (p[0] == '\0' || (p[0] == '/' && p[1] == '\0')) 193 final++; 194 else if (p[0] != '/') 195 continue; 196 *p = '\0'; 197 if (stat(path, &sb)) { 198 if (errno != ENOENT) { | 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(); |
|
199 msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno)); | 263 msgConfirm("Couldn't stat directory %s: %s", path, strerror(errno)); |
200 return 1; | 264 return RET_FAIL; |
201 } 202 if (isDebug()) 203 msgDebug("mkdir(%s..)\n", path); 204 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { | 265 } 266 if (isDebug()) 267 msgDebug("mkdir(%s..)\n", path); 268 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { |
269 dialog_clear(); |
|
205 msgConfirm("Couldn't create directory %s: %s", path,strerror(errno)); | 270 msgConfirm("Couldn't create directory %s: %s", path,strerror(errno)); |
206 return 1; | 271 return RET_FAIL; |
207 } 208 } 209 *p = '/'; 210 } 211 free(path); | 272 } 273 } 274 *p = '/'; 275 } 276 free(path); |
212 return 0; | 277 return RET_SUCCESS; |
213} 214 215int 216Mount(char *mountp, void *dev) 217{ 218 struct ufs_args ufsargs; 219 char device[80]; 220 char mountpoint[FILENAME_MAX]; --- 4 unchanged lines hidden (view full) --- 225 } 226 else { 227 strcpy(device, dev); 228 strcpy(mountpoint, mountp); 229 } 230 memset(&ufsargs,0,sizeof ufsargs); 231 232 if (Mkdir(mountpoint, NULL)) { | 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(); |
|
233 msgConfirm("Unable to make directory mountpoint for %s!", mountpoint); | 299 msgConfirm("Unable to make directory mountpoint for %s!", mountpoint); |
234 return 1; | 300 return RET_FAIL; |
235 } 236 if (isDebug()) 237 msgDebug("mount %s %s\n", device, mountpoint); 238 bzero(&ufsargs, sizeof(ufsargs)); 239 ufsargs.fspec = device; 240 if (mount(MOUNT_UFS, mountpoint, 0, (caddr_t)&ufsargs) == -1) { | 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; |
243 } | 310 } |
244 return 0; | 311 return RET_SUCCESS; |
245} | 312} |