jim mcnamara 19 Junior Poster

The sleep command in unix is guaranteed to sleep a minimum.
Due to the way quanta (time slices) work in OS schedulers, other processes may get the cpu ahead of the sleeper who just awoke.

ie., there is no guarantee of an upper limit. sleep 1 may last longer than 1 second.

Linux sleep calls nanosleep - read the man page for that.

jim mcnamara 19 Junior Poster
char str[10]={0x0};
char *p=str;
strcpy(str,"string");
*(p+2)='a';

You have to allocate storage in memory for data - first. Then place data in the storage next - if you want to be able to modify the data.

jim mcnamara 19 Junior Poster

FWIW - Oracle is as good as it gets on sorting. If the fields being sorted are indexed, you get maximum speed.

We sort 200,000,000 records in 10-12 minutes - but that doesn't mean anything - performance is relative to the database schema and the available hardware.

Sybase and DB2 are just as well-desgined.

jim mcnamara 19 Junior Poster

I don't have a quick good answer, getopts only returns the first string of multiple arguments into OPTARG for a given option

Here is a good discussion of argument handling:

http://www.shelldorado.com/goodcoding/cmdargs.html

jim mcnamara 19 Junior Poster

Ten lines of shell script is easier to maintain than 70 lines of perl.
It's also faster to develop.

Experienced sysadmins use shell scripts, then use perl/ruby/python when they cannot do whatever it is in shell coveniently or very well.

In practice what usually happens is that new sysadmins learn one thing really well and use it for everything. eg, perl or ksh. This practice is usually frowned upon in big companies where creating hard to maintain code is frowned upon. Hard to maintain code is what you often get when you push a facility to the very edge of it's capbilities - usually because you don't any other way to do it.

jim mcnamara 19 Junior Poster

cksum is a fast way to see if two files are the same.

However, ftp-ing folders of files over just to compare files is a waste of bandwidth and time. What exactly are you trying to do - not how, but what do you need to acheive?

jim mcnamara 19 Junior Poster

Interpreted?

write() calls are deferred to some future time by the kernel. However, any read() to that file after a write is guaranteed to be able to read the data written. This means that if the kernel panics just when a deferred write() call retruns, the data will be lost - i.e., not physically written to disk.

If a write cannot complete, it returns -1, and sets errno. If errno is set to EINTR, then the write can be re-tried. Most other errors indicate some kind of fatal condition. Always check the return code from a write.

If you need to guarantee that data is physically written to disk, there are the aio calls. Depending on your kernel release, these calls may not be implemented fully. With 2.6 I bleive they are now fully POSIX compliant. Try info aio or man aio_write

jim mcnamara 19 Junior Poster

Your script is tough to read - for me at least.

I don't see basic things like exec, closing stdin, stdout, etc.

Try reading this see if it makes sense in the context of your shell script.
In general, it is possible to have a script "daemonized", but you have to do some things to make it work, so you need to use a robust shell like bash or ksh....

OTH, almost all daemons are written in low-languages like C, to my knowledge.

http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

jim mcnamara 19 Junior Poster

Sounds like a class asignment.
try a here document

oldpw="$1"
newpw="$2"
passwd <<EOF
"$1"
"$2"
"$2"
EOF
jim mcnamara 19 Junior Poster

Note: files appear in a directory and can be ftp'ed even though another process is still writing to them.

jim mcnamara 19 Junior Poster
echo $LOCATION | awk -F"_" '{print $2}'
jim mcnamara 19 Junior Poster

Can I make a suggestion before you spend six months learning this on your own?
It may save your spouse, children, and pets endless hours of anxiety watching you bash your head into a doorframe. :)

Richard Stevens book 'Advanced Programming in the UNIX Environment' spends about 90 pages on all aspects of threads, including 5 pages on threads and signals, which can drive you insane all by itself.

Consider reading it. There are dozens of gotchas in thread programming.

jim mcnamara 19 Junior Poster
SQL> col brackets format a7 trunc
SQL> select '[     ]' brackets,
                my_field1
           from mytable;

BRACKET MY_FIELD1
-------   ---------------
[     ]      JOHN SMITH
jim mcnamara 19 Junior Poster

Hardcoded passwords are a security risk - FWIW.

if you are root you can use sudo or su by default. Otherwise use a here document:

su username <<!
password
/bin/ksh
command 
command 
command
exit
!

su may also give you the wrong shell, which is why I put "/bin/ksh" in there.

jim mcnamara 19 Junior Poster

You are optimizaing this loop because you ran a profiler and too much time is being spent there, right?

Otherwise DON'T optimize.

jim mcnamara 19 Junior Poster

UNIX does - and you can create files with "holes" in them - literally.
In other words, a block (say 512 bytes) then a hole which uses ~twenty bytes but actually is say ten blocks of ASCII nul, then another block of data.

When this file is created it reports a file size of 1044 bytes or something close. When the identical file is restored from backup it creates a file 5120 + 1024 bytes long. 5120 bytes longer than it started. Backup sees the hole and creates it in the file, as it is "supposed" to be. stat() always reports it's size correctly - before or after.

All of this is merely filesystem stuff -DOS or UNIX or FAT32 or NTFS nonsense. Not really C.

jim mcnamara 19 Junior Poster

FWIW -

opening an MS-DOS text file (FAT16) in binary mode doesn't necessarily work as described above. The reason is that the reported size of the file by the filesystem is not the same as the length of the data in the file - because text files in MS-DOS end with ASCII 26, but the filesystem actually has more space allocated to the file. Binary access reads past the ASCII 26 - unless you know ahead of time that CTRL/Z ends the file. Which is not a "universal" approach IMO. YMMV.

IMO the clear intent of file size is knowing how much data is the file, for this kind of question, not necessarily how many sectors are allocated but not used.

If memory serves, stat() on these systems - as in TURBO C - has the samrts to deal with this. Does anyone know definitively?

jim mcnamara 19 Junior Poster

IF you're on unix:

sort -t ',' k- 1,1 -o newfile.csv  oldfile.csv

will sort the file the way you want - outside C++.

jim mcnamara 19 Junior Poster

If you are in Unix:
setitimer() and getitimer() have microsecond resolution on POSIX-compliant systems.

Do you know about profiling and profilers?

jim mcnamara 19 Junior Poster

try

status = tibrvTransport_Create(transport, serviceStr, networkStr, daemonStr);

transport is already a pointer, so using *transport says to pass that value of the object pointed to, not the address. Pointers are addresses.

It also seems you are not prototyping functions. Or maybe not including header files.

There should be separate prototypes for each of the functions so that compiler won't issue a warning, instead it will issue a fatal error if the datatype of an argument is wrong.

I made up this prototype:
void tibrvTransport_Create(tibrvTransport *, char *, char *, char *);

It's possible *transport was actually correct, but the compiler does not seem to know.

jim mcnamara 19 Junior Poster
#!/bin/ksh
if [ $# -eq 0 ]; then
        echo  "$# is zero "
else
        echo "$# is greater than zero"
fi
jim mcnamara 19 Junior Poster

If your commands produce output consider using popen() which works like somewhat system() but allows one-way communication between the shell and the C program.

system() does'nt do that.

man popen

jim mcnamara 19 Junior Poster

I'm sure it was homework. S/He should show what effort s/he made so far.

jim mcnamara 19 Junior Poster

And. You have to source the script. It may or may not run in a subshell (another process) with

[b].[/b] myfscript.sh

note the leading dot - in linux there is the source command in bash which does the same thing.

jim mcnamara 19 Junior Poster

It means that the dirlog variable contains a string literal value. The value is randomdir.log

The other two statements are attempting to:
1. to append (add-on to the end of) a file
2. read from a file


On failure they print messages and then exit.

jim mcnamara 19 Junior Poster

You need to give us exactly what you are doing. Your expected input, expected output.

The code as written will trash every file it works on except that last one, ie. dozens of files may become newname. How do you want the new name to be?

jim mcnamara 19 Junior Poster

If the box in question a unix box, try setting up a signal handler - with use sigtrap

alarm() is supposed to send a SIGALRM to the process, which by default terminates.
Unless another handler (trap statement) is active

This does not work in Windows.

jim mcnamara 19 Junior Poster

>> means append to an existing file, do not overwrite.

I think it may solve your ip issue as well. What I'm saying: your expect script looks okay, your problem seems to be how you are invoking it from the command line.

jim mcnamara 19 Junior Poster

Dave -

I actually made the mistake of giving him working Pro*C code - another forum. He did not seem to get that it worked. But it wasn't exactly what he specified, because his requirements are non-sequitur in a production world.

It's probably a weird homework assignment.

jim mcnamara 19 Junior Poster

Let's start here:

expect firewall.exp > javier-$(date +%d%b%Y).txt

should be:

expect firewall.exp >> javier-$(date +%d%b%Y).txt

I'm not clear about the ip thing at the moment, this part should work for you.

jim mcnamara 19 Junior Poster

\\ in literal strings = \ because a single \ "escapes" the next character

"C:\\myfile\\hi.txt"
jim mcnamara 19 Junior Poster

You have to understand what globbing is. the * character is automatically globbed (turned into matching file names) when the shell sees it. This has to do with shell settings WHEN you enter the command

test.sh *

In order to do what you want you will have to turn off globbing.

Read the info file on bash to turn off/on globbing. Normally you would put this command in .bash_profile for bash. BTW- It will mess up every other script you have already written. That's why we use the '*' trick. Or write a wrapper.

The alternative is to write a wrapper script that does these things:
turn off globbing
ask for the parameter(s)
execute test.sh {paramters]
exit

test.sh will have to turn globbing back on when run like this


In ksh you put

set -o noglob

in /etc/profile or execute it from the command line to turn off globbing

set +0 noglob

truns globbing back on.

jim mcnamara 19 Junior Poster
#!/bin/ksh
ls /home/FTPXFER/$1

finds files in the /home/FTPXFER path. The example below was run with the current working directory set to /home/jmcnama

usage:

test.sh '*'

output:

/home/FTPXFER/UX1006                 /home/FTPXFER/curocfix               /home/FTPXFER/premd_0404
/home/FTPXFER/UX1617                 /home/FTPXFER/curocsbp               /home/FTPXFER/premd_04_11
/home/FTPXFER/UXDEV3a                /home/FTPXFER/curocsfix              /home/FTPXFER/premd_04_12
/home/FTPXFER/UXURR                  /home/FTPXFER/custd_04_12            /home/FTPXFER/premd_04_19
/home/FTPXFER/UZBBMSG.DAT            /home/FTPXFER/custd_04_19            /home/FTPXFER/premd_04_22
/home/FTPXFER/UZBMSGT.DAT            /home/FTPXFER/custd_04_22            /home/FTPXFER/premd_04_25
/home/FTPXFER/UZBSRAT.DAT            /home/FTPXFER/custd_04_25            /home/FTPXFER/premd_0525
/home/FTPXFER/UZBSTUF.DAT            /home/FTPXFER/custd_05_02            /home/FTPXFER/premd_05_02
/home/FTPXFER/UZBVRAT.DAT            /home/FTPXFER/custd_bad_04_12        /home/FTPXFER/premd_05_03
/home/FTPXFER/UZBWDIS.DAT            /home/FTPXFER/custd_gasall           /home/FTPXFER/premd_0928
/home/FTPXFER/UZPNCOA.LIS            /home/FTPXFER/custd_gasall_old       /home/FTPXFER/premd_bad_04_11
/home/FTPXFER/UZWIND
jim mcnamara 19 Junior Poster

www.physicsforums.com

go to the homework forums - there is a calc section for textbook and homework problems.

jim mcnamara 19 Junior Poster

This is a simple-minded C version. You can use strtok in C++ as well.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXFLDS 200     /* maximum possible number of fields */
#define MAXFLDSIZE 32   /* longest possible field + 1 = 31 byte field */

void parse( char *record, char *delim, char arr[][MAXFLDSIZE],int *fldcnt)
{
    char*p=strtok(record,delim);
    int fld=0;
    
    while(*p)
    {
        strcpy(arr[fld],p);
		fld++;
		p=strtok('\0',delim);
	}		
	*fldcnt=fld;
}

int main(int argc, char *argv[])
{
	char tmp[1024]={0x0};
	int fldcnt=0;
	char arr[MAXFLDS][MAXFLDSIZE]={0x0};
	int recordcnt=0;	
	FILE *in=fopen(argv[1],"r");         /* open file on command line */
	
	if(in==NULL)
	{
		perror("File open error");
		exit(EXIT_FAILURE);
	}
	while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */
	{
	    int i=0;
	    recordcnt++;
		printf("Record number: %d\n",recordcnt);
		parse(tmp,",",arr,&fldcnt);    /* whack record into fields */
		for(i=0;i<fldcnt;i++)
		{                              /* print each field */
			printf("\tField number: %3d==%s\n",i,arr[i]);
		}
	}
    fclose(in);
    return 0;	
}
jim mcnamara 19 Junior Poster

This looks like MS code.

If that's the case try www.msdn.microsoft.com


OpenFileId is a file handle created by open.
int is the size of the buffer you want to read
buf is the address of the buffer

Read returns (-1 or possibly zero) for a variety of reasons, including being pre-empted (interrupted by another higher priority process). I don't recall the value Windows sets errno to. POSIX standard is EINTR - but Windows doesn't follow any standards.

You MUST check errno if you get an EOF or a zero back from read to determine what to do.

jim mcnamara 19 Junior Poster

It's not clear to me what you want. But here is very basic code for structs.

struct Household
{
	int Noperson;
	char letters[20];
	double income;
};
/* find the maximum income */
struct Household *foo(struct Household *src, int cnt)
{
    struct Household *p=src;
    struct Household *retval=src;
    double max = -999999.
    int i=0;
    while(i<=cnt)
    {
         if(p->income > max)
         {
              max=p->income;
              retval=p;
         }
         cnt ++;
         p++;
    }
    return retval;
}

int main()
{
     struct Household arr[100]={0,NULL,0.}
     struct Household *p=arr;
     int cnt=0;
     int i=0;
     for (i=0;i<100; i++, p++)
     {
           p->Noperson=i;
           p->income= i*2.;
           strcpy(p-Letters,"Hi there");
           cnt++;
     }
     p=foo(arr,cnt);
     printf("Maximum income %f\n",p->income);
}
jim mcnamara 19 Junior Poster

We are now clearly doing homework.

This is the limit of help->
1. redirect the output of the awk script into a loop instead of into a file.

for word in `awk script above`
do

done

the part between do ... done is now yours.
read the man page for rev, and learn how to compare two strings in an if statement.
use one string for the original version of the $word variable, and reverse another.

jim mcnamara 19 Junior Poster

After a ver quick look, I think that you overwrote a pointer - having it point to a bogus address.

malloc works (It's called Doug Lea malloc which is the basis for many versions of malloc) kinda like this:

typedef
union
{
    ptrdiff_t malloc_size;
    void *ptr;
} malloc_t;

The malloc routine returns ptr to your program. When you call free later on, it assumes there is a value in the address immediately "underneath" the pointer. And that it is the same value you started with. Well, if ptr was changed, the value under it could be 0xfffffff, or literally anything else. When free tries to work with the block referenced by ptr, it bumps into memory your process does not own because:

1. ptr references memory you don't own or is read-only
2. malloc_size is the forcing free to reference memory you don't own or is read-only.

jim mcnamara 19 Junior Poster

Try some local contract work. Or take on a pro-Bono project for Salvation Army or something. Startup companies are far more likely to hire you as well.

The reason things are like this is because the companies who view IT more as an expense than a resource (the same ones more likely to hire you without experience because you cost less) have outsourced coding overseas. To save money. This decreased jobs markedly. It is starting to cause these same companies problems. Defective software takes forever to get fixed and they lose revenue. This is what you're competing against:

http://www.unix.com/showthread.php?p=92348#post92348

[see post #4] The companies over in India cost 1/6 of US costs, less benefits. Those same contracting companies exploit the market over there and claim they have expertise. This post shows the level of expertise they have. "Learn unix in 10 days". Talk about no experience.

jim mcnamara 19 Junior Poster

try awk. This is a start:

awk '{ 
       for(i=1;i<=NF;i++) 
       {
         if(length($i)<5) 
         {print $i}
       }
       }'  filename  > newfilename
jim mcnamara 19 Junior Poster

Your specifications are kinda fuzzy - I used PL/SQL to get what I thought you wanted - one weeks worth of data. The &1 thing is for interactive input.
If you saved this as "someday.sql" then usage in sqlplus would be

@someday 01-NOV-2005
SET SERVEROUT ON SIZE 1000000
spool t.lis
DECLARE
    mydate DATE:='&1';
    endit NUMBER(2):=1;


cursor getbydate is
    select count(*) cnt , TRUNC(datefld) fld 
    from mytable
    where
        datefld>=mydate and datefld<=mydate+6
    group by TRUNC(datefld);
    

BEGIN
  dbms_output.enable(1000000);
  for x in getbydate
  loop
    dbms_output.put_line(
         to_char(x.cnt,'999999') || '  ' || 
         rpad(TO_CHAR(x.fld,'DAY'),10) || ' ' || x.fld );
  end loop;
END;
/
jim mcnamara 19 Junior Poster

This is a QA kind of question.

You should feed your alogorithm data which has:

1. values outside given bounds, like what the sqrt() runtime function does:
It is able able to return NaN or +INFINITY and sets errno=EDOM (unix only).

2. validate output for known value sets, with extremely large/small or postivie/negative values

3. Deliberately try to break the code - if it expects numeric data feed it text instead of numbers, for example. In all cases it should simply return an error, not crash.
It should NOT exit. Whether or not an exit is called depends on the rules of operation of the program, not on an individual function.


Consider reading 'The Pragmatic Programmer' - it explains how functions (algorithms) are supposed to behave when users are trying to feed them data.

jim mcnamara 19 Junior Poster

Chris is right - the answer is totally language dependent. In general there no functions like that in most high level languages, some databases provide that capability, as do a few specialy languages.

Normally you write a small function/procedure to do it.

jim mcnamara 19 Junior Poster

So, how did you compile and link the .so file?

jim mcnamara 19 Junior Poster

If you're an advanced CS student asking this question, find another final project.
You're going to have trouble.

Otherwise, consider looking at what other people have done before. First off, you should have read at least one advanced Unix programming book - like Richard Steven's book. I know you haven't read Marc Rochkind's 'Advanced Unix Programming' because he develops a shell in Chapter 6.

The example code for his shell is here:
http://www.basepath.com/aup/ex/group__AUP2ex.html#Chap.%206

Look for "real shell" ... c6/sh3.c Note: if your prof has any ability at all he will spot a a plagiarized copy of this code, so you need to work up all new code.

jim mcnamara 19 Junior Poster

My bad.

Just to clarify:
1. You can link an entire module statically.
2. You can link just one or two libraries statically, and link the rest of the image dynamically:

cc myfile.c /path/to/mylib.a /path/to/anotherlib.a -o myfile
jim mcnamara 19 Junior Poster

No, no, not flags...

I'm assuming you're using C++ code - look for something like "libc++.a" and link against that. I don't know the name for the library file in Solaris.

jim mcnamara 19 Junior Poster

The "moving over" part is the reason for which static linking exists.

Your problem could be looked at another way: as an implementation/deployment issue.
Matching up correct code with correct run-times. If you are working for a company ask them to look at TeamTrack and ChangeMan from Serena. You can also package everything as one tarball and use cvs or whatever to manage your tarballs.

jim mcnamara 19 Junior Poster

You're on the right track. I don't use Solaris, but I do know it has some (what I think are) funny conventions.

Now that I know it's Solaris, you can use versioning of your .so files, as long as you keep all the old ones out there. You can park all you stuff in a special area that won't get clobbered in an update, then define LD_LIBRARY_PATH (or SHRLIB_PATH) so that dld can open the libraries -if you plan to move them around later. Link specifically against a version like "-l mylib.3.2"