简体   繁体   中英

arguments] Problem Objective-C

I am having trouble with NSProcessInfo's arguments property. I am creating a command line tool that needs to decode base64 code that it has been passed from the internet using a PHP script, along with some other arguments. The data is passed fine, but for some reason. [[NSProcessInfo processInfo] arguments] returns 21 arguments, even though I pass just one base64 string.

Here's the objective-c side of it:

NSArray *arguments = [[NSProcessInfo processInfo] arguments];

if ([[arguments objectAtIndex:1] isEqualToString:@"-s"])
{
    if ([arguments objectAtIndex:2] == nil)
    {
        printf("Error: No data\n");
        [pool drain];
        return 0;
    }

    NSString*data = [arguments objectAtIndex:2];

    if ([data length] == 0)
    {
        printf("Error: No data\n");
        [pool drain];
        return 0;
    }

    NSString*password = @"";

    if ([[arguments objectAtIndex:3] isEqualToString:@"-p"])
    {
        if ([arguments objectAtIndex:4] == nil)
        {
            printf("Error: No password\n");
            [pool drain];
            return 0;
        }
        else
        {
            password = [NSString stringWithString:[arguments lastObject]];
        }
    }

NSLog(@"Args: %i\n\n",[arguments count]); //returns 21? I expect 3.

The base64 code is a bit long, so I've put it here . Does anyone know why this code returns this many arguments? It's supposed to be just one string? Edit: I am stripping whitespaces in my PHP script. See here:

<?php

$url = $_GET['data'];

$query = "/Library/WebServer/email/emailsender -s";
$password = "-p somePassword";

$commandStr = trim("$query $url $password");

$commandStr = removeNewLines($commandStr);

echo $commandStr;

$output = shell_exec($commandStr);

echo "<pre>Output: $output</pre>";

function removeNewLines($string) {

    $string = str_replace( "\t", ' ', $string );
    $string = str_replace( "\n", ' ', $string );
    $string = str_replace( "\r", ' ', $string );
    $string = str_replace( "\0", ' ', $string );
    $string = str_replace( "\x0B", ' ', $string );

    return $string;

}

?>

When I display the Base64 string on your pastie page as "raw" I see a lot of spaces in it. So most likely the arguments is correct and your PHP script is calling the Objective-C program the wrong way. An easy fix might be to just strip out any whitespace before passing the string, or properly escape it.

When you send arguments to a program through the command-line, each argument is separated by a whitespace character. This means that if you post a string that contains spaces, your program will interpret it as many arguments. To prevent this behavior, you need to quote your strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM