strio.c (90792) | strio.c (94334) |
---|---|
1/* | 1/* |
2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers. | 2 * Copyright (c) 2000-2002 Sendmail, Inc. and its suppliers. |
3 * All rights reserved. 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * By using this file, you agree to the terms and conditions set 11 * forth in the LICENSE file which can be found at the top level of 12 * the sendmail distribution. 13 */ 14 15#include <sm/gen.h> | 3 * All rights reserved. 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * By using this file, you agree to the terms and conditions set 11 * forth in the LICENSE file which can be found at the top level of 12 * the sendmail distribution. 13 */ 14 15#include <sm/gen.h> |
16SM_IDSTR(id, "@(#)$Id: strio.c,v 1.40 2001/09/11 04:04:49 gshapiro Exp $") | 16SM_IDSTR(id, "@(#)$Id: strio.c,v 1.42 2002/02/11 23:05:50 gshapiro Exp $") |
17#include <stdlib.h> 18#include <unistd.h> 19#include <fcntl.h> 20#include <string.h> 21#include <errno.h> 22#include <sm/rpool.h> 23#include <sm/io.h> 24#include <sm/heap.h> 25#include <sm/conf.h> 26#include "local.h" 27 28/* 29** Cookie structure for the "strio" file type 30*/ 31 32struct sm_str_obj 33{ | 17#include <stdlib.h> 18#include <unistd.h> 19#include <fcntl.h> 20#include <string.h> 21#include <errno.h> 22#include <sm/rpool.h> 23#include <sm/io.h> 24#include <sm/heap.h> 25#include <sm/conf.h> 26#include "local.h" 27 28/* 29** Cookie structure for the "strio" file type 30*/ 31 32struct sm_str_obj 33{ |
34 char *strio_base; 35 char *strio_end; 36 size_t strio_size; 37 size_t strio_offset; 38 int strio_flags; | 34 char *strio_base; 35 char *strio_end; 36 size_t strio_size; 37 size_t strio_offset; 38 int strio_flags; 39 const void *strio_rpool; |
39}; 40 41typedef struct sm_str_obj SM_STR_OBJ_T; 42 43/* 44** SM_STRGROW -- increase storage space for string 45** 46** Parameters: --- 57 unchanged lines hidden (view full) --- 104 return len; 105} 106 107/* 108** SM_STRWRITE -- write a portion of the string 109** 110** Parameters: 111** fp -- the file pointer | 40}; 41 42typedef struct sm_str_obj SM_STR_OBJ_T; 43 44/* 45** SM_STRGROW -- increase storage space for string 46** 47** Parameters: --- 57 unchanged lines hidden (view full) --- 105 return len; 106} 107 108/* 109** SM_STRWRITE -- write a portion of the string 110** 111** Parameters: 112** fp -- the file pointer |
112** buf -- location of data for writting | 113** buf -- location of data for writing |
113** n -- number of bytes to write 114** 115** Returns: 116** Failure: -1 and sets errno 117** Success: >=0, number of bytes written 118*/ 119 120ssize_t --- 74 unchanged lines hidden (view full) --- 195 return ret; 196} 197 198/* 199** SM_STROPEN -- open a string file type 200** 201** Parameters: 202** fp -- file pointer open to be associated with | 114** n -- number of bytes to write 115** 116** Returns: 117** Failure: -1 and sets errno 118** Success: >=0, number of bytes written 119*/ 120 121ssize_t --- 74 unchanged lines hidden (view full) --- 196 return ret; 197} 198 199/* 200** SM_STROPEN -- open a string file type 201** 202** Parameters: 203** fp -- file pointer open to be associated with |
203** info -- flags for methods of access (was mode) 204** flags -- ignored | 204** info -- initial contents (NULL for none) 205** flags -- flags for methods of access (was mode) |
205** rpool -- resource pool to use memory from (if applicable) 206** 207** Results: 208** Success: 0 (zero) 209** Failure: -1 and sets errno 210*/ 211 212int 213sm_stropen(fp, info, flags, rpool) 214 SM_FILE_T *fp; 215 const void *info; 216 int flags; 217 const void *rpool; 218{ 219 register SM_STR_OBJ_T *s; | 206** rpool -- resource pool to use memory from (if applicable) 207** 208** Results: 209** Success: 0 (zero) 210** Failure: -1 and sets errno 211*/ 212 213int 214sm_stropen(fp, info, flags, rpool) 215 SM_FILE_T *fp; 216 const void *info; 217 int flags; 218 const void *rpool; 219{ 220 register SM_STR_OBJ_T *s; |
220 int *strmode = (int *) info; | |
221 222#if SM_RPOOL 223 s = sm_rpool_malloc_x(rpool, sizeof(SM_STR_OBJ_T)); 224#else /* SM_RPOOL */ 225 s = sm_malloc(sizeof(SM_STR_OBJ_T)); 226 if (s == NULL) | 221 222#if SM_RPOOL 223 s = sm_rpool_malloc_x(rpool, sizeof(SM_STR_OBJ_T)); 224#else /* SM_RPOOL */ 225 s = sm_malloc(sizeof(SM_STR_OBJ_T)); 226 if (s == NULL) |
227 { 228 errno = ENOMEM; | |
229 return -1; | 227 return -1; |
230 } | |
231#endif /* SM_RPOOL */ 232 233 fp->f_cookie = s; | 228#endif /* SM_RPOOL */ 229 230 fp->f_cookie = s; |
231 s->strio_rpool = rpool; |
|
234 s->strio_offset = 0; | 232 s->strio_offset = 0; |
235 s->strio_base = 0; | 233 s->strio_size = 0; 234 s->strio_base = NULL; |
236 s->strio_end = 0; | 235 s->strio_end = 0; |
237 switch (*strmode) | 236 237 switch (flags) |
238 { 239 case SM_IO_RDWR: 240 s->strio_flags = SMRW; 241 break; 242 case SM_IO_RDONLY: 243 s->strio_flags = SMRD; 244 break; 245 case SM_IO_WRONLY: 246 s->strio_flags = SMWR; 247 break; 248 case SM_IO_APPEND: | 238 { 239 case SM_IO_RDWR: 240 s->strio_flags = SMRW; 241 break; 242 case SM_IO_RDONLY: 243 s->strio_flags = SMRD; 244 break; 245 case SM_IO_WRONLY: 246 s->strio_flags = SMWR; 247 break; 248 case SM_IO_APPEND: |
249 if (s->strio_rpool == NULL) 250 sm_free(s); 251 errno = EINVAL; |
|
249 return -1; 250 default: | 252 return -1; 253 default: |
254 if (s->strio_rpool == NULL) 255 sm_free(s); |
|
251 errno = EINVAL; 252 return -1; 253 } | 256 errno = EINVAL; 257 return -1; 258 } |
259 260 if (info != NULL) 261 { 262 s->strio_base = sm_strdup_x(info); 263 if (s->strio_base == NULL) 264 { 265 int save_errno = errno; 266 267 if (s->strio_rpool == NULL) 268 sm_free(s); 269 errno = save_errno; 270 return -1; 271 } 272 s->strio_size = strlen(info); 273 s->strio_end = s->strio_base + s->strio_size; 274 } |
|
254 return 0; 255} 256 257/* 258** SM_STRCLOSE -- close the string file type and free resources 259** 260** Parameters: 261** fp -- file pointer --- 205 unchanged lines hidden --- | 275 return 0; 276} 277 278/* 279** SM_STRCLOSE -- close the string file type and free resources 280** 281** Parameters: 282** fp -- file pointer --- 205 unchanged lines hidden --- |