delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/12/01/03:48:56

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Date: Wed, 1 Dec 2004 00:48:41 -0800
From: Yitzchak Scott-Thoennes <sthoenna AT efn DOT org>
To: cygwin AT cygwin DOT com
Subject: truncate() improperly returns EBADF
Message-ID: <20041201084841.GA2356@efn.org>
Mime-Version: 1.0
User-Agent: Mutt/1.4.1i
Organization: bs"d
X-IsSubscribed: yes

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019