akaros/tests/read_once.c
<<
>>
Prefs
   1/* Copyright (c) 2015 Google Inc
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * Debugging app: read_once PATH
   6 *
   7 * opens PATH, does one read syscall, outputs resulting buffer as a string. */
   8
   9#include <stdlib.h>
  10#include <stdio.h>
  11#include <string.h>
  12#include <unistd.h>
  13#include <assert.h>
  14
  15#include <sys/types.h>
  16#include <sys/stat.h>
  17#include <fcntl.h>
  18
  19
  20#define handle_error(msg) \
  21        do { perror(msg); exit(-1); } while (0)
  22
  23int main(int argc, char *argv[])
  24{
  25        char *path;
  26        int fd, ret;
  27        char buf[1024];
  28
  29        if (argc != 2) {
  30                printf("Usage: %s PATH\n", argv[0]);
  31                exit(-1);
  32        }
  33        path = argv[1];
  34
  35        fd = open(path, O_READ);
  36        if (fd < 0)
  37                handle_error("Can't open path");
  38        ret = read(fd, buf, sizeof(buf));
  39        if (ret < 0)
  40                handle_error("Can't read");
  41        buf[ret] = 0;
  42        printf("%s", buf);
  43        close(fd);
  44        return 0;
  45}
  46