1 /************************************************************************
2 Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ************************************************************************/
29 
30 /**
31  * @file datatop_fileops.c
32  * @brief Declares functions for reading and writing to files.
33  *
34  * Declares functions called when reading from files which data is collected.
35  * Also contains methods to handle files which will be written to.
36  */
37 
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include "datatop_interface.h"
44 #include "datatop_linked_list.h"
45 #include "datatop_opt.h"
46 #include "datatop_fileops.h"
47 
48 /**
49  * @brief Reads the lines from files which we are collecting data from.
50  *
51  * @param file File which is read from
52  * @param buffer Pointer to buffer where data will be read. The buffer is allocated
53  *               in dt_read_file() and passed back to the caller. Caller should
54  *               free this when done.
55  * @param len Maximum amount of data which should be read from the file.
56  * @return Number of bytes of data placed in *buffer.
57  */
dt_read_file(const char * file,char ** buffer,int len)58 int dt_read_file(const char *file, char **buffer, int len)
59 {
60 	int read;
61 	FILE *fp;
62 
63 	*buffer = (char *)malloc(len);
64 	if (!(*buffer)) {
65 		fprintf(stderr, "%s(): malloc(%d) failed\n", __func__, len);
66 		return 0;
67 	}
68 
69 	fp = fopen(file, "r");
70 	if (!fp) {
71 		fprintf(stderr, "%s(): Failed to open %s: ", __func__, file);
72 		fprintf(stderr, "Error: %s\n", strerror(errno));
73 		free(*buffer);
74 		*buffer = 0;
75 		return 0;
76 	}
77 	read = fread(*buffer, sizeof(char), len, fp);
78 	fclose(fp);
79 
80 	return read;
81 }
82 
83 /**
84  * @brief Deallocates memory no longer being used.
85  *
86  * @param buffer Buffer to be deallocated.
87  */
dt_free(char ** buffer)88 void dt_free(char **buffer)
89 {
90 	free(*buffer);
91 	*buffer = 0;
92 }
93 
94 /**
95  * @brief Checks for access to a file for writing.
96  *
97  * @param fw File to check access of.
98  * @return INVALID - File already exists or write access denied.
99  * @return VALID - File does not exist and can be written to.
100  */
dtop_check_writefile_access(char * fw)101 int dtop_check_writefile_access(char *fw)
102 {
103 	if (!access(fw, F_OK)) {
104 		printf("File specified already exists\n");
105 		return INVALID;
106 	}
107 
108 	if (!access(fw, W_OK)) {
109 		printf("Permission to write to specified file denied\n");
110 		return INVALID;
111 	}
112 
113 	return VALID;
114 }
115 
116 /**
117  * @brief Opens file and handles possible errors.
118  *
119  * @param fw File path to be opened.
120  * @param to_file Pointer to the *file that is opened.
121  * @return VALID - File opened successfully.
122  * @return INVALID - File could not be opened.
123  */
dtop_open_writing_file(char * fw,FILE ** to_file)124 int dtop_open_writing_file(char *fw, FILE **to_file)
125 {
126 	*to_file = fopen(fw, "w");
127 	if (*to_file) {
128 		return VALID;
129 	} else {
130 		fprintf(stderr, "Value of errno: %d\n", errno);
131 		fprintf(stderr, "Error opening file: %s\n", strerror(errno));
132 		fprintf(stderr, "Please try writing to a non-existent file\n");
133 		printf("See datatop -h for help\n");
134 		return INVALID;
135 	}
136 }
137 
138 /**
139  * @brief Closes a file if not a standard stream.
140  *
141  * @param fw File to be closed.
142  */
dtop_close_file(FILE * fw)143 void dtop_close_file(FILE *fw)
144 {
145 	if (fw != stdout && fw != stderr && fw != stdin)
146 		fclose(fw);
147 }
148 
149 /**
150  * @brief Helper function to find number of lines in dual_line file.
151  *
152  * @return Number of lines in a dual_line file.
153  */
dtop_get_file_line_amount(char * name)154 int dtop_get_file_line_amount(char *name)
155 {
156 	signed char rc = 0;
157 	int line_count = 0;
158 	FILE *file = fopen(name, "r");
159 	while (rc != EOF) {
160 		if (rc == '\n')
161 			line_count++;
162 		rc = fgetc(file);
163 	}
164 
165 	fclose(file);
166 	return line_count;
167 }
168