Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
POKEMAN SAM
Jul 8, 2004

ShoulderDaemon posted:

Untested, but this should work. I don't know if there's a shorter way; this is just the first solution that I thought of.

code:
#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

bool same_file( FILE *a, FILE *b ) {

  int a_fd = fileno( a );
  int b_fd = fileno( b );

  if (a_fd == b_fd) return true;

  struct stat a_stat;
  struct stat b_stat;

  if (fstat( a_fd, &a_stat )) return false;
  if (fstat( b_fd, &b_stat )) return false;

  return (a_stat.st_dev == b_stat.st_dev) && (a_stat.st_ino == b_stat.st_ino);

}

Good call with the st_dev and st_ino. I'm already using the stat struct on one of the files, so I can keep the stat struct info around for the other one (that is static during program execution.)

Thanks.

Adbot
ADBOT LOVES YOU

ZorbaTHut
May 5, 2005

wake me when the world is saved
Time for a horrifying Lua question! :dance:

code:
  for name, origin in pairs(all_input_files) do
    counter = counter + 1
    print("Compiling " .. counter .. "/" .. total_count .. ": ", origin)
    --CompileInputFile(name)
    assert(os.execute("true") == 0)
    if not pcall(CompileInputFile, name, pairfrequencies) then
      print("!!!!! FAILURE")
    end
    assert(os.execute("true") == 0)
  end
The above snatch of code is a small part of a much larger program. When I run it, it occasionally errors on one of the two os.execute lines. This invariably happens a few thousand iterations in, but not at any predictable time. Note that sometimes it happens on the first os.execute line, but after several thousand iterations of the loop - the only stuff of note between that and the "previous" one is a print and a pairs(). There's no wacky metatables involved and I haven't modified "pairs" or "print".

Without those asserts, it fails later, in some os.execute() lines that actually matter. I'm trying to track down what causes os.execute() to eventually simply stop working. What could it be? I do not know!

Unfortunately, duplicating it involves a few thousand lines of code, five gigabytes of data files, and running the program for several hours. Partially due to its unpredictability I haven't been able to isolate it in any useful fashion.

I've gotten it to fail immediately after an os.close() call with moderate reliability. I don't know if that helps in any way, since obviously there isn't one in the critical segment of this example. The return value is invariably -1 during this failure mode. I don't know if that helps either. Once it fails once, it continues failing forever in the same manner until Lua exits (this means I can't just retry until it works) and, obviously, I don't know why. Increasing the number of open files in ulimit doesn't help (I've increased it by a factor of 100 without any change). I'm running on an updated Ubuntu 64, Lua 5.1.4.

Help? Does anyone at least have ideas on how to track it down? Remember that it takes hours to duplicate :(

ZorbaTHut fucked around with this message at 04:56 on Oct 28, 2008

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

ZorbaTHut posted:

Time for a horrifying Lua question! :dance:

You could be leaking zombie processes, which would explain why trivial fork-and-exec tasks are failing and also why the problem immediately goes away when you kill the parent lua process.

ZorbaTHut
May 5, 2005

wake me when the world is saved

ShoulderDaemon posted:

You could be leaking zombie processes, which would explain why trivial fork-and-exec tasks are failing and also why the problem immediately goes away when you kill the parent lua process.

I came up with another thing to test, including this. First, "ps -A | wc -l" doesn't show any process leakage, and neither does top. So, no zombie leakage.

That said, on a hunch I added a little instrumentation to see how long os.execute took. At the beginning of my program, it was 0.04 seconds. It's up to 0.19 and rising. So . . . there's some leak going on.

I'm pretty sure I'm looking at digging into the Lua source.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
I have been getting frustrated with the sort times in tables for Eclipse's SWT GUI interface. I found some code that claims nlog(n) complexity, and it still takes about 2 seconds to do 1,000 elements. Doesn't that seem like a long time? I'm wondering if the sorting algorithm is the weak point in SWT or if some of the SWT stuff gets in the way.

Hmm does anybody even use SWT? Maybe I should run off to the Eclipse forums for this one.

csammis
Aug 26, 2003

Mental Institution

Rocko Bonaparte posted:

I have been getting frustrated with the sort times in tables for Eclipse's SWT GUI interface. I found some code that claims nlog(n) complexity, and it still takes about 2 seconds to do 1,000 elements. Doesn't that seem like a long time? I'm wondering if the sorting algorithm is the weak point in SWT or if some of the SWT stuff gets in the way.

I did some SWT at my last job...you might want to disable updates on the table while you're sorting. If I remember correctly, the table tries to redraw itself with every reorder operation. The reason for that is that SWT is loving retarded. I hate the Eclipse platform with every fiber of my being :shobon:

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

csammis posted:

I did some SWT at my last job...you might want to disable updates on the table while you're sorting. If I remember correctly, the table tries to redraw itself with every reorder operation. The reason for that is that SWT is loving retarded. I hate the Eclipse platform with every fiber of my being :shobon:
I'll give it a shot this evening. SWT doesn't seem so bad from a developer point of view, but it's because I think I'm biased towards it. I couldn't get AWT or Swing to stick in my head, but I've been mostly successful with SWT. I did get whacked trying to do something with a FormLayout and not seeing the right value.

I suspect that everybody learns to hate a GUI library with sufficient time.

csammis
Aug 26, 2003

Mental Institution

Rocko Bonaparte posted:

SWT doesn't seem so bad from a developer point of view

It's from my own developer point of view that I despise it - in its effort to support everything under the sun, it gets really awkward on some platforms. Couple that with SWT crippling the ability to extend widget classes and you can easily get stuck doing some unfortunate things in a system of high GUI complexity.

Creating a TextBox from a Composite because I want the ability to underline text - sign me right the hell up :v:

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
Let's say I have a handful of functions that are functional (side effect free), and the first argument to all of these functions refers to the same thing (an object, or id, whatever represents the main concept they operate on).

Should these functions be bundled together into an instantiatable class?

I actually already asked this some time ago but I can't find the original thread and I think I described it a little differently.

csammis
Aug 26, 2003

Mental Institution

Triple Tech posted:

Let's say I have a handful of functions that are functional (side effect free), and the first argument to all of these functions refers to the same thing (an object, or id, whatever represents the main concept they operate on).

Should these functions be bundled together into an instantiatable class?

Since they share a concept, and in fact operate on the same object, I vote yes. Pass the operated-on object into the constructor of the class, then have all the functions operate on this->object.

Triple Tech
Jul 28, 2006

So what, are you quitting to join Homo Explosion?
I was told this was a stupid idea because it didn't relate to state at all. Like, the functions don't necessarily change the concept, or anything else for that manner (again, functional, stateless).

I'm trying to see if there's like an OOP rule of thumb that will tell me who was right here.

e: spelling

Triple Tech fucked around with this message at 22:24 on Oct 28, 2008

gold brick
Jun 19, 2001

no he isn't

Triple Tech posted:

I was told this was a stupid idea because it didn't relate to state at all. Like, the functions don't necessarily change the concept, or anything else for that mannner (again, functional, stateless).

I'm trying to see if there's like an OOP rule of thumb that will tell me who was right here.
Cohesion is the concept you're thinking of I think:

http://en.wikipedia.org/wiki/Cohesion_(computer_science)

I agree with csammis, incidentally.

ryo
Jan 15, 2003
How is PHP's security when it comes to MySQL statements?
If I have a form that has has a text field on it, and I insert the value from it into a MySQL table, is it possible for someone to add malicious things into the text or will PHP protect you from this?

No Safe Word
Feb 26, 2005

ryo posted:

How is PHP's security when it comes to MySQL statements?
If I have a form that has has a text field on it, and I insert the value from it into a MySQL table, is it possible for someone to add malicious things into the text or will PHP protect you from this?
It's fine if you know what you're doing. Check the SQL megathread and also Google "prevent SQL injection" and read up.

Wardhog
Nov 30, 2001
I have an awk/sed question. I tried to search to see if there was an awk or sed thread, but search is still not working, so forgive me if I'm rehashing old questions.

I'm writing a bash shell script that is trying to break up a string into two components.
The string is a Windows OR *ix style filename with full path prepended.
eg. /aaa/bbb/ccc ddd/eee.txt
-or-
c:\aaa\bbb\ccc ddd\eee.txt
I want to wind up with $filename and $pathname having the filename(eee.txt) and the directory respectively.

dirname and basename covers the *ix style filenames, it's the windows ones that are giving me gyp. So far, I've found that

filename=`echo ${data} | sed 's/.*\\\//'`

will reliably retrieve the filename in the case of a Windows-style filename. However, I'm drawing a blank as to the best, most efficient way of capturing the prepended directory into a variable.
Can anyone offer any solutions/advice?

covener
Jan 10, 2004

You know, for kids!

Wardhog posted:


I'm writing a bash shell script that is trying to break up a string into two components.
...

filename=`echo ${data} | sed 's/.*\\\//'`

will reliably retrieve the filename in the case of a Windows-style filename. However, I'm drawing a blank as to the best, most efficient way of capturing the prepended directory into a variable.
Can anyone offer any solutions/advice?

since you already have filename, you can just lop it off:

dirname=${data%$filename}

Wardhog
Nov 30, 2001

covener posted:

since you already have filename, you can just lop it off:

dirname=${data%$filename}

You haven't exactly solved my problem, as dirname won't handle Windows-style filenames, but you've suggested another course of action that's looking promising.

Thanks!

dirname won't cope with c:\aaa\bbb\ccc\ddd\eee
but it WILL cope with c:/aaa/bbb/ccc/ddd/eee and sed is pretty good at changing all occurrences of \ to / and back again

Wardhog fucked around with this message at 03:14 on Oct 29, 2008

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Triple Tech posted:

I was told this was a stupid idea because it didn't relate to state at all. Like, the functions don't necessarily change the concept, or anything else for that manner (again, functional, stateless).

I'm trying to see if there's like an OOP rule of thumb that will tell me who was right here.

e: spelling

I would say it depends on the pattern of use - if you're always going to be using several of them at around the same time, so you can instantiate the class and then use it over and over, put them in a class. If you'd end up with a common pattern of "Manipulator m(object); m.some_function()" then just put them in a namespace instead.

It might make most sense to add them directly to the object, though.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

csammis posted:

It's from my own developer point of view that I despise it - in its effort to support everything under the sun, it gets really awkward on some platforms. Couple that with SWT crippling the ability to extend widget classes and you can easily get stuck doing some unfortunate things in a system of high GUI complexity.

Creating a TextBox from a Composite because I want the ability to underline text - sign me right the hell up :v:
Sorry to bug you again on this, but it looked like the example I was trying out already does shut off redraw for the table while it's sorting. I was looking at the code from: http://www.java2s.com/Code/Java/SWT...enerFactory.htm

Particularly, I see:
code:
public void handleEvent(Event e) {
        
        updown = (updown == 1 ? -1 : 1);
        
        TableColumn currentColumn = (TableColumn)e.widget;
        Table table = currentColumn.getParent();
    
        colIndex = searchColumnIndex(currentColumn);
        
        table.setRedraw(false);
        
        TableItem[] items = table.getItems();
       
        Arrays.sort(items,currentComparator);
        
        table.setItemCount(items.length);
        
        for (int i = 0;i<items.length;i++)
        {   
            TableItem item = new TableItem(table,SWT.NONE,i);
            item.setText(getData(items[i]));
            items[i].dispose();
        }
        
        table.setRedraw(true);     
    }
So maybe their sorting algorithm sucks. Just looking quick, I'm not really sure it's as fast as advertised.

KaeseEs
Feb 23, 2007

by Fragmaster

ryo posted:

How is PHP's security when it comes to MySQL statements?
If I have a form that has has a text field on it, and I insert the value from it into a MySQL table, is it possible for someone to add malicious things into the text or will PHP protect you from this?

Just don't rely on any sort of escaping mechanism; there's a lot of PHP tutorials out there (including those on php.net) that recommend the use of obsolete or insecure functions to prevent SQL injection. Here's a helpful list of bad ideas:
  • magic quotes [really bad]
  • addslashes()/stripslashes() [really bad]
  • mysql_escape_string() [really bad]
  • mysql_real_escape_string() [if you must use escaping do this]

What you actually want to use to prevent SQL injection is a prepared (sometimes called 'parameterized') query, which you can do by using the MySqlI ('improved') library of PHP5. There's a nice overview here, and some simple examples here.

covener
Jan 10, 2004

You know, for kids!

Wardhog posted:

You haven't exactly solved my problem, as dirname won't handle Windows-style filenames, but you've suggested another course of action that's looking promising.

Thanks!

dirname won't cope with c:\aaa\bbb\ccc\ddd\eee
but it WILL cope with c:/aaa/bbb/ccc/ddd/eee and sed is pretty good at changing all occurrences of \ to / and back again

I didn't mean /usr/bin/dirname, just chose a variable that matches your "filename":

code:
$ data='c:\foo\bar\baz.txt'
$ filename=baz.txt
$ echo ${data%$filename}
c:\foo\bar\

hey mom its 420
May 12, 2007

Triple Tech posted:

I was told this was a stupid idea because it didn't relate to state at all. Like, the functions don't necessarily change the concept, or anything else for that manner (again, functional, stateless).

I'm trying to see if there's like an OOP rule of thumb that will tell me who was right here.

e: spelling

Eh, I wouldn't worry about that. Some people say instance methods should only be the ones that change state, but I think it's OK for them to be instance methods if they just depend on the state or the identity of the object.

csammis
Aug 26, 2003

Mental Institution

Rocko Bonaparte posted:

Sorry to bug you again on this, but it looked like the example I was trying out already does shut off redraw for the table while it's sorting. I was looking at the code from: http://www.java2s.com/Code/Java/SWT...enerFactory.htm

That's some pretty :psyduck: code...it's completely recreating the table every time you click on a column header to sort, no wonder it's slow as poo poo. Use a TableVIewer instead, it provides a sorted view onto the underlying table that doesn't require the table to get torn down constantly.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

csammis posted:

That's some pretty :psyduck: code...it's completely recreating the table every time you click on a column header to sort, no wonder it's slow as poo poo. Use a TableVIewer instead, it provides a sorted view onto the underlying table that doesn't require the table to get torn down constantly.
Is jface stuff included in Eclipse SWT, or would I have to install it separately? I always run into jface stuff when I was searching for information on SWT, but I never quite pieced it together.

csammis
Aug 26, 2003

Mental Institution

Rocko Bonaparte posted:

Is jface stuff included in Eclipse SWT, or would I have to install it separately? I always run into jface stuff when I was searching for information on SWT, but I never quite pieced it together.

I'm fairly sure it's included, but I don't know for sure. I wasn't involved in setting up our package dependencies.


e: I guess I don't know what you mean by "included" - it's not the same jar as the basic SWT stuff, if that's what you're getting at.

Stephen
Feb 6, 2004

Stoned
This is an amazingly simple question (I hope) but I can't seem to find relevant info for it on Google.

My site displays a user's webpage that they've specified in their account info. When they type in a full URL (http://www.example.com) it works perfectly, but when they type in a partial URL (https://www.example.com), the link directs to http://www.mypage.com/www.example.com.

I can parse out URL's and change them based on the user's input, but I'm just hoping there's something easy I can do to prevent this.

Vanadium
Jan 8, 2005

Stephen posted:

My site displays a user's webpage that they've specified in their account info. When they type in a full URL (http://www.example.com) it works perfectly, but when they type in a partial URL (www.example.com), the link directs to http://www.mypage.com/www.example.com.

That is not a partial URL, that is just a host name. Just tell your users to gently caress off until they have learned to write URLs! :mad:

Vanadium fucked around with this message at 19:14 on Jun 7, 2014

Stephen
Feb 6, 2004

Stoned

Vanadium posted:

That is not a partial URL, that is just a host name. Just tell your users to gently caress off until they have learned to write URLs! :mad:

Hah! I wish. So I guess I just write a PHP function to parse the URLs before they're displayed?

Edit: In any case, here's what I did that worked:
php:
<?
function parseURL($url) {
    //This function will parse a URL for proper use in an <a> link
    //Parameters: URL string
    //Output: Formatted URL string
    if(!stripos($url, 'http://')) {
        $url = 'http://'.$url;
    }
    
    return $url;
}
?>

Stephen fucked around with this message at 21:50 on Oct 29, 2008

csammis
Aug 26, 2003

Mental Institution

Stephen posted:

function parseURL($url)

I hate your function name, and by extension you and puppies and kids on my drat lawn, because it doesn't parse the URL. It verifies it ~:mad:>

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

csammis posted:

I hate your function name, and by extension you and puppies and kids on my drat lawn, because it doesn't parse the URL. It verifies it ~:mad:>

Let us not forget that it will fail on the following:
https://example.com
example.com.[url]http://[/url]
ftp://example.com

Stephen
Feb 6, 2004

Stoned

Avenging Dentist posted:

Let us not forget that it will fail on the following:
https://example.com
example.com.[url]http://[/url]
ftp://example.com
Good point about the https. gently caress the user in the other two cases, they're too stupid.

fischtick
Jul 9, 2001

CORGO, THE DESTROYER

Fun Shoe
...and mailto:
...and java script:
...and relative URLs

[edit] which may be beyond the scope of your function's use in this one case [/edit]

fischtick fucked around with this message at 22:26 on Oct 29, 2008

Stephen
Feb 6, 2004

Stoned

fischtick posted:

...and mailto:
...and java script:
This is why I was hoping there was something built-in that I could use, since I always miss this kind of poo poo. Fortunately I never have to verify anything other than http: or https:

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Stephen posted:

This is why I was hoping there was something built-in that I could use, since I always miss this kind of poo poo. Fortunately I never have to verify anything other than http: or https:

URI schemes have to match [-+.a-zA-Z0-9]+, and colon is not a valid hostname character ([-.A-Za-z0-9]+, ignoring internationalization), so if you're willing to require absolute URLs, you can just prepend http:// if the URL doesn't match ^[-+.a-zA-Z0-9]+:.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

rjmccall posted:

URI schemes have to match [-+.a-zA-Z0-9]+, and colon is not a valid hostname character ([-.A-Za-z0-9]+, ignoring internationalization), so if you're willing to require absolute URLs, you can just prepend http:// if the URL doesn't match ^[-+.a-zA-Z0-9]+:.

Good job, you just broke URLs for everything with an explicit port.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Avenging Dentist posted:

Good job, you just broke URLs for everything with an explicit port.

Hey, I knew I was forgetting some valid use for colons.

Yeah, probably just go with a heuristic, or demand more user intelligence.

Vanadium
Jan 8, 2005

If your webpage is delivered by anything but http, you do not deserve to get linked to anyway. :mad:

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.
Seriously, though, a quick google for "php parse url" returns parse_url, which splits a URL into its component parts (scheme, hostname, etc). Try feeding https://www.example.com into that and find out if it returns a blank scheme, defaults to "http" or chokes. Then the correct way to do this (in fact, the correct thing to do even if you hadn't noticed a specific problem) is to use that function to split the url's into parts and then rebuild them into a canonical form.

JoeNotCharles fucked around with this message at 23:52 on Oct 29, 2008

rotor
Jun 11, 2001

classic case of pineapple derangement syndrome

Stephen posted:

Hah! I wish. So I guess I just write a PHP function to parse the URLs before they're displayed?

Edit: In any case, here's what I did that worked:
php:
<?
function parseURL($url) {
    //This function will parse a URL for proper use in an <a> link
    //Parameters: URL string
    //Output: Formatted URL string
    if(!stripos($url, 'http://')) {
        $url = 'http://'.$url;
    }
    
    return $url;
}
?>

I don't know poo poo about php but:

http://www.php.net/function.parse_url

can you just use that and check some fields for validity?

Adbot
ADBOT LOVES YOU

Sparta
Aug 14, 2003

the other white meat
Help me program better!

Right now I'm coding things in Notepad++ (PHP, HTML, JS), then just transfer files to my server with SSH Secure Shell FTP.

I have to think there's a more efficient way to do this. also, I want to be able to code on my Ubuntu laptop that is similar to what I'm doing. Point me in the right direction!

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply