+ if( !Name || Name[0] == '\0' )
+ {
+ RWLock_AcquireWrite(&glPTY_NumPTYs);
+ // Get a pty ID if Name==NULL
+ prev_np = &gpPTY_FirstNumPTY;
+ for( tPTY *pty = gpPTY_FirstNumPTY; pty; prev_np = &pty->Next, pty = pty->Next )
+ {
+ if( pty->NumericName > idx )
+ break;
+ idx ++;
+ }
+ namelen = snprintf(NULL,0, "%u", idx);
+ }
+ else if( Name[strlen(Name)-1] == '#' )
+ {
+ // Sequenced PTYs
+ // - "gui#" would translate to "gui0", "gui1", "gui2", ...
+ // whichever is free
+ prev_np = &gpPTY_FirstNamedPTY;
+
+ idx = 0;
+ namelen = strlen(Name)-1;
+ for( tPTY *pty = gpPTY_FirstNamedPTY; pty; prev_np = &pty->Next, pty = pty->Next )
+ {
+ int cmp = strncmp(pty->Name, Name, namelen);
+ if( cmp < 0 )
+ continue ;
+ if( cmp > 0 )
+ break;
+
+ // Skip non-numbered
+ if( pty->Name[namelen] == '\0' )
+ continue ;
+
+ // Find an unused index
+ char *name_end;
+ int this_idx = strtol(pty->Name+namelen, &name_end, 10);
+ if( *name_end != '\0' )
+ continue;
+ if( this_idx > idx )
+ break;
+ idx ++;
+ }
+
+ namelen += snprintf(NULL, 0, "%u", idx);
+ }
+ else