how to implement command ls -l using execvp? - system-calls

I try to implement command ls -l using execvp.
I have code
execvp("ls",&argv[0]);
work good, but i use
execvp("ls -l",&argv[0]);
doesn't work

The first argument to execvp() needs to be the binary name you want to run. You can't just add additional arguments in there. The argument list is coming from the &argv[0]. You'll need to create a new array to insert your argument. Something like this:
char** new_args = malloc((argc + 1) * sizeof(char*));
new_args[0] = argv[0];
new_args[1] = "-l";
memcpy(&new_args[2], &argv[1], (argc - 1) * sizeof(char*));
execvp("ls", new_args);

Related

How to pass arguments within a function in imageJ macro?

I want to pass in foldername into the run() function and the save() function but they are within quotes and I don't know how to pass the variable names.
I generated the run() and save() function using the record command in imageJ.
The aim of this program to to automate the process of running the plugin "T2 analysis mouse" on multiple folders.
Any other tweets or help will be appreciated.
I found this online source https://imagej.nih.gov/ij/macros/ArgumentPassingDemo.txt but it does not work.
I tried multiple ways inclusing /&foldername/3 T2 &filename/3 T2 /&foldername 3 T2``/&foldername3 T2 /&filename 3 T2``/&filename3 T2
Here is the code I wrote:
function makeT2(foldername)
{
filename = foldername;
print(filename);
run("T2 Analysis mouse", "select=[/Users/Vineeth/Desktop/New stuff Feb 5/&filename 3 T2]
image=[32-bit Signed] width=256 height=128 offset=0 number=902 gap=0 little-endian");
run("Save", "save=[/Users/Vineeth/Desktop/New stuff Feb 5/&foldername3 T2/T2map.tif]");
close();
}
folders = getFileList("/Users/Vineeth/Desktop/New stuff Feb 5/");
for( i = 0; i < folders.length; i++)
{
print(folders[i]);
file = getFileList("/Users/Vineeth/Desktop/New stuff Feb 5/" + folders[i]);
for( j = 0; j < file.length; j++)
{
if(file[j] == "3 T2/")
{
print("made t2 for " + folders[i] + file[j]);
makeT2(folders[i]);
}
}
}
The various print() functions are for testing the code and the code mostly works expect for the makeT2 function.
This is the error:
java.lang.NullPointerException
at T2_Analysis_mouse.getImage(T2_Analysis_mouse.java:183)
at T2_Analysis_mouse.run(T2_Analysis_mouse.java:56)
at ij.IJ.runUserPlugIn(IJ.java:183)
at ij.IJ.runPlugIn(IJ.java:150)
at ij.Executer.runCommand(Executer.java:124)
at ij.Executer.run(Executer.java:61)
at ij.IJ.run(IJ.java:249)
at ij.macro.Functions.doRun(Functions.java:561)
at ij.macro.Functions.doFunction(Functions.java:79)
at ij.macro.Interpreter.doStatement(Interpreter.java:203)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.runUserFunction(Interpreter.java:278)
at ij.macro.Interpreter.doStatement(Interpreter.java:206)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.doStatement(Interpreter.java:239)
at ij.macro.Interpreter.doIf(Interpreter.java:852)
at ij.macro.Interpreter.doStatement(Interpreter.java:215)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.doStatement(Interpreter.java:239)
at ij.macro.Interpreter.doFor(Interpreter.java:464)
at ij.macro.Interpreter.doStatement(Interpreter.java:221)
at ij.macro.Interpreter.doBlock(Interpreter.java:518)
at ij.macro.Interpreter.doStatement(Interpreter.java:239)
at ij.macro.Interpreter.doFor(Interpreter.java:464)
at ij.macro.Interpreter.doStatement(Interpreter.java:221)
at ij.macro.Interpreter.doStatements(Interpreter.java:191)
at ij.macro.Interpreter.run(Interpreter.java:102)
at ij.macro.Interpreter.run(Interpreter.java:72)
at ij.macro.MacroRunner.run(MacroRunner.java:124)
at java.lang.Thread.run(Thread.java:695)
and this is the log after running the progran:
LeeMouseOld_30Jan18.LH1/
made t2 for LeeMouseOld_30Jan18.LH1/3 T2/
LeeMouseOld_30Jan18.LH1/
Please choose a directory where the T1 or T2 folder is the last item in the path (i.e. 5_t1 or 6_t2)
Directory return for General T folder: /Users/Vineeth/Desktop/New stuff Feb 5/&filename 3 T2/
Please adjust import settings to:
Image type: 32-bit signed
Width: 128 pixels
Height: 256 pixels
Number of images: 50 images
Check Little-endian byte order
As you can see &filename remains the same but I want the data within the variable filename to be in its place.
Use string concatenation:
run("T2 Analysis mouse", "select=[/Users/Vineeth/Desktop/New stuff Feb 5/" + filename + " 3 T2]
image=[32-bit Signed] width=256 height=128 offset=0 number=902 gap=0 little-endian");
run("Save", "save=[/Users/Vineeth/Desktop/New stuff Feb 5/" + foldername + "3 T2/T2map.tif]");
See the macro function documentation (emphasis added):
run("command"[, "options"])
Executes an ImageJ menu command. The optional second argument contains values that are automatically entered into dialog boxes (must be GenericDialog or OpenDialog). Use the Command Recorder (Plugins>Macros>Record) to generate run() function calls. Use string concatentation to pass a variable as an argument. With ImageJ 1.43 and later, variables can be passed without using string concatenation by adding "&" to the variable name. For examples, see the ArgumentPassingDemo macro.
The mentioned shortcut syntax with & does not work between square brackets [] in the option string.
A number of questions on the ImageJ forum also discuss this.

Deploy an matlab file to executable

I want to deploy an m file into an executable. I am using mcc command: mcc -m epidemic.m. Epidemic is my function which takes no arguments and returns a vector and write that vector to txt. Mcc creates epidemic.exe and when I am running that exe it creates the txt file however it seems that it doesnt return values (the return value of .exe). I am trying to run the exe from matlab using:
cmd = ['epidemic.exe '];
system(cmd);
It return cmdout " and status 0. How can I take the returned values of the .exe?
When you compile matlab code like:
function [out1, out2] = epidemic(in1, in2, in3)
%[
...
%]
to standalone (mcc -m epidemeic.m), Matlab produces somehow the following pseudo c-code and compiles it to .exe:
int main(int argc, char** argv)
{
// Load compiled code produced by mcc
HMCRInstance* hInst = loadByteCodeProducedByMccFromResources();
// Similar to have wrote in matlab "epidemic(argv[0], argv[1], ...)"
// 1) Without asking for any argument output
// 2) Argument inputs are passed as strings
int errorCode = mclFevalFromExeArg(hInst, "epidemic", argc, argv);
return errorCode; // only indicates if call to 'mclFEvalFromExeArg'
// succeded, it does not relate to out1, out2 at all.
}
NB: If you want to see the exact code produced by mcc, use mcc -W main -T codegen epidemic.m
So, directly compiling to standalone, you cannot work with outputs of your Matlab function. If you need to play around with output arguments of epidemic, either
[Simple solution] Consider saving outputs to files or display them to shell console using disp (NB: you can use isdeployed in your .m file to check if you're running from matlab or from compiled code).
[Advanced solution] Consider compiling your code to shared library (mcc -l epidemic.m) instead of standalone (mcc -m epidemeic.m)
NB: When you compile your code to shared library, mcc will produce a dll that exports the following function:
extern LIB_epidemeic_C_API
bool MW_CALL_CONV mlxEpidemic(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
nrhs/prhs are the number of input arguments and their values (as mxArray type). And nlhs/plhs are the ouput arguments you want to have when calling epidemic. Up to you to do the marshaling between mxArray and equivalent C native type.
EDIT
As you indicate that epidemic returns a vector of values, you can display them from standalone like this:
function [output] = epidemic(v1, v2, v3)
%[
% When called from system cmd line, v1, v2, v3 are passed
% as string. Here is how to convert them to expected type if required
if (ischar(v1)), v1 = str2double(v1); end
if (ischar(v2), v2 = str2double(v2); end
if (ischar(v3)), v3 = str2double(v3); end
...
output = ...;
...
if (isdeployed())
disp(output);
end
%]
An exe does not have a return value, you need to find another way to transport the data back, for example via console outputs or text files. What you get is the error code and error message.

Return a field from a function that returns a struct

I'm using the function dbstack to get the name of the currently executing function. dbstack returns a struct, with three fields: file, name and line. I'm only interested in name. Is there any way of returning just the name field when I call the dbstack function, or do I need to use two lines (the following)?
thisFunction = dbstack;
thisFunctionName = thisFunction.name;
Your solution is the easiest (and propebly the best) way of doing what you want.
Alternatively, you can use getfield
>> thisFunctionName = getfield( dbstack, 'name' )

How to test, if an element is in a list?

Is there a built-in function to test, if a given element is in a list in Rexx?
I could not find one. The alternative would be to loop over the list and check each element manually.
No (unless things have changed); just loop through the list.
An alternative is instead / as well have a lookup variable
i.e.
lookup. = 0 /* not all versions of Rexx support
default initialisation like this */
....
addToList:
parse arg item
numberInList = numberInList + 1
list.numberInList = item
lookup.item = 1
return
You can then check if item is in the list by
if lookup.item = 1 then do
......
It depends what you mean by a list.
At work, I use classic REXX. I frequently store lists of words in a single variable, space delimited. So WORDPOS() is the built-in function I use.
If you are using a List class in ooREXX. then why not use the hasItem method from the Collection class.

Is there any way to give a format to arithmetic computations in Objective-C?

i want to define something similar to a computation method:
NSString *format = #"%d + 1";
In my code i want to do something like:
int computedNumber = sum(format,5) => the result should be 6.
could you give some suggestions?
thank you
EDIT:
or something similar to:
NSNumber *no = [NSNumber numberWithFormat:format,5];
It is not normally possible, however there have been written parsers for this specific tasks, to mention DDMathParser by SO user Dave DeLong.
But for what task do you really need this? You have the + operator, and then you perform the sum function? Couldn't you simply parse the number at the end of the format, then perform the operation you'd like?
Using Macro can be an alternative solution to your problem. You can define a macro like the following,
#define INC(a) (a + 1)
Here, INC and a are user-defined. You can give them any name you want. Compiler will substitute (a + 1) in your code, where ever you call INC(a). For example, consider the following,
int computedNumber = INC(5);
After the compilation the code will be,
int computedNumber = (5 + 1); // the result during the execution is 6.