/* 
   Netdrive Samba client plugin
   general utility functions
   Copyright (C) netlabs.org 2003-2012
   Copyright (C) bww bitwise works GmbH 2012-2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
*/

#define INCL_DOS
#define INCL_DOSERRORS
#include <os2.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "smbwrp.h"

// map errno errors to API errors
int maperror(int rc)
{
        switch (rc)
        {
           case 0 :  // NO_ERROR
              return NO_ERROR;
           case 1 :  // EPERM - Operation nt permitted
           case 13 : // EACCES - Permission denied
              return ERROR_ACCESS_DENIED;
           case 2 :  // ENOENT - No such file or directory
              return ERROR_FILE_NOT_FOUND;
           case 3 :  // ESRCH - No such process
              return ERROR_PID_MISMATCH; 
           case 4 :  // EINTR - Interrupted system call
              return ERROR_INTERRUPT;
           case 5 :  // EIO - I/O error
              return ERROR_READ_FAULT;
           case 6 :  // ENXIO - No such device or address
              return ERROR_BAD_UNIT;
           case 7 :  // E2BIG - Arguments or environment too big
              return ERROR_INVALID_DATA;
           case 8 :  // ENOEXEC - Invalid executable file format
              return ERROR_BAD_EXE_FORMAT;
           case 9 :  // EBADF - Bad file number
              return ERROR_INVALID_HANDLE;
           case 10 : // ECHILD - No child processes
              return ERROR_NO_CHILD_PROCESS;
           case 11 : // EAGAIN - Resource temporarily unavailable
           case 16 : // EBUSY - Resource busy
              return ERROR_BUSY;
           case 12 : // ENOMEM - Not enough memory
              return ERROR_NOT_ENOUGH_MEMORY;
           case 14 : // EFAULT - Bad address
              return ERROR_INVALID_ADDRESS;
           case 15 : // ENOLCK - No locks available
              return ERROR_NOT_LOCKED;
           case 17 : // EEXIST - File exists
              return ERROR_FILE_EXISTS;
           case 18 : // EXDEV - Cross-device link
              return ERROR_NOT_SAME_DEVICE;
           case 19 : // ENODEV - No such device
           case 57 : // ENOTCONN - Socket is not connected
              return ERROR_REM_NOT_LIST;
           case 20 : // ENOTDIR - Not a directory
              return ERROR_PATH_NOT_FOUND;
           case 21 : // EISDIR - Is a directory
              return ERROR_DIRECTORY;
           case 22 : // EINVAL - Invalid argument
              return ERROR_INVALID_PARAMETER;
           case 23 : // ENFILE - Too many open files in system
           case 31 : // EMLINK - Too many links
              return ERROR_TOO_MANY_OPEN_FILES;
           case 24 : // EMFILE - Too many open files
              return ERROR_TOO_MANY_OPENS;
           case 25 : // ENOTTY - Inappropriate ioctl
              return ERROR_MOD_NOT_FOUND;
           case 26 : // EDEADLK - Resource deadlock avoided
              return ERROR_LOCK_VIOLATION;
           case 27 : // EFBIG - File too large
           case 40 : // EMSGSIZE - Message too long
              return ERROR_TRANSFER_TOO_LONG;
           case 28 : // ENOSPC - Disk full
              return ERROR_DISK_FULL;
           case 29 : // ESPIPE - Invalid seek
              return ERROR_SEEK;
           case 30 : // EROFS - Read-only file system
              return ERROR_WRITE_PROTECT;
           case 32 : // EPIPE - Broken pipe
              return ERROR_BROKEN_PIPE;
           case 33 : // EDOM - Domain error
              return ERROR_INVALID_LEVEL;
           case 34 : // ENAMETOOLONG - File name too long
           case 38 : // ERANGE - Result too large
              return ERROR_FILENAME_EXCED_RANGE;
           case 35 : // ENOTEMPTY - Directory not empty
              return ERROR_DIR_NOT_EMPTY;
           case 36 : // EINPROGRESS - Operation now in progress
           case 66 : // EALREADY - Operation already in progress
              return ERROR_BUSY_DRIVE;
           case 37 : // ENOSYS - Function not implemented
              return ERROR_INVALID_FUNCTION;
           case 39 : // EDESTADDRREQ - Destination address required
              return ERROR_KBD_FOCUS_REQUIRED;
           case 48 : // EADDRINUSE - Address already in use
              return ERROR_NETWORK_BUSY;
           case 49 : // EADDRNOTAVAIL - Can't assigned requested address
              return ERROR_INFO_NOT_AVAIL;
           case 50 : // ENETDOWN - Network is down
           case 51 : // ENETUNREACH - Network is unreachable
           case 52 : // ENETRESET - Network dropped connection on reset
           case 53 : // ECONNABORTED - Software caused connection abort
           case 54 : // ECONNRESET - Connection reset by peer
              return ERROR_NETWORK_ACCESS_DENIED;
           case 55 : // ENOBUFS - No buffer space available
              return ERROR_BUFFER_OVERFLOW;
           case 56 : // EISCONN - Socket is already connected
              return ERROR_PIPE_BUSY;
           case 58 : // ESHUTDOWN - Can't send after socket shutdown
              return ERROR_ALREADY_SHUTDOWN;
           case 60 : // ETIMEDOUT - Connection timed out
              return ERROR_TIMEOUT;
           case 61 : // ECONNREFUSED - Connection refused
              return ERROR_NETWORK_ACCESS_DENIED;
           case 63 : // ENOTSOCK - Socket operation on non-socket
              return ERROR_INVALID_BLOCK;
           case 64 : // EHOSTDOWN - Host is down
              return ERROR_BAD_FORMAT;
           case 65 : // EHOSTUNREACH - No route to host
              return ERROR_BAD_NETPATH;
        }
//	debug_printf( "Unhandled return code %d\n");
        return rc + 40000;
}

char * getlastslash(char * path)
{
        char * p;
        if (!path)
           return NULL;

        for (p = path + strlen(path) - 1; p >= path; p--)
        {
           if (*p == '\\' || *p == '/')
              return p;
        }
        return NULL;
}
