GSS-API のプログラミング

read_file()

転送されるメッセージがファイルに格納されている場合、read_file() 関数は (call_server() から呼び出されて) ファイルを開いて読み取ります。


例 A–5 read_file()


void read_file(file_name, in_buf)
    char                *file_name;
    gss_buffer_t        in_buf;
{
    int fd, bytes_in, count;
    struct stat stat_buf;

    if ((fd = open(file_name, O_RDONLY, 0)) < 0) {
        perror("open");
        fprintf(stderr, "Couldn't open file %s\n", file_name);
        exit(1);
    }
    if (fstat(fd, &stat_buf) < 0) {
        perror("fstat");
        exit(1);
    }
    in_buf->length = stat_buf.st_size;
    in_buf->value = malloc(in_buf->length);
    if (in_buf->value == 0) {
        fprintf(stderr, "Couldn't allocate %ld byte buffer for reading file\n",
                in_buf->length);
        exit(1);
    }
    memset(in_buf->value, 0, in_buf->length);
    for (bytes_in = 0; bytes_in < in_buf->length; bytes_in += count) {
        count = read(fd, in_buf->value, (OM_uint32)in_buf->length);
        if (count < 0) {
            perror("read");
            exit(1);
        }
        if (count == 0)
            break;
    }
    if (bytes_in != count)
        fprintf(stderr, "Warning, only read in %d bytes, expected %d\n",
                bytes_in, count);
}