Extracting sequence header from mpeg video file - c

I am new too mpeg2dec and I Have been trying to extract sequence header from a video file with mpeg2.h(from libmpeg2) but not getting anywhere.
I tried by getting STATE_SEQUENCE and tried to point to that location in the file but could not copy that to another file.
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
//mpeg2dec library
#include "mpeg2dec/mpeg2.h"
//jni header
#include "VideoProc.h"
int mpeg2Fun(uint8_t * pointer);
//jni function to get byte array from and return integer
JNIEXPORT jint JNICALL Java_VideoProc_mpeg2FunC(JNIEnv * env, jobject jobj, jbyteArray bytes)
{
jbyte u_byte[4096] ;
(*env)->GetByteArrayRegion(env,bytes, 0, 4096, u_byte);
jint reply=mpeg2Fun(u_byte);
return reply;
}
int mpeg2Fun(uint8_t * pointer)
{
mpeg2dec_t * decoder;
const mpeg2_info_t * info;
const mpeg2_sequence_t * sequence;
mpeg2_state_t state;
size_t size;
int framenum = 0;
decoder = mpeg2_init ();
if (decoder == NULL) {
fprintf (stderr, "Could not allocate a decoder object.\n");
exit (1);
}
info = mpeg2_info (decoder);
int t;
size = (size_t)-1;
do {
state = mpeg2_parse (decoder);
sequence = info->sequence;
switch (state) {
case STATE_BUFFER:
mpeg2_buffer (decoder, pointer, pointer + 4096);
break;
case STATE_SEQUENCE:
t=mpeg2_getpos(decoder);
return 4096-t;
break;
default:
break;
}
} while (size);
mpeg2_close (decoder);
}
In the statement of case STATE_SEQUENCE: instead of returning position, I wanted to write the whole sequence header to the another file. Is that correct or not ?

Related

How to check if a file exists in a given path in C?

I am trying to find the file(say marks.txt) in the particular path passed as argument to a function. Is it possible to give the filename and path as arguments to a function which checks if the file exists and prints out the path?
The below function only takes path as argument.
int fileexists(const char *path){
File *ptr = fopen(path, "r");
if (fptr == NULL)
return 0;
fclose(fptr);
return 1;
}
The required function prototype :
int fileexists(const char *path, const char *filename)
There are two parts to this question, and the right answers to them depend on what you're trying to do.
Concatenate a directory name and a file name to form a full path name.
Determine whether a file (referred to by a full path name) exists or not.
Concatenating a directory name and a file name is straightforward. Your friendsstrcpy and strcat will do most of the work. There are a few minor details to be careful of: (a) You'll need a big enough buffer for the full pathname, and you'll need to decide whether to use a fixed-size array (perhaps of size MAX_PATH), or a malloc'ed buffer; (b) you might need to insert an explicit '/' character (and it usually doesn't hurt to stick one in even if the directory string already ends in one); (c) under Windows you might want to use '\\' instead of '/'.
And then determining whether a file named by a full pathname exists is already well answered over at What's the best way to check if a file exists in C?. The big question to ask here is, are you asking whether the file exists in preparation to doing something with the file? If so, you have a serious vulnerability if you check for the file's existence, but then before you do the other thing, something else happens to cause the file to appear or disappear. So rather than checking-and-then-doing, it's usually better to just try doing the other thing, and deal gracefully with any errors.
The function you have checks if the file can be opened, but it will fail for some files that exist but you have no rights to open. I'd use stat instead. To concatenate the path and filename you can use string functions.
The usual Unix C APIs are dismal. It takes lots of effort to do the simplest of things correctly - and even then I'm not sure that I didn't forget some Unix-ism like signal handling or some obscure error cases. I.e. stuff that's rather trivial to get right in modern C++.
I wish someone designed a modern C system API and implemented it for at least Linux, so that our suffering would end...
Usually, string concatenation requires some higher level API to be done while maintaining a modicum of sanity. Thus, the example below uses a strbuilder class to build the string. This makes things vaguely readable and avoids most common mistakes.
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct strbuilder {
unsigned items, item;
size_t length, *lengths;
char *str, *dst;
};
bool strbuilder_pass(struct strbuilder *builder, int *rc);
void strcat_str(struct strbuilder *builder, const char *src);
void strcat_c_ifnone(struct strbuilder *builder, char c);
bool strbuilder_is_freed(const struct strbuilder *builder);
int fileExists(const char *path, const char *filename)
{
const char pathSep = '/';
int rc;
struct strbuilder bld = {0};
while (strbuilder_pass(&bld, &rc))
{
strcat_str(&bld, path);
strcat_c_ifnone(&bld, pathSep);
strcat_str(&bld, filename);
if (!rc)
{
struct stat statbuf;
printf("path = %s\n", bld.str);
rc = stat(bld.str, &statbuf);
}
}
assert(strbuilder_is_freed(&bld));
return rc;
}
int main()
{
int rc = fileExists("/", "dev");
assert(rc == 0);
return 0;
}
The string building is controlled by a strbuilder_pass function, which advances the string builder's state through five passes of operation:
Determine the number of items whose width has to be stored (avoids the need to call strlen twice).
Prepare the length storage vector. Determine the length of the buffer needed.
Prepare the output string buffer. Concatenate the elements into the buffer.
Use the output string buffer.
Free the output string buffer.
This API is not particularly special, but fits this use case. Some other ad-hoc approach would work too, but this is IMHO a bit more elegant.
void strbuilder_free(struct strbuilder *builder)
{
free(builder->lengths);
free(builder->str);
memset(builder, 0, sizeof(*builder));
}
bool strbuilder_pass(struct strbuilder *builder, int *rc)
{
if (!builder->length) {// start of pass 1
builder->length = 1; /*term*/
*rc = EAGAIN;
return true;
}
else if (!builder->lengths) // end of pass 1
{
builder->lengths = malloc(sizeof(*builder->lengths) * builder->items);
if (builder->lengths)
return true;
*rc = ENOMEM;
}
else if (!builder->str) // end of pass 2
{
builder->dst = (builder->str = malloc(builder->length));
builder->item = 0;
builder->length = 0;
if (builder->dst) {
*builder->dst = '\0';
return true;
}
*rc = ENOMEM;
}
else if (builder->dst) // end of pass 3
{
while (*builder->dst) { // include optional content
builder->dst++; // skip
builder->length++;
}
builder->dst = NULL;
*rc = 0;
return true;
}
else if (!builder->dst) // end of pass 4 (if any)
{}
else {
*rc = EINVAL;
}
strbuilder_free(builder);
return false;
}
void strcat_str(struct strbuilder *builder, const char *src)
{
if (!src)
return;
if (!builder->lengths) // pass 1
builder->items ++;
else if (!builder->str) // pass 2
{
size_t len = strlen(src);
builder->lengths[builder->item++] = len;
builder->length += len;
}
else if (builder->dst) // pass 3
{
size_t len = builder->lengths[builder->item++];
if (*builder->dst && (!len || *builder->dst != *src))
{
builder->dst++;
builder->length++;
}
memcpy(builder->dst, src, len);
builder->dst += len;
builder->length += len;
*builder->dst = '\0';
}
}
void strcat_c_ifnone(struct strbuilder *builder, char c)
{
if (!builder->lengths) {} // pass 1
else if (!builder->str) // pass 2
{
if (c) builder->length ++;
}
else if (builder->dst) // pass 3
{
if (!builder->length || builder->dst[-1] != c)
*(builder->dst) = c;
}
}
bool strbuilder_is_freed(const struct strbuilder *builder)
{
return !builder || (!builder->lengths && !builder->str);
}
You probably want something like this (no error checking for brevity):
...
#include <string.h> // for str* functions
#include <unistd.h> // for access
#include <stdlib.h> // for malloc
...
int fileexists(const char *path, const char *filename)
{
char *name= malloc(strlen(path) + strlen(filename) + 1);
strcpy(name, path);
strcat(name, filename);
int retval = access(name, F_OK) == 0;
free(name);
return retval;
}
Call like this:
if (fileexists("/some/path/", "somefilename.txt")) ...

Problems using OpenCV with ZBar in C

I'm trying to use OpenCV to load a jpg image from file and pass it to zbar library to decode a barcode. However, no barcodes are decoded properly, even though the code below works when I use functions from libpng to load the image. I have no errors, and I have no idea where the problem is, as I have already checked all posts I could find and nothing worked.
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <zbar.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
zbar_image_scanner_t *scanner = NULL;
IplImage* cvLoadImage(const char* filename, int iscolor);
int main (int argc, char **argv)
{
// create a reader
scanner = zbar_image_scanner_create();
// configure the reader
zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
// obtain image data with opencv
IplImage* img = 0;
int height,width,step,channels;
img = cvLoadImage(argv[1], 1);
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
void *raw = (void *)(img->imageData);
printf("Processing a %dx%d image \n",height,width);
// wrap image data
zbar_image_t *image = zbar_image_create();
zbar_image_set_format(image, *(int*)"Y800");
zbar_image_set_size(image, width, height);
zbar_image_set_data(image, raw, width * height, zbar_image_free_data);
// scan the image for barcodes
int n = zbar_scan_image(scanner, image);
if (n==0){
printf("No barcode detected for image %s\n", argv[1]);
return 1;
}
// extract results
if (n!=0) {
const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
printf("symbol extracted \n");
for(; symbol; symbol = zbar_symbol_next(symbol)) {
// do something useful with results
zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
const char *dataZ = zbar_symbol_get_data(symbol);
printf("decoded %s symbol \"%s\" of image %s \n", zbar_get_symbol_name(typ), dataZ, argv[1]);
}
}
// clean up
zbar_image_destroy(image);
zbar_image_scanner_destroy(scanner);
return 0;
}
That code worked perfectly for me. I used it in my program with small changes:
I don't show how I get the 'struct _IplImage *' {aka 'IplImage *'} because it is done in another file, and get it as param, but it is of course done with cvLoadImage().
I used "GREY" instead of "Y800", but I tried "Y800" and also worked, as they are fundamentally the same.
This works (at least with openCV 2.4.9; openCV is deprecating its C API, and its C++ API should be used instead (I'm against this, but nothing can be done :( )):
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
/* snprintf() */
#include <stdio.h>
/* Packages ------------------------------------------------------------------*/
/* opencv */
#include <cv.h>
/* zbar */
#include <zbar.h>
/* Module --------------------------------------------------------------------*/
#include "this_file.h"
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
# define ZB_CODES_MAX (10)
# define ZBAR_LEN_MAX (1048576)
/******************************************************************************
******* structs **************************************************************
******************************************************************************/
struct ZB_Codes {
int n;
struct {
int type;
char sym_name [80];
char data [ZBAR_LEN_MAX];
} arr [ZB_CODES_MAX];
};
/******************************************************************************
******* variables ************************************************************
******************************************************************************/
struct ZB_Codes zb_codes;
/******************************************************************************
******* functions ************************************************************
******************************************************************************/
void img_zb_decode (struct _IplImage *imgptr)
{
struct zbar_image_scanner_s *scanner;
struct zbar_image_s *image_zb;
const struct zbar_symbol_s *symbol;
/* Type of code to scan */
/* 0 for all; set to another if used only for a specific barcode */
int code_type;
code_type = 0;
/* create & configure a reader */
scanner = zbar_image_scanner_create();
zbar_image_scanner_set_config(scanner, code_type, ZBAR_CFG_ENABLE, 1);
/* wrap image data */
image_zb = zbar_image_create();
zbar_image_set_format(image_zb, *(int *)"GREY");
zbar_image_set_size(image_zb, imgptr->width, imgptr->height);
zbar_image_set_data(image_zb, (void *)(imgptr->imageData),
(imgptr->width * imgptr->height), NULL);
/* scan the image for barcodes */
int i;
zb_codes.n = zbar_scan_image(scanner, image_zb);
if (zb_codes.n) {
/* extract results */
symbol = zbar_image_first_symbol(image_zb);
for (i = 0; i < ZB_CODES_MAX && symbol; i++) {
/* Write results into array */
zb_codes.arr[i].type = zbar_symbol_get_type(symbol);
snprintf(zb_codes.arr[i].sym_name, 80, "%s",
zbar_get_symbol_name(
zb_codes.arr[i].type));
snprintf(zb_codes.arr[i].data, ZBAR_LEN_MAX, "%s",
zbar_symbol_get_data(symbol));
/* Load next symbol */
symbol = zbar_symbol_next(symbol);
}
}
/* clean up */
zbar_image_destroy(image_zb);
zbar_image_scanner_destroy(scanner);
}
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/

Read POST parameters from apache module

I'm trying to read POST parameters from an apache c module.
Here's the code I'm using :
/* Include the required headers from httpd */
#include "httpd.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_config.h"
#include "apr_strings.h"
#include "apr_network_io.h"
#include "apr_dbd.h"
#include <apr_file_info.h>
#include <apr_file_io.h>
#include <apr_tables.h>
#include "util_script.h"
/* Define prototypes of our functions in this module */
typedef struct {
const char *key;
const char *value;
} keyValuePair;
static void register_hooks(apr_pool_t *pool);
static int example_handler(request_rec *r);
keyValuePair *readPost(request_rec *r);
/* Define our module as an entity and assign a function for registering hooks */
module AP_MODULE_DECLARE_DATA example_module =
{
STANDARD20_MODULE_STUFF,
NULL, // Per-directory configuration handler
NULL, // Merge handler for per-directory configurations
NULL, // Per-server configuration handler
NULL, // Merge handler for per-server configurations
NULL, // Any directives we may have for httpd
register_hooks // Our hook registering function
};
/* register_hooks: Adds a hook to the httpd process */
static void register_hooks(apr_pool_t *pool)
{
/* Hook the request handler */
ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
}
/* The handler function for our module.
* This is where all the fun happens!
*/
static int example_handler(request_rec *r)
{
/* First off, we need to check if this is a call for the "example" handler.
* If it is, we accept it and do our things, it not, we simply return DECLINED,
* and Apache will try somewhere else.
*/
if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
// The first thing we will do is write a simple "Hello, world!" back to the client.
ap_rputs("Hello, world!<br/>", r);
return OK;
}
keyValuePair *readPost(request_rec *r) {
apr_array_header_t *pairs = NULL;
apr_off_t len;
apr_size_t size;
int res;
int i = 0;
char *buffer;
keyValuePair *kvp;
res = ap_parse_form_data(r, NULL, &pairs, -1, HUGE_STRING_LEN);
if (res != OK || !pairs) return NULL; /* Return NULL if we failed or if there are is no POST data */
kvp = apr_pcalloc(r->pool, sizeof(keyValuePair) * (pairs->nelts + 1));
while (pairs && !apr_is_empty_array(pairs)) {
ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);
apr_brigade_length(pair->value, 1, &len);
size = (apr_size_t) len;
buffer = apr_palloc(r->pool, size + 1);
apr_brigade_flatten(pair->value, buffer, &size);
buffer[len] = 0;
kvp[i].key = apr_pstrdup(r->pool, pair->name);
kvp[i].value = buffer;
ap_rputs(kvp[i].key,r);
ap_rputs(kvp[i].value,r);
i++;
}
return kvp;
}
I have copied the read post function from the apache website:
https://httpd.apache.org/docs/2.4/developer/modguide.html#snippets
I get the following error while trying to compile the module:
mod_example.c:82:9: error: use of undeclared identifier
'ap_form_pair_t'
ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);
apxs does not recognize ap_form_pair_t. Am I missing any header file ?
Can you please help me resolve this ?
ap_form_pair_t comes with apache version 2.4, so I think you use a lower version.
This function writes all post data in a buffer, it may help you:
int util_read(request_rec *r, char **rbuf, size_t &length){
int rc;
length = 0;
if((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK){
return rc;
}
if(ap_should_client_block(r)){
char argsbuffer[HUGE_STRING_LEN];
int rsize, len_read, rpos=0;
length = r->remaining;
*rbuf = (char*)apr_pcalloc(r->pool, length + 1);
while((len_read = ap_get_client_block(r, argsbuffer, sizeof(argsbuffer))) > 0){
if((rpos + len_read) > length){
rsize = length - rpos;
} else {
rsize = len_read;
}
memcpy((char*)*rbuf + rpos, argsbuffer, rsize);
rpos += rsize;
}
}
return rc;
}

I can not understand how to use buffers in the audio resampling

What i have tryed:
Player.c
const int BITS=8;
int isPaused=0;
static mpg123_handle *mh;
static unsigned char *buffer;
static size_t buffer_size;
static size_t done;
char * resBuffer;
//from global stat!
int outfreq=22050; int infreq=44100;
int resetMp3(char * song)
{
int err;
int channels, encoding;
long rate;
/* Inizialize */
mpg123_init();
mh = mpg123_new(NULL, &err);
buffer_size = mpg123_outblock(mh);
buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));
resBuffer=(unsigned char*) malloc(buffer_size * sizeof(unsigned char) * (1.0+(outfreq/infreq)));
/* open the file and get the decoding format */
mpg123_open(mh,song);
mpg123_getformat(mh, &rate, &channels, &encoding);
/* set the output format and open the output device */
int bits=(mpg123_encsize(encoding) * BITS);
initAudioDev(bits,rate,channels);
//Add for resampling
inizializeResample(infreq,outfreq);
}
int playMp3(){
/* decode and play */
if (isPaused==0 && mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
{
//char * resBuffer=malloc(sizeof(unsigned char)*buffer_size/(outfreq/infreq));
resBuffer=&buffer[0];
resample(buffer,resBuffer,done);
writeAudio((char*)resBuffer,done);
} else {
if (isPaused==0) return 2;
}
return 0;
}
int freeMp3()
{
free(buffer);
mpg123_close(mh);
mpg123_delete(mh);
mpg123_exit();
freeAudioDev();
endResample();
}
void setMp3Status(int value)
{
if(value==0||value==1)
{
isPaused=value;
} else {
isPaused=!isPaused;
}
}
Resampling.c
#include <assert.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include "libavcodec/avcodec.h"
#define LENGTH_MS 1000 // how many milliseconds of speech to store
#define RATE 44100 // the sampling rate (input)
struct AVResampleContext* audio_cntx = 0;
int samples_consumed;
void inizializeResample(int inRate, int outRate)
{
//char out_buffer[ sizeof( in_buffer ) * 4];
audio_cntx = av_resample_init( outRate, //out rate
inRate, //in rate
4, //filter length 16
3, //phase count 10
0, //linear FIR filter
0.6 ); //cutoff frequency
assert( audio_cntx && "Failed to create resampling context!");
}
void resample(char dataIn[],char dataOut[],int nsamples)
{
int samples_output = av_resample( audio_cntx, //resample context
(short*)dataOut, //buffout
(short*)dataIn, //buffin
&samples_consumed, //&consumed
nsamples, //nb_samples
(sizeof(dataOut)*nsamples),//lenout sizeof(out_buffer)/2
0);//is_last
assert( samples_output > 0 && "Error calling av_resample()!" );
}
void endResample()
{
av_resample_close( audio_cntx );
}
Now, when I execute this code, the audio isn't clear and resampling isn't done, problably because have wrong something with pointers and buffers.I review this code hundreds of time without find the error.
Can help me to find it and explain right how i should correctly size the buffers?
Now i think another problem is that i can't define what mpg123_read give me, if you explain this i love you.
Library used:Libmpg123 - The decoder function: mpg123_read
Resampling Library: FFMpeg AVCodec Library.
Ps: yes problaby a noob question, but after many attempts, StackOverflow is the only thing that can help me, THANK YOU

Search for a file in $PATH on Linux in C

I would like to test whether GNUPlot is installed on the system on which my program is running.
For that, I figured I'll test for the existence of the gnuplot executable in the user's install locations through stat() call.
However, I don't know how to read the $PATH environment variable in C so I can test for the existence of the file in those locations.
Use the getenv() function.
char *paths = getenv("PATH");
To loop through the parts of the column-separated list of paths, use strchr():
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *dup = strdup(getenv("PATH"));
char *s = dup;
char *p = NULL;
do {
p = strchr(s, ':');
if (p != NULL) {
p[0] = 0;
}
printf("Path in $PATH: %s\n", s);
s = p + 1;
} while (p != NULL);
free(dup);
Use getenv() to inspect the value of a particular environment variable.
To read the PATH environment variable, use getenv("PATH").
However, if you just want to run gnuplot if it's available, and perform some fallback action if it's not, then you should just try to run it (e.g. with fork and execvp or posix_spawnp) and handle the failure case.
Let which do the work for you
if (system("which gnuplot"))
/* not installed or not in path or not executable or some other error */
If you need the full path for some reason, run which with popen.
Or run gnuplot with some flag which makes it return immediately with 0 */
if (system("gnuplot --version"))
/* not installed ... */
I had a similar need and resolved it by copying libc execvp code source. I did in the most cross platform I could think of(I have no guatanty and tested just on linux). If it's not such a matter to you and you care about performances, you should use acess or _acess. Note that there is no error check whatsoever and it will just return NULL or a founded openable file in path.
The accepted answer is sometime not acceptable, when you are willing to run the same small binary over and over, redoing the path search every time by calling execvp can be non negligable overhead.
So here is the code and associated tests, you will be mainely interested in the search_in_path_openable_file function.
.h file:
bool is_openable_file(char* path);
/*Return true if path is a readable file. You can call perror if return false to check what happened*/
char* search_in_path_openable_file(char* file_name);
/*Search into PATH env variable a file_name and return the full path of the first that is openable, NULL if not in path*/
char* search_executable(char* file_name);
/*Search file, if not openable and not absolute path(contain /), look for opennable file in the path. If nothing is openable, return NULL. If something is openable, return it as it is (not guaratented to have a full path, but garatanted to be openable)*/
.c file:
#include "file_info.h"
#include <stdio.h>
#include <string.h> //strcpy
/*I wanted to do a really cross platform way. access or _acess may be better*/
bool is_openable_file(char *path) {
FILE *fp = fopen(path, "r");
if (fp) {
// exists
fclose(fp);
return true;
}
return false;
}
bool is_openable_file_until(char *path_begin, size_t until) {
char old = path_begin[until];
path_begin[until] = 0;
bool res = is_openable_file(path_begin);
path_begin[until] = old;
return res;
}
/*You may thinks that libc would have done this function and use it to implement execp function family, but you would be wrong. They just hardcoded the search in every execp function. Unbelievable.
*
* So this function is a modification of their execvp function.
*
* */
char* search_in_path_openable_file(char* file){
char *path = getenv("PATH");
if (path == NULL)
return NULL;
size_t pathlen = strlen(path);
size_t len = strlen(file) + 1;
int total_max_size=pathlen + len;
char* buf=malloc(sizeof(char)*total_max_size);
if (*file == '\0') {
return NULL;
}
char *name, *p;
/* Copy the file name at the top. */
name = memcpy(buf + pathlen + 1, file, len);
/* And add the slash. */
*--name = '/';
p = path;
do {
char *startp;
path = p;
//Let's avoid this GNU extension.
//p = strchrnul (path, ':');
p = strchr(path, ':');
if (!p)
p = strchr(path, '\0');
if (p == path)
/* Two adjacent colons, or a colon at the beginning or the end
of `PATH' means to search the current directory. */
startp = name + 1;
else
startp = memcpy(name - (p - path), path, p - path);
/* Try to execute this name. If it works, execv will not return. */
if (is_openable_file(startp))
return startp;
} while (*p++ != '\0');
/* We tried every element and none of them worked. */
return NULL;
}
char* search_executable(char* file_name){
if (is_openable_file(file_name)){//See realpath manual bug. Watch out
return file_name;
}
if (strchr (file_name, '/') != NULL) //Don't search when it contains a slash.
return NULL;
return search_in_path_openable_file(file_name);
}
tests (As you see I did not test a lot this function, there may exist some problem, use at your risk):
#include "file_info.h"
#include "munit.h"
#include <stdbool.h>
#include <unistd.h>
static void generate_search_executable(char* test_str, char* expected){
char* res= search_executable(test_str);
if (res==NULL)
munit_assert_ptr(expected,==,NULL );
else
munit_assert_string_equal(expected,res);
}
static void generate_openable(char* test_str, bool expected){
bool res= is_openable_file(test_str);
munit_assert_true(expected==res);
}
static void generate_path_search(char* test_str, char* expected_res){
char* res= search_in_path_openable_file(test_str);
if (res==NULL)
munit_assert_ptr(expected_res,==,NULL );
else
munit_assert_string_equal(expected_res,res);
}
//TODO do for other platform, better test would also set path to a custom folder that we control
#define EXISTING_FILE_NOT_IN_PATH "/usr/include/stdlib.h"
#define EXISTING_FILE_IN_PATH "ls"
#define EXISTING_FILE_IN_PATH_FULL "/bin/ls"
#define NOT_EXISTING_FILE "/usrarfzsvdvwxv/ixvxwvnxcvcelgude/ssdvtdbool.h"
int main() {
generate_openable(EXISTING_FILE_IN_PATH, false);
generate_openable(EXISTING_FILE_NOT_IN_PATH, true);
generate_openable(NOT_EXISTING_FILE, false);
generate_path_search(EXISTING_FILE_IN_PATH, EXISTING_FILE_IN_PATH_FULL);
generate_path_search(NOT_EXISTING_FILE, NULL);
generate_path_search(EXISTING_FILE_NOT_IN_PATH, NULL);
generate_search_executable(EXISTING_FILE_IN_PATH, EXISTING_FILE_IN_PATH_FULL);
generate_search_executable(NOT_EXISTING_FILE, NULL);
generate_search_executable(EXISTING_FILE_NOT_IN_PATH, EXISTING_FILE_NOT_IN_PATH);
generate_search_executable("", NULL );
//test current folder existence(maybe it just depend on path containing .,I am not sure, in that case we should remove thoses tests
generate_search_executable("file_info_test", "file_info_test" );
}
To build on one of the previous answers, you can use getenv to get the contents of PATH and then iterate over its components. Instead of using strchr you can use strsep:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
bool exists(const char fname[])
{
return access(fname, F_OK | X_OK) != -1;
}
bool find_in_path(const char name[], char *fullpath, size_t sz) {
char *paths = strdup(getenv("PATH"));
char *tmp = paths; // to use in free
const char *item;
bool found = false;
while ((item = strsep(&paths, ":")) != NULL) {
snprintf(fullpath, sz, "%s/%s", item, name);
if (exists(fullpath)) {
found = true;
break;
}
}
free(tmp);
return found;
}
int main() {
char fullpath[512];
bool found = find_in_path("uname", fullpath, sizeof(fullpath));
if (found) {
printf("found: %s\n", fullpath);
}
return 0;
}
Using C++17 to get a vector of path elements.
% a.out ls
/bin/ls
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
using namespace std;
vector<string> get_paths (string str)
{
vector<string> result;
while(!str.empty())
{
if (auto pos { str.find_first_of (':') }; pos == string::npos)
{
result.push_back(str);
break;
}
else
{
result.emplace_back(str.substr(0, pos));
str.erase(0, pos + 1);
}
}
return move(result);
}
bool exist(const string& fname, int perm=F_OK) { return access(fname.c_str(), perm) == 0; }
int main (int argc, char *argv[])
{
auto result { get_paths(getenv("PATH")) };
for (auto pp : result)
{
string npath { pp };
if (*npath.rbegin() != '/')
npath += '/';
npath += argv[1];
if (exist(npath))
cout << npath << endl;
}
return 0;
}

Resources