| 798 | */ |
| 799 | |
| 800 | int /* O - New number of destinations */ |
| 801 | cupsCopyDest(cups_dest_t *dest, /* I - Destination to copy */ |
| 802 | int num_dests, /* I - Number of destinations */ |
| 803 | cups_dest_t **dests) /* IO - Destination array */ |
| 804 | { |
| 805 | int i; /* Looping var */ |
| 806 | cups_dest_t *new_dest; /* New destination pointer */ |
| 807 | cups_option_t *new_option, /* Current destination option */ |
| 808 | *option; /* Current parent option */ |
| 809 | |
| 810 | |
| 811 | /* |
| 812 | * Range check input... |
| 813 | */ |
| 814 | |
| 815 | if (!dest || num_dests < 0 || !dests) |
| 816 | return (num_dests); |
| 817 | |
| 818 | /* |
| 819 | * See if the destination already exists... |
| 820 | */ |
| 821 | |
| 822 | if ((new_dest = cupsGetDest(dest->name, dest->instance, num_dests, |
| 823 | *dests)) != NULL) |
| 824 | { |
| 825 | /* |
| 826 | * Protect against copying destination to itself... |
| 827 | */ |
| 828 | |
| 829 | if (new_dest == dest) |
| 830 | return (num_dests); |
| 831 | |
| 832 | /* |
| 833 | * Otherwise, free the options... |
| 834 | */ |
| 835 | |
| 836 | cupsFreeOptions(new_dest->num_options, new_dest->options); |
| 837 | |
| 838 | new_dest->num_options = 0; |
| 839 | new_dest->options = NULL; |
| 840 | } |
| 841 | else |
| 842 | new_dest = cups_add_dest(dest->name, dest->instance, &num_dests, dests); |
| 843 | |
| 844 | if (new_dest) |
| 845 | { |
| 846 | new_dest->is_default = dest->is_default; |
| 847 | |
| 848 | if ((new_dest->options = calloc((size_t)dest->num_options, sizeof(cups_option_t))) == NULL) |
| 849 | return (cupsRemoveDest(dest->name, dest->instance, num_dests, dests)); |
| 850 | |
| 851 | new_dest->num_options = dest->num_options; |
| 852 | |
| 853 | for (i = dest->num_options, option = dest->options, |
| 854 | new_option = new_dest->options; |
| 855 | i > 0; |
| 856 | i --, option ++, new_option ++) |
| 857 | { |