Changes in uspace/lib/posix/unistd.c [32fb6944:221afc9e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/unistd.c
r32fb6944 r221afc9e 39 39 #include "unistd.h" 40 40 41 /** 42 * Dummy function. Always returns false, because there is no easy way to find 43 * out under HelenOS. 44 * 45 * @param fd 46 * @return Always false. 41 #include "errno.h" 42 #include "string.h" 43 #include "fcntl.h" 44 45 #include "libc/task.h" 46 #include "libc/stats.h" 47 #include "libc/malloc.h" 48 49 /* Array of environment variable strings (NAME=VALUE). */ 50 char **posix_environ = NULL; 51 52 /** 53 * Get current user name. 54 * 55 * @return User name (static) string or NULL if not found. 56 */ 57 char *posix_getlogin(void) 58 { 59 /* There is currently no support for user accounts in HelenOS. */ 60 return (char *) "user"; 61 } 62 63 /** 64 * Get current user name. 65 * 66 * @param name Pointer to a user supplied buffer. 67 * @param namesize Length of the buffer. 68 * @return Zero on success, error code otherwise. 69 */ 70 int posix_getlogin_r(char *name, size_t namesize) 71 { 72 /* There is currently no support for user accounts in HelenOS. */ 73 if (namesize >= 5) { 74 posix_strcpy(name, (char *) "user"); 75 return 0; 76 } else { 77 errno = ERANGE; 78 return -1; 79 } 80 } 81 82 /** 83 * Test whether open file descriptor is associated with a terminal. 84 * 85 * @param fd Open file descriptor to test. 86 * @return Boolean result of the test. 47 87 */ 48 88 int posix_isatty(int fd) 49 89 { 90 /* Always returns false, because there is no easy way to find 91 * out under HelenOS. */ 50 92 return false; 51 93 } 52 94 53 95 /** 54 * 55 * @return 96 * Get the pathname of the current working directory. 97 * 98 * @param buf Buffer into which the pathname shall be put. 99 * @param size Size of the buffer. 100 * @return Buffer pointer on success, NULL on failure. 101 */ 102 char *posix_getcwd(char *buf, size_t size) 103 { 104 /* Native getcwd() does not set any errno despite the fact that general 105 * usage pattern of this function depends on it (caller is repeatedly 106 * guessing the required size of the buffer by checking for ERANGE on 107 * failure). */ 108 if (size == 0) { 109 errno = EINVAL; 110 return NULL; 111 } 112 char *ret = getcwd(buf, size); 113 if (ret == NULL && errno == EOK) { 114 errno = ERANGE; 115 } 116 return ret; 117 } 118 119 /** 120 * Determine the page size of the current run of the process. 121 * 122 * @return Page size of the process. 123 */ 124 int posix_getpagesize(void) 125 { 126 return getpagesize(); 127 } 128 129 /** 130 * Get the process ID of the calling process. 131 * 132 * @return Process ID. 133 */ 134 posix_pid_t posix_getpid(void) 135 { 136 return task_get_id(); 137 } 138 139 /** 140 * Get the real user ID of the calling process. 141 * 142 * @return User ID. 56 143 */ 57 144 posix_uid_t posix_getuid(void) 58 145 { 59 // TODO 60 not_implemented(); 61 } 62 63 /** 64 * 65 * @return 146 /* There is currently no support for user accounts in HelenOS. */ 147 return 0; 148 } 149 150 /** 151 * Get the real group ID of the calling process. 152 * 153 * @return Group ID. 66 154 */ 67 155 posix_gid_t posix_getgid(void) 68 156 { 69 // TODO 70 not_implemented(); 157 /* There is currently no support for user accounts in HelenOS. */ 158 return 0; 159 } 160 161 /** 162 * Read from a file. 163 * 164 * @param fildes File descriptor of the opened file. 165 * @param buf Buffer to which the read bytes shall be stored. 166 * @param nbyte Upper limit on the number of read bytes. 167 * @return Number of read bytes on success, -1 otherwise. 168 */ 169 ssize_t posix_read(int fildes, void *buf, size_t nbyte) 170 { 171 int rc = read(fildes, buf, nbyte); 172 if (rc < 0) { 173 errno = -rc; 174 return -1; 175 } else { 176 return rc; 177 } 178 } 179 180 /** 181 * Remove a link to a file. 182 * 183 * @param path File pathname. 184 * @return Zero on success, -1 otherwise. 185 */ 186 int posix_unlink(const char *path) 187 { 188 int rc = unlink(path); 189 if (rc < 0) { 190 errno = -rc; 191 return -1; 192 } else { 193 return rc; 194 } 195 } 196 197 /** 198 * Determine accessibility of a file. 199 * 200 * @param path File to check accessibility for. 201 * @param amode Either check for existence or intended access mode. 202 * @return Zero on success, -1 otherwise. 203 */ 204 int posix_access(const char *path, int amode) 205 { 206 if (amode == F_OK) { 207 /* Check file existence by attempt to open it. */ 208 int fd = open(path, O_RDONLY); 209 if (fd < 0) { 210 /* FIXME: open() returns error code as negative retval. */ 211 errno = -fd; 212 fd = -1; 213 } else { 214 close(fd); 215 } 216 return fd; 217 } else if (amode & (X_OK | W_OK | R_OK)) { 218 /* HelenOS doesn't support permissions, return success. */ 219 return 0; 220 } else { 221 /* Invalid amode argument. */ 222 errno = EINVAL; 223 return -1; 224 } 225 } 226 227 /** 228 * Get configurable system variables. 229 * 230 * @param name Variable name. 231 * @return Variable value. 232 */ 233 long posix_sysconf(int name) 234 { 235 long clk_tck = 0; 236 size_t cpu_count = 0; 237 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count); 238 if (cpu_stats && cpu_count > 0) { 239 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L; 240 } 241 free(cpu_stats); 242 cpu_stats = 0; 243 244 long phys_pages = 0; 245 long avphys_pages = 0; 246 stats_physmem_t *mem_stats = stats_get_physmem(); 247 if (mem_stats) { 248 phys_pages = (long) (mem_stats->total / getpagesize()); 249 avphys_pages = (long) (mem_stats->free / getpagesize()); 250 } 251 free(mem_stats); 252 mem_stats = 0; 253 254 switch (name) { 255 case _SC_PHYS_PAGES: 256 return phys_pages; 257 case _SC_AVPHYS_PAGES: 258 return avphys_pages; 259 case _SC_PAGESIZE: 260 return getpagesize(); 261 case _SC_CLK_TCK: 262 return clk_tck; 263 default: 264 errno = EINVAL; 265 return -1; 266 } 71 267 } 72 268 … … 74 270 * 75 271 * @param path 76 * @param amode77 * @return78 */79 int posix_access(const char *path, int amode)80 {81 // TODO82 not_implemented();83 }84 85 /**86 *87 272 * @param name 88 273 * @return 89 274 */ 90 long posix_sysconf(int name) 91 { 92 // TODO 275 long posix_pathconf(const char *path, int name) 276 { 277 // TODO: low priority, just a compile-time dependency of binutils 278 not_implemented(); 279 } 280 281 /** 282 * 283 * @return 284 */ 285 posix_pid_t posix_fork(void) 286 { 287 // TODO: low priority, just a compile-time dependency of binutils 288 not_implemented(); 289 } 290 291 /** 292 * 293 * @param path 294 * @param argv 295 * @return 296 */ 297 int posix_execv(const char *path, char *const argv[]) 298 { 299 // TODO: low priority, just a compile-time dependency of binutils 300 not_implemented(); 301 } 302 303 /** 304 * 305 * @param file 306 * @param argv 307 * @return 308 */ 309 int posix_execvp(const char *file, char *const argv[]) 310 { 311 // TODO: low priority, just a compile-time dependency of binutils 312 not_implemented(); 313 } 314 315 /** 316 * 317 * @param fildes 318 * @return 319 */ 320 int posix_pipe(int fildes[2]) 321 { 322 // TODO: low priority, just a compile-time dependency of binutils 93 323 not_implemented(); 94 324 }
Note:
See TracChangeset
for help on using the changeset viewer.