Skip to content
Snippets Groups Projects
utils.c 1.14 KiB
Newer Older
/* utils.c
 *
 * Copyright 2019 Bruce Cowan <bruce@bcowan.me.uk>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

Bruce Cowan's avatar
Bruce Cowan committed
#include <stdio.h>
#include <stdlib.h>

int *
get_ints (int         argc,
          char      **argv,
          int         n,
          const char *prompt)
{
    int *ret = malloc (sizeof (int) * n);

    if (argc != (n + 1))
    {
        for (int i = 0; i < n; i++)
        {
            printf ("%s: ", prompt);
            scanf ("%d", &ret[i]);
        }
    }
    else
    {
        for (int i = 0; i < n; i++)
            sscanf (argv[i + 1], "%d", &ret[i]);
    }
    
    return ret;
}