]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/remove.c
Added testdriver for _PDCLIB_remove().
[pdclib] / platform / example / functions / _PDCLIB / remove.c
1 /* $Id$ */
2
3 /* _PDCLIB_remove( const char * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* This is an example implementation of _PDCLIB_remove() (declared in
10    _PDCLIB_glue.h), fit for use in POSIX kernels.
11    NOTE: Linux is *not* POSIX-compliant in this, as it sets EISDIR instead of
12    EPERM if you try to unlink a directory. Check the manpage for unlink(2).
13 */
14
15 #ifndef REGTEST
16 #include <_PDCLIB_glue.h>
17 #include <errno.h>
18 #include <unistd.h>
19
20 int _PDCLIB_remove( const char * filename )
21 {
22     int prev_errno = errno;
23     int rc;
24     errno = 0;
25     if ( ( ( rc = unlink( filename ) ) != 0 ) && ( errno == EISDIR ) )
26     {
27         rc = rmdir( filename );
28     }
29     errno = prev_errno;
30     return rc;
31 }
32
33 #endif
34
35 #ifdef TEST
36 /* TODO: Work around the following undef */
37 #undef SEEK_SET
38 #include <_PDCLIB_test.h>
39
40 #include <stdlib.h>
41 #include <string.h>
42
43 int main( void )
44 {
45     char filename[ L_tmpnam + 6 ] = "touch ";
46     tmpnam( filename + 6 );
47     /* create file */
48     system( filename );
49     /* file is actually readable */
50     TESTCASE( fopen( filename + 6, "r" ) != NULL );
51     /* remove function does not return error */
52     TESTCASE( _PDCLIB_remove( filename + 6 ) == 0 );
53     /* file is no longer readable */
54     TESTCASE( fopen( filename + 6, "r" ) == NULL );
55     /* remove function does return error */
56     TESTCASE( _PDCLIB_remove( filename + 6 ) != 0 );
57     memcpy( filename, "mkdir", 5 );
58     /* create directory */
59     system( filename );
60     /* remove function does not return error */
61     TESTCASE( _PDCLIB_remove( filename + 6 ) == 0 );
62     /* remove function does return error */
63     TESTCASE( _PDCLIB_remove( filename + 6 ) != 0 );
64     return TEST_RESULTS;
65 }
66
67 #endif