|
56 | 56 | #include <fcntl.h> |
57 | 57 | #endif |
58 | 58 |
|
| 59 | +#ifndef _WIN32 |
| 60 | +#include <dirent.h> |
| 61 | +#endif |
| 62 | + |
59 | 63 | #ifdef _WIN32 |
60 | 64 |
|
61 | 65 | #define WIN32_LEAN_AND_MEAN |
@@ -199,6 +203,14 @@ struct cobsort { |
199 | 203 |
|
200 | 204 | /* End SORT definitions */ |
201 | 205 |
|
| 206 | +#ifdef _WIN32 |
| 207 | +HANDLE listdir_handle; |
| 208 | +LPWIN32_FIND_DATA listdir_filedata; |
| 209 | +#else |
| 210 | +DIR *listdir_handle; |
| 211 | +struct dirent *listdir_filedata; |
| 212 | +#endif |
| 213 | + |
202 | 214 | #define OPENMODESIZE 3 |
203 | 215 | #define READOPTSSIZE 4 |
204 | 216 | #define STARTCONDSIZE 2 |
@@ -5456,6 +5468,124 @@ cob_acuw_file_delete (unsigned char *file_name, unsigned char *file_type) |
5456 | 5468 | return ret; |
5457 | 5469 | } |
5458 | 5470 |
|
| 5471 | +int |
| 5472 | +cob_listdir_open(cob_field *f_dirname, cob_field *f_pattern) |
| 5473 | +{ |
| 5474 | + //FIXME: now not use file pattern(ex. *). |
| 5475 | +#ifdef _WIN32 |
| 5476 | + char *dirname = cob_str_from_fld(f_dirname); |
| 5477 | + char *pattern = cob_str_from_fld(f_pattern); |
| 5478 | + |
| 5479 | + LPCTSTR lpFileName = cob_malloc(strlen(dirname) + 1 + strlen(pattern) + 1); |
| 5480 | + |
| 5481 | + if (listdir_filedata == NULL){ |
| 5482 | + listdir_filedata = cob_malloc(sizeof(LPWIN32_FIND_DATA)); |
| 5483 | + } |
| 5484 | + |
| 5485 | + strcpy (lpFileName, dirname); |
| 5486 | + strcat (lpFileName, "\\"); |
| 5487 | + strcat (lpFileName, pattern); |
| 5488 | + listdir_handle = FindFirstFile (lpFileName, listdir_filedata); |
| 5489 | + free (dirname); |
| 5490 | + free (pattern); |
| 5491 | + free (lpFileName); |
| 5492 | + if (listdir_handle == INVALID_HANDLE_VALUE) { |
| 5493 | + return 0; |
| 5494 | + } |
| 5495 | + |
| 5496 | +#else |
| 5497 | + char *dirname = cob_str_from_fld(f_dirname); |
| 5498 | + listdir_handle = opendir(dirname); |
| 5499 | + free(dirname); |
| 5500 | + if (listdir_handle == NULL) { |
| 5501 | + return 0; |
| 5502 | + } |
| 5503 | + |
| 5504 | +#endif |
| 5505 | + return listdir_handle; |
| 5506 | +} |
| 5507 | + |
| 5508 | +int |
| 5509 | +cob_listdir_next(cob_field *f_handle, cob_field *f_filename) |
| 5510 | +{ |
| 5511 | + //FIXME: now not use handle. |
| 5512 | + char *filename; |
| 5513 | + int length; |
| 5514 | + |
| 5515 | +#ifndef _WIN32 |
| 5516 | + listdir_filedata = readdir(listdir_handle); |
| 5517 | +#endif |
| 5518 | +#ifdef _WIN32 |
| 5519 | + filename = listdir_filedata->cFileName; |
| 5520 | +#else |
| 5521 | + listdir_filedata = readdir(listdir_handle); |
| 5522 | + if (listdir_filedata == NULL){ |
| 5523 | + filename = " "; |
| 5524 | + }else{ |
| 5525 | + filename = listdir_filedata->d_name; |
| 5526 | + } |
| 5527 | +#endif |
| 5528 | + length = strlen(filename); |
| 5529 | + |
| 5530 | + if (length > f_filename->size) { |
| 5531 | + length = f_filename->size; |
| 5532 | + } |
| 5533 | + memset(f_filename->data, ' ', f_filename->size); |
| 5534 | + memcpy(f_filename->data, filename, length); |
| 5535 | +#ifdef _WIN32 |
| 5536 | + if(!FindNextFile (listdir_handle, listdir_filedata)) { |
| 5537 | + strcpy(listdir_filedata->cFileName, " "); |
| 5538 | + } |
| 5539 | +#endif |
| 5540 | + return 0; |
| 5541 | +} |
| 5542 | + |
| 5543 | +int |
| 5544 | +cob_listdir_close(cob_field *f_handle) |
| 5545 | +{ |
| 5546 | + //FIXME: now not use handle. |
| 5547 | +#ifdef _WIN32 |
| 5548 | + FindClose (listdir_handle); |
| 5549 | +#else |
| 5550 | + closedir (listdir_handle); |
| 5551 | +#endif |
| 5552 | + return 0; |
| 5553 | +} |
| 5554 | + |
| 5555 | +int |
| 5556 | +cob_acuw_list_directory (unsigned char *data, ...) |
| 5557 | +{ |
| 5558 | + int operation_code = -1; |
| 5559 | + int return_code; |
| 5560 | + |
| 5561 | + COB_CHK_PARMS(C$LIST-DIRECTORY, 1); |
| 5562 | + |
| 5563 | + if (cob_current_module->cob_procedure_parameters[0] == NULL){ |
| 5564 | + return -1; |
| 5565 | + } |
| 5566 | + |
| 5567 | + operation_code = cob_get_int(cob_current_module->cob_procedure_parameters[0]); |
| 5568 | + |
| 5569 | + switch (operation_code) |
| 5570 | + { |
| 5571 | + case 1://LISTDIR-OPEN(value:1) |
| 5572 | + return_code = cob_listdir_open(cob_current_module->cob_procedure_parameters[1], |
| 5573 | + cob_current_module->cob_procedure_parameters[2]); |
| 5574 | + break; |
| 5575 | + case 2://LISTDIR-NEXT(value:2) |
| 5576 | + return_code = cob_listdir_next(cob_current_module->cob_procedure_parameters[1], |
| 5577 | + cob_current_module->cob_procedure_parameters[2]); |
| 5578 | + break; |
| 5579 | + case 3://LISTDIR-CLOSE(value:3) |
| 5580 | + return_code = cob_listdir_close(cob_current_module->cob_procedure_parameters[1]); |
| 5581 | + break; |
| 5582 | + default: |
| 5583 | + //error |
| 5584 | + return -1; |
| 5585 | + } |
| 5586 | + return return_code; |
| 5587 | +} |
| 5588 | + |
5459 | 5589 | /* SORT */ |
5460 | 5590 |
|
5461 | 5591 | static int |
|
0 commit comments