Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*----------------------------------------------------------------------------
- ------------------------------ Part 0: contents ------------------------------
- ----------------------------------------------------------------------------*/
- /*
- Part 0: contents
- Part 1: #includes
- Part 2: #defines
- Part 3: globals
- Part 4: library function declarations
- Part 5: function declarations
- Part 6: library function definitions
- Part 7: function definitions
- Part 8: main()
- */
- /*----------------------------------------------------------------------------
- ------------------------------ Part 1: #includes -----------------------------
- ----------------------------------------------------------------------------*/
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- /*----------------------------------------------------------------------------
- ------------------------------ Part 2: #defines ------------------------------
- ----------------------------------------------------------------------------*/
- #define BLOCK_SIZE 4096
- #define Open(a,b,c) \
- OPen(__LINE__,a,b,c)
- #define Lseek(a,b,c) \
- LSeek(__LINE__,a,b,c)
- /*----------------------------------------------------------------------------
- ------------------------------- Part 3: globals ------------------------------
- ----------------------------------------------------------------------------*/
- char *gl_errno_names[]=
- {
- "NO_ERROR",
- "EPERM",
- "ENOENT",
- "ESRCH",
- "EINTR",
- "EIO",
- "ENXIO",
- "E2BIG",
- "ENOEXEC",
- "EBADF",
- "ECHILD",
- "EAGAIN or EWOULDBLOCK",
- "ENOMEM",
- "EACCES",
- "EFAULT",
- "ENOTBLK",
- "EBUSY",
- "EEXIST",
- "EXDEV",
- "ENODEV",
- "ENOTDIR",
- "EISDIR",
- "EINVAL",
- "ENFILE",
- "EMFILE",
- "ENOTTY",
- "ETXTBSY",
- "EFBIG",
- "ENOSPC",
- "ESPIPE",
- "EROFS",
- "EMLINK",
- "EPIPE",
- "EDOM",
- "ERANGE",
- "EDEADLK or EDEADLOCK",
- "ENAMETOOLONG",
- "ENOLCK",
- "ENOSYS",
- "ENOTEMPTY",
- "ELOOP",
- "quasi-EWOULDBLOCK (which is now error 11)",
- "ENOMSG",
- "EIDRM",
- "ECHRNG",
- "EL2NSYNC",
- "EL3HLT",
- "EL3RST",
- "ELNRNG",
- "EUNATCH",
- "ENOCSI",
- "EL2HLT",
- "EBADE",
- "EBADR",
- "EXFULL",
- "ENOANO",
- "EBADRQC",
- "EBADSLT",
- "quasi-EDEADLOCK (which is now error 35)",
- "EBFONT",
- "ENOSTR",
- "ENODATA",
- "ETIME",
- "ENOSR",
- "ENONET",
- "ENOPKG",
- "EREMOTE",
- "ENOLINK",
- "EADV",
- "ESRMNT",
- "ECOMM",
- "EPROTO",
- "EMULTIHOP",
- "EDOTDOT",
- "EBADMSG",
- "EOVERFLOW",
- "ENOTUNIQ",
- "EBADFD",
- "EREMCHG",
- "ELIBACC",
- "ELIBBAD",
- "ELIBSCN",
- "ELIBMAX",
- "ELIBEXEC",
- "EILSEQ",
- "ERESTART",
- "ESTRPIPE",
- "EUSERS",
- "ENOTSOCK",
- "EDESTADDRREQ",
- "EMSGSIZE",
- "EPROTOTYPE",
- "ENOPROTOOPT",
- "EPROTONOSUPPORT",
- "ESOCKTNOSUPPORT",
- "EOPNOTSUPP",
- "EPFNOSUPPORT",
- "EAFNOSUPPORT",
- "EADDRINUSE",
- "EADDRNOTAVAIL",
- "ENETDOWN",
- "ENETUNREACH",
- "ENETRESET",
- "ECONNABORTED",
- "ECONNRESET",
- "ENOBUFS",
- "EISCONN",
- "ENOTCONN",
- "ESHUTDOWN",
- "ETOOMANYREFS",
- "ETIMEDOUT",
- "ECONNREFUSED",
- "EHOSTDOWN",
- "EHOSTUNREACH",
- "EALREADY",
- "EINPROGRESS",
- "ESTALE",
- "EUCLEAN",
- "ENOTNAM",
- "ENAVAIL",
- "EISNAM",
- "EREMOTEIO",
- "EDQUOT",
- "ENOMEDIUM",
- "EMEDIUMTYPE"
- };
- /*----------------------------------------------------------------------------
- -------------------- Part 4: library function declarations -------------------
- ----------------------------------------------------------------------------*/
- void
- Exit(int ar_status);
- off_t
- LSeek(int ar_line,
- int ar_fd,
- off_t ar_offset,
- int ar_whence
- );
- int
- OPen(int ar_line,
- char const *ar_pathname,
- int ar_flags,
- int ar_mode
- );
- /*----------------------------------------------------------------------------
- ------------------------ Part 5: function declarations -----------------------
- ----------------------------------------------------------------------------*/
- char *
- get_errno_name(int ar_error_number);
- void
- oops_errno(int ar_line,
- char const *ar_format,
- ...
- );
- void
- oops_plain(int ar_line,
- char const *ar_format,
- ...
- );
- void
- oops_v(char const *ar_format,
- va_list ar_args
- );
- void
- usage(int ar_argc,
- char **ar_argv,
- FILE *ar_phyle,
- int ar_exit_code
- );
- /*----------------------------------------------------------------------------
- -------------------- Part 6: library function definitions --------------------
- ----------------------------------------------------------------------------*/
- void
- Exit(int ar_status)
- {
- exit(ar_status);
- } /* Exit() */
- /*-------------------------------------------------------------------------- */
- off_t
- LSeek(int ar_line,
- int ar_fd,
- off_t ar_offset,
- int ar_whence
- )
- {
- off_t lo_result;
- lo_result=lseek(ar_fd,
- ar_offset,
- ar_whence
- );
- if(lo_result==-1)
- {
- oops_errno(ar_line,
- "lseek(%d,%ld 0x%lX,%d)",
- ar_fd,
- ar_offset,
- ar_offset,
- ar_whence
- );
- }
- return lo_result;
- } /* LSeek() */
- /*-------------------------------------------------------------------------- */
- int
- OPen(int ar_line,
- char const *ar_pathname,
- int ar_flags,
- int ar_mode
- )
- {
- int lo_result;
- lo_result=open(ar_pathname,
- ar_flags,
- ar_mode
- );
- if(lo_result==-1)
- {
- oops_errno(ar_line,
- "open(\"%s\")",
- ar_pathname
- );
- }
- return lo_result;
- } /* OPen() */
- /*----------------------------------------------------------------------------
- ------------------------ Part 7: function definitions ------------------------
- ----------------------------------------------------------------------------*/
- char *
- get_errno_name(int ar_error_number)
- {
- if((ar_error_number>=0) &&
- (ar_error_number<sizeof(gl_errno_names)/sizeof(gl_errno_names[0]))
- )
- {
- return gl_errno_names[ar_error_number];
- }
- else
- {
- return "[invalid error number]";
- }
- } /* get_errno_name() */
- /*--------------------------------------------------------------------------*/
- void
- oops_errno(int ar_line,
- char const *ar_format,
- ...
- )
- {
- va_list lo_arg_pointer;
- int lo_error;
- lo_error=errno;
- fprintf(stderr,
- "error %d (%s) %s on line %d\nduring ",
- lo_error,
- get_errno_name(lo_error),
- strerror(lo_error),
- ar_line
- );
- va_start(lo_arg_pointer,
- ar_format
- );
- oops_v(ar_format,
- lo_arg_pointer
- );
- va_end(lo_arg_pointer);
- fprintf(stderr,"\n");
- Exit(2);
- } /* oops_errno() */
- /*--------------------------------------------------------------------------*/
- void
- oops_plain(int ar_line,
- char const *ar_format,
- ...
- )
- {
- va_list lo_arg_pointer;
- fprintf(stderr,
- "error on line %d:\n",
- ar_line
- );
- va_start(lo_arg_pointer,
- ar_format
- );
- oops_v(ar_format,
- lo_arg_pointer
- );
- va_end(lo_arg_pointer);
- fprintf(stderr,"\n");
- Exit(2);
- } /* oops_plain() */
- /*--------------------------------------------------------------------------*/
- void
- oops_v(char const *ar_format,
- va_list ar_args
- )
- {
- vfprintf(stderr,
- ar_format,
- ar_args
- );
- } /* oops_v() */
- /*--------------------------------------------------------------------------*/
- void
- usage(int ar_argc,
- char **ar_argv,
- FILE *ar_phyle,
- int ar_exit_code
- )
- {
- fprintf(ar_phyle,
- "usage:\n"
- " %s file-or-block-device-name exercise this device\n",
- ar_argv[0]
- );
- Exit(ar_exit_code);
- } /* usage() */
- /*----------------------------------------------------------------------------
- ------------------------------- Part 8: main() -------------------------------
- ----------------------------------------------------------------------------*/
- int
- main(int ar_argc,
- char **ar_argv
- )
- {
- int lo_fd;
- char lo_buffer[4];
- off_t lo_block_count;
- off_t lo_block_index;
- off_t lo_high_bit;
- off_t lo_mask;
- off_t lo_top_displacement;
- if(ar_argc!=2)
- {
- usage(ar_argc,
- ar_argv,
- stderr,
- 1
- );
- }
- lo_fd=Open(ar_argv[1],
- O_RDONLY,
- 0
- );
- lo_high_bit=1;
- while(lo_high_bit>0)
- {
- if(lseek(lo_fd,
- lo_high_bit,
- SEEK_SET
- )
- ==
- (off_t)-1
- )
- {
- lo_high_bit>>=1;
- break;
- }
- if(read(lo_fd,
- lo_buffer,
- 1
- )
- !=
- 1
- )
- {
- lo_high_bit>>=1;
- break;
- }
- lo_high_bit<<=1;
- }
- lo_top_displacement=lo_high_bit;
- for(lo_mask=lo_high_bit>>1;
- lo_mask;
- lo_mask>>=1
- )
- {
- if(lseek(lo_fd,
- lo_top_displacement | lo_mask,
- SEEK_SET
- )
- ==
- (off_t)-1
- )
- {
- continue;
- }
- if(read(lo_fd,
- lo_buffer,
- 1
- )
- !=
- 1
- )
- {
- continue;
- }
- lo_top_displacement |= lo_mask;
- }
- printf("%lld\n",lo_top_displacement);
- lo_block_count=(lo_top_displacement+1)/BLOCK_SIZE;
- for(lo_block_index=0;
- lo_block_index<lo_block_count/2;
- lo_block_index+=10000
- )
- {
- printf("%lld %lld\n",lo_block_index,(100*lo_block_index)/lo_block_count/2);
- Lseek(lo_fd,
- lo_block_index*BLOCK_SIZE,
- SEEK_SET
- );
- read(lo_fd,
- lo_buffer,
- 1
- );
- Lseek(lo_fd,
- (lo_block_index+lo_block_count/2)*BLOCK_SIZE,
- SEEK_SET
- );
- read(lo_fd,
- lo_buffer,
- 1
- );
- }
- return 0;
- } /* main() */
- ---------------------------------------------------
- =============================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement