misc.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #include "proc.h"
  6. /*
  7. * Description: Convert a NULL string terminated UNIX environment block to
  8. * an environment block suitable for a windows32 system call
  9. *
  10. * Returns: TRUE= success, FALSE=fail
  11. *
  12. * Notes/Dependencies: the environment block is sorted in case-insensitive
  13. * order, is double-null terminated, and is a char *, not a char **
  14. */
  15. int _cdecl compare(const void *a1, const void *a2)
  16. {
  17. return _stricoll(*((char**)a1),*((char**)a2));
  18. }
  19. bool_t
  20. arr2envblk(char **arr, char **envblk_out)
  21. {
  22. char **tmp;
  23. int size_needed;
  24. int arrcnt;
  25. char *ptr;
  26. arrcnt = 0;
  27. while (arr[arrcnt]) {
  28. arrcnt++;
  29. }
  30. tmp = (char**) calloc(arrcnt + 1, sizeof(char *));
  31. if (!tmp) {
  32. return FALSE;
  33. }
  34. arrcnt = 0;
  35. size_needed = 0;
  36. while (arr[arrcnt]) {
  37. tmp[arrcnt] = arr[arrcnt];
  38. size_needed += strlen(arr[arrcnt]) + 1;
  39. arrcnt++;
  40. }
  41. size_needed++;
  42. qsort((void *) tmp, (size_t) arrcnt, sizeof (char*), compare);
  43. ptr = *envblk_out = calloc(size_needed, 1);
  44. if (!ptr) {
  45. free(tmp);
  46. return FALSE;
  47. }
  48. arrcnt = 0;
  49. while (tmp[arrcnt]) {
  50. strcpy(ptr, tmp[arrcnt]);
  51. ptr += strlen(tmp[arrcnt]) + 1;
  52. arrcnt++;
  53. }
  54. free(tmp);
  55. return TRUE;
  56. }