/* * As its sole author, I explicitly place this program in the public domain. * It may be used by anyone in any way for any purpose, though I would * appreciate credit where it's due. * der Mouse, mouse@rodents.montreal.qc.ca, 1998-08-05 */ #include #include #include #include #include #include extern const char *__progname; static int fd; static int whence = SEEK_SET; static off_t offset; static int have = 0; #define HAVE_FD 1 #define HAVE_OFFSET 2 static int qflag = 0; static void handleargs(int ac, char **av) { int skip; int errs; char *ep; skip = 0; errs = 0; for (ac--,av++;ac;ac--,av++) { if (skip > 0) { skip --; continue; } if ((**av != '-') || isdigit((unsigned char)av[0][1])) { if (! (have & HAVE_FD)) { fd = strtol(*av,&ep,0); if ((fd < 0) || *ep || (ep == *av)) { if (! qflag) fprintf(stderr,"%s: invalid file descriptor argument `%s'\n",__progname,*av); errs ++; } else { have |= HAVE_FD; } } else if (! (have & HAVE_OFFSET)) { offset = strtoq(*av,&ep,0); if (*ep || (ep == *av)) { if (! qflag) fprintf(stderr,"%s: invalid offset argument `%s'\n",__progname,*av); errs ++; } else { have |= HAVE_OFFSET; } } else { if (! qflag) fprintf(stderr,"%s: extra argument `%s'\n",__progname,*av); errs ++; } continue; } #if 0 if (0) { needarg:; if (! qflag) fprintf(stderr,"%s: %s needs a following argument\n",__progname,*av); errs ++; continue; } #endif #define WANTARG() do { if (++skip >= ac) goto needarg; } while (0) if (!strcmp(*av,"-set")) { whence = SEEK_SET; continue; } if (!strcmp(*av,"-cur") || !strcmp(*av,"-incr")) { whence = SEEK_CUR; continue; } if (!strcmp(*av,"-end") || !strcmp(*av,"-XTND")) { whence = SEEK_END; continue; } #undef WANTARG if (! qflag) fprintf(stderr,"%s: unrecognized option `%s'\n",__progname,*av); errs ++; } if (errs) exit(qflag?EINVAL:1); } int main(int, char **); int main(int ac, char **av) { handleargs(ac,av); if (lseek(fd,offset,whence) < 0) { if (qflag) exit(errno); fprintf(stderr,"%s: %s\n",__progname,strerror(errno)); exit(2); } exit(0); }