0

How to print specific user temp directory in macOS? It should be able to determine the temp dir depending on which user is currently logged in. I see that my current user temp dir is set to /Users/myuser/private/tmp but i have no generic way to get hold of this

Googled a bit and it recommeds using $TMPDIR but looks like my user temp dir is somewhat set to /Users/myuser/private/tmp

I should be able to determine the exact tmp directory whether the current user is root or other user

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1
    How can something be *"somewhat set"*? Surely it is either set to that or it isn't? I am not sure what you are trying to work out, but for reference, if I do `echo $TMPDIR` I get `/var/folders/0m/f8tx2qn9179f9g1pmg1g2ymr0000gn/T/` – Mark Setchell Sep 13 '19 at 09:08

1 Answers1

0

Related SO question and answer, cross-platform: Cross-platform way of getting temp directory in Python It's pretty generic.

In Qt5:

#include <QCoreApplication>
#include <QTemporaryDir>
#include <QDebug>

int main(int argc, char *argv[])
{
    QTemporaryDir dir;
    if (dir.isValid()) {
        // dir.path() returns the unique directory path
        qDebug() << "Path name: " << dir.path() ;
    }
}

Also pretty generic.

In bash (terminal or script): echo $TMPDIR

The /var/folders/ is user-specific because if you run ls -l /var/folders/.../ (right before the -Tmp- directory, you will see that the folder is owned by the logged in user. If another user is logged in, their folder is protected by their permissions/ownership and they won't be able to read/modify another users' data. I suggest looking into unix/linux/mac file and folder permissions if you want to know more.

new-host-2:Applications ThisIsMe$ ls -l /var/folders/cp/cpI9fK8qG3y4x4sedjGIOE+++TI/ 
total 0
drwx------  16 ThisIsMe  staff  544 Jan  6 22:12 -Caches-
drwx------  11 ThisIsMe  staff  374 Jan  6 22:56 -Tmp-

http://www.filepermissions.com/articles/what-are-file-permissions-in-linux-and-mac https://www.linux.com/tutorials/understanding-linux-file-permissions/

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52
  • Even xcode sees the temporary directory as that. I followed this SO question https://stackoverflow.com/questions/11263692/how-to-return-the-app-temp-directory, built an example Cocoa project and inserted the `NSLog(@"Temporary directory is: %@", NSTemporaryDirectory());` line into the main function to verify. – NuclearPeon Jan 07 '20 at 07:19