jim mcnamara 19 Junior Poster

UNIX only:

getenv("TZ");

will return the timezone setting. The result is in the form
xxxNxxx, example MST7MDT. TZ has to be set for localtime() to return the correct time. MST is GMT +7. If TZ is not set the system is running GMT by default. Assuming time has been set.....
NY is EST5EDT. It's GMT+5.

SpS commented: Ah ~~ SunnyPalSingh +3
jim mcnamara 19 Junior Poster

What normally is done (assuming that only one of the code pieces has a main() ) is to compile each of the sourcefiles into a separate object file and then link them all into one program.

To simplify a compile you can create a makefile to do this, or create a project in IDE's like Visual Studio.

An example using g++

g++  file1.cpp file2.cpp file3.cpp -o myprogram
~s.o.s~ commented: Good help - ~s.o.s~ +8
jim mcnamara 19 Junior Poster

This is allowable C, but can cause you problems if you are not super careful - meaning: it will cause problems. But it does answer your question.

#include <string.h>
int myfunc(const char *a, const char *b, const int i)
{
   ............. stuff
   return 1;
}

void foo(char *a, char *b, int c)
{
  typedef int (*Fx)();
  Fx fx[2]={strcmp,myfunc};
  int result=0;

  if (c==1)  
     result=fx[0](a,b);
  else
     result=fx[1](a,b,c);
  if(result==0) 
     printf("okay\n");
}
jim mcnamara 19 Junior Poster

Wow.

void main()  - don't use void main. main() returns an int.
{
  clrscr();
  while(i--!=6)      - where is i set to some intitial value?
      i=i+2;           - this is the end of the while loop 
                              I think you are missing {} to make a while block?
   printf("%d\n",i);
   getch();
}

You do realize that your while loop subtracts then adds to the same variable, essentially in the same statement. Eeeek...

Back up.

What are you trying to do?

andor commented: andor agrees +4
jim mcnamara 19 Junior Poster

HPUX supports alloca() -
it has advantages and disadvantages

good - it automatically frees all objects created by alloca calls when
         the function making those calls exits

good - it is about 100X faster than malloc

bad - it allocates from stack, not heap.  stack space size limits
       are nowhere near a generous
bad - it is REALLY easy to reference a pointer to garbage 
       or trash the  stack of another function

Example mediocre code:

#include <stdlib.h>
#include <time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>     
#include <alloca.h>
#define MAX 100000
void foo(void)
{
	int i=0;
	char *p[MAX]={NULL};
	for ( i = 0 ; i < MAX ; i++ )
		p[i] = malloc( rand()%456 );
	for ( i = MAX ; i>0 ; i-- )free( p[i] );

}
int foo1(void)
{
	int i=0;
	char *p[MAX]={NULL};
	for ( i = 0 ; i < MAX ; i++ )
		p[i] = alloca( rand()%456 );	
}

void badfoo(void)
{
	int i=0;
	char *p[MAX]={NULL};
	for ( i = 0 ; i < MAX ; i++ )
		p[i] = malloc( rand()%456 );
}

void process(struct rusage *p, char *when)
{
	printf("%s\n", when);
	printf(" /* user time used */                   %8d  %8d\n",  p->ru_utime.tv_sec,p->ru_utime.tv_usec   );
	printf(" /* system time used */                 %8d  %8d\n",  p->ru_stime.tv_sec,p->ru_stime.tv_usec   );
	printf(" /* integral shared memory size */      %8d\n",  p->ru_ixrss           );
	printf(" /* integral unshared data  */          %8d\n",  p->ru_idrss           );
	printf(" /* integral unshared stack  */         %8d\n",  p->ru_isrss           );

}


int main()
{
	  int ret=0;
	  int i=0;
	  int who= RUSAGE_SELF; …
jim mcnamara 19 Junior Poster

I'd use arrays:

#!/bin/ksh
optfile=filename
set -A opts  \
$(awk 'BEGIN {FS="="} { if($0~ / page_size/) { prinf("%s  ", $2}}' $optfile; echo"")
for i in 0 1 2
do
    echo ${opts[i]}
done

If you're using bash just "declare" opts as an array.

I changed it so awk puts it all on the same line.

jim mcnamara 19 Junior Poster

I'd use arrays:

#!/bin/ksh
optfile=filename
set -A opts $(awk 'BEGIN {FS="="} { if($0~ / page_size/) { print $2}}' $optfile)
for i in 0 1 2
do
    echo ${opts[i]}
done

If you're using bash just "declare" opts as an array.

WolfPack commented: Thanks. +3
jim mcnamara 19 Junior Poster
#!/bin/bash
# we want to copy from path1 to path2 
# step 1 make all the subdirectories
find /path1 -type d | \
while read dir 
do
    mkdir /path2"${dir#/path1}"
done
# step 2 cp the files
find /path1 -type f | \
while read $file
do
     cp "$file" /path2"${file#/path1}"
done
jim mcnamara 19 Junior Poster

In that case you have to use cp. mv does not mv over mount points.

jim mcnamara 19 Junior Poster
cat /proc/meminfo

shows the current state of memory. The format varies somewhat with Linux distributions. My version of Linux has what you want in the 4 th column on the second line. So, you will need to get the awk statement working for you. Then put it in the C code.
You could also just read /proc/meminfo (read only) as a regular file and find the value using C only.
snippet:

#include <stdio.h>
void foo(void)
{
   char *cmd="awk '{ if(NR==2){print $4}}' /proc/meminfo";
   FILE *cmdfile=popen(cmd,"r");
   char result[256]={0x0};
   while(fgets(result,sizeof(result),cmdfile)!=NULL) 
   {
       printf("%s\n",result);
   }
   pclose(cmdfile);
}
jim mcnamara 19 Junior Poster

Are you talking about using NFA regexp in a DFA environment?
Start with a DFA tool is the simple answer. There really is no code to convert from one to another, because it doesn't make much sense to do so. C and C++ have regexp libraries available for them. Pick one. Windows provides the .NET version.

You do realize that which tool you use dictates which type of regex processing occurs? To get a list of which tools have what regex flavor (including POSIX) try:

J Friedl 'Mastering Regular Expressions' 2nd ed 2004

Then if you want, download source for one of them.

jim mcnamara 19 Junior Poster

You are not calling strtok correctly.

try something like this:

void foo(char *string1, char *string2)
{
    char *first=strtok(string1,"|");
    char *second=string2(string2,"|");
    while(first!=NULL)
    {
        printf("%s\n",first);
        first=strtok(NULL,"|");
    }
    while(second!=NULL)
    {
        printf("%s\n",second);
        second=strtok(NULL,"|");    
    }
}
jim mcnamara 19 Junior Poster

Your logic doesn't make any sense to me...

You want to get to the "got it" statement - this does it.

#!bin/ksh
a=5
while [[ $a -gt 5 ]]
do
	echo "a=5 and not went to else cdn."

	if [[ $a==5 ]]
	then
		echo "a=6" 
	fi
	echo "a=5 and not worked" 
done
echo "got it"
jim mcnamara 19 Junior Poster

Here is a start - you need to read up on shell scripting

#!/bin/ksh

# function gen_data
# record performance data, place in a file with each node's
#    information all on one line
gen_data()
{
    for i in 54 72 114 122 123 139 73
    do 
        ssh 10.42.1.$i "hostname && uptime"  
    done | \
    awk ' BEGIN {cnt=0}
        { printf("%s, ", $0)
          cnt++
          if(cnt % 3 == 0) {printf("\n") }
        }' > ./perf_data
}

#function check_data
# check for load averages (fields 6,7,8) which are greater than 7

check_data()
{
	awk -F"," '{ if($6 > 7.0 || $7 > 7.0 || $8 > 7.0) 
	                 {print $0 } 
	           }' ./perf_data > ./email.dat
	           
# do we have any warnings to email ?	           

	if [ -s email.dat ]; then
	   cat ./email.dat | \
	       usr/bin/mailx -s 'Performance warning' you@someplace.com
	fi
}

# main control loop
# run the two functions every ten minutes  60 seconds * 10 = 600

sleepy_time=600

# loop forever .....
while true
do
	gen_data
	check_data
	cat ./perf_data >> ./perf_data.log
	sleep $sleepy_time
done
evlmcgyver commented: Excellent! Clean, neat, and tidy!! +0
jim mcnamara 19 Junior Poster

chris - you're correct. Perl is overkill which I guess was my point to start with.
3 lines of awk:

awk ' BEGIN {connected=-1}
     $2 ~ /^connected/  {connected*=-1 ; continue}
     connected<0 {print $0 } ' filename

on this file:

PIN connected<112>
USE TEST ;
PORT
LAYER TRUE ;
END
END connected<112>

PIN address<110>
USE TEST ;
PORT
LAYER TRUE ;
END
END address<110>

PIN connected<11>
USE TEST ;
PORT
LAYER TRUE ;
END
END connected<11>

produce this output:

kcsdev:/home/jmcnama> t.awk

PIN address<110>
USE TEST ;
PORT
LAYER TRUE ;
END
END address<110>
voidyman commented: brilliant! +1