Deshim VirtualExecutor in folly
[hiphop-php.git] / hphp / neo / neo_files.c
blob00ada88995b33f68be9feb255518427cead501ec
1 /*
2 * Copyright 2001-2004 Brandon Long
3 * All Rights Reserved.
5 * ClearSilver Templating System
7 * This code is made available under the terms of the ClearSilver License.
8 * http://www.clearsilver.net/license.hdf
12 #include "cs_config.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <sys/types.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <sys/stat.h>
23 #ifdef _MSC_VER
24 #include <io.h>
25 #include <windows.h>
26 #include <shellapi.h>
27 #include <strsafe.h>
28 #else
29 #include <unistd.h>
30 #include <dirent.h>
31 #endif
33 #include "neo_misc.h"
34 #include "neo_err.h"
35 #include "neo_files.h"
37 NEOERR *ne_load_file_len (const char *path, char **str, int *out_len)
39 struct stat s;
40 int fd;
41 int len;
42 int bytes_read;
44 *str = NULL;
45 if (out_len) *out_len = 0;
47 if (stat(path, &s) == -1)
49 if (errno == ENOENT)
50 return nerr_raise (NERR_NOT_FOUND, "File %s not found", path);
51 return nerr_raise_errno (NERR_SYSTEM, "Unable to stat file %s", path);
54 if (s.st_size >= INT_MAX)
55 return nerr_raise (NERR_ASSERT, "File %s too large (%ld >= INT_MAX)",
56 path, s.st_size);
58 if (s.st_size < 0)
59 return nerr_raise (NERR_ASSERT, "File %s size error? (%ld < 0)", path,
60 s.st_size);
62 fd = open (path, O_RDONLY);
63 if (fd == -1)
65 return nerr_raise_errno (NERR_SYSTEM, "Unable to open file %s", path);
67 len = s.st_size;
68 *str = (char *) malloc (len + 1);
70 if (*str == NULL)
72 close(fd);
73 return nerr_raise (NERR_NOMEM,
74 "Unable to allocate memory (%d) to load file %s", len + 1, path);
76 if ((bytes_read = read (fd, *str, len)) == -1)
78 close(fd);
79 free(*str);
80 return nerr_raise_errno (NERR_SYSTEM, "Unable to read file %s", path);
83 (*str)[bytes_read] = '\0';
84 close(fd);
85 if (out_len) *out_len = bytes_read;
87 return STATUS_OK;
90 NEOERR *ne_load_file (const char *path, char **str) {
91 return ne_load_file_len (path, str, NULL);