This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

truncate() improperly returns EBADF


When using truncate(), EBADF is sometimes improperly returned.  susv3
doesn't list this as a possible errno value for this function.  In
particular, ENOENT should be returned when when path doesn't exist and
EACCES when write permission is denied.


truncate.c:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv) {
   off_t size;
   char *endptr = argv[2];

   if (argc != 3) {
      fprintf(stderr, "usage: truncate path size\n");
      exit(1);
   }

   errno = 0;
   size = strtoull(argv[2], &endptr, 10);
   if (!endptr || endptr == argv[2] || *endptr || size < 0 ||
       (size == 0 && errno)) {
      fprintf(stderr, "truncate: invalid size\n");
      exit(1);
   }
   printf("truncating %s to %lld bytes\n", argv[1], size);

   if (truncate(argv[1], size)) {
      int saveerr = errno;
      perror("truncate");
      fprintf(stderr, "errno was %d\n", saveerr);
      exit(1);
   }
   exit(0);
}
======================================================================

$ rm foo
$ ./truncate foo 0
truncating foo to 0 bytes
truncate: Bad file descriptor
errno was 9
$ touch foo
$ chmod u-w foo
$ ls -l foo
-r--r--r--    1 sthoenna None            0 Dec  1 00:06 foo
$ ./truncate foo 0
truncating foo to 0 bytes
truncate: Bad file descriptor
errno was 9

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]