Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 896187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:41:18+00:00 2026-05-15T14:41:18+00:00

I was writing a CLI-Tool for Mac OS X (10.5+) that has to deal

  • 0

I was writing a CLI-Tool for Mac OS X (10.5+) that has to deal with command-line arguments which are very likely to contain non-ASCII characters.

For further processing, I convert these arguments using +[NSString stringWithCString:encoding:].

My problem is, that I couldn’t find good information on how to determine the character-encoding used by the shell in which said cli-tool is running in.
What I came up with as a solution is the following:

NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *ianaName = [[environment objectForKey:@"LANG"] pathExtension];
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(
  CFStringConvertIANACharSetNameToEncoding( (CFStringRef)ianaName ) );

NSString *someArgument = [NSString stringWithCString:argv[someIndex] encoding:encoding];

I find that a little crude, however — which makes me think that I missed out something obvious…but what?

Is there a saner/cleaner way of achieving essentially the same?

Thanks in advance

D

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:41:18+00:00Added an answer on May 15, 2026 at 2:41 pm

    Okay, it turns out there seems to be none!

    As Yuji pointed out, the underlying encoding of filenames is UTF-8, no matter what. Therefore, one needed to handle two scenarios:

    1. Arguments that are typed in, character for character, by the user.
    2. Arguments that are tab-completed or the output of commands like ls, as they do not convert any characters.

    The second case is simply covered by the assumption of UTF-8.

    The first case, however, is problematic:

    • On Mac OS 10.6, $LANG contains the IANA-name of the used encoding like de_DE.IANA_NAME.
    • Prior to Snow Leopard, this is not the case for charsets other than UTF-8!

    I didn’t test each and every charset I could think of, but none of the european ones were included. Instead, $LANG only was the language-locale (de_DE in my case)!

    Since the results of calling +[NSString stringWithCString:encoding:] with an incorrect encoding are undefined, you cannot safely assume that it will return nil in that case* (if eg. it’s ASCII-only, it might work perfectly fine!).

    What adds to the overall mess is that $LANG is not guarateed to be around, anyway: There’s a checkbox in Terminal.app’s preferences, that enables a user to not set $LANG at all (not to speak of X11.app which doesn’t seem to handle any non-ASCII input…).

    So what’s left:

    1. Check for presence of $LANG. If it’s not set, Goto:4!
    2. Check if $LANG contains information on the encoding. If it doesn’t, Goto:4!
    3. Check if the encoding you find there is UTF-8. If it is Goto:6, else…
    4. If argc is greater than 2 and [[NSString stringWithCString: argv[0] encoding: NSUTF8StringEncoding] isEqualToString: yourForceUTFArgumentFlag], print that you are forcing UTF-8 now and Goto 6. If not:
    5. Assume you don’t know anything, issue a warning that your user should set the Terminal encoding to UTF-8 and may consider passing yourForceUTFArgumentFlag as the first argument and exit().
    6. Assume UTF-8 and do what you have to…

    Sounds shitty? That’s because it is, but I can’t think of any saner way of doing it.


    One further note though:
    If you are using UTF-8 as an encoding, stringWithCString:encoding: returns nil whenever it encounters non-ASCII characters in a C-String that is not encoded in UTF-8.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Java application that takes command line arguments which are processed
I'm trying to develop a command line interface for a tool I'm writing. I'm
I am writing an application that has both CLI and GUI. I read most
Title explains. I have native C++ dlls that I'm writing C++/CLI wrappers for, which
I am writing a Ruby CLI (Command Line Interface) program and I would like
I'm writing an application that sends requests to the PHP CLI. However, as i
I am writing a wrapper around C DLL in C++/CLI so that it can
I am writing a small Ruby script that will run in a CLI. To
I am interested in writing a simple CLI program that will send an SMS
I'm writing a mixed C++/CLI DLL with both managed and unmanaged classes. Both need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.