glsl/glcpp: Use stdio.h instead of unistd.h.

This commit is contained in:
José Fonseca
2011-03-04 12:53:14 +00:00
parent f52660c3dc
commit 12d17bcadf

View File

@@ -21,10 +21,7 @@
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
*/ */
#include <sys/types.h> #include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "glcpp.h" #include "glcpp.h"
@@ -40,16 +37,16 @@ _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
*ptr = sh; *ptr = sh;
} }
/* Read from fd until EOF and return a string of everything read. /* Read from fp until EOF and return a string of everything read.
*/ */
static char * static char *
load_text_fd (void *ctx, int fd) load_text_fp (void *ctx, FILE *fp)
{ {
#define CHUNK 4096 #define CHUNK 4096
char *text = NULL; char *text = NULL;
ssize_t text_size = 0; size_t text_size = 0;
ssize_t total_read = 0; size_t total_read = 0;
ssize_t bytes; size_t bytes;
while (1) { while (1) {
if (total_read + CHUNK + 1 > text_size) { if (total_read + CHUNK + 1 > text_size) {
@@ -60,19 +57,12 @@ load_text_fd (void *ctx, int fd)
return NULL; return NULL;
} }
} }
bytes = read (fd, text + total_read, CHUNK); bytes = fread (text + total_read, 1, CHUNK, fp);
if (bytes < 0) { total_read += bytes;
fprintf (stderr, "Error while reading: %s\n",
strerror (errno));
ralloc_free (text);
return NULL;
}
if (bytes == 0) { if (bytes < CHUNK) {
break; break;
} }
total_read += bytes;
} }
text[total_read] = '\0'; text[total_read] = '\0';
@@ -84,21 +74,21 @@ static char *
load_text_file(void *ctx, const char *filename) load_text_file(void *ctx, const char *filename)
{ {
char *text; char *text;
int fd; FILE *fp;
if (filename == NULL || strcmp (filename, "-") == 0) if (filename == NULL || strcmp (filename, "-") == 0)
return load_text_fd (ctx, STDIN_FILENO); return load_text_fp (ctx, stdin);
fd = open (filename, O_RDONLY); fp = fopen (filename, "r");
if (fd < 0) { if (fp == NULL) {
fprintf (stderr, "Failed to open file %s: %s\n", fprintf (stderr, "Failed to open file %s: %s\n",
filename, strerror (errno)); filename, strerror (errno));
return NULL; return NULL;
} }
text = load_text_fd (ctx, fd); text = load_text_fp (ctx, fp);
close(fd); fclose(fp);
return text; return text;
} }