* New alpha version 2.24.1
[alpine.git] / pith / readfile.c
blob443953d4168e91aaa92ccde0286ad398c68adc58
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: readfile.c 761 2007-10-23 22:35:18Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2013-2021 Eduardo Chappa
8 * Copyright 2006 University of Washington
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../pith/headers.h"
21 #include "../pith/store.h"
23 #include "readfile.h"
26 /*----------------------------------------------------------------------
27 Read whole file into memory
29 Args: filename -- path name of file to read
31 Result: Returns pointer to malloced memory with the contents of the file
32 or NULL
34 This won't work very well if the file has NULLs in it.
35 ----*/
36 char *
37 read_file(char *filename, int so_get_flags)
39 STORE_S *in_file = NULL, *out_store = NULL;
40 unsigned char c;
41 char *return_text = NULL;
43 if((in_file = so_get(FileStar, filename, so_get_flags | READ_ACCESS))){
46 if(!(out_store = so_get(CharStar, NULL, EDIT_ACCESS))){
47 so_give(&in_file);
48 return NULL;
52 * We're just using the READ_FROM_LOCALE flag to translate
53 * to UTF-8.
55 while(so_readc(&c, in_file))
56 so_writec(c, out_store);
58 if(in_file)
59 so_give(&in_file);
61 if(out_store){
62 return_text = (char *) so_text(out_store);
63 /* avoid freeing this */
64 if(out_store->txt)
65 out_store->txt = NULL;
67 so_give(&out_store);
71 return(return_text);
74 /* our copy, to_file and from_file must be full paths. from_file
75 * must exist.
77 int
78 our_copy(char *to_file, char *from_file)
80 STORE_S *in_cert, *out_cert;
81 unsigned char c;
83 in_cert = so_get(FileStar, from_file, READ_ACCESS | READ_FROM_LOCALE);
84 if (in_cert == NULL)
85 return -1;
87 out_cert = so_get(FileStar, to_file, WRITE_ACCESS | WRITE_TO_LOCALE);
88 if (out_cert == NULL){
89 so_give(&in_cert);
90 return -1;
93 so_seek(out_cert, 0L, 0);
94 so_truncate(out_cert, 0);
96 while(so_readc(&c, in_cert) > 0)
97 so_writec(c, out_cert);
99 so_give(&in_cert);
100 so_give(&out_cert);
102 return 0;