#include #include #include #include #define CHECK(cond) do { \ if( !(cond) ) { \ printf("%s failed at %s:%d\n", #cond, __FILE__, __LINE__); \ exit(1); \ } } while(0) int main(int argc, char *argv[]) { if( argc < 2 ) { fprintf(stderr, "Syntax: a.exe fname [mode]\n"); exit(1); } const char *fname = argv[1]; int mode = O_CREAT | O_TRUNC; if( argc > 2 && 0 == strcmp("rw", argv[2]) ) mode |= O_RDWR; else mode |= O_WRONLY; const int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; const unsigned data = -1; int fd = open(fname, mode, perm); CHECK( -1 != fd ); close(fd); fd = open(fname, mode, perm); CHECK( -1 != fd ); CHECK( sizeof(data) == write(fd, &data, sizeof(data)) ); CHECK( sizeof(data) == lseek(fd, 0, SEEK_END) ); CHECK( sizeof(data) == lseek(fd, 0, SEEK_CUR) ); printf("success\n"); return 0; }