* Create help for explaining how encrypted password file support
[alpine.git] / pith / readfile.c
blobcae03fc17d9f014d123723bb11216bbe80ab86df
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 2006 University of Washington
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * ========================================================================
18 #include "../pith/headers.h"
20 #include "../pith/store.h"
22 #include "readfile.h"
25 /*----------------------------------------------------------------------
26 Read whole file into memory
28 Args: filename -- path name of file to read
30 Result: Returns pointer to malloced memory with the contents of the file
31 or NULL
33 This won't work very well if the file has NULLs in it.
34 ----*/
35 char *
36 read_file(char *filename, int so_get_flags)
38 STORE_S *in_file = NULL, *out_store = NULL;
39 unsigned char c;
40 char *return_text = NULL;
42 if((in_file = so_get(FileStar, filename, so_get_flags | READ_ACCESS))){
45 if(!(out_store = so_get(CharStar, NULL, EDIT_ACCESS))){
46 so_give(&in_file);
47 return NULL;
51 * We're just using the READ_FROM_LOCALE flag to translate
52 * to UTF-8.
54 while(so_readc(&c, in_file))
55 so_writec(c, out_store);
57 if(in_file)
58 so_give(&in_file);
60 if(out_store){
61 return_text = (char *) so_text(out_store);
62 /* avoid freeing this */
63 if(out_store->txt)
64 out_store->txt = NULL;
66 so_give(&out_store);
70 return(return_text);
73 /* our copy, to_file and from_file must be full paths. from_file
74 * must exist.
76 int
77 our_copy(char *to_file, char *from_file)
79 STORE_S *in_cert, *out_cert;
80 unsigned char c;
82 in_cert = so_get(FileStar, from_file, READ_ACCESS | READ_FROM_LOCALE);
83 if (in_cert == NULL)
84 return -1;
86 out_cert = so_get(FileStar, to_file, WRITE_ACCESS | WRITE_TO_LOCALE);
87 if (out_cert == NULL){
88 so_give(&in_cert);
89 return -1;
92 while(so_readc(&c, in_cert) > 0)
93 so_writec(c, out_cert);
95 so_give(&in_cert);
96 so_give(&out_cert);
98 return 0;