1 #define _XOPEN_SOURCE 700
4 /*#include <locale.h>*/
10 #include <sys/types.h>
17 char credentials_file
[] = "../test_credentials";
19 static int read_config(char **line
, int order
) {
28 file
= fopen(credentials_file
, "r");
30 fprintf(stderr
, "Could open %s\n", credentials_file
);
34 for (int i
= 0; i
< order
; i
++) {
35 if (-1 == getline(line
, &length
, file
)) {
36 fprintf(stderr
, "Could not read line #%d from %s: ",
37 i
+ 1, credentials_file
);
39 fprintf(stderr
, "error occured\n");
41 fprintf(stderr
, "end of file reached\n");
43 fprintf(stderr
, "I don't know why\n");
53 eol
= strpbrk(*line
, "\r\n");
59 const char *username(void) {
60 static char *username
;
63 username
= getenv("ISDS_USERNAME");
65 read_config(&username
, 1);
70 const char *password(void) {
71 static char *password
;
74 password
= getenv("ISDS_PASSWORD");
76 read_config(&password
, 2);
81 void print_DbState(const long int state
) {
83 case DBSTATE_ACCESSIBLE
: printf("ACCESSIBLE\n"); break;
84 case DBSTATE_TEMP_UNACCESSIBLE
: printf("TEMP_UNACCESSIBLE\n"); break;
85 case DBSTATE_NOT_YET_ACCESSIBLE
: printf("NOT_YET_ACCESSIBLE\n"); break;
86 case DBSTATE_PERM_UNACCESSIBLE
: printf("PERM_UNACCESSIBLE\n"); break;
87 case DBSTATE_REMOVED
: printf("REMOVED\n"); break;
88 case DBSTATE_TEMP_UNACCESSIBLE_LAW
: printf("DBSTATE_TEMP_UNACCESSIBLE_LAW"); break;
89 default: printf("<unknown state %ld>\n", state
);
93 void print_DbType(const long int *type
) {
94 if (!type
) printf("NULL\n");
97 case DBTYPE_SYSTEM
: printf("SYSTEM\n"); break;
98 case DBTYPE_FO
: printf("FO\n"); break;
99 case DBTYPE_PFO
: printf("PFO\n"); break;
100 case DBTYPE_PFO_ADVOK
: printf("PFO_ADVOK\n"); break;
101 case DBTYPE_PFO_DANPOR
: printf("PFO_DAPOR\n"); break;
102 case DBTYPE_PFO_INSSPR
: printf("PFO_INSSPR\n"); break;
103 case DBTYPE_PFO_AUDITOR
: printf("PFO_AUDITOR\n"); break;
104 case DBTYPE_PO
: printf("PO\n"); break;
105 case DBTYPE_PO_ZAK
: printf("PO_ZAK\n"); break;
106 case DBTYPE_PO_REQ
: printf("PO_REQ\n"); break;
107 case DBTYPE_OVM
: printf("OVM\n"); break;
108 case DBTYPE_OVM_NOTAR
: printf("OVM_NOTAR\n"); break;
109 case DBTYPE_OVM_EXEKUT
: printf("OVM_EXEKUT\n"); break;
110 case DBTYPE_OVM_REQ
: printf("OVM_REQ\n"); break;
111 default: printf("<unknown type %ld>\n", *type
);
116 void print_UserType(const long int *type
) {
117 if (!type
) printf("NULL\n");
120 case USERTYPE_PRIMARY
: printf("PRIMARY\n"); break;
121 case USERTYPE_ENTRUSTED
: printf("ENTRUSTED\n"); break;
122 case USERTYPE_ADMINISTRATOR
: printf("ADMINISTRATOR\n"); break;
123 case USERTYPE_OFFICIAL
: printf("OFFICIAL\n"); break;
124 case USERTYPE_OFFICIAL_CERT
: printf("OFFICIAL_CERT\n"); break;
125 case USERTYPE_LIQUIDATOR
: printf("LIQUIDATOR\n"); break;
126 case USERTYPE_RECEIVER
: printf("RECEIVER\n"); break;
127 case USERTYPE_GUARDIAN
: printf("GUARDIAN\n"); break;
128 default: printf("<unknown type %ld>\n", *type
);
133 void print_sender_type(const isds_sender_type
*type
) {
134 if (!type
) printf("NULL\n");
137 case SENDERTYPE_PRIMARY
: printf("PRIMARY\n"); break;
138 case SENDERTYPE_ENTRUSTED
: printf("ENTRUSTED\n"); break;
139 case SENDERTYPE_ADMINISTRATOR
: printf("ADMINISTRATOR\n"); break;
140 case SENDERTYPE_OFFICIAL
: printf("OFFICIAL\n"); break;
141 case SENDERTYPE_VIRTUAL
: printf("VIRTUAL\n"); break;
142 case SENDERTYPE_OFFICIAL_CERT
: printf("OFFICIAL_CERT\n"); break;
143 case SENDERTYPE_LIQUIDATOR
: printf("LIQUIDATOR\n"); break;
144 case SENDERTYPE_RECEIVER
: printf("RECEIVER\n"); break;
145 case SENDERTYPE_GUARDIAN
: printf("GUARDIAN\n"); break;
146 default: printf("<unknown type %u>\n", *type
);
151 void print_UserPrivils(const long int *privils
) {
153 const char *priviledges
[] = {
163 const int priviledges_count
= sizeof(priviledges
)/sizeof(priviledges
[0]);
165 if (!privils
) printf("NULL\n");
167 printf("%ld (", *privils
);
169 for (int i
= 0; i
< priviledges_count
; i
++) {
170 if (*privils
& (1<<i
)) {
172 ((i
+ 1) == priviledges_count
) ? "%s" : "%s|",
182 void print_hash(const struct isds_hash
*hash
) {
188 switch(hash
->algorithm
) {
189 case HASH_ALGORITHM_MD5
: printf("MD5 "); break;
190 case HASH_ALGORITHM_SHA_1
: printf("SHA-1 "); break;
191 case HASH_ALGORITHM_SHA_256
: printf("SHA-256 "); break;
192 case HASH_ALGORITHM_SHA_512
: printf("SHA-512 "); break;
193 default: printf("<Unknown hash algorithm %d> ", hash
->algorithm
);
197 if (!hash
->value
) printf("<NULL>");
199 for (size_t i
= 0; i
< hash
->length
; i
++) {
200 if (i
> 0) printf(":");
201 printf("%02x", ((uint8_t *)(hash
->value
))[i
]);
208 void print_raw_type(const isds_raw_type type
) {
210 case RAWTYPE_INCOMING_MESSAGE
:
211 printf("INCOMING_MESSAGE\n"); break;
212 case RAWTYPE_PLAIN_SIGNED_INCOMING_MESSAGE
:
213 printf("PLAIN_SIGNED_INCOMING_MESSAGE\n"); break;
214 case RAWTYPE_CMS_SIGNED_INCOMING_MESSAGE
:
215 printf("CMS_SIGNED_INCOMING_MESSAGE\n"); break;
216 case RAWTYPE_PLAIN_SIGNED_OUTGOING_MESSAGE
:
217 printf("PLAIN_SIGNED_OUTGOING_MESSAGE\n"); break;
218 case RAWTYPE_CMS_SIGNED_OUTGOING_MESSAGE
:
219 printf("CMS_SIGNED_OUTGOING_MESSAGE\n"); break;
220 case RAWTYPE_DELIVERYINFO
:
221 printf("DELIVERYINFO\n"); break;
222 case RAWTYPE_PLAIN_SIGNED_DELIVERYINFO
:
223 printf("PLAIN_SIGNED_DELIVERYINFO\n"); break;
224 case RAWTYPE_CMS_SIGNED_DELIVERYINFO
:
225 printf("CMS_SIGNED_DELIVERYINFO\n"); break;
227 printf("<Unknown raw type %d> ", type
);
232 static void print_dmMessageStatus(const isds_message_status
*status
) {
233 if (!status
) printf("NULL\n");
236 case MESSAGESTATE_SENT
: printf("SENT\n"); break;
237 case MESSAGESTATE_STAMPED
: printf("STAMPED\n"); break;
238 case MESSAGESTATE_INFECTED
: printf("INFECTED\n"); break;
239 case MESSAGESTATE_DELIVERED
: printf("DELIVERED\n"); break;
240 case MESSAGESTATE_SUBSTITUTED
: printf("SUBSTITUTED\n"); break;
241 case MESSAGESTATE_RECEIVED
: printf("RECEIVED\n"); break;
242 case MESSAGESTATE_READ
: printf("READ\n"); break;
243 case MESSAGESTATE_UNDELIVERABLE
: printf("UNDELIVERABLE\n"); break;
244 case MESSAGESTATE_REMOVED
: printf("REMOVED\n"); break;
245 case MESSAGESTATE_IN_SAFE
: printf("IN_SAFE\n"); break;
246 default: printf("<unknown type %d>\n", *status
);
250 void print_bool(const _Bool
*boolean
) {
251 printf("%s\n", (!boolean
) ? "NULL" : ((*boolean
)? "true" : "false") );
255 void print_longint(const long int *number
) {
256 if (!number
) printf("NULL\n");
257 else printf("%ld\n", *number
);
261 void print_PersonName(const struct isds_PersonName
*personName
) {
262 printf("\tpersonName = ");
263 if (!personName
) printf("NULL\n");
266 printf("\t\tpnFirstName = %s\n", personName
->pnFirstName
);
267 printf("\t\tpnMiddleName = %s\n", personName
->pnMiddleName
);
268 printf("\t\tpnLastName = %s\n", personName
->pnLastName
);
269 printf("\t\tpnLastNameAtBirth = %s\n", personName
->pnLastNameAtBirth
);
275 void print_Address(const struct isds_Address
*address
) {
276 printf("\taddress = ");
277 if (!address
) printf("NULL\n");
281 printf("\t\tadCode = ");
282 print_longint(address
->adCode
);
284 printf("\t\tadCity = %s\n", address
->adCity
);
285 printf("\t\tadDistrict = %s\n", address
->adDistrict
);
286 printf("\t\tadStreet = %s\n", address
->adStreet
);
287 printf("\t\tadNumberInStreet = %s\n", address
->adNumberInStreet
);
288 printf("\t\tadNumberInMunicipality = %s\n",
289 address
->adNumberInMunicipality
);
290 printf("\t\tadZipCode = %s\n", address
->adZipCode
);
291 printf("\t\tadState = %s\n", address
->adState
);
297 void print_date(const struct tm
*date
) {
298 if (!date
) printf("NULL\n");
299 else printf("%s", asctime(date
));
303 void print_DbOwnerInfo(const struct isds_DbOwnerInfo
*info
) {
304 printf("dbOwnerInfo = ");
312 printf("\tdbID = %s\n", info
->dbID
);
314 printf("\tdbType = ");
315 print_DbType((long int *) (info
->dbType
));
316 printf("\tic = %s\n", info
->ic
);
318 print_PersonName(info
->personName
);
320 printf("\tfirmName = %s\n", info
->firmName
);
322 printf("\tbirthInfo = ");
323 if (!info
->birthInfo
) printf("NULL\n");
327 printf("\t\tbiDate = ");
328 print_date(info
->birthInfo
->biDate
);
330 printf("\t\tbiCity = %s\n", info
->birthInfo
->biCity
);
331 printf("\t\tbiCounty = %s\n", info
->birthInfo
->biCounty
);
332 printf("\t\tbiState = %s\n", info
->birthInfo
->biState
);
336 print_Address(info
->address
);
338 printf("\tnationality = %s\n", info
->nationality
);
339 printf("\temail = %s\n", info
->email
);
340 printf("\ttelNumber = %s\n", info
->telNumber
);
341 printf("\tidentifier = %s\n", info
->identifier
);
343 printf("\taifoIsds = ");
344 print_bool(info
->aifoIsds
);
346 printf("\tregistryCode = %s\n", info
->registryCode
);
348 printf("\tdbState = ");
349 if (!info
->dbState
) printf("NULL\n");
350 else print_DbState(*(info
->dbState
));
352 printf("\tdbEffectiveOVM = ");
353 print_bool(info
->dbEffectiveOVM
);
355 printf("\tdbOpenAddressing = ");
356 print_bool(info
->dbOpenAddressing
);
363 void print_DbUserInfo(const struct isds_DbUserInfo
*info
) {
364 printf("dbUserInfo = ");
372 printf("\tuserID = %s\n", info
->userID
);
374 printf("\tuserType = ");
375 print_UserType((long int *) (info
->userType
));
377 printf("\tuserPrivils = ");
378 print_UserPrivils(info
->userPrivils
);
380 print_PersonName(info
->personName
);
381 print_Address(info
->address
);
383 printf("\tbiDate = ");
384 print_date(info
->biDate
);
386 printf("\tic = %s\n", info
->ic
);
387 printf("\tfirmName = %s\n", info
->firmName
);
389 printf("\tcaStreet = %s\n", info
->caStreet
);
390 printf("\tcaCity = %s\n", info
->caCity
);
391 printf("\tcaZipCode = %s\n", info
->caZipCode
);
392 printf("\tcaState = %s\n", info
->caState
);
393 printf("\taifo_ticket = %s\n", info
->aifo_ticket
);
399 void print_timeval(const struct timeval
*time
) {
402 time_t seconds_as_time_t
;
409 /* MinGW32 GCC 4.8+ uses 64-bit time_t but time->tv_sec is defined as
410 * 32-bit long in Microsoft API. Convert value to the type expected by
412 seconds_as_time_t
= time
->tv_sec
;
413 if (!localtime_r(&seconds_as_time_t
, &broken
)) goto error
;
414 if (!strftime(buffer
, sizeof(buffer
)/sizeof(char), "%c", &broken
))
416 printf("%s, %" PRIdMAX
" us\n", buffer
, (intmax_t)time
->tv_usec
);
420 printf("<Error while formating>\n>");
425 void print_event_type(const isds_event_type
*type
) {
431 case EVENT_UKNOWN
: printf("UNKNOWN\n"); break;
432 case EVENT_ENTERED_SYSTEM
: printf("ENTERED_SYSTEM\n"); break;
433 case EVENT_ACCEPTED_BY_RECIPIENT
:
434 printf("ACCEPTED_BY_RECIPIENT\n"); break;
435 case EVENT_ACCEPTED_BY_FICTION
:
436 printf("DELIVERED_BY_FICTION\n"); break;
437 case EVENT_UNDELIVERABLE
:
438 printf("UNDELIVERABLE\n"); break;
439 case EVENT_COMMERCIAL_ACCEPTED
:
440 printf("COMMERCIAL_ACCEPTED\n"); break;
441 case EVENT_DELIVERED
:
442 printf("DELIVERED\n"); break;
443 case EVENT_PRIMARY_LOGIN
:
444 printf("PRIMARY_LOGIN\n"); break;
445 case EVENT_ENTRUSTED_LOGIN
:
446 printf("ENTRUSTED_LOGIN\n"); break;
447 case EVENT_SYSCERT_LOGIN
:
448 printf("SYSCERT_LOGIN\n"); break;
449 default: printf("<unknown type %d>\n", *type
);
454 void print_events(const struct isds_list
*events
) {
455 const struct isds_list
*item
;
456 const struct isds_event
*event
;
465 for (item
= events
; item
; item
= item
->next
) {
466 event
= (struct isds_event
*) item
->data
;
467 printf("\t\t\tevent = ");
468 if (!event
) printf("NULL");
472 printf("\t\t\t\ttype = ");
473 print_event_type(event
->type
);
475 printf("\t\t\t\tdescription = %s\n", event
->description
);
477 printf("\t\t\t\ttime = ");
478 print_timeval(event
->time
);
488 void print_envelope(const struct isds_envelope
*envelope
) {
489 printf("\tenvelope = ");
497 printf("\t\tdmID = %s\n", envelope
->dmID
);
498 printf("\t\tdbIDSender = %s\n", envelope
->dbIDSender
);
499 printf("\t\tdmSender = %s\n", envelope
->dmSender
);
500 printf("\t\tdmSenderAddress = %s\n", envelope
->dmSenderAddress
);
501 printf("\t\tdmSenderType = ");
502 print_DbType(envelope
->dmSenderType
);
503 printf("\t\tdmRecipient = %s\n", envelope
->dmRecipient
);
504 printf("\t\tdmRecipientAddress = %s\n", envelope
->dmRecipientAddress
);
505 printf("\t\tdmAmbiguousRecipient = ");
506 print_bool(envelope
->dmAmbiguousRecipient
);
507 printf("\t\tdmType = %s\n", envelope
->dmType
);
509 printf("\t\tdmSenderOrgUnit = %s\n", envelope
->dmSenderOrgUnit
);
510 printf("\t\tdmSenderOrgUnitNum = ");
511 print_longint(envelope
->dmSenderOrgUnitNum
);
512 printf("\t\tdbIDRecipient = %s\n", envelope
->dbIDRecipient
);
513 printf("\t\tdmRecipientOrgUnit = %s\n", envelope
->dmRecipientOrgUnit
);
514 printf("\t\tdmRecipientOrgUnitNum = ");
515 print_longint(envelope
->dmRecipientOrgUnitNum
);
516 printf("\t\tdmToHands = %s\n", envelope
->dmToHands
);
517 printf("\t\tdmAnnotation = %s\n", envelope
->dmAnnotation
);
518 printf("\t\tdmRecipientRefNumber = %s\n", envelope
->dmRecipientRefNumber
);
519 printf("\t\tdmSenderRefNumber = %s\n", envelope
->dmSenderRefNumber
);
520 printf("\t\tdmRecipientIdent = %s\n", envelope
->dmRecipientIdent
);
521 printf("\t\tdmSenderIdent = %s\n", envelope
->dmSenderIdent
);
523 printf("\t\tdmLegalTitleLaw = ");
524 print_longint(envelope
->dmLegalTitleLaw
);
525 printf("\t\tdmLegalTitleYear = ");
526 print_longint(envelope
->dmLegalTitleYear
);
527 printf("\t\tdmLegalTitleSect = %s\n", envelope
->dmLegalTitleSect
);
528 printf("\t\tdmLegalTitlePar = %s\n", envelope
->dmLegalTitlePar
);
529 printf("\t\tdmLegalTitlePoint = %s\n", envelope
->dmLegalTitlePoint
);
531 printf("\t\tdmPersonalDelivery = ");
532 print_bool(envelope
->dmPersonalDelivery
);
533 printf("\t\tdmAllowSubstDelivery = ");
534 print_bool(envelope
->dmAllowSubstDelivery
);
535 printf("\t\tdmOVM = ");
536 print_bool(envelope
->dmOVM
);
537 printf("\t\tdmPublishOwnID = ");
538 print_bool(envelope
->dmPublishOwnID
);
540 printf("\t\tdmOrdinal = ");
541 if (!envelope
->dmOrdinal
) printf("NULL\n");
542 else printf("%lu\n", *(envelope
->dmOrdinal
));
544 printf("\t\tdmMessageStatus = ");
545 print_dmMessageStatus(envelope
->dmMessageStatus
);
547 printf("\t\tdmAttachmentSize = ");
548 if (!envelope
->dmAttachmentSize
) printf("NULL\n");
549 else printf("%lu kB\n", *(envelope
->dmAttachmentSize
));
551 printf("\t\tdmDeliveryTime = ");
552 print_timeval(envelope
->dmDeliveryTime
);
554 printf("\t\tdmAcceptanceTime = ");
555 print_timeval(envelope
->dmAcceptanceTime
);
557 printf("\t\thash = ");
558 print_hash(envelope
->hash
);
560 printf("\t\ttimestamp = %p\n", envelope
->timestamp
);
561 printf("\t\ttimestamp_length = %zu\n", envelope
->timestamp_length
);
563 printf("\t\tevents = ");
564 print_events(envelope
->events
);
570 void print_document(const struct isds_document
*document
) {
571 printf("\t\tdocument = ");
579 printf("\t\t\tis_xml = %u\n", !!document
->is_xml
);
580 printf("\t\t\txml_node_list = %p\n", document
->xml_node_list
);
582 printf("\t\t\tdata = %p\n", document
->data
);
583 printf("\t\t\tdata_length = %zu\n", document
->data_length
);
584 printf("\t\t\tdmMimeType = %s\n", document
->dmMimeType
);
586 printf("\t\t\tdmFileMetaType = ");
587 switch(document
->dmFileMetaType
) {
588 case FILEMETATYPE_MAIN
: printf("MAIN\n"); break;
589 case FILEMETATYPE_ENCLOSURE
: printf("ENCLOSURE\n"); break;
590 case FILEMETATYPE_SIGNATURE
: printf("SIGNATURE\n"); break;
591 case FILEMETATYPE_META
: printf("META\n"); break;
592 default: printf("<unknown type %d>\n", document
->dmFileMetaType
);
595 printf("\t\t\tdmFileGuid = %s\n", document
->dmFileGuid
);
596 printf("\t\t\tdmUpFileGuid = %s\n", document
->dmUpFileGuid
);
597 printf("\t\t\tdmFileDescr = %s\n", document
->dmFileDescr
);
598 printf("\t\t\tdmFormat = %s\n", document
->dmFormat
);
603 void print_documents(const struct isds_list
*documents
) {
604 const struct isds_list
*item
;
606 printf("\tdocuments = ");
614 for (item
= documents
; item
; item
= item
->next
) {
615 print_document((struct isds_document
*) (item
->data
));
622 void print_message(const struct isds_message
*message
) {
623 printf("message = ");
632 printf("\traw = %p\n", message
->raw
);
633 printf("\traw_length = %zu\n", message
->raw_length
);
634 printf("\traw_type = ");
635 print_raw_type(message
->raw_type
);
636 printf("\txml = %p\n", message
->xml
);
637 print_envelope(message
->envelope
);
638 print_documents(message
->documents
);
643 void print_copies(const struct isds_list
*copies
) {
644 const struct isds_list
*item
;
645 struct isds_message_copy
*copy
;
654 for (item
= copies
; item
; item
= item
->next
) {
655 copy
= (struct isds_message_copy
*) item
->data
;
662 printf("\t\tdbIDRecipient = %s\n", copy
->dbIDRecipient
);
663 printf("\t\tdmRecipientOrgUnit = %s\n", copy
->dmRecipientOrgUnit
);
665 printf("\t\tdmRecipientOrgUnitNum = ");
666 if (copy
->dmRecipientOrgUnitNum
)
667 printf("%ld\n", *copy
->dmRecipientOrgUnitNum
);
670 printf("\t\tdmToHands = %s\n", copy
->dmToHands
);
672 printf("\t\terror = %s\n", isds_strerror(copy
->error
));
673 printf("\t\tdmStatus = %s\n", copy
->dmStatus
);
674 printf("\t\tdmID = %s\n", copy
->dmID
);
681 void print_message_status_change(
682 const struct isds_message_status_change
*changed_status
) {
683 printf("message_status_change = ");
685 if (!changed_status
) {
692 printf("\tdmID = %s\n", changed_status
->dmID
);
694 printf("\tdmMessageStatus = ");
695 print_dmMessageStatus(changed_status
->dmMessageStatus
);
698 print_timeval(changed_status
->time
);
703 void compare_hashes(const struct isds_hash
*hash1
,
704 const struct isds_hash
*hash2
) {
707 printf("Comparing hashes... ");
708 err
= isds_hash_cmp(hash1
, hash2
);
709 if (err
== IE_SUCCESS
)
710 printf("Hashes equal\n");
712 (err
== IE_NOTEQUAL
) printf("Hashes differ\n");
714 printf("isds_hash_cmp() failed: %s\n", isds_strerror(err
));
718 int progressbar(double upload_total
, double upload_current
,
719 double download_total
, double download_current
,
722 printf("Progress: upload %0f/%0f, download %0f/%0f, data=%p\n",
723 upload_current
, upload_total
, download_current
, download_total
,
726 printf("Aborting transfer...\n");
733 int mmap_file(const char *file
, int *fd
, void **buffer
, size_t *length
) {
734 struct stat file_info
;
736 if (!file
|| !fd
|| !buffer
|| !length
) return -1;
739 *fd
= open(file
, O_RDONLY
);
741 fprintf(stderr
, "%s: Could not open file: %s\n", file
, strerror(errno
));
745 if (-1 == fstat(*fd
, &file_info
)) {
746 fprintf(stderr
, "%s: Could not get file size: %s\n", file
,
751 if (file_info
.st_size
< 0) {
752 fprintf(stderr
, "File `%s' has negative size: %" PRIdMAX
"\n", file
,
753 (intmax_t) file_info
.st_size
);
757 *length
= file_info
.st_size
;
760 /* Empty region cannot be mmapped */
763 *buffer
= mmap(NULL
, *length
, PROT_READ
, MAP_PRIVATE
, *fd
, 0);
764 if (*buffer
== MAP_FAILED
) {
765 fprintf(stderr
, "%s: Could not map file to memory: %s\n", file
,
776 int munmap_file(int fd
, void *buffer
, size_t length
) {
778 long int page_size
= sysconf(_SC_PAGE_SIZE
);
779 size_t pages
= (length
% page_size
) ?
780 ((length
/ page_size
) + 1) * page_size
:
784 err
= munmap(buffer
, pages
);
787 "Could not unmap memory at %p and length %zu: %s\n",
788 buffer
, pages
, strerror(errno
));
794 fprintf(stderr
, "Could close file descriptor %d: %s\n", fd
,
802 static int save_data_to_file(const char *file
, const void *data
,
803 const size_t length
) {
805 ssize_t written
, left
= length
;
807 if (!file
) return -1;
808 if (length
> 0 && !data
) return -1;
810 fd
= open(file
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0666);
812 fprintf(stderr
, "%s: Could not open file for writing: %s\n",
813 file
, strerror(errno
));
817 printf("Writing %zu bytes to file `%s'...\n", length
, file
);
819 written
= write(fd
, data
+ length
- left
, left
);
821 fprintf(stderr
, "%s: Could not save file: %s\n",
822 file
, strerror(errno
));
829 if (-1 == close(fd
)) {
830 fprintf(stderr
, "%s: Closing file failed: %s\n",
831 file
, strerror(errno
));
840 int save_data(const char *message
, const void *data
, const size_t length
) {
842 printf("%s\n", message
);
843 return save_data_to_file("output", data
, length
);