LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2012, 04:12 AM   #1
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Rep: Reputation: 21
Howto run bash-built-in programs from inside a Ada-Program ?


Hi Friends,

usually I launch an external program from inside the Ada code this way:

Spawn
( Program_Name => "/bin/ls", -- example with the ls command
Args => Arguments,
Output_File_Descriptor => Standout,
Return_Code => Result
);

The example above works well since "ls" is not bash built-in and it has a definitive location "/bin/ls".
The Ada-Spawn function requires the absolute path of the executable. But the actual problem is to launch something like "umask". Umask is built-in. If I run

whereis umask

I don't get any absolute path where umask is located. So how do I tell the GNAT compiler where "umask" is to be found ?

Thanks.
 
Old 09-20-2012, 07:06 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You could execute /bin/sh (or other shell), with parameters -c 'umask 0022'. But it wouldn't help, because setting umask would affect only the shell itself, that terminates immediately.

So you have to find a way to set umask in the Ada program.
http://pnyf.inf.elte.hu/kto/oktatas/...x_Book/16.html
 
Old 09-20-2012, 07:29 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Are you trying to set or look at the umask value?

If you only need to look, you can use the stat utility to do so (see the manual page for stat); e.g.,
Code:
stat /bin/ls
  File: `/bin/ls'
  Size: 113176    	Blocks: 224        IO Block: 4096   regular file
Device: 801h/2049d	Inode: 390994      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-09-19 11:13:37.339737917 -0400
Modify: 2012-01-16 21:37:52.000000000 -0500
Change: 2012-02-07 13:06:06.614323313 -0500
 Birth: -
You can get only the Access values by using format arguments to stat, say
Code:
stat -c %a /bin/ls
755
Note that when you create a file it will (usually) be created with the system default file creation mask; if the system mask is 0022, files are created with umask 0644 -rw-r--r--. If you need to change that (or want the created file to be a different mask) you would do that with appropriate arguments to the function used to create the file.

For example, if I were to
Code:
umask
0022               # this is the system default mask
touch gob          # create a new file
stat -c %a gob     # see the umask value of the file 
644
The stat utility is really useful for a whole lot of things and it's worth a little time to get familiar with it, methinks.

Hope this helps some.
 
Old 09-21-2012, 01:47 AM   #4
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Original Poster
Rep: Reputation: 21
Thanks for your replies !

@NevemTeve : Ok, the full command reads: /bin/sh -c 'umask 003' . Yes, even if I run it directly from the command line, it does not change the umask value of the current shell. If I just run umask 003 it does what I want.

@tronayne : I'm trying to set the umask value. The command in full reads "umask 003". This should apply for all files the Ada-program is going to create further on.

So, I need to dig into http://pnyf.inf.elte.hu/kto/oktatas/...x_Book/16.html
unless someone gives me an idea.
 
Old 09-21-2012, 07:57 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
OK, that explains what you're trying to do. So let's see if we can explain what's going on.

When you execute anything it does not execute in your current shell, rather, a new shell is created, the program executes and the new shell dies at completion. That new shell inherits all the environment settings from the current shell. The umask value is a shell environment variable for newly created files (and directories), not a file-by-file mask.

So, what do you do so that any files created by your running program will have the mask -rw-rw-r-- instead of the default -rw-r--r--?

Easy way is simply execute umask 003 then execute your program. That will only "stick" as long as the user is logged in but it's kind of a pain to do.

Another way is to create a little program launcher (a shell program) that sets umask the executes the program. That may get complicated if there are command line arguments to the program but it's easily handled too. Call it something like, oh, runprog.

Yet another way is to set the file creation mask in your ADA program every time you create a file in it; no mistakes that way.

The second is probably your best bet, though, because it's easy and nobody who is going to execute your program needs to know that they're simply typing a command with some arguments.

For example,
Code:
#!/bin/sh     (or bash or ksh or whatever)
# set umask
umask 003
# execute prog
prog
exit
It's just a shell program that sets an environment value then executes a command using that environment. You'd handle command line arguments as you would in any shell program.

Hope this helps some.
 
Old 09-26-2012, 09:40 AM   #6
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Original Poster
Rep: Reputation: 21
Smile

Quote:
Originally Posted by tronayne View Post

So, what do you do so that any files created by your running program will have the mask -rw-rw-r-- instead of the default -rw-r--r--?
That is another long story. Better not talking to much on that :-)

My question of how to run a built-in bash command was more of general nature. I know the issue with a subshell created inside another shell. If I run umask 003 from inside a shell script, the files that very script is creating further on, have mask -rw-rw-r-- . If I do the umask from inside the ADA code the aren't.

However, my initial questions is answered: A built in command is to be executed like /bin/sh -c 'umask 003'

Thanks to all of you ! :-)
 
Old 09-26-2012, 12:31 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Couldn't you read the linked documentation yet?

Code:
function umask( mask : integer ) return integer;
pragma import( c, umask );
 
Old 10-15-2012, 05:08 AM   #8
Mario Blunk
Member
 
Registered: Dec 2008
Location: Wild Eastern Germany
Distribution: OpenSuse Tumbleweed
Posts: 131

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by NevemTeve View Post
Couldn't you read the linked documentation yet?

Code:
function umask( mask : integer ) return integer;
pragma import( c, umask );
Hello NevemTeve ! That works fine ! Great, Thanks also four http://pnyf.inf.elte.hu/kto/oktatas/...x_Book/16.html - very helpful. cheers from Germany !
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Interpreting $variables inside programs run from a shell script TheBigH Programming 6 06-10-2011 08:36 AM
i want to run python program on new built lfs shankara Linux From Scratch 1 02-03-2006 02:42 PM
how do I run a command from inside a c++ program? exodist Programming 1 04-06-2004 04:34 PM
run java programs from the command line...howto? rmanocha Programming 3 03-06-2004 02:50 AM
howto run blackbox in bash? ichbinesderelch Linux - Software 3 10-22-2003 12:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration