summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/src/perror.c
blob: c26dfa1e33497a4911404e03121c2ebd5044437e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
 * The MIT License
 *
 * Copyright (C) 2016-2018 Alexander Saprykin <saprykin.spb@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "perror.h"
#include "pmem.h"
#include "pstring.h"
#include "perror-private.h"

#ifndef P_OS_WIN
#  if defined (P_OS_OS2)
#    define INCL_DOSERRORS
#    include <os2.h>
#    include <sys/socket.h>
#  endif
#  include <errno.h>
#endif

struct PError_ {
	pint	code;
	pint	native_code;
	pchar	*message;
};

PErrorIO
p_error_get_io_from_system (pint err_code)
{
	switch (err_code) {
	case 0:
		return P_ERROR_IO_NONE;
#if defined (P_OS_WIN)
#  ifdef WSAEADDRINUSE
	case WSAEADDRINUSE:
		return P_ERROR_IO_ADDRESS_IN_USE;
#  endif
#  ifdef WSAEWOULDBLOCK
	case WSAEWOULDBLOCK:
		return P_ERROR_IO_WOULD_BLOCK;
#  endif
#  ifdef WSAEACCES
	case WSAEACCES:
		return P_ERROR_IO_ACCESS_DENIED;
#  endif
#  ifdef WSA_INVALID_HANDLE
	case WSA_INVALID_HANDLE:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef WSA_INVALID_PARAMETER
	case WSA_INVALID_PARAMETER:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef WSAEBADF
	case WSAEBADF:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef WSAENOTSOCK
	case WSAENOTSOCK:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef WSAEINVAL
	case WSAEINVAL:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef WSAESOCKTNOSUPPORT
	case WSAESOCKTNOSUPPORT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#  ifdef WSAEOPNOTSUPP
	case WSAEOPNOTSUPP:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#  ifdef WSAEPFNOSUPPORT
	case WSAEPFNOSUPPORT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#  ifdef WSAEAFNOSUPPORT
	case WSAEAFNOSUPPORT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#  ifdef WSAEPROTONOSUPPORT
	case WSAEPROTONOSUPPORT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#  ifdef WSAECANCELLED
	case WSAECANCELLED:
		return P_ERROR_IO_ABORTED;
#  endif
#  ifdef ERROR_ALREADY_EXISTS
	case ERROR_ALREADY_EXISTS:
		return P_ERROR_IO_EXISTS;
#  endif
#  ifdef ERROR_FILE_NOT_FOUND
	case ERROR_FILE_NOT_FOUND:
		return P_ERROR_IO_NOT_EXISTS;
#  endif
#  ifdef ERROR_NO_MORE_FILES
	case ERROR_NO_MORE_FILES:
		return P_ERROR_IO_NO_MORE;
#  endif
#  ifdef ERROR_ACCESS_DENIED
	case ERROR_ACCESS_DENIED:
		return P_ERROR_IO_ACCESS_DENIED;
#  endif
#  ifdef ERROR_OUTOFMEMORY
	case ERROR_OUTOFMEMORY:
		return P_ERROR_IO_NO_RESOURCES;
#  endif
#  ifdef ERROR_NOT_ENOUGH_MEMORY
	case ERROR_NOT_ENOUGH_MEMORY:
		return P_ERROR_IO_NO_RESOURCES;
#  endif
#  ifdef ERROR_INVALID_HANDLE
#    if !defined(WSA_INVALID_HANDLE) || (ERROR_INVALID_HANDLE != WSA_INVALID_HANDLE)
	case ERROR_INVALID_HANDLE:
		return P_ERROR_IO_INVALID_ARGUMENT;
#    endif
#  endif
#  ifdef ERROR_INVALID_PARAMETER
#    if !defined(WSA_INVALID_PARAMETER) || (ERROR_INVALID_PARAMETER != WSA_INVALID_PARAMETER)
	case ERROR_INVALID_PARAMETER:
		return P_ERROR_IO_INVALID_ARGUMENT;
#    endif
#  endif
#  ifdef ERROR_NOT_SUPPORTED
	case ERROR_NOT_SUPPORTED:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#elif defined (P_OS_OS2)
#  ifdef ERROR_FILE_NOT_FOUND
	case ERROR_FILE_NOT_FOUND:
		return P_ERROR_IO_NOT_EXISTS;
#  endif
#  ifdef ERROR_PATH_NOT_FOUND
	case ERROR_PATH_NOT_FOUND:
		return P_ERROR_IO_NOT_EXISTS;
#  endif
#  ifdef ERROR_TOO_MANY_OPEN_FILES
	case ERROR_TOO_MANY_OPEN_FILES:
		return P_ERROR_IO_NO_RESOURCES;
#  endif
#  ifdef ERROR_NOT_ENOUGH_MEMORY
	case ERROR_NOT_ENOUGH_MEMORY:
		return P_ERROR_IO_NO_RESOURCES;
#  endif
#  ifdef ERROR_ACCESS_DENIED
	case ERROR_ACCESS_DENIED:
		return P_ERROR_IO_ACCESS_DENIED;
#  endif
#  ifdef ERROR_INVALID_HANDLE
	case ERROR_INVALID_HANDLE:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_INVALID_PARAMETER
	case ERROR_INVALID_PARAMETER:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_INVALID_ADDRESS
	case ERROR_INVALID_ADDRESS:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_NO_MORE_FILES
	case ERROR_NO_MORE_FILES:
		return P_ERROR_IO_NO_MORE;
#  endif
#  ifdef ERROR_NOT_SUPPORTED
	case ERROR_NOT_SUPPORTED:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif
#  ifdef ERROR_FILE_EXISTS
	case ERROR_FILE_EXISTS:
		return P_ERROR_IO_EXISTS;
#  endif
#else /* !P_OS_WIN && !P_OS_OS2 */
#  ifdef EACCES
	case EACCES:
		return P_ERROR_IO_ACCESS_DENIED;
#  endif

#  ifdef EPERM
	case EPERM:
		return P_ERROR_IO_ACCESS_DENIED;
#  endif

#  ifdef ENOMEM
	case ENOMEM:
		return P_ERROR_IO_NO_RESOURCES;
#  endif

#  ifdef ENOSR
	case ENOSR:
		return P_ERROR_IO_NO_RESOURCES;
#  endif

#  ifdef ENOBUFS
	case ENOBUFS:
		return P_ERROR_IO_NO_RESOURCES;
#  endif

#  ifdef ENFILE
	case ENFILE:
		return P_ERROR_IO_NO_RESOURCES;
#  endif

#  ifdef ENOSPC
	case ENOSPC:
		return P_ERROR_IO_NO_RESOURCES;
#  endif

#  ifdef EMFILE
	case EMFILE:
		return P_ERROR_IO_NO_RESOURCES;
#  endif

#  ifdef EINVAL
	case EINVAL:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif

#  ifdef EBADF
	case EBADF:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif

#  ifdef ENOTSOCK
	case ENOTSOCK:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif

#  ifdef EFAULT
	case EFAULT:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif

#  ifdef EPROTOTYPE
	case EPROTOTYPE:
		return P_ERROR_IO_INVALID_ARGUMENT;
#  endif

	/* On Linux these errors can have same codes */
#  if defined(ENOTSUP) && (!defined(EOPNOTSUPP) || ENOTSUP != EOPNOTSUPP)
	case ENOTSUP:
			return P_ERROR_IO_NOT_SUPPORTED;
#  endif

#  ifdef ENOPROTOOPT
	case ENOPROTOOPT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif

#  ifdef EPROTONOSUPPORT
	case EPROTONOSUPPORT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif

#  ifdef EAFNOSUPPORT
	case EAFNOSUPPORT:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif

#  ifdef EOPNOTSUPP
	case EOPNOTSUPP:
		return P_ERROR_IO_NOT_SUPPORTED;
#  endif

#  ifdef EADDRNOTAVAIL
	case EADDRNOTAVAIL:
		return P_ERROR_IO_NOT_AVAILABLE;
#  endif

#  ifdef ENETUNREACH
	case ENETUNREACH:
		return P_ERROR_IO_NOT_AVAILABLE;
#  endif

#  ifdef ENETDOWN
	case ENETDOWN:
		return P_ERROR_IO_NOT_AVAILABLE;
#  endif

#  ifdef EHOSTDOWN
	case EHOSTDOWN:
		return P_ERROR_IO_NOT_AVAILABLE;
#  endif

#  ifdef ENONET
	case ENONET:
		return P_ERROR_IO_NOT_AVAILABLE;
#  endif

#  ifdef EHOSTUNREACH
	case EHOSTUNREACH:
		return P_ERROR_IO_NOT_AVAILABLE;
#  endif

#  ifdef EINPROGRESS
	case EINPROGRESS:
		return P_ERROR_IO_IN_PROGRESS;
#  endif

#  ifdef EALREADY
	case EALREADY:
		return P_ERROR_IO_IN_PROGRESS;
#  endif

#  ifdef EISCONN
	case EISCONN:
		return P_ERROR_IO_CONNECTED;
#  endif

#  ifdef ECONNREFUSED
	case ECONNREFUSED:
		return P_ERROR_IO_CONNECTION_REFUSED;
#  endif

#  ifdef ENOTCONN
	case ENOTCONN:
		return P_ERROR_IO_NOT_CONNECTED;
#  endif

#  ifdef ECONNABORTED
	case ECONNABORTED:
		return P_ERROR_IO_ABORTED;
#  endif

#  ifdef EADDRINUSE
	case EADDRINUSE:
		return P_ERROR_IO_ADDRESS_IN_USE;
#  endif

#  ifdef ETIMEDOUT
	case ETIMEDOUT:
		return P_ERROR_IO_TIMED_OUT;
#  endif

#  ifdef EDQUOT
	case EDQUOT:
		return P_ERROR_IO_QUOTA;
#  endif

#  ifdef EISDIR
	case EISDIR:
		return P_ERROR_IO_IS_DIRECTORY;
#  endif

#  ifdef ENOTDIR
	case ENOTDIR:
		return P_ERROR_IO_NOT_DIRECTORY;
#  endif

#  ifdef EEXIST
	case EEXIST:
		return P_ERROR_IO_EXISTS;
#  endif

#  ifdef ENOENT
	case ENOENT:
		return P_ERROR_IO_NOT_EXISTS;
#  endif

#  ifdef ENAMETOOLONG
	case ENAMETOOLONG:
		return P_ERROR_IO_NAMETOOLONG;
#  endif

#  ifdef ENOSYS
	case ENOSYS:
		return P_ERROR_IO_NOT_IMPLEMENTED;
#  endif

	/* Some magic to deal with EWOULDBLOCK and EAGAIN.
	 * Apparently on HP-UX these are actually defined to different values,
	 * but on Linux, for example, they are the same. */
#  if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK == EAGAIN
	/* We have both and they are the same: only emit one case. */
	case EAGAIN:
		return P_ERROR_IO_WOULD_BLOCK;
#  else
	/* Else: consider each of them separately. This handles both the
	 * case of having only one and the case where they are different values. */
#    ifdef EAGAIN
	case EAGAIN:
		return P_ERROR_IO_WOULD_BLOCK;
#    endif

#    ifdef EWOULDBLOCK
	case EWOULDBLOCK:
		return P_ERROR_IO_WOULD_BLOCK;
#    endif
#  endif
#endif /* !P_OS_WIN */
	default:
		return P_ERROR_IO_FAILED;
	}
}

PErrorIO
p_error_get_last_io (void)
{
	return p_error_get_io_from_system (p_error_get_last_system ());
}

PErrorIPC
p_error_get_ipc_from_system (pint err_code)
{
	switch (err_code) {
	case 0:
		return P_ERROR_IPC_NONE;
#ifdef P_OS_WIN
#  ifdef ERROR_ALREADY_EXISTS
	case ERROR_ALREADY_EXISTS:
		return P_ERROR_IPC_EXISTS;
#  endif
#  ifdef ERROR_SEM_OWNER_DIED
	case ERROR_SEM_OWNER_DIED:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif
#  ifdef ERROR_SEM_NOT_FOUND
	case ERROR_SEM_NOT_FOUND:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif
#  ifdef ERROR_SEM_USER_LIMIT
	case ERROR_SEM_USER_LIMIT:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_TOO_MANY_SEMAPHORES
	case ERROR_TOO_MANY_SEMAPHORES:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_ACCESS_DENIED
	case ERROR_ACCESS_DENIED:
		return P_ERROR_IPC_ACCESS;
#  endif
#  ifdef ERROR_EXCL_SEM_ALREADY_OWNED
	case ERROR_EXCL_SEM_ALREADY_OWNED:
		return P_ERROR_IPC_ACCESS;
#  endif
#  ifdef ERROR_TOO_MANY_SEM_REQUESTS
	case ERROR_TOO_MANY_SEM_REQUESTS:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_TOO_MANY_POSTS
	case ERROR_TOO_MANY_POSTS:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_OUTOFMEMORY
	case ERROR_OUTOFMEMORY:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_NOT_ENOUGH_MEMORY
	case ERROR_NOT_ENOUGH_MEMORY:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_INVALID_HANDLE
	case ERROR_INVALID_HANDLE:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_INVALID_PARAMETER
	case ERROR_INVALID_PARAMETER:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_NOT_SUPPORTED
	case ERROR_NOT_SUPPORTED:
		return P_ERROR_IPC_NOT_IMPLEMENTED;
#  endif
#elif defined (P_OS_OS2)
#  ifdef ERROR_NOT_ENOUGH_MEMORY
	case ERROR_NOT_ENOUGH_MEMORY:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_INVALID_PARAMETER
	case ERROR_INVALID_PARAMETER:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_INVALID_NAME
	case ERROR_INVALID_NAME:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_INVALID_HANDLE
	case ERROR_INVALID_HANDLE:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_FILE_NOT_FOUND
	case ERROR_FILE_NOT_FOUND:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif
#  ifdef ERROR_INVALID_ADDRESS
	case ERROR_INVALID_ADDRESS:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif
#  ifdef ERROR_GEN_FAILURE
	case ERROR_GEN_FAILURE:
		return P_ERROR_IPC_FAILED;
#  endif
#  ifdef ERROR_LOCKED
	case ERROR_LOCKED:
		return P_ERROR_IPC_ACCESS;
#  endif
#  ifdef ERROR_DUPLICATE_NAME
	case ERROR_DUPLICATE_NAME:
		return P_ERROR_IPC_EXISTS;
#  endif
#  ifdef ERROR_TOO_MANY_HANDLES
	case ERROR_TOO_MANY_HANDLES:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_TOO_MANY_OPENS
	case ERROR_TOO_MANY_OPENS:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_TOO_MANY_SEM_REQUESTS
	case ERROR_TOO_MANY_SEM_REQUESTS:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif
#  ifdef ERROR_SEM_OWNER_DIED
	case ERROR_SEM_OWNER_DIED:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif
#  ifdef ERROR_NOT_OWNER
	case ERROR_NOT_OWNER:
		return P_ERROR_IPC_ACCESS;
#  endif
#  ifdef ERROR_SEM_NOT_FOUND
	case ERROR_SEM_NOT_FOUND:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif
#else /* !P_OS_WINDOWS && !P_OS_OS2 */
#  ifdef EACCES
	case EACCES:
		return P_ERROR_IPC_ACCESS;
#  endif

#  ifdef EPERM
	case EPERM:
		return P_ERROR_IPC_ACCESS;
#  endif

#  ifdef EEXIST
	case EEXIST:
		return P_ERROR_IPC_EXISTS;
#  endif

#  ifdef E2BIG
	case E2BIG:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif

#  ifdef EFAULT
	case EFAULT:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif

#  ifdef EFBIG
	case EFBIG:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif

#  ifdef EINVAL
	case EINVAL:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif

#  ifdef ELOOP
	case ELOOP:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif

#  ifdef ERANGE
	case ERANGE:
		return P_ERROR_IPC_INVALID_ARGUMENT;
#  endif

#  ifdef ENOMEM
	case ENOMEM:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif

#  ifdef EMFILE
	case EMFILE:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif

#  ifdef ENFILE
	case ENFILE:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif

#  ifdef ENOSPC
	case ENOSPC:
		return P_ERROR_IPC_NO_RESOURCES;
#  endif

#  ifdef EIDRM
	case EIDRM:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif

#  ifdef ENOENT
	case ENOENT:
		return P_ERROR_IPC_NOT_EXISTS;
#  endif

#  ifdef EOVERFLOW
	case EOVERFLOW:
		return P_ERROR_IPC_OVERFLOW;
#  endif

#  ifdef ENOSYS
	case ENOSYS:
		return P_ERROR_IPC_NOT_IMPLEMENTED;
#  endif

#  ifdef EDEADLK
	case EDEADLK:
		return P_ERROR_IPC_DEADLOCK;
#  endif

#  ifdef ENAMETOOLONG
	case ENAMETOOLONG:
		return P_ERROR_IPC_NAMETOOLONG;
#  endif
#endif /* !P_OS_WIN */
	default:
		return P_ERROR_IPC_FAILED;
	}
}

PErrorIPC
p_error_get_last_ipc (void)
{
	return p_error_get_ipc_from_system (p_error_get_last_system ());
}

P_LIB_API PError *
p_error_new (void)
{
	PError *ret;

	if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PError))) == NULL))
		return NULL;

	return ret;
}

P_LIB_API PError *
p_error_new_literal (pint		code,
		     pint		native_code,
		     const pchar	*message)
{
	PError *ret;

	if (P_UNLIKELY ((ret = p_error_new ()) == NULL))
		return NULL;

	ret->code        = code;
	ret->native_code = native_code;
	ret->message     = p_strdup (message);

	return ret;
}

P_LIB_API const pchar *
p_error_get_message (PError *error)
{
	if (P_UNLIKELY (error == NULL))
		return NULL;

	return error->message;
}

P_LIB_API pint
p_error_get_code (PError *error)
{
	if (P_UNLIKELY (error == NULL))
		return 0;

	return error->code;
}

P_LIB_API pint
p_error_get_native_code	(PError	*error)
{
	if (P_UNLIKELY (error == NULL))
		return 0;

	return error->native_code;
}

P_LIB_API PErrorDomain
p_error_get_domain (PError *error)
{
	if (P_UNLIKELY (error == NULL))
		return P_ERROR_DOMAIN_NONE;

	if (error->code >= (pint) P_ERROR_DOMAIN_IPC)
		return P_ERROR_DOMAIN_IPC;
	else if (error->code >= (pint) P_ERROR_DOMAIN_IO)
		return P_ERROR_DOMAIN_IO;
	else
		return P_ERROR_DOMAIN_NONE;
}

P_LIB_API PError *
p_error_copy (PError *error)
{
	PError *ret;

	if (P_UNLIKELY (error == NULL))
		return NULL;

	if (P_UNLIKELY ((ret = p_error_new_literal (error->code,
						    error->native_code,
						    error->message)) == NULL))
		return NULL;

	return ret;
}

P_LIB_API void
p_error_set_error (PError	*error,
		   pint		code,
		   pint		native_code,
		   const pchar	*message)
{
	if (P_UNLIKELY (error == NULL))
		return;

	if (error->message != NULL)
		p_free (error->message);

	error->code        = code;
	error->native_code = native_code;
	error->message     = p_strdup (message);
}

P_LIB_API void
p_error_set_error_p (PError		**error,
		     pint		code,
		     pint		native_code,
		     const pchar	*message)
{
	if (error == NULL || *error != NULL)
		return;

	*error = p_error_new_literal (code, native_code, message);
}

P_LIB_API void
p_error_set_code (PError	*error,
		  pint		code)
{
	if (P_UNLIKELY (error == NULL))
		return;

	error->code = code;
}

P_LIB_API void
p_error_set_native_code	(PError	*error,
			 pint	native_code)
{
	if (P_UNLIKELY (error == NULL))
		return;

	error->native_code = native_code;
}

P_LIB_API void
p_error_set_message (PError		*error,
		     const pchar	*message)
{
	if (P_UNLIKELY (error == NULL))
		return;

	if (error->message != NULL)
		p_free (error->message);

	error->message = p_strdup (message);
}

P_LIB_API void
p_error_clear (PError *error)
{
	if (P_UNLIKELY (error == NULL))
		return;

	if (error->message != NULL)
		p_free (error->message);

	error->message     = NULL;
	error->code        = 0;
	error->native_code = 0;
}

P_LIB_API void
p_error_free (PError	*error)
{
	if (P_UNLIKELY (error == NULL))
		return;

	if (error->message != NULL)
		p_free (error->message);

	p_free (error);
}

P_LIB_API pint
p_error_get_last_system (void)
{
#ifdef P_OS_WIN
	return (pint) GetLastError ();
#else
#  ifdef P_OS_VMS
	pint error_code = errno;

	if (error_code == EVMSERR)
		return vaxc$errno;
	else
		return error_code;
#  else
	return errno;
#  endif
#endif
}

P_LIB_API pint
p_error_get_last_net (void)
{
#if defined (P_OS_WIN)
	return WSAGetLastError ();
#elif defined (P_OS_OS2)
	return sock_errno ();
#else
	return p_error_get_last_system ();
#endif
}

P_LIB_API void
p_error_set_last_system (pint code)
{
#ifdef P_OS_WIN
	SetLastError (code);
#else
	errno = code;
#endif
}

P_LIB_API void
p_error_set_last_net (pint code)
{
#if defined (P_OS_WIN)
	WSASetLastError (code);
#elif defined (P_OS_OS2)
	P_UNUSED (code);
#else
	errno = code;
#endif
}